d3d10core: Implement d3d10_shader_resource_view_SetPrivateDataInterface().
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blob0b239f9a1826ce161d3dcd57ba164d340fa1f8b2
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 reg_name[200];
1763 switch (wined3d_src->reg.data_type)
1765 case WINED3D_DATA_FLOAT:
1766 sprintf(reg_name, "%s", glsl_src->reg_name);
1767 break;
1768 case WINED3D_DATA_INT:
1769 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
1770 break;
1771 case WINED3D_DATA_RESOURCE:
1772 case WINED3D_DATA_SAMPLER:
1773 case WINED3D_DATA_UINT:
1774 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
1775 break;
1776 default:
1777 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1778 sprintf(reg_name, "%s", glsl_src->reg_name);
1779 break;
1782 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
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_OR: op = "|"; break;
2323 case WINED3DSIH_SUB: op = "-"; break;
2324 case WINED3DSIH_USHR: op = ">>"; break;
2325 case WINED3DSIH_XOR: op = "^"; break;
2326 default:
2327 op = "<unhandled operator>";
2328 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2329 break;
2332 write_mask = shader_glsl_append_dst(buffer, ins);
2333 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2334 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2335 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2338 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2340 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2341 struct glsl_src_param src0_param;
2342 struct glsl_src_param src1_param;
2343 unsigned int mask_size;
2344 DWORD write_mask;
2345 const char *op;
2347 write_mask = shader_glsl_append_dst(buffer, ins);
2348 mask_size = shader_glsl_get_write_mask_size(write_mask);
2349 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2350 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2352 if (mask_size > 1)
2354 switch (ins->handler_idx)
2356 case WINED3DSIH_EQ: op = "equal"; break;
2357 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2358 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2359 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2360 case WINED3DSIH_LT: op = "lessThan"; break;
2361 case WINED3DSIH_NE: op = "notEqual"; break;
2362 default:
2363 op = "<unhandled operator>";
2364 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2365 break;
2368 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2369 mask_size, op, src0_param.param_str, src1_param.param_str);
2371 else
2373 switch (ins->handler_idx)
2375 case WINED3DSIH_EQ: op = "=="; break;
2376 case WINED3DSIH_GE: op = ">="; break;
2377 case WINED3DSIH_IGE: op = ">="; break;
2378 case WINED3DSIH_UGE: op = ">="; break;
2379 case WINED3DSIH_LT: op = "<"; break;
2380 case WINED3DSIH_NE: op = "!="; break;
2381 default:
2382 op = "<unhandled operator>";
2383 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2384 break;
2387 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2388 src0_param.param_str, op, src1_param.param_str);
2392 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2394 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2395 struct glsl_src_param src0_param;
2396 struct glsl_src_param src1_param;
2397 DWORD write_mask;
2399 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2400 * not, we can emulate it. */
2401 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2402 FIXME("64-bit integer multiplies not implemented.\n");
2404 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2406 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2407 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2408 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2410 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2411 src0_param.param_str, src1_param.param_str);
2415 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2417 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2418 struct glsl_src_param src0_param, src1_param;
2419 DWORD write_mask;
2421 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2424 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2426 char dst_mask[6];
2428 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2429 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2430 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2431 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2432 dst_mask, src0_param.param_str, src1_param.param_str);
2434 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2435 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2436 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2437 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2439 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2440 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2442 else
2444 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2445 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2446 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2447 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2450 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2452 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2453 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2454 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2455 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2459 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2460 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2462 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2463 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2464 struct glsl_src_param src0_param;
2465 DWORD write_mask;
2467 write_mask = shader_glsl_append_dst(buffer, ins);
2468 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2470 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2471 * shader versions WINED3DSIO_MOVA is used for this. */
2472 if (ins->ctx->reg_maps->shader_version.major == 1
2473 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2474 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2476 /* This is a simple floor() */
2477 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2478 if (mask_size > 1) {
2479 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2480 } else {
2481 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2484 else if(ins->handler_idx == WINED3DSIH_MOVA)
2486 /* We need to *round* to the nearest int here. */
2487 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2489 if (gl_info->supported[EXT_GPU_SHADER4])
2491 if (mask_size > 1)
2492 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2493 else
2494 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2496 else
2498 if (mask_size > 1)
2499 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2500 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2501 else
2502 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2503 src0_param.param_str, src0_param.param_str);
2506 else
2508 shader_addline(buffer, "%s);\n", src0_param.param_str);
2512 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2513 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2515 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2516 struct glsl_src_param src0_param;
2517 struct glsl_src_param src1_param;
2518 DWORD dst_write_mask, src_write_mask;
2519 unsigned int dst_size = 0;
2521 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2522 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2524 /* dp3 works on vec3, dp4 on vec4 */
2525 if (ins->handler_idx == WINED3DSIH_DP4)
2527 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2528 } else {
2529 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2532 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2533 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2535 if (dst_size > 1) {
2536 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2537 } else {
2538 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2542 /* Note that this instruction has some restrictions. The destination write mask
2543 * can't contain the w component, and the source swizzles have to be .xyzw */
2544 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2546 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2547 struct glsl_src_param src0_param;
2548 struct glsl_src_param src1_param;
2549 char dst_mask[6];
2551 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2552 shader_glsl_append_dst(ins->ctx->buffer, ins);
2553 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2554 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2555 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2558 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2560 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2563 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2564 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2565 * GLSL uses the value as-is. */
2566 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2568 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2569 struct glsl_src_param src0_param;
2570 struct glsl_src_param src1_param;
2571 DWORD dst_write_mask;
2572 unsigned int dst_size;
2574 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2575 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2577 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2578 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2580 if (dst_size > 1)
2582 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2583 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2585 else
2587 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2588 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2592 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2593 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2595 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2596 struct glsl_src_param src_param;
2597 const char *instruction;
2598 DWORD write_mask;
2599 unsigned i;
2601 /* Determine the GLSL function to use based on the opcode */
2602 /* TODO: Possibly make this a table for faster lookups */
2603 switch (ins->handler_idx)
2605 case WINED3DSIH_MIN: instruction = "min"; break;
2606 case WINED3DSIH_MAX: instruction = "max"; break;
2607 case WINED3DSIH_ABS: instruction = "abs"; break;
2608 case WINED3DSIH_FRC: instruction = "fract"; break;
2609 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2610 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2611 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2612 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
2613 default: instruction = "";
2614 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2615 break;
2618 write_mask = shader_glsl_append_dst(buffer, ins);
2620 shader_addline(buffer, "%s(", instruction);
2622 if (ins->src_count)
2624 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2625 shader_addline(buffer, "%s", src_param.param_str);
2626 for (i = 1; i < ins->src_count; ++i)
2628 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2629 shader_addline(buffer, ", %s", src_param.param_str);
2633 shader_addline(buffer, "));\n");
2636 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2638 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2640 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2641 struct glsl_src_param src_param;
2642 unsigned int mask_size;
2643 DWORD write_mask;
2644 char dst_mask[6];
2646 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2647 mask_size = shader_glsl_get_write_mask_size(write_mask);
2648 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2650 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2651 src_param.param_str, src_param.param_str);
2652 shader_glsl_append_dst(buffer, ins);
2654 if (mask_size > 1)
2656 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2657 mask_size, src_param.param_str);
2659 else
2661 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2662 src_param.param_str);
2666 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2668 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2669 struct glsl_src_param src0_param;
2670 const char *prefix, *suffix;
2671 unsigned int dst_size;
2672 DWORD dst_write_mask;
2674 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2675 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2677 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2679 switch (ins->handler_idx)
2681 case WINED3DSIH_EXP:
2682 case WINED3DSIH_EXPP:
2683 prefix = "exp2(";
2684 suffix = ")";
2685 break;
2687 case WINED3DSIH_LOG:
2688 case WINED3DSIH_LOGP:
2689 prefix = "log2(abs(";
2690 suffix = "))";
2691 break;
2693 case WINED3DSIH_RCP:
2694 prefix = "1.0 / ";
2695 suffix = "";
2696 break;
2698 case WINED3DSIH_RSQ:
2699 prefix = "inversesqrt(abs(";
2700 suffix = "))";
2701 break;
2703 default:
2704 prefix = "";
2705 suffix = "";
2706 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2707 break;
2710 if (dst_size > 1)
2711 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2712 else
2713 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2716 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2717 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2718 * dst.x = 2^(floor(src))
2719 * dst.y = src - floor(src)
2720 * dst.z = 2^src (partial precision is allowed, but optional)
2721 * dst.w = 1.0;
2722 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2723 * dst = 2^src; (partial precision is allowed, but optional)
2725 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2727 if (ins->ctx->reg_maps->shader_version.major < 2)
2729 struct glsl_src_param src_param;
2730 char dst_mask[6];
2732 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2734 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2735 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2736 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2737 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2739 shader_glsl_append_dst(ins->ctx->buffer, ins);
2740 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2741 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2742 return;
2745 shader_glsl_scalar_op(ins);
2748 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2750 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2751 struct glsl_src_param src_param;
2752 unsigned int mask_size;
2753 DWORD write_mask;
2755 write_mask = shader_glsl_append_dst(buffer, ins);
2756 mask_size = shader_glsl_get_write_mask_size(write_mask);
2757 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2759 if (mask_size > 1)
2760 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2761 else
2762 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2765 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2767 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2768 struct glsl_src_param src_param;
2769 unsigned int mask_size;
2770 DWORD write_mask;
2772 write_mask = shader_glsl_append_dst(buffer, ins);
2773 mask_size = shader_glsl_get_write_mask_size(write_mask);
2774 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2776 if (mask_size > 1)
2777 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2778 else
2779 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2782 /** Process signed comparison opcodes in GLSL. */
2783 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2785 struct glsl_src_param src0_param;
2786 struct glsl_src_param src1_param;
2787 DWORD write_mask;
2788 unsigned int mask_size;
2790 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2791 mask_size = shader_glsl_get_write_mask_size(write_mask);
2792 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2793 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2795 if (mask_size > 1) {
2796 const char *compare;
2798 switch(ins->handler_idx)
2800 case WINED3DSIH_SLT: compare = "lessThan"; break;
2801 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2802 default: compare = "";
2803 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2806 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2807 src0_param.param_str, src1_param.param_str);
2808 } else {
2809 switch(ins->handler_idx)
2811 case WINED3DSIH_SLT:
2812 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2813 * to return 0.0 but step returns 1.0 because step is not < x
2814 * An alternative is a bvec compare padded with an unused second component.
2815 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2816 * issue. Playing with not() is not possible either because not() does not accept
2817 * a scalar.
2819 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2820 src0_param.param_str, src1_param.param_str);
2821 break;
2822 case WINED3DSIH_SGE:
2823 /* Here we can use the step() function and safe a conditional */
2824 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2825 break;
2826 default:
2827 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2833 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2835 const char *condition_prefix, *condition_suffix;
2836 struct wined3d_shader_dst_param dst;
2837 struct glsl_src_param src0_param;
2838 struct glsl_src_param src1_param;
2839 struct glsl_src_param src2_param;
2840 BOOL temp_destination = FALSE;
2841 DWORD cmp_channel = 0;
2842 unsigned int i, j;
2843 char mask_char[6];
2844 DWORD write_mask;
2846 switch (ins->handler_idx)
2848 case WINED3DSIH_CMP:
2849 condition_prefix = "";
2850 condition_suffix = " >= 0.0";
2851 break;
2853 case WINED3DSIH_CND:
2854 condition_prefix = "";
2855 condition_suffix = " > 0.5";
2856 break;
2858 case WINED3DSIH_MOVC:
2859 condition_prefix = "bool(";
2860 condition_suffix = ")";
2861 break;
2863 default:
2864 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2865 condition_prefix = "<unhandled prefix>";
2866 condition_suffix = "<unhandled suffix>";
2867 break;
2870 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2872 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2873 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2874 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2875 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2877 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2878 condition_prefix, src0_param.param_str, condition_suffix,
2879 src1_param.param_str, src2_param.param_str);
2880 return;
2883 dst = ins->dst[0];
2885 /* Splitting the instruction up in multiple lines imposes a problem:
2886 * The first lines may overwrite source parameters of the following lines.
2887 * Deal with that by using a temporary destination register if needed. */
2888 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2889 && ins->src[0].reg.type == dst.reg.type)
2890 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2891 && ins->src[1].reg.type == dst.reg.type)
2892 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2893 && ins->src[2].reg.type == dst.reg.type))
2894 temp_destination = TRUE;
2896 /* Cycle through all source0 channels. */
2897 for (i = 0; i < 4; ++i)
2899 write_mask = 0;
2900 /* Find the destination channels which use the current source0 channel. */
2901 for (j = 0; j < 4; ++j)
2903 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2905 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2906 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2909 dst.write_mask = ins->dst[0].write_mask & write_mask;
2911 if (temp_destination)
2913 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2914 continue;
2915 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2917 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
2918 continue;
2920 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2921 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2922 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2924 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2925 condition_prefix, src0_param.param_str, condition_suffix,
2926 src1_param.param_str, src2_param.param_str);
2929 if (temp_destination)
2931 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2932 shader_glsl_append_dst(ins->ctx->buffer, ins);
2933 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2937 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2938 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2939 * the compare is done per component of src0. */
2940 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2942 struct glsl_src_param src0_param;
2943 struct glsl_src_param src1_param;
2944 struct glsl_src_param src2_param;
2945 DWORD write_mask;
2946 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2947 ins->ctx->reg_maps->shader_version.minor);
2949 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2951 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2952 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2953 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2954 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2956 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
2957 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2958 else
2959 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2960 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2961 return;
2964 shader_glsl_conditional_move(ins);
2967 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2968 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2970 struct glsl_src_param src0_param;
2971 struct glsl_src_param src1_param;
2972 struct glsl_src_param src2_param;
2973 DWORD write_mask;
2975 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2976 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2977 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2978 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2979 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2980 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2983 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2984 Vertex shaders to GLSL codes */
2985 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2987 int i;
2988 int nComponents = 0;
2989 struct wined3d_shader_dst_param tmp_dst = {{0}};
2990 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2991 struct wined3d_shader_instruction tmp_ins;
2993 memset(&tmp_ins, 0, sizeof(tmp_ins));
2995 /* Set constants for the temporary argument */
2996 tmp_ins.ctx = ins->ctx;
2997 tmp_ins.dst_count = 1;
2998 tmp_ins.dst = &tmp_dst;
2999 tmp_ins.src_count = 2;
3000 tmp_ins.src = tmp_src;
3002 switch(ins->handler_idx)
3004 case WINED3DSIH_M4x4:
3005 nComponents = 4;
3006 tmp_ins.handler_idx = WINED3DSIH_DP4;
3007 break;
3008 case WINED3DSIH_M4x3:
3009 nComponents = 3;
3010 tmp_ins.handler_idx = WINED3DSIH_DP4;
3011 break;
3012 case WINED3DSIH_M3x4:
3013 nComponents = 4;
3014 tmp_ins.handler_idx = WINED3DSIH_DP3;
3015 break;
3016 case WINED3DSIH_M3x3:
3017 nComponents = 3;
3018 tmp_ins.handler_idx = WINED3DSIH_DP3;
3019 break;
3020 case WINED3DSIH_M3x2:
3021 nComponents = 2;
3022 tmp_ins.handler_idx = WINED3DSIH_DP3;
3023 break;
3024 default:
3025 break;
3028 tmp_dst = ins->dst[0];
3029 tmp_src[0] = ins->src[0];
3030 tmp_src[1] = ins->src[1];
3031 for (i = 0; i < nComponents; ++i)
3033 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3034 shader_glsl_dot(&tmp_ins);
3035 ++tmp_src[1].reg.idx[0].offset;
3040 The LRP instruction performs a component-wise linear interpolation
3041 between the second and third operands using the first operand as the
3042 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3043 This is equivalent to mix(src2, src1, src0);
3045 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3047 struct glsl_src_param src0_param;
3048 struct glsl_src_param src1_param;
3049 struct glsl_src_param src2_param;
3050 DWORD write_mask;
3052 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3054 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3055 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3056 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3058 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3059 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3062 /** Process the WINED3DSIO_LIT instruction in GLSL:
3063 * dst.x = dst.w = 1.0
3064 * dst.y = (src0.x > 0) ? src0.x
3065 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3066 * where src.w is clamped at +- 128
3068 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3070 struct glsl_src_param src0_param;
3071 struct glsl_src_param src1_param;
3072 struct glsl_src_param src3_param;
3073 char dst_mask[6];
3075 shader_glsl_append_dst(ins->ctx->buffer, ins);
3076 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3078 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3079 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3080 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3082 /* The sdk specifies the instruction like this
3083 * dst.x = 1.0;
3084 * if(src.x > 0.0) dst.y = src.x
3085 * else dst.y = 0.0.
3086 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3087 * else dst.z = 0.0;
3088 * dst.w = 1.0;
3089 * (where power = src.w clamped between -128 and 128)
3091 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3092 * dst.x = 1.0 ... No further explanation needed
3093 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3094 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3095 * dst.w = 1.0. ... Nothing fancy.
3097 * So we still have one conditional in there. So do this:
3098 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3100 * 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),
3101 * 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.
3102 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3104 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3105 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3106 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3108 shader_addline(ins->ctx->buffer,
3109 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3110 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3111 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3112 src0_param.param_str, src3_param.param_str, dst_mask);
3115 /** Process the WINED3DSIO_DST instruction in GLSL:
3116 * dst.x = 1.0
3117 * dst.y = src0.x * src0.y
3118 * dst.z = src0.z
3119 * dst.w = src1.w
3121 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3123 struct glsl_src_param src0y_param;
3124 struct glsl_src_param src0z_param;
3125 struct glsl_src_param src1y_param;
3126 struct glsl_src_param src1w_param;
3127 char dst_mask[6];
3129 shader_glsl_append_dst(ins->ctx->buffer, ins);
3130 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3132 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3133 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3134 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3135 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3137 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3138 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3141 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3142 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3143 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3145 * dst.x = cos(src0.?)
3146 * dst.y = sin(src0.?)
3147 * dst.z = dst.z
3148 * dst.w = dst.w
3150 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3152 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3153 struct glsl_src_param src0_param;
3154 DWORD write_mask;
3156 if (ins->ctx->reg_maps->shader_version.major < 4)
3158 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3160 write_mask = shader_glsl_append_dst(buffer, ins);
3161 switch (write_mask)
3163 case WINED3DSP_WRITEMASK_0:
3164 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3165 break;
3167 case WINED3DSP_WRITEMASK_1:
3168 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3169 break;
3171 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3172 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3173 src0_param.param_str, src0_param.param_str);
3174 break;
3176 default:
3177 ERR("Write mask should be .x, .y or .xy\n");
3178 break;
3181 return;
3184 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3187 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3189 char dst_mask[6];
3191 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3192 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3193 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3195 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3196 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3197 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3199 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3200 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3202 else
3204 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3205 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3206 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3209 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3211 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3212 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3213 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3217 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3218 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3219 * generate invalid code
3221 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3223 struct glsl_src_param src0_param;
3224 DWORD write_mask;
3226 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3227 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3229 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3232 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3233 * Start a for() loop where src1.y is the initial value of aL,
3234 * increment aL by src1.z for a total of src1.x iterations.
3235 * Need to use a temporary variable for this operation.
3237 /* FIXME: I don't think nested loops will work correctly this way. */
3238 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3240 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3241 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3242 const struct wined3d_shader *shader = ins->ctx->shader;
3243 const struct wined3d_shader_lconst *constant;
3244 struct glsl_src_param src1_param;
3245 const DWORD *control_values = NULL;
3247 if (ins->ctx->reg_maps->shader_version.major < 4)
3249 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3251 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3252 * class hardware doesn't support real varying indexing, but Microsoft
3253 * designed this feature for Shader model 2.x+. If the loop control is
3254 * known at compile time, the GLSL compiler can unroll the loop, and
3255 * replace indirect addressing with direct addressing. */
3256 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3258 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3260 if (constant->idx == ins->src[1].reg.idx[0].offset)
3262 control_values = constant->value;
3263 break;
3268 if (control_values)
3270 struct wined3d_shader_loop_control loop_control;
3271 loop_control.count = control_values[0];
3272 loop_control.start = control_values[1];
3273 loop_control.step = (int)control_values[2];
3275 if (loop_control.step > 0)
3277 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3278 loop_state->current_depth, loop_control.start,
3279 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3280 loop_state->current_depth, loop_control.step);
3282 else if (loop_control.step < 0)
3284 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3285 loop_state->current_depth, loop_control.start,
3286 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3287 loop_state->current_depth, loop_control.step);
3289 else
3291 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3292 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3293 loop_state->current_depth, loop_control.count,
3294 loop_state->current_depth);
3297 else
3299 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3300 loop_state->current_depth, loop_state->current_reg,
3301 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3302 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3305 ++loop_state->current_reg;
3307 else
3309 shader_addline(buffer, "for (;;)\n{\n");
3312 ++loop_state->current_depth;
3315 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3317 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3319 shader_addline(ins->ctx->buffer, "}\n");
3321 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3323 --loop_state->current_depth;
3324 --loop_state->current_reg;
3327 if (ins->handler_idx == WINED3DSIH_ENDREP)
3329 --loop_state->current_depth;
3333 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3335 const struct wined3d_shader *shader = ins->ctx->shader;
3336 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3337 const struct wined3d_shader_lconst *constant;
3338 struct glsl_src_param src0_param;
3339 const DWORD *control_values = NULL;
3341 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3342 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3344 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3346 if (constant->idx == ins->src[0].reg.idx[0].offset)
3348 control_values = constant->value;
3349 break;
3354 if (control_values)
3356 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3357 loop_state->current_depth, loop_state->current_depth,
3358 control_values[0], loop_state->current_depth);
3360 else
3362 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3363 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3364 loop_state->current_depth, loop_state->current_depth,
3365 src0_param.param_str, loop_state->current_depth);
3368 ++loop_state->current_depth;
3371 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3373 struct glsl_src_param src0_param;
3375 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3376 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3379 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3381 struct glsl_src_param src0_param;
3382 struct glsl_src_param src1_param;
3384 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3385 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3387 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3388 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3391 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3393 shader_addline(ins->ctx->buffer, "} else {\n");
3396 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3398 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3401 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3403 shader_addline(ins->ctx->buffer, "break;\n");
3406 /* FIXME: According to MSDN the compare is done per component. */
3407 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3409 struct glsl_src_param src0_param;
3410 struct glsl_src_param src1_param;
3412 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3413 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3415 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3416 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3419 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3421 struct glsl_src_param src_param;
3423 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3424 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3427 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3429 shader_addline(ins->ctx->buffer, "}\n");
3430 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3433 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3435 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3438 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3440 struct glsl_src_param src1_param;
3442 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3443 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3444 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3447 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3449 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3450 * function only suppresses the unhandled instruction warning
3454 /*********************************************
3455 * Pixel Shader Specific Code begins here
3456 ********************************************/
3457 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3459 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3460 ins->ctx->reg_maps->shader_version.minor);
3461 struct glsl_sample_function sample_function;
3462 DWORD sample_flags = 0;
3463 DWORD resource_idx;
3464 DWORD mask = 0, swizzle;
3465 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3467 /* 1.0-1.4: Use destination register as sampler source.
3468 * 2.0+: Use provided sampler source. */
3469 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3470 resource_idx = ins->dst[0].reg.idx[0].offset;
3471 else
3472 resource_idx = ins->src[1].reg.idx[0].offset;
3474 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3476 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3477 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3478 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3480 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3481 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3483 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3484 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3486 case WINED3D_TTFF_COUNT1:
3487 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3488 break;
3489 case WINED3D_TTFF_COUNT2:
3490 mask = WINED3DSP_WRITEMASK_1;
3491 break;
3492 case WINED3D_TTFF_COUNT3:
3493 mask = WINED3DSP_WRITEMASK_2;
3494 break;
3495 case WINED3D_TTFF_COUNT4:
3496 case WINED3D_TTFF_DISABLE:
3497 mask = WINED3DSP_WRITEMASK_3;
3498 break;
3502 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3504 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3506 if (src_mod == WINED3DSPSM_DZ) {
3507 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3508 mask = WINED3DSP_WRITEMASK_2;
3509 } else if (src_mod == WINED3DSPSM_DW) {
3510 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3511 mask = WINED3DSP_WRITEMASK_3;
3514 else
3516 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3517 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3519 /* ps 2.0 texldp instruction always divides by the fourth component. */
3520 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3521 mask = WINED3DSP_WRITEMASK_3;
3525 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
3526 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3528 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
3529 mask |= sample_function.coord_mask;
3531 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3532 else swizzle = ins->src[1].swizzle;
3534 /* 1.0-1.3: Use destination register as coordinate source.
3535 1.4+: Use provided coordinate source register. */
3536 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3538 char coord_mask[6];
3539 shader_glsl_write_mask_to_str(mask, coord_mask);
3540 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3541 "T%u%s", resource_idx, coord_mask);
3543 else
3545 struct glsl_src_param coord_param;
3546 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3547 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3549 struct glsl_src_param bias;
3550 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3551 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3552 "%s", coord_param.param_str);
3553 } else {
3554 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3555 "%s", coord_param.param_str);
3560 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3562 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3563 struct glsl_src_param coord_param, dx_param, dy_param;
3564 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3565 struct glsl_sample_function sample_function;
3566 DWORD sampler_idx;
3567 DWORD swizzle = ins->src[1].swizzle;
3568 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3570 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3572 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3573 shader_glsl_tex(ins);
3574 return;
3577 sampler_idx = ins->src[1].reg.idx[0].offset;
3578 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3579 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3581 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3582 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3583 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3584 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3586 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3587 "%s", coord_param.param_str);
3590 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3592 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3593 struct glsl_src_param coord_param, lod_param;
3594 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3595 struct glsl_sample_function sample_function;
3596 DWORD sampler_idx;
3597 DWORD swizzle = ins->src[1].swizzle;
3598 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3600 sampler_idx = ins->src[1].reg.idx[0].offset;
3601 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3602 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3603 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3605 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3606 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3608 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3610 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3611 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3613 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3614 * However, the NVIDIA drivers allow them in fragment shaders as well,
3615 * even without the appropriate extension. */
3616 WARN("Using %s in fragment shader.\n", sample_function.name);
3618 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3619 "%s", coord_param.param_str);
3622 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
3623 unsigned int resource_idx, unsigned int sampler_idx)
3625 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
3626 unsigned int i;
3628 for (i = 0; i < sampler_map->count; ++i)
3630 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
3631 return entries[i].bind_idx;
3634 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
3636 return ~0u;
3639 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
3641 struct glsl_sample_function sample_function;
3642 struct glsl_src_param coord_param;
3643 unsigned int sampler_idx;
3645 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
3646 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3647 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
3648 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
3649 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
3650 NULL, NULL, NULL, "%s", coord_param.param_str);
3653 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3655 /* FIXME: Make this work for more than just 2D textures */
3656 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3657 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3659 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3661 char dst_mask[6];
3663 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3664 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3665 ins->dst[0].reg.idx[0].offset, dst_mask);
3667 else
3669 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3670 DWORD reg = ins->src[0].reg.idx[0].offset;
3671 char dst_swizzle[6];
3673 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3675 if (src_mod == WINED3DSPSM_DZ)
3677 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3678 struct glsl_src_param div_param;
3680 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3682 if (mask_size > 1) {
3683 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3684 } else {
3685 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3688 else if (src_mod == WINED3DSPSM_DW)
3690 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3691 struct glsl_src_param div_param;
3693 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3695 if (mask_size > 1) {
3696 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3697 } else {
3698 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3700 } else {
3701 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3706 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3707 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3708 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3709 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3711 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3712 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3713 struct glsl_sample_function sample_function;
3714 struct glsl_src_param src0_param;
3715 UINT mask_size;
3717 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3719 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3720 * scalar, and projected sampling would require 4.
3722 * It is a dependent read - not valid with conditional NP2 textures
3724 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3725 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3727 switch(mask_size)
3729 case 1:
3730 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3731 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3732 break;
3734 case 2:
3735 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3736 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3737 break;
3739 case 3:
3740 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3741 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3742 break;
3744 default:
3745 FIXME("Unexpected mask size %u\n", mask_size);
3746 break;
3750 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3751 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3752 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3754 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3755 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3756 struct glsl_src_param src0_param;
3757 DWORD dst_mask;
3758 unsigned int mask_size;
3760 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3761 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3762 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3764 if (mask_size > 1) {
3765 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3766 } else {
3767 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3771 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3772 * Calculate the depth as dst.x / dst.y */
3773 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3775 struct glsl_dst_param dst_param;
3777 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3779 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3780 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3781 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3782 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3783 * >= 1.0 or < 0.0
3785 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3786 dst_param.reg_name, dst_param.reg_name);
3789 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3790 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3791 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3792 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3794 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3796 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3797 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3798 struct glsl_src_param src0_param;
3800 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3802 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3803 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3806 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3807 * Calculate the 1st of a 2-row matrix multiplication. */
3808 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3810 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3811 DWORD reg = ins->dst[0].reg.idx[0].offset;
3812 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3813 struct glsl_src_param src0_param;
3815 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3816 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3819 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3820 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3821 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3823 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3824 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3825 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3826 DWORD reg = ins->dst[0].reg.idx[0].offset;
3827 struct glsl_src_param src0_param;
3829 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3830 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3831 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3834 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3836 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3837 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3838 struct glsl_sample_function sample_function;
3839 DWORD reg = ins->dst[0].reg.idx[0].offset;
3840 struct glsl_src_param src0_param;
3842 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3843 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3845 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3847 /* Sample the texture using the calculated coordinates */
3848 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3851 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3852 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3853 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3855 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3856 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3857 struct glsl_sample_function sample_function;
3858 DWORD reg = ins->dst[0].reg.idx[0].offset;
3859 struct glsl_src_param src0_param;
3861 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3862 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3864 /* Dependent read, not valid with conditional NP2 */
3865 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3867 /* Sample the texture using the calculated coordinates */
3868 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3870 tex_mx->current_row = 0;
3873 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3874 * Perform the 3rd row of a 3x3 matrix multiply */
3875 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3877 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3878 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3879 DWORD reg = ins->dst[0].reg.idx[0].offset;
3880 struct glsl_src_param src0_param;
3881 char dst_mask[6];
3883 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3885 shader_glsl_append_dst(ins->ctx->buffer, ins);
3886 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3887 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3889 tex_mx->current_row = 0;
3892 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3893 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3894 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3896 struct glsl_src_param src0_param;
3897 struct glsl_src_param src1_param;
3898 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3899 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3900 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3901 struct glsl_sample_function sample_function;
3902 DWORD reg = ins->dst[0].reg.idx[0].offset;
3903 char coord_mask[6];
3905 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3906 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3908 /* Perform the last matrix multiply operation */
3909 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3910 /* Reflection calculation */
3911 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3913 /* Dependent read, not valid with conditional NP2 */
3914 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3915 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3917 /* Sample the texture */
3918 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3919 NULL, NULL, NULL, "tmp0%s", coord_mask);
3921 tex_mx->current_row = 0;
3924 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3925 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3926 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3928 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3929 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3930 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3931 struct glsl_sample_function sample_function;
3932 DWORD reg = ins->dst[0].reg.idx[0].offset;
3933 struct glsl_src_param src0_param;
3934 char coord_mask[6];
3936 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3938 /* Perform the last matrix multiply operation */
3939 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3941 /* Construct the eye-ray vector from w coordinates */
3942 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3943 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3944 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3946 /* Dependent read, not valid with conditional NP2 */
3947 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3948 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3950 /* Sample the texture using the calculated coordinates */
3951 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3952 NULL, NULL, NULL, "tmp0%s", coord_mask);
3954 tex_mx->current_row = 0;
3957 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3958 * Apply a fake bump map transform.
3959 * texbem is pshader <= 1.3 only, this saves a few version checks
3961 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3963 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3964 struct glsl_sample_function sample_function;
3965 struct glsl_src_param coord_param;
3966 DWORD sampler_idx;
3967 DWORD mask;
3968 DWORD flags;
3969 char coord_mask[6];
3971 sampler_idx = ins->dst[0].reg.idx[0].offset;
3972 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3973 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3975 /* Dependent read, not valid with conditional NP2 */
3976 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3977 mask = sample_function.coord_mask;
3979 shader_glsl_write_mask_to_str(mask, coord_mask);
3981 /* With projected textures, texbem only divides the static texture coord,
3982 * not the displacement, so we can't let GL handle this. */
3983 if (flags & WINED3D_PSARGS_PROJECTED)
3985 DWORD div_mask=0;
3986 char coord_div_mask[3];
3987 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3989 case WINED3D_TTFF_COUNT1:
3990 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3991 break;
3992 case WINED3D_TTFF_COUNT2:
3993 div_mask = WINED3DSP_WRITEMASK_1;
3994 break;
3995 case WINED3D_TTFF_COUNT3:
3996 div_mask = WINED3DSP_WRITEMASK_2;
3997 break;
3998 case WINED3D_TTFF_COUNT4:
3999 case WINED3D_TTFF_DISABLE:
4000 div_mask = WINED3DSP_WRITEMASK_3;
4001 break;
4003 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4004 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4007 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4009 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4010 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4011 coord_param.param_str, coord_mask);
4013 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4015 struct glsl_src_param luminance_param;
4016 struct glsl_dst_param dst_param;
4018 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4019 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4021 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4022 dst_param.reg_name, dst_param.mask_str,
4023 luminance_param.param_str, sampler_idx, sampler_idx);
4027 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4029 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4030 struct glsl_src_param src0_param, src1_param;
4032 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4033 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4035 shader_glsl_append_dst(ins->ctx->buffer, ins);
4036 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4037 src0_param.param_str, sampler_idx, src1_param.param_str);
4040 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4041 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4042 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4044 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4045 struct glsl_sample_function sample_function;
4046 struct glsl_src_param src0_param;
4048 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4050 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4051 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4052 "%s.wx", src0_param.reg_name);
4055 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4056 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4057 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4059 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4060 struct glsl_sample_function sample_function;
4061 struct glsl_src_param src0_param;
4063 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4065 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4066 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4067 "%s.yz", src0_param.reg_name);
4070 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4071 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4072 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4074 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4075 struct glsl_sample_function sample_function;
4076 struct glsl_src_param src0_param;
4078 /* Dependent read, not valid with conditional NP2 */
4079 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4080 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4082 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4083 "%s", src0_param.param_str);
4086 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4087 * If any of the first 3 components are < 0, discard this pixel */
4088 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4090 struct glsl_dst_param dst_param;
4092 /* The argument is a destination parameter, and no writemasks are allowed */
4093 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4094 if (ins->ctx->reg_maps->shader_version.major >= 2)
4096 if (ins->ctx->reg_maps->shader_version.major >= 4)
4097 FIXME("SM4 discard not implemented.\n");
4098 /* 2.0 shaders compare all 4 components in texkill */
4099 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4100 } else {
4101 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4102 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4103 * 4 components are defined, only the first 3 are used
4105 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4109 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4110 * dst = dot2(src0, src1) + src2 */
4111 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4113 struct glsl_src_param src0_param;
4114 struct glsl_src_param src1_param;
4115 struct glsl_src_param src2_param;
4116 DWORD write_mask;
4117 unsigned int mask_size;
4119 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4120 mask_size = shader_glsl_get_write_mask_size(write_mask);
4122 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4123 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4124 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4126 if (mask_size > 1) {
4127 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4128 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4129 } else {
4130 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4131 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4135 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4136 const struct wined3d_shader_signature_element *input_signature,
4137 const struct wined3d_shader_reg_maps *reg_maps,
4138 enum vertexprocessing_mode vertexprocessing)
4140 WORD map = reg_maps->input_registers;
4141 unsigned int i;
4143 for (i = 0; map; map >>= 1, ++i)
4145 const char *semantic_name;
4146 UINT semantic_idx;
4147 char reg_mask[6];
4149 /* Unused */
4150 if (!(map & 1)) continue;
4152 semantic_name = input_signature[i].semantic_name;
4153 semantic_idx = input_signature[i].semantic_idx;
4154 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4156 if (vertexprocessing == vertexshader)
4158 if (!strcmp(semantic_name, "SV_POSITION") && !semantic_idx)
4159 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4160 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4161 else
4162 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4163 shader->u.ps.input_reg_map[i], reg_mask, shader->u.ps.input_reg_map[i], reg_mask);
4165 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4167 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4168 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4169 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4170 else
4171 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4172 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4174 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4176 if (!semantic_idx)
4177 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4178 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4179 else if (semantic_idx == 1)
4180 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4181 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4182 else
4183 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4184 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4186 else
4188 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4189 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4194 /*********************************************
4195 * Vertex Shader Specific Code begins here
4196 ********************************************/
4198 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4200 struct glsl_program_key key;
4202 key.vs_id = entry->vs.id;
4203 key.gs_id = entry->gs.id;
4204 key.ps_id = entry->ps.id;
4206 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4208 ERR("Failed to insert program entry.\n");
4212 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4213 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4215 struct wine_rb_entry *entry;
4216 struct glsl_program_key key;
4218 key.vs_id = vs_id;
4219 key.gs_id = gs_id;
4220 key.ps_id = ps_id;
4222 entry = wine_rb_get(&priv->program_lookup, &key);
4223 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4226 /* Context activation is done by the caller. */
4227 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4228 struct glsl_shader_prog_link *entry)
4230 struct glsl_program_key key;
4232 key.vs_id = entry->vs.id;
4233 key.gs_id = entry->gs.id;
4234 key.ps_id = entry->ps.id;
4235 wine_rb_remove(&priv->program_lookup, &key);
4237 GL_EXTCALL(glDeleteProgram(entry->id));
4238 if (entry->vs.id)
4239 list_remove(&entry->vs.shader_entry);
4240 if (entry->gs.id)
4241 list_remove(&entry->gs.shader_entry);
4242 if (entry->ps.id)
4243 list_remove(&entry->ps.shader_entry);
4244 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4245 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4246 HeapFree(GetProcessHeap(), 0, entry);
4249 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4250 const struct wined3d_gl_info *gl_info, const DWORD *map,
4251 const struct wined3d_shader_signature_element *input_signature,
4252 const struct wined3d_shader_reg_maps *reg_maps_in,
4253 const struct wined3d_shader_signature_element *output_signature,
4254 const struct wined3d_shader_reg_maps *reg_maps_out)
4256 unsigned int i, j;
4257 const char *semantic_name_in;
4258 UINT semantic_idx_in;
4259 DWORD *set;
4260 DWORD in_idx;
4261 unsigned int in_count = vec4_varyings(3, gl_info);
4262 char reg_mask[6];
4263 char destination[50];
4264 WORD input_map, output_map;
4266 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4268 input_map = reg_maps_in->input_registers;
4269 for (i = 0; input_map; input_map >>= 1, ++i)
4271 if (!(input_map & 1)) continue;
4273 in_idx = map[i];
4274 /* Declared, but not read register */
4275 if (in_idx == ~0U) continue;
4276 if (in_idx >= (in_count + 2))
4278 FIXME("More input varyings declared than supported, expect issues.\n");
4279 continue;
4282 if (in_idx == in_count)
4283 sprintf(destination, "gl_FrontColor");
4284 else if (in_idx == in_count + 1)
4285 sprintf(destination, "gl_FrontSecondaryColor");
4286 else
4287 sprintf(destination, "ps_link[%u]", in_idx);
4289 semantic_name_in = input_signature[i].semantic_name;
4290 semantic_idx_in = input_signature[i].semantic_idx;
4291 set[in_idx] = ~0U;
4293 output_map = reg_maps_out->output_registers;
4294 for (j = 0; output_map; output_map >>= 1, ++j)
4296 DWORD mask;
4298 if (!(output_map & 1)
4299 || semantic_idx_in != output_signature[j].semantic_idx
4300 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4301 || !(mask = input_signature[i].mask & output_signature[j].mask))
4302 continue;
4304 set[in_idx] = mask;
4305 shader_glsl_write_mask_to_str(mask, reg_mask);
4307 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4308 destination, reg_mask, j, reg_mask);
4312 for (i = 0; i < in_count + 2; ++i)
4314 unsigned int size;
4316 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4317 continue;
4319 if (set[i] == ~0U) set[i] = 0;
4321 size = 0;
4322 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4323 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4324 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4325 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4326 reg_mask[size] = '\0';
4328 if (i == in_count)
4329 sprintf(destination, "gl_FrontColor");
4330 else if (i == in_count + 1)
4331 sprintf(destination, "gl_FrontSecondaryColor");
4332 else
4333 sprintf(destination, "ps_link[%u]", i);
4335 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4336 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4339 HeapFree(GetProcessHeap(), 0, set);
4342 /* Context activation is done by the caller. */
4343 static GLuint generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4344 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4345 const struct wined3d_gl_info *gl_info)
4347 GLuint ret = 0;
4348 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4349 unsigned int i;
4350 const char *semantic_name;
4351 UINT semantic_idx;
4352 char reg_mask[6];
4353 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4354 WORD map = vs->reg_maps.output_registers;
4356 shader_buffer_clear(buffer);
4358 shader_addline(buffer, "#version 120\n");
4360 if (ps_major < 3)
4362 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4364 for (i = 0; map; map >>= 1, ++i)
4366 DWORD write_mask;
4368 if (!(map & 1)) continue;
4370 semantic_name = output_signature[i].semantic_name;
4371 semantic_idx = output_signature[i].semantic_idx;
4372 write_mask = output_signature[i].mask;
4373 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4375 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4377 if (!semantic_idx)
4378 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4379 reg_mask, i, reg_mask);
4380 else if (semantic_idx == 1)
4381 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4382 reg_mask, i, reg_mask);
4384 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4386 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4387 reg_mask, i, reg_mask);
4389 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4391 if (semantic_idx < 8)
4393 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4394 write_mask |= WINED3DSP_WRITEMASK_3;
4396 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4397 semantic_idx, reg_mask, i, reg_mask);
4398 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4399 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4402 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4404 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4406 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4408 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4411 shader_addline(buffer, "}\n");
4413 else
4415 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4416 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4417 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4418 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4420 /* First, sort out position and point size. Those are not passed to the pixel shader */
4421 for (i = 0; map; map >>= 1, ++i)
4423 if (!(map & 1)) continue;
4425 semantic_name = output_signature[i].semantic_name;
4426 semantic_idx = output_signature[i].semantic_idx;
4427 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4429 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4431 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4432 reg_mask, i, reg_mask);
4434 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4436 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4440 /* Then, fix the pixel shader input */
4441 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4442 &ps->reg_maps, output_signature, &vs->reg_maps);
4444 shader_addline(buffer, "}\n");
4447 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4448 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4449 shader_glsl_compile(gl_info, ret, buffer->buffer);
4451 return ret;
4454 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4456 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4457 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4458 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4459 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4460 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4461 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4464 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4466 switch (mode)
4468 case WINED3D_FFP_PS_FOG_OFF:
4469 return;
4471 case WINED3D_FFP_PS_FOG_LINEAR:
4472 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4473 break;
4475 case WINED3D_FFP_PS_FOG_EXP:
4476 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4477 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4478 break;
4480 case WINED3D_FFP_PS_FOG_EXP2:
4481 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4482 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4483 break;
4485 default:
4486 ERR("Invalid fog mode %#x.\n", mode);
4487 return;
4490 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4493 /* Context activation is done by the caller. */
4494 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4495 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4496 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4498 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4499 const struct wined3d_gl_info *gl_info = context->gl_info;
4500 const DWORD *function = shader->function;
4501 struct shader_glsl_ctx_priv priv_ctx;
4503 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4504 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
4506 memset(&priv_ctx, 0, sizeof(priv_ctx));
4507 priv_ctx.cur_ps_args = args;
4508 priv_ctx.cur_np2fixup_info = np2fixup_info;
4510 shader_addline(buffer, "#version 120\n");
4512 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4513 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4514 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4515 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4516 /* The spec says that it doesn't have to be explicitly enabled, but the
4517 * nvidia drivers write a warning if we don't do so. */
4518 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4519 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4520 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4521 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4522 if (gl_info->supported[EXT_GPU_SHADER4])
4523 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4525 /* Base Declarations */
4526 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4528 /* Pack 3.0 inputs */
4529 if (reg_maps->shader_version.major >= 3)
4530 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4532 /* Base Shader Body */
4533 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4535 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4536 if (reg_maps->shader_version.major < 2)
4538 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4539 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4542 if (args->srgb_correction)
4543 shader_glsl_generate_srgb_write_correction(buffer);
4545 /* SM < 3 does not replace the fog stage. */
4546 if (reg_maps->shader_version.major < 3)
4547 shader_glsl_generate_fog_code(buffer, args->fog);
4549 shader_addline(buffer, "}\n");
4551 TRACE("Compiling shader object %u.\n", shader_id);
4552 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4554 return shader_id;
4557 /* Context activation is done by the caller. */
4558 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4559 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4560 const struct vs_compile_args *args)
4562 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4563 const struct wined3d_gl_info *gl_info = context->gl_info;
4564 const DWORD *function = shader->function;
4565 struct shader_glsl_ctx_priv priv_ctx;
4567 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4568 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4570 shader_addline(buffer, "#version 120\n");
4572 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4573 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4574 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4575 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4576 if (gl_info->supported[EXT_GPU_SHADER4])
4577 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4579 memset(&priv_ctx, 0, sizeof(priv_ctx));
4580 priv_ctx.cur_vs_args = args;
4582 /* Base Declarations */
4583 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4585 /* Base Shader Body */
4586 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4588 /* Unpack outputs */
4589 shader_addline(buffer, "order_ps_input(vs_out);\n");
4591 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4592 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4593 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4594 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4596 if (args->fog_src == VS_FOG_Z)
4597 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4598 else if (!reg_maps->fog)
4599 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4601 /* We always store the clipplanes without y inversion */
4602 if (args->clip_enabled)
4603 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4605 /* Write the final position.
4607 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4608 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4609 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4610 * contains 1.0 to allow a mad.
4612 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4613 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4615 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4617 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4618 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4619 * which is the same as z = z * 2 - w.
4621 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4623 shader_addline(buffer, "}\n");
4625 TRACE("Compiling shader object %u.\n", shader_id);
4626 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4628 return shader_id;
4631 /* Context activation is done by the caller. */
4632 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4633 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4635 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4636 const struct wined3d_gl_info *gl_info = context->gl_info;
4637 const DWORD *function = shader->function;
4638 struct shader_glsl_ctx_priv priv_ctx;
4639 GLuint shader_id;
4641 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
4643 shader_addline(buffer, "#version 120\n");
4645 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4646 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4647 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4648 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4649 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4650 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4651 if (gl_info->supported[EXT_GPU_SHADER4])
4652 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4654 memset(&priv_ctx, 0, sizeof(priv_ctx));
4655 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4656 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4657 shader_addline(buffer, "}\n");
4659 TRACE("Compiling shader object %u.\n", shader_id);
4660 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4662 return shader_id;
4665 static GLuint find_glsl_pshader(const struct wined3d_context *context,
4666 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4667 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4669 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4670 struct glsl_shader_private *shader_data;
4671 struct ps_np2fixup_info *np2fixup;
4672 UINT i;
4673 DWORD new_size;
4674 GLuint ret;
4676 if (!shader->backend_data)
4678 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4679 if (!shader->backend_data)
4681 ERR("Failed to allocate backend data.\n");
4682 return 0;
4685 shader_data = shader->backend_data;
4686 gl_shaders = shader_data->gl_shaders.ps;
4688 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4689 * so a linear search is more performant than a hashmap or a binary search
4690 * (cache coherency etc)
4692 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4694 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4696 if (args->np2_fixup)
4697 *np2fixup_info = &gl_shaders[i].np2fixup;
4698 return gl_shaders[i].id;
4702 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4703 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4704 if (shader_data->num_gl_shaders)
4706 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4707 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4708 new_size * sizeof(*gl_shaders));
4710 else
4712 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4713 new_size = 1;
4716 if(!new_array) {
4717 ERR("Out of memory\n");
4718 return 0;
4720 shader_data->gl_shaders.ps = new_array;
4721 shader_data->shader_array_size = new_size;
4722 gl_shaders = new_array;
4725 gl_shaders[shader_data->num_gl_shaders].args = *args;
4727 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4728 memset(np2fixup, 0, sizeof(*np2fixup));
4729 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4731 pixelshader_update_resource_types(shader, args->tex_types);
4733 shader_buffer_clear(buffer);
4734 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4735 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4737 return ret;
4740 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4741 const DWORD use_map) {
4742 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4743 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4744 return stored->fog_src == new->fog_src;
4747 static GLuint find_glsl_vshader(const struct wined3d_context *context,
4748 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4749 const struct vs_compile_args *args)
4751 UINT i;
4752 DWORD new_size;
4753 DWORD use_map = context->stream_info.use_map;
4754 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4755 struct glsl_shader_private *shader_data;
4756 GLuint ret;
4758 if (!shader->backend_data)
4760 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4761 if (!shader->backend_data)
4763 ERR("Failed to allocate backend data.\n");
4764 return 0;
4767 shader_data = shader->backend_data;
4768 gl_shaders = shader_data->gl_shaders.vs;
4770 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4771 * so a linear search is more performant than a hashmap or a binary search
4772 * (cache coherency etc)
4774 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4776 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4777 return gl_shaders[i].id;
4780 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4782 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4783 if (shader_data->num_gl_shaders)
4785 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4786 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4787 new_size * sizeof(*gl_shaders));
4789 else
4791 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4792 new_size = 1;
4795 if(!new_array) {
4796 ERR("Out of memory\n");
4797 return 0;
4799 shader_data->gl_shaders.vs = new_array;
4800 shader_data->shader_array_size = new_size;
4801 gl_shaders = new_array;
4804 gl_shaders[shader_data->num_gl_shaders].args = *args;
4806 shader_buffer_clear(buffer);
4807 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4808 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4810 return ret;
4813 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
4814 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4816 struct glsl_gs_compiled_shader *gl_shaders;
4817 struct glsl_shader_private *shader_data;
4818 GLuint ret;
4820 if (!shader->backend_data)
4822 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4824 ERR("Failed to allocate backend data.\n");
4825 return 0;
4828 shader_data = shader->backend_data;
4829 gl_shaders = shader_data->gl_shaders.gs;
4831 if (shader_data->num_gl_shaders)
4832 return gl_shaders[0].id;
4834 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4836 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4838 ERR("Failed to allocate GL shader array.\n");
4839 return 0;
4841 shader_data->shader_array_size = 1;
4842 gl_shaders = shader_data->gl_shaders.gs;
4844 shader_buffer_clear(buffer);
4845 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4846 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4848 return ret;
4851 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4853 switch (mcs)
4855 case WINED3D_MCS_MATERIAL:
4856 return material;
4857 case WINED3D_MCS_COLOR1:
4858 return "gl_Color";
4859 case WINED3D_MCS_COLOR2:
4860 return "gl_SecondaryColor";
4861 default:
4862 ERR("Invalid material color source %#x.\n", mcs);
4863 return "<invalid>";
4867 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4868 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4870 const char *diffuse, *specular, *emission, *ambient;
4871 enum wined3d_light_type light_type;
4872 unsigned int i;
4874 if (!settings->lighting)
4876 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4877 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4878 return;
4881 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4882 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4883 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4884 shader_addline(buffer, "vec3 dir, dst;\n");
4885 shader_addline(buffer, "float att, t;\n");
4887 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4888 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4889 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4890 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4892 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4894 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4895 switch (light_type)
4897 case WINED3D_LIGHT_POINT:
4898 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4899 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4900 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4901 shader_addline(buffer, "dst.x = 1.0;\n");
4902 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4903 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4904 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4905 if (!settings->normal)
4906 break;
4907 shader_addline(buffer, "dir = normalize(dir);\n");
4908 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4909 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4910 if (settings->localviewer)
4911 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4912 else
4913 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4914 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4915 " * gl_LightSource[%u].specular) / att;\n", i);
4916 break;
4918 case WINED3D_LIGHT_SPOT:
4919 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4920 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4921 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4922 shader_addline(buffer, "dst.x = 1.0;\n");
4923 shader_addline(buffer, "dir = normalize(dir);\n");
4924 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4925 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4926 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4927 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4928 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4929 i, i, i, i);
4930 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4931 if (!settings->normal)
4932 break;
4933 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4934 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4935 if (settings->localviewer)
4936 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4937 else
4938 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4939 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4940 " * gl_LightSource[%u].specular) * att;\n", i);
4941 break;
4943 case WINED3D_LIGHT_DIRECTIONAL:
4944 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4945 if (!settings->normal)
4946 break;
4947 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4948 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
4949 " * gl_LightSource[%u].diffuse.xyz;\n", i);
4950 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4951 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4952 " * gl_LightSource[%u].specular;\n", i);
4953 break;
4955 default:
4956 if (light_type)
4957 FIXME("Unhandled light type %#x.\n", light_type);
4958 continue;
4962 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4963 ambient, diffuse, emission);
4964 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4965 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4968 /* Context activation is done by the caller. */
4969 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4970 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4972 GLuint shader_obj;
4973 unsigned int i;
4975 shader_buffer_clear(buffer);
4977 shader_addline(buffer, "#version 120\n");
4978 shader_addline(buffer, "\n");
4979 shader_addline(buffer, "void main()\n{\n");
4980 shader_addline(buffer, "float m;\n");
4981 shader_addline(buffer, "vec3 r;\n");
4983 if (settings->transformed)
4985 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
4986 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4987 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
4989 else
4991 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
4992 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4993 if (settings->clipping)
4994 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
4995 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
4998 if (!settings->normal)
4999 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5000 else if (settings->normalize)
5001 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
5002 else
5003 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
5005 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
5007 for (i = 0; i < MAX_TEXTURES; ++i)
5009 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
5011 case WINED3DTSS_TCI_PASSTHRU:
5012 if (settings->texcoords & (1 << i))
5013 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
5014 i, i, i);
5015 break;
5017 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5018 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
5019 break;
5021 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5022 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
5023 break;
5025 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5026 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5027 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5028 break;
5030 case WINED3DTSS_TCI_SPHEREMAP:
5031 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5032 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5033 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5034 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5035 break;
5037 default:
5038 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5039 break;
5043 switch (settings->fog_mode)
5045 case WINED3D_FFP_VS_FOG_OFF:
5046 break;
5048 case WINED3D_FFP_VS_FOG_FOGCOORD:
5049 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5050 break;
5052 case WINED3D_FFP_VS_FOG_RANGE:
5053 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5054 break;
5056 case WINED3D_FFP_VS_FOG_DEPTH:
5057 if (settings->ortho_fog)
5058 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5059 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5060 else if (settings->transformed)
5061 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5062 else
5063 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5064 break;
5066 default:
5067 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5068 break;
5071 if (settings->point_size)
5073 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5074 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5075 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5076 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5079 shader_addline(buffer, "}\n");
5081 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5082 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5084 return shader_obj;
5087 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5088 DWORD argnum, unsigned int stage, DWORD arg)
5090 const char *ret;
5092 if (arg == ARG_UNUSED)
5093 return "<unused arg>";
5095 switch (arg & WINED3DTA_SELECTMASK)
5097 case WINED3DTA_DIFFUSE:
5098 ret = "gl_Color";
5099 break;
5101 case WINED3DTA_CURRENT:
5102 if (!stage)
5103 ret = "gl_Color";
5104 else
5105 ret = "ret";
5106 break;
5108 case WINED3DTA_TEXTURE:
5109 switch (stage)
5111 case 0: ret = "tex0"; break;
5112 case 1: ret = "tex1"; break;
5113 case 2: ret = "tex2"; break;
5114 case 3: ret = "tex3"; break;
5115 case 4: ret = "tex4"; break;
5116 case 5: ret = "tex5"; break;
5117 case 6: ret = "tex6"; break;
5118 case 7: ret = "tex7"; break;
5119 default:
5120 ret = "<invalid texture>";
5121 break;
5123 break;
5125 case WINED3DTA_TFACTOR:
5126 ret = "tex_factor";
5127 break;
5129 case WINED3DTA_SPECULAR:
5130 ret = "gl_SecondaryColor";
5131 break;
5133 case WINED3DTA_TEMP:
5134 ret = "temp_reg";
5135 break;
5137 case WINED3DTA_CONSTANT:
5138 switch (stage)
5140 case 0: ret = "tss_const0"; break;
5141 case 1: ret = "tss_const1"; break;
5142 case 2: ret = "tss_const2"; break;
5143 case 3: ret = "tss_const3"; break;
5144 case 4: ret = "tss_const4"; break;
5145 case 5: ret = "tss_const5"; break;
5146 case 6: ret = "tss_const6"; break;
5147 case 7: ret = "tss_const7"; break;
5148 default:
5149 ret = "<invalid constant>";
5150 break;
5152 break;
5154 default:
5155 return "<unhandled arg>";
5158 if (arg & WINED3DTA_COMPLEMENT)
5160 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5161 if (argnum == 0)
5162 ret = "arg0";
5163 else if (argnum == 1)
5164 ret = "arg1";
5165 else if (argnum == 2)
5166 ret = "arg2";
5169 if (arg & WINED3DTA_ALPHAREPLICATE)
5171 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5172 if (argnum == 0)
5173 ret = "arg0";
5174 else if (argnum == 1)
5175 ret = "arg1";
5176 else if (argnum == 2)
5177 ret = "arg2";
5180 return ret;
5183 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5184 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5186 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5188 if (color && alpha)
5189 dstmask = "";
5190 else if (color)
5191 dstmask = ".xyz";
5192 else
5193 dstmask = ".w";
5195 if (dst == tempreg)
5196 dstreg = "temp_reg";
5197 else
5198 dstreg = "ret";
5200 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5201 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5202 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5204 switch (op)
5206 case WINED3D_TOP_DISABLE:
5207 if (!stage)
5208 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5209 break;
5211 case WINED3D_TOP_SELECT_ARG1:
5212 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5213 break;
5215 case WINED3D_TOP_SELECT_ARG2:
5216 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5217 break;
5219 case WINED3D_TOP_MODULATE:
5220 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5221 break;
5223 case WINED3D_TOP_MODULATE_4X:
5224 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5225 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5226 break;
5228 case WINED3D_TOP_MODULATE_2X:
5229 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5230 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5231 break;
5233 case WINED3D_TOP_ADD:
5234 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5235 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5236 break;
5238 case WINED3D_TOP_ADD_SIGNED:
5239 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5240 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5241 break;
5243 case WINED3D_TOP_ADD_SIGNED_2X:
5244 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5245 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5246 break;
5248 case WINED3D_TOP_SUBTRACT:
5249 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5250 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5251 break;
5253 case WINED3D_TOP_ADD_SMOOTH:
5254 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5255 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5256 break;
5258 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5259 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5260 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5261 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5262 break;
5264 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5265 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5266 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5267 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5268 break;
5270 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5271 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5272 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5273 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5274 break;
5276 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5277 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5278 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5279 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5280 break;
5282 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5283 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5284 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5285 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5286 break;
5288 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5289 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5290 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5291 break;
5293 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5294 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5295 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5296 break;
5298 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5299 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5300 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5301 break;
5302 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5303 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5304 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5305 break;
5307 case WINED3D_TOP_BUMPENVMAP:
5308 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5309 /* These are handled in the first pass, nothing to do. */
5310 break;
5312 case WINED3D_TOP_DOTPRODUCT3:
5313 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5314 dstreg, dstmask, arg1, arg2, dstmask);
5315 break;
5317 case WINED3D_TOP_MULTIPLY_ADD:
5318 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5319 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5320 break;
5322 case WINED3D_TOP_LERP:
5323 /* MSDN isn't quite right here. */
5324 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5325 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5326 break;
5328 default:
5329 FIXME("Unhandled operation %#x.\n", op);
5330 break;
5334 /* Context activation is done by the caller. */
5335 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5336 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5338 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5339 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5340 const char *final_combiner_src = "ret";
5341 UINT lowest_disabled_stage;
5342 GLuint shader_id;
5343 DWORD arg0, arg1, arg2;
5344 unsigned int stage;
5346 shader_buffer_clear(buffer);
5348 /* Find out which textures are read */
5349 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5351 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5352 break;
5354 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5355 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5356 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5358 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5359 tex_map |= 1 << stage;
5360 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5361 tfactor_used = TRUE;
5362 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5363 tempreg_used = TRUE;
5364 if (settings->op[stage].dst == tempreg)
5365 tempreg_used = TRUE;
5366 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5367 tss_const_map |= 1 << stage;
5369 switch (settings->op[stage].cop)
5371 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5372 lum_map |= 1 << stage;
5373 /* fall through */
5374 case WINED3D_TOP_BUMPENVMAP:
5375 bump_map |= 1 << stage;
5376 /* fall through */
5377 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5378 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5379 tex_map |= 1 << stage;
5380 break;
5382 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5383 tfactor_used = TRUE;
5384 break;
5386 default:
5387 break;
5390 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5391 continue;
5393 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5394 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5395 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5397 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5398 tex_map |= 1 << stage;
5399 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5400 tfactor_used = TRUE;
5401 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5402 tempreg_used = TRUE;
5403 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5404 tss_const_map |= 1 << stage;
5406 lowest_disabled_stage = stage;
5408 shader_addline(buffer, "#version 120\n");
5410 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5411 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5413 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5414 shader_addline(buffer, "vec4 ret;\n");
5415 if (tempreg_used || settings->sRGB_write)
5416 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
5417 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5419 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5421 if (tss_const_map & (1 << stage))
5422 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5424 if (!(tex_map & (1 << stage)))
5425 continue;
5427 switch (settings->op[stage].tex_type)
5429 case tex_1d:
5430 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5431 break;
5432 case tex_2d:
5433 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5434 break;
5435 case tex_3d:
5436 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5437 break;
5438 case tex_cube:
5439 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5440 break;
5441 case tex_rect:
5442 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5443 break;
5444 default:
5445 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5446 break;
5449 shader_addline(buffer, "vec4 tex%u;\n", stage);
5451 if (!(bump_map & (1 << stage)))
5452 continue;
5453 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5455 if (!(lum_map & (1 << stage)))
5456 continue;
5457 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5458 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5460 if (tfactor_used)
5461 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5462 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5464 if (settings->sRGB_write)
5466 shader_addline(buffer, "const vec4 srgb_const0 = ");
5467 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5468 shader_addline(buffer, ";\n");
5469 shader_addline(buffer, "const vec4 srgb_const1 = ");
5470 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5471 shader_addline(buffer, ";\n");
5474 shader_addline(buffer, "void main()\n{\n");
5476 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5477 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5479 /* Generate texture sampling instructions) */
5480 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5482 const char *texture_function, *coord_mask;
5483 char tex_reg_name[8];
5484 BOOL proj;
5486 if (!(tex_map & (1 << stage)))
5487 continue;
5489 if (settings->op[stage].projected == proj_none)
5491 proj = FALSE;
5493 else if (settings->op[stage].projected == proj_count4
5494 || settings->op[stage].projected == proj_count3)
5496 proj = TRUE;
5498 else
5500 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5501 proj = TRUE;
5504 switch (settings->op[stage].tex_type)
5506 case tex_1d:
5507 if (proj)
5509 texture_function = "texture1DProj";
5510 coord_mask = "xw";
5512 else
5514 texture_function = "texture1D";
5515 coord_mask = "x";
5517 break;
5518 case tex_2d:
5519 if (proj)
5521 texture_function = "texture2DProj";
5522 coord_mask = "xyw";
5524 else
5526 texture_function = "texture2D";
5527 coord_mask = "xy";
5529 break;
5530 case tex_3d:
5531 if (proj)
5533 texture_function = "texture3DProj";
5534 coord_mask = "xyzw";
5536 else
5538 texture_function = "texture3D";
5539 coord_mask = "xyz";
5541 break;
5542 case tex_cube:
5543 texture_function = "textureCube";
5544 coord_mask = "xyz";
5545 break;
5546 case tex_rect:
5547 if (proj)
5549 texture_function = "texture2DRectProj";
5550 coord_mask = "xyw";
5552 else
5554 texture_function = "texture2DRect";
5555 coord_mask = "xy";
5557 break;
5558 default:
5559 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5560 texture_function = "";
5561 coord_mask = "xyzw";
5562 break;
5565 if (stage > 0
5566 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5567 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5569 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5571 /* With projective textures, texbem only divides the static
5572 * texture coord, not the displacement, so multiply the
5573 * displacement with the dividing parameter before passing it to
5574 * TXP. */
5575 if (settings->op[stage].projected != proj_none)
5577 if (settings->op[stage].projected == proj_count4)
5579 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5580 stage, stage);
5581 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5583 else
5585 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5586 stage, stage);
5587 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5590 else
5592 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5595 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5596 stage, texture_function, stage, coord_mask);
5598 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5599 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5600 stage, stage - 1, stage - 1, stage - 1);
5602 else if (settings->op[stage].projected == proj_count3)
5604 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5605 stage, texture_function, stage, stage);
5607 else
5609 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5610 stage, texture_function, stage, stage, coord_mask);
5613 sprintf(tex_reg_name, "tex%u", stage);
5614 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5615 settings->op[stage].color_fixup);
5618 /* Generate the main shader */
5619 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5621 BOOL op_equal;
5623 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5625 if (!stage)
5626 final_combiner_src = "gl_Color";
5627 break;
5630 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5631 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5632 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5633 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5634 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5635 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5636 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5637 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5638 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5639 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5640 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5641 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5642 else
5643 op_equal = settings->op[stage].aop == settings->op[stage].cop
5644 && settings->op[stage].carg0 == settings->op[stage].aarg0
5645 && settings->op[stage].carg1 == settings->op[stage].aarg1
5646 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5648 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5650 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5651 settings->op[stage].cop, settings->op[stage].carg0,
5652 settings->op[stage].carg1, settings->op[stage].carg2);
5653 if (!stage)
5654 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5656 else if (op_equal)
5658 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5659 settings->op[stage].cop, settings->op[stage].carg0,
5660 settings->op[stage].carg1, settings->op[stage].carg2);
5662 else
5664 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5665 settings->op[stage].cop, settings->op[stage].carg0,
5666 settings->op[stage].carg1, settings->op[stage].carg2);
5667 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5668 settings->op[stage].aop, settings->op[stage].aarg0,
5669 settings->op[stage].aarg1, settings->op[stage].aarg2);
5673 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5675 if (settings->sRGB_write)
5676 shader_glsl_generate_srgb_write_correction(buffer);
5678 shader_glsl_generate_fog_code(buffer, settings->fog);
5680 shader_addline(buffer, "}\n");
5682 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5683 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5684 return shader_id;
5687 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5688 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5690 struct glsl_ffp_vertex_shader *shader;
5691 const struct wine_rb_entry *entry;
5693 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5694 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5696 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5697 return NULL;
5699 shader->desc.settings = *settings;
5700 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5701 list_init(&shader->linked_programs);
5702 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5703 ERR("Failed to insert ffp vertex shader.\n");
5705 return shader;
5708 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5709 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5711 struct glsl_ffp_fragment_shader *glsl_desc;
5712 const struct ffp_frag_desc *desc;
5714 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5715 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5717 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5718 return NULL;
5720 glsl_desc->entry.settings = *args;
5721 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5722 list_init(&glsl_desc->linked_programs);
5723 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5725 return glsl_desc;
5729 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5730 GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5732 unsigned int i;
5733 char name[32];
5735 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5736 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
5737 for (i = 0; i < vs_c_count; ++i)
5739 snprintf(name, sizeof(name), "vs_c[%u]", i);
5740 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5742 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5743 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
5745 for (i = 0; i < MAX_CONST_I; ++i)
5747 snprintf(name, sizeof(name), "vs_i[%u]", i);
5748 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5751 for (i = 0; i < MAX_CONST_B; ++i)
5753 snprintf(name, sizeof(name), "vs_b[%u]", i);
5754 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5757 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
5760 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5761 GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5763 unsigned int i;
5764 char name[32];
5766 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5767 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
5768 for (i = 0; i < ps_c_count; ++i)
5770 snprintf(name, sizeof(name), "ps_c[%u]", i);
5771 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5773 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5774 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
5776 for (i = 0; i < MAX_CONST_I; ++i)
5778 snprintf(name, sizeof(name), "ps_i[%u]", i);
5779 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5782 for (i = 0; i < MAX_CONST_B; ++i)
5784 snprintf(name, sizeof(name), "ps_b[%u]", i);
5785 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5788 for (i = 0; i < MAX_TEXTURES; ++i)
5790 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5791 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5792 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5793 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5794 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5795 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5796 snprintf(name, sizeof(name), "tss_const%u", i);
5797 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name));
5800 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
5801 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
5802 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
5803 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
5806 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info, GLuint program_id,
5807 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
5809 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
5810 GLuint block_idx;
5811 unsigned int i;
5812 char name[16];
5814 for (i = 0; i < count; ++i)
5816 if (!reg_maps->cb_sizes[i])
5817 continue;
5819 snprintf(name, sizeof(name), "block_%s_cb%u", prefix, i);
5820 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name));
5821 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
5823 checkGLcall("glUniformBlockBinding");
5826 /* Context activation is done by the caller. */
5827 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5828 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5830 const struct wined3d_gl_info *gl_info = context->gl_info;
5831 const struct ps_np2fixup_info *np2fixup_info = NULL;
5832 struct glsl_shader_prog_link *entry = NULL;
5833 struct wined3d_shader *vshader = NULL;
5834 struct wined3d_shader *gshader = NULL;
5835 struct wined3d_shader *pshader = NULL;
5836 GLuint program_id = 0;
5837 GLuint reorder_shader_id = 0;
5838 unsigned int i;
5839 GLuint vs_id = 0;
5840 GLuint gs_id = 0;
5841 GLuint ps_id = 0;
5842 struct list *ps_list, *vs_list;
5844 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5846 vs_id = ctx_data->glsl_program->vs.id;
5847 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5849 if (use_vs(state))
5851 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5852 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5854 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5855 && ctx_data->glsl_program->gs.id)
5856 gs_id = ctx_data->glsl_program->gs.id;
5857 else if (gshader)
5858 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5861 else if (use_vs(state))
5863 struct vs_compile_args vs_compile_args;
5864 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5866 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
5867 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5868 vs_list = &vshader->linked_programs;
5870 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
5871 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5873 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5875 struct glsl_ffp_vertex_shader *ffp_shader;
5876 struct wined3d_ffp_vs_settings settings;
5878 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
5879 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5880 vs_id = ffp_shader->id;
5881 vs_list = &ffp_shader->linked_programs;
5884 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
5886 ps_id = ctx_data->glsl_program->ps.id;
5887 ps_list = &ctx_data->glsl_program->ps.shader_entry;
5889 if (use_ps(state))
5890 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5892 else if (use_ps(state))
5894 struct ps_compile_args ps_compile_args;
5895 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5896 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
5897 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5898 pshader, &ps_compile_args, &np2fixup_info);
5899 ps_list = &pshader->linked_programs;
5901 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5903 struct glsl_ffp_fragment_shader *ffp_shader;
5904 struct ffp_frag_settings settings;
5906 gen_ffp_frag_op(context, state, &settings, FALSE);
5907 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5908 ps_id = ffp_shader->id;
5909 ps_list = &ffp_shader->linked_programs;
5912 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5914 ctx_data->glsl_program = entry;
5915 return;
5918 /* If we get to this point, then no matching program exists, so we create one */
5919 program_id = GL_EXTCALL(glCreateProgram());
5920 TRACE("Created new GLSL shader program %u.\n", program_id);
5922 /* Create the entry */
5923 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5924 entry->id = program_id;
5925 entry->vs.id = vs_id;
5926 entry->gs.id = gs_id;
5927 entry->ps.id = ps_id;
5928 entry->constant_version = 0;
5929 entry->ps.np2_fixup_info = np2fixup_info;
5930 /* Add the hash table entry */
5931 add_glsl_program_entry(priv, entry);
5933 /* Set the current program */
5934 ctx_data->glsl_program = entry;
5936 /* Attach GLSL vshader */
5937 if (vs_id)
5939 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
5940 GL_EXTCALL(glAttachShader(program_id, vs_id));
5941 checkGLcall("glAttachShader");
5943 list_add_head(vs_list, &entry->vs.shader_entry);
5946 if (vshader)
5948 WORD map = vshader->reg_maps.input_registers;
5949 char tmp_name[10];
5951 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5952 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
5953 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
5954 checkGLcall("glAttachShader");
5955 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5956 * is destroyed
5958 GL_EXTCALL(glDeleteShader(reorder_shader_id));
5960 /* Bind vertex attributes to a corresponding index number to match
5961 * the same index numbers as ARB_vertex_programs (makes loading
5962 * vertex attributes simpler). With this method, we can use the
5963 * exact same code to load the attributes later for both ARB and
5964 * GLSL shaders.
5966 * We have to do this here because we need to know the Program ID
5967 * in order to make the bindings work, and it has to be done prior
5968 * to linking the GLSL program. */
5969 for (i = 0; map; map >>= 1, ++i)
5971 if (!(map & 1)) continue;
5973 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5974 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name));
5976 checkGLcall("glBindAttribLocation");
5979 if (gshader)
5981 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
5982 GL_EXTCALL(glAttachShader(program_id, gs_id));
5983 checkGLcall("glAttachShader");
5985 TRACE("input type %s, output type %s, vertices out %u.\n",
5986 debug_d3dprimitivetype(gshader->u.gs.input_type),
5987 debug_d3dprimitivetype(gshader->u.gs.output_type),
5988 gshader->u.gs.vertices_out);
5989 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
5990 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5991 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5992 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5993 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
5994 gshader->u.gs.vertices_out));
5995 checkGLcall("glProgramParameteriARB");
5997 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
6000 /* Attach GLSL pshader */
6001 if (ps_id)
6003 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
6004 GL_EXTCALL(glAttachShader(program_id, ps_id));
6005 checkGLcall("glAttachShader");
6007 list_add_head(ps_list, &entry->ps.shader_entry);
6010 /* Link the program */
6011 TRACE("Linking GLSL shader program %u.\n", program_id);
6012 GL_EXTCALL(glLinkProgram(program_id));
6013 shader_glsl_validate_link(gl_info, program_id);
6015 shader_glsl_init_vs_uniform_locations(gl_info, program_id, &entry->vs,
6016 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6017 shader_glsl_init_ps_uniform_locations(gl_info, program_id, &entry->ps,
6018 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6019 checkGLcall("Find glsl program uniform locations");
6021 if (pshader && pshader->reg_maps.shader_version.major >= 3
6022 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6024 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6025 entry->vs.vertex_color_clamp = GL_FALSE;
6027 else
6029 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6032 /* Set the shader to allow uniform loading on it */
6033 GL_EXTCALL(glUseProgram(program_id));
6034 checkGLcall("glUseProgram");
6036 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6037 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6038 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6039 * vertex shader with fixed function pixel processing is used we make sure that the card
6040 * supports enough samplers to allow the max number of vertex samplers with all possible
6041 * fixed function fragment processing setups. So once the program is linked these samplers
6042 * won't change. */
6043 shader_glsl_load_samplers(gl_info, context->tex_unit_map, program_id);
6045 entry->constant_update_mask = 0;
6046 if (vshader)
6048 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6049 if (vshader->reg_maps.integer_constants)
6050 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6051 if (vshader->reg_maps.boolean_constants)
6052 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6053 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6055 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &vshader->reg_maps,
6056 0, gl_info->limits.vertex_uniform_blocks);
6059 if (gshader)
6060 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &gshader->reg_maps,
6061 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6063 if (ps_id)
6065 if (pshader)
6067 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6068 if (pshader->reg_maps.integer_constants)
6069 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6070 if (pshader->reg_maps.boolean_constants)
6071 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6072 if (entry->ps.ycorrection_location != -1)
6073 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6075 shader_glsl_init_uniform_block_bindings(gl_info, program_id, &pshader->reg_maps,
6076 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6077 gl_info->limits.fragment_uniform_blocks);
6079 else
6081 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6084 for (i = 0; i < MAX_TEXTURES; ++i)
6086 if (entry->ps.bumpenv_mat_location[i] != -1)
6088 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6089 break;
6093 if (entry->ps.np2_fixup_location != -1)
6094 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6098 /* Context activation is done by the caller. */
6099 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6101 GLuint program_id;
6102 GLuint vshader_id, pshader_id;
6103 const char *blt_pshader;
6105 static const char blt_vshader[] =
6106 "#version 120\n"
6107 "void main(void)\n"
6108 "{\n"
6109 " gl_Position = gl_Vertex;\n"
6110 " gl_FrontColor = vec4(1.0);\n"
6111 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6112 "}\n";
6114 static const char * const blt_pshaders_full[tex_type_count] =
6116 /* tex_1d */
6117 NULL,
6118 /* tex_2d */
6119 "#version 120\n"
6120 "uniform sampler2D sampler;\n"
6121 "void main(void)\n"
6122 "{\n"
6123 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6124 "}\n",
6125 /* tex_3d */
6126 NULL,
6127 /* tex_cube */
6128 "#version 120\n"
6129 "uniform samplerCube sampler;\n"
6130 "void main(void)\n"
6131 "{\n"
6132 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6133 "}\n",
6134 /* tex_rect */
6135 "#version 120\n"
6136 "#extension GL_ARB_texture_rectangle : enable\n"
6137 "uniform sampler2DRect sampler;\n"
6138 "void main(void)\n"
6139 "{\n"
6140 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6141 "}\n",
6144 static const char * const blt_pshaders_masked[tex_type_count] =
6146 /* tex_1d */
6147 NULL,
6148 /* tex_2d */
6149 "#version 120\n"
6150 "uniform sampler2D sampler;\n"
6151 "uniform vec4 mask;\n"
6152 "void main(void)\n"
6153 "{\n"
6154 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6155 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6156 "}\n",
6157 /* tex_3d */
6158 NULL,
6159 /* tex_cube */
6160 "#version 120\n"
6161 "uniform samplerCube sampler;\n"
6162 "uniform vec4 mask;\n"
6163 "void main(void)\n"
6164 "{\n"
6165 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6166 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6167 "}\n",
6168 /* tex_rect */
6169 "#version 120\n"
6170 "#extension GL_ARB_texture_rectangle : enable\n"
6171 "uniform sampler2DRect sampler;\n"
6172 "uniform vec4 mask;\n"
6173 "void main(void)\n"
6174 "{\n"
6175 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6176 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6177 "}\n",
6180 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6181 if (!blt_pshader)
6183 FIXME("tex_type %#x not supported\n", tex_type);
6184 return 0;
6187 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6188 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6190 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6191 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6193 program_id = GL_EXTCALL(glCreateProgram());
6194 GL_EXTCALL(glAttachShader(program_id, vshader_id));
6195 GL_EXTCALL(glAttachShader(program_id, pshader_id));
6196 GL_EXTCALL(glLinkProgram(program_id));
6198 shader_glsl_validate_link(gl_info, program_id);
6200 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6201 * is destroyed
6203 GL_EXTCALL(glDeleteShader(vshader_id));
6204 GL_EXTCALL(glDeleteShader(pshader_id));
6205 return program_id;
6208 /* Context activation is done by the caller. */
6209 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6210 const struct wined3d_state *state)
6212 struct glsl_context_data *ctx_data = context->shader_backend_data;
6213 const struct wined3d_gl_info *gl_info = context->gl_info;
6214 struct shader_glsl_priv *priv = shader_priv;
6215 GLuint program_id = 0, prev_id = 0;
6216 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6218 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6219 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6221 if (ctx_data->glsl_program)
6223 prev_id = ctx_data->glsl_program->id;
6224 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6226 else
6228 prev_id = 0;
6229 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6232 set_glsl_shader_program(context, state, priv, ctx_data);
6234 if (ctx_data->glsl_program)
6236 program_id = ctx_data->glsl_program->id;
6237 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6239 else
6241 program_id = 0;
6242 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6245 if (old_vertex_color_clamp != current_vertex_color_clamp)
6247 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6249 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6250 checkGLcall("glClampColorARB");
6252 else
6254 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6258 TRACE("Using GLSL program %u.\n", program_id);
6260 if (prev_id != program_id)
6262 GL_EXTCALL(glUseProgram(program_id));
6263 checkGLcall("glUseProgram");
6265 if (program_id)
6266 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6270 /* "context" is not necessarily the currently active context. */
6271 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6273 struct glsl_context_data *ctx_data = context->shader_backend_data;
6275 ctx_data->glsl_program = NULL;
6276 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6277 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6278 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6281 /* Context activation is done by the caller. */
6282 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6284 const struct wined3d_gl_info *gl_info = context->gl_info;
6285 struct shader_glsl_priv *priv = shader_priv;
6287 shader_glsl_invalidate_current_program(context);
6288 GL_EXTCALL(glUseProgram(0));
6289 checkGLcall("glUseProgram");
6291 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6292 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6294 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6296 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6297 checkGLcall("glClampColorARB");
6301 /* Context activation is done by the caller. */
6302 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6303 enum tex_types tex_type, const SIZE *ds_mask_size)
6305 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6306 struct shader_glsl_priv *priv = shader_priv;
6307 GLuint *blt_program;
6308 GLint loc;
6310 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6311 if (!*blt_program)
6313 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6314 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
6315 GL_EXTCALL(glUseProgram(*blt_program));
6316 GL_EXTCALL(glUniform1i(loc, 0));
6318 else
6320 GL_EXTCALL(glUseProgram(*blt_program));
6323 if (masked)
6325 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
6326 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6330 /* Context activation is done by the caller. */
6331 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6333 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6334 GLuint program_id;
6336 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
6337 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6339 GL_EXTCALL(glUseProgram(program_id));
6340 checkGLcall("glUseProgram");
6343 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6344 const struct glsl_shader_prog_link *program)
6346 const struct glsl_context_data *ctx_data;
6347 struct wined3d_context *context;
6348 unsigned int i;
6350 for (i = 0; i < device->context_count; ++i)
6352 context = device->contexts[i];
6353 ctx_data = context->shader_backend_data;
6355 if (ctx_data->glsl_program == program)
6356 shader_glsl_invalidate_current_program(context);
6360 static void shader_glsl_destroy(struct wined3d_shader *shader)
6362 struct glsl_shader_private *shader_data = shader->backend_data;
6363 struct wined3d_device *device = shader->device;
6364 struct shader_glsl_priv *priv = device->shader_priv;
6365 const struct wined3d_gl_info *gl_info;
6366 const struct list *linked_programs;
6367 struct wined3d_context *context;
6369 if (!shader_data || !shader_data->num_gl_shaders)
6371 HeapFree(GetProcessHeap(), 0, shader_data);
6372 shader->backend_data = NULL;
6373 return;
6376 context = context_acquire(device, NULL);
6377 gl_info = context->gl_info;
6379 TRACE("Deleting linked programs.\n");
6380 linked_programs = &shader->linked_programs;
6381 if (linked_programs->next)
6383 struct glsl_shader_prog_link *entry, *entry2;
6384 UINT i;
6386 switch (shader->reg_maps.shader_version.type)
6388 case WINED3D_SHADER_TYPE_PIXEL:
6390 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6392 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6394 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
6395 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6396 checkGLcall("glDeleteShader");
6398 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6400 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6401 struct glsl_shader_prog_link, ps.shader_entry)
6403 shader_glsl_invalidate_contexts_program(device, entry);
6404 delete_glsl_program_entry(priv, gl_info, entry);
6407 break;
6410 case WINED3D_SHADER_TYPE_VERTEX:
6412 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6414 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6416 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
6417 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6418 checkGLcall("glDeleteShader");
6420 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6422 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6423 struct glsl_shader_prog_link, vs.shader_entry)
6425 shader_glsl_invalidate_contexts_program(device, entry);
6426 delete_glsl_program_entry(priv, gl_info, entry);
6429 break;
6432 case WINED3D_SHADER_TYPE_GEOMETRY:
6434 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6436 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6438 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6439 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6440 checkGLcall("glDeleteShader");
6442 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6444 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6445 struct glsl_shader_prog_link, gs.shader_entry)
6447 shader_glsl_invalidate_contexts_program(device, entry);
6448 delete_glsl_program_entry(priv, gl_info, entry);
6451 break;
6454 default:
6455 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6456 break;
6460 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6461 shader->backend_data = NULL;
6463 context_release(context);
6466 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6468 const struct glsl_program_key *k = key;
6469 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6470 const struct glsl_shader_prog_link, program_lookup_entry);
6472 if (k->vs_id > prog->vs.id) return 1;
6473 else if (k->vs_id < prog->vs.id) return -1;
6475 if (k->gs_id > prog->gs.id) return 1;
6476 else if (k->gs_id < prog->gs.id) return -1;
6478 if (k->ps_id > prog->ps.id) return 1;
6479 else if (k->ps_id < prog->ps.id) return -1;
6481 return 0;
6484 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6486 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6487 + constant_count * sizeof(*heap->contained)
6488 + constant_count * sizeof(*heap->positions);
6489 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6491 if (!mem)
6493 ERR("Failed to allocate memory\n");
6494 return FALSE;
6497 heap->entries = mem;
6498 heap->entries[1].version = 0;
6499 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6500 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6501 heap->positions = (unsigned int *)(heap->contained + constant_count);
6502 heap->size = 1;
6504 return TRUE;
6507 static void constant_heap_free(struct constant_heap *heap)
6509 HeapFree(GetProcessHeap(), 0, heap->entries);
6512 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6514 wined3d_rb_alloc,
6515 wined3d_rb_realloc,
6516 wined3d_rb_free,
6517 glsl_program_key_compare,
6520 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6521 const struct fragment_pipeline *fragment_pipe)
6523 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6524 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6525 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6526 gl_info->limits.glsl_ps_float_constants)) + 1;
6527 struct fragment_caps fragment_caps;
6528 void *vertex_priv, *fragment_priv;
6530 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6532 ERR("Failed to initialize vertex pipe.\n");
6533 HeapFree(GetProcessHeap(), 0, priv);
6534 return E_FAIL;
6537 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6539 ERR("Failed to initialize fragment pipe.\n");
6540 vertex_pipe->vp_free(device);
6541 HeapFree(GetProcessHeap(), 0, priv);
6542 return E_FAIL;
6545 if (!shader_buffer_init(&priv->shader_buffer))
6547 ERR("Failed to initialize shader buffer.\n");
6548 goto fail;
6551 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6552 if (!priv->stack)
6554 ERR("Failed to allocate memory.\n");
6555 goto fail;
6558 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6560 ERR("Failed to initialize vertex shader constant heap\n");
6561 goto fail;
6564 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6566 ERR("Failed to initialize pixel shader constant heap\n");
6567 goto fail;
6570 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6572 ERR("Failed to initialize rbtree.\n");
6573 goto fail;
6576 priv->next_constant_version = 1;
6577 priv->vertex_pipe = vertex_pipe;
6578 priv->fragment_pipe = fragment_pipe;
6579 fragment_pipe->get_caps(gl_info, &fragment_caps);
6580 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6582 device->vertex_priv = vertex_priv;
6583 device->fragment_priv = fragment_priv;
6584 device->shader_priv = priv;
6586 return WINED3D_OK;
6588 fail:
6589 constant_heap_free(&priv->pconst_heap);
6590 constant_heap_free(&priv->vconst_heap);
6591 HeapFree(GetProcessHeap(), 0, priv->stack);
6592 shader_buffer_free(&priv->shader_buffer);
6593 fragment_pipe->free_private(device);
6594 vertex_pipe->vp_free(device);
6595 HeapFree(GetProcessHeap(), 0, priv);
6596 return E_OUTOFMEMORY;
6599 /* Context activation is done by the caller. */
6600 static void shader_glsl_free(struct wined3d_device *device)
6602 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6603 struct shader_glsl_priv *priv = device->shader_priv;
6604 int i;
6606 for (i = 0; i < tex_type_count; ++i)
6608 if (priv->depth_blt_program_full[i])
6610 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
6612 if (priv->depth_blt_program_masked[i])
6614 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
6618 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6619 constant_heap_free(&priv->pconst_heap);
6620 constant_heap_free(&priv->vconst_heap);
6621 HeapFree(GetProcessHeap(), 0, priv->stack);
6622 shader_buffer_free(&priv->shader_buffer);
6623 priv->fragment_pipe->free_private(device);
6624 priv->vertex_pipe->vp_free(device);
6626 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6627 device->shader_priv = NULL;
6630 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6632 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6633 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6636 static void shader_glsl_free_context_data(struct wined3d_context *context)
6638 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6641 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6643 UINT shader_model;
6645 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6646 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6647 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
6648 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
6649 shader_model = 4;
6650 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6651 * texldd and texldl instructions. */
6652 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6653 shader_model = 3;
6654 else
6655 shader_model = 2;
6656 TRACE("Shader model %u.\n", shader_model);
6658 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6659 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6660 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6662 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6663 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6665 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6666 * Direct3D minimum requirement.
6668 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6669 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6671 * The problem is that the refrast clamps temporary results in the shader to
6672 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6673 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6674 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6675 * offer a way to query this.
6677 if (shader_model >= 4)
6678 caps->ps_1x_max_value = FLT_MAX;
6679 else
6680 caps->ps_1x_max_value = 1024.0f;
6682 /* Ideally we'd only set caps like sRGB writes here if supported by both
6683 * the shader backend and the fragment pipe, but we can get called before
6684 * shader_glsl_alloc(). */
6685 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6686 | WINED3D_SHADER_CAP_SRGB_WRITE;
6689 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6691 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6693 TRACE("Checking support for fixup:\n");
6694 dump_color_fixup_desc(fixup);
6697 /* We support everything except YUV conversions. */
6698 if (!is_complex_fixup(fixup))
6700 TRACE("[OK]\n");
6701 return TRUE;
6704 TRACE("[FAILED]\n");
6705 return FALSE;
6708 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6710 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6711 /* WINED3DSIH_ADD */ shader_glsl_binop,
6712 /* WINED3DSIH_AND */ shader_glsl_binop,
6713 /* WINED3DSIH_BEM */ shader_glsl_bem,
6714 /* WINED3DSIH_BREAK */ shader_glsl_break,
6715 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6716 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6717 /* WINED3DSIH_CALL */ shader_glsl_call,
6718 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6719 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6720 /* WINED3DSIH_CND */ shader_glsl_cnd,
6721 /* WINED3DSIH_CRS */ shader_glsl_cross,
6722 /* WINED3DSIH_CUT */ shader_glsl_cut,
6723 /* WINED3DSIH_DCL */ shader_glsl_nop,
6724 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6725 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6726 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6727 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6728 /* WINED3DSIH_DEF */ shader_glsl_nop,
6729 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6730 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6731 /* WINED3DSIH_DIV */ shader_glsl_binop,
6732 /* WINED3DSIH_DP2 */ shader_glsl_dot,
6733 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6734 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6735 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6736 /* WINED3DSIH_DST */ shader_glsl_dst,
6737 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6738 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6739 /* WINED3DSIH_ELSE */ shader_glsl_else,
6740 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6741 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6742 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6743 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6744 /* WINED3DSIH_EQ */ shader_glsl_relop,
6745 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6746 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6747 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6748 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6749 /* WINED3DSIH_GE */ shader_glsl_relop,
6750 /* WINED3DSIH_IADD */ shader_glsl_binop,
6751 /* WINED3DSIH_IEQ */ NULL,
6752 /* WINED3DSIH_IF */ shader_glsl_if,
6753 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6754 /* WINED3DSIH_IGE */ shader_glsl_relop,
6755 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6756 /* WINED3DSIH_ISHL */ shader_glsl_binop,
6757 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6758 /* WINED3DSIH_LABEL */ shader_glsl_label,
6759 /* WINED3DSIH_LD */ NULL,
6760 /* WINED3DSIH_LIT */ shader_glsl_lit,
6761 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6762 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6763 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6764 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6765 /* WINED3DSIH_LT */ shader_glsl_relop,
6766 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6767 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6768 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6769 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6770 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6771 /* WINED3DSIH_MAD */ shader_glsl_mad,
6772 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6773 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6774 /* WINED3DSIH_MOV */ shader_glsl_mov,
6775 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6776 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6777 /* WINED3DSIH_MUL */ shader_glsl_binop,
6778 /* WINED3DSIH_NE */ shader_glsl_relop,
6779 /* WINED3DSIH_NOP */ shader_glsl_nop,
6780 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6781 /* WINED3DSIH_OR */ shader_glsl_binop,
6782 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6783 /* WINED3DSIH_POW */ shader_glsl_pow,
6784 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6785 /* WINED3DSIH_REP */ shader_glsl_rep,
6786 /* WINED3DSIH_RET */ shader_glsl_ret,
6787 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6788 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6789 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
6790 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6791 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6792 /* WINED3DSIH_SETP */ NULL,
6793 /* WINED3DSIH_SGE */ shader_glsl_compare,
6794 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6795 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6796 /* WINED3DSIH_SLT */ shader_glsl_compare,
6797 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
6798 /* WINED3DSIH_SUB */ shader_glsl_binop,
6799 /* WINED3DSIH_TEX */ shader_glsl_tex,
6800 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6801 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6802 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6803 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6804 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6805 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6806 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6807 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6808 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6809 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6810 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6811 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6812 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6813 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6814 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6815 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6816 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6817 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6818 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6819 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6820 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6821 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6822 /* WINED3DSIH_UGE */ shader_glsl_relop,
6823 /* WINED3DSIH_USHR */ shader_glsl_binop,
6824 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6825 /* WINED3DSIH_XOR */ shader_glsl_binop,
6828 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6829 SHADER_HANDLER hw_fct;
6831 /* Select handler */
6832 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6834 /* Unhandled opcode */
6835 if (!hw_fct)
6837 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6838 return;
6840 hw_fct(ins);
6842 shader_glsl_add_instruction_modifiers(ins);
6845 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6847 struct shader_glsl_priv *priv = shader_priv;
6849 return priv->ffp_proj_control;
6852 const struct wined3d_shader_backend_ops glsl_shader_backend =
6854 shader_glsl_handle_instruction,
6855 shader_glsl_select,
6856 shader_glsl_disable,
6857 shader_glsl_select_depth_blt,
6858 shader_glsl_deselect_depth_blt,
6859 shader_glsl_update_float_vertex_constants,
6860 shader_glsl_update_float_pixel_constants,
6861 shader_glsl_load_constants,
6862 shader_glsl_destroy,
6863 shader_glsl_alloc,
6864 shader_glsl_free,
6865 shader_glsl_allocate_context_data,
6866 shader_glsl_free_context_data,
6867 shader_glsl_get_caps,
6868 shader_glsl_color_fixup_supported,
6869 shader_glsl_has_ffp_proj_control,
6872 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6874 if (enable)
6875 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6876 else
6877 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6878 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6881 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6883 caps->xyzrhw = TRUE;
6884 caps->max_active_lights = gl_info->limits.lights;
6885 caps->max_vertex_blend_matrices = 1;
6886 caps->max_vertex_blend_matrix_index = 0;
6887 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6888 | WINED3DVTXPCAPS_MATERIALSOURCE7
6889 | WINED3DVTXPCAPS_VERTEXFOG
6890 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6891 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6892 | WINED3DVTXPCAPS_LOCALVIEWER
6893 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6894 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6895 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6896 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6899 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6901 struct shader_glsl_priv *priv;
6903 if (shader_backend == &glsl_shader_backend)
6905 priv = shader_priv;
6907 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6909 ERR("Failed to initialize rbtree.\n");
6910 return NULL;
6913 return priv;
6916 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6918 return NULL;
6921 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6923 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6924 struct glsl_ffp_vertex_shader, desc.entry);
6925 struct glsl_shader_prog_link *program, *program2;
6926 struct glsl_ffp_destroy_ctx *ctx = context;
6928 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6929 struct glsl_shader_prog_link, vs.shader_entry)
6931 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6933 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
6934 HeapFree(GetProcessHeap(), 0, shader);
6937 /* Context activation is done by the caller. */
6938 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6940 struct shader_glsl_priv *priv = device->vertex_priv;
6941 struct glsl_ffp_destroy_ctx ctx;
6943 ctx.priv = priv;
6944 ctx.gl_info = &device->adapter->gl_info;
6945 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6948 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6949 const struct wined3d_state *state, DWORD state_id)
6951 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6954 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
6955 const struct wined3d_state *state, DWORD state_id)
6957 /* Table fog behavior depends on the projection matrix. */
6958 if (state->render_states[WINED3D_RS_FOGENABLE]
6959 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6960 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6961 transform_projection(context, state, state_id);
6964 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6966 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6967 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6968 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6969 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6970 /* Clip planes */
6971 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6972 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6973 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6974 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6975 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6976 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6977 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6978 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6979 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6980 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6981 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6982 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6983 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6984 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6985 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6986 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6987 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6988 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6989 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6990 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6991 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6992 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6993 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6994 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6995 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6996 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6997 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6998 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6999 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
7000 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
7001 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
7002 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
7003 /* Lights */
7004 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7005 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
7006 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
7007 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
7008 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
7009 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
7010 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
7011 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
7012 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
7013 /* Viewport */
7014 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
7015 /* Transform states */
7016 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
7017 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
7018 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7019 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7020 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7021 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7022 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7023 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7024 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7025 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7026 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
7027 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7028 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7029 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7030 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7031 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7032 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7033 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7034 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
7035 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7036 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7037 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7038 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7039 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7040 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7041 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7042 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7043 /* Fog */
7044 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7045 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7046 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7047 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7048 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7049 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7050 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7051 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
7052 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7053 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7054 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7055 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7056 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7057 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7058 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7059 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7060 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7061 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7062 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7063 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7064 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7065 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7066 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7067 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7068 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7069 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7070 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7071 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7072 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7073 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7074 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7075 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7076 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7077 * handler in that case to get the vertex part of sampler() skipped (VTF
7078 * is handled in the misc states). Otherwise, register
7079 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7080 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7081 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7082 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7083 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7084 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7085 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7086 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7087 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7088 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7089 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7090 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7091 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7092 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7093 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7094 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7095 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7096 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7097 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7098 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7099 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7100 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7101 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7102 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7103 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7104 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7105 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7108 /* TODO:
7109 * - This currently depends on GL fixed function functions to set things
7110 * like light parameters. Ideally we'd use regular uniforms for that.
7111 * - In part because of the previous point, much of this is modelled after
7112 * GL fixed function, and has much of the same limitations. For example,
7113 * D3D spot lights are slightly different from GL spot lights.
7114 * - We can now implement drawing transformed vertices using the GLSL pipe,
7115 * instead of using the immediate mode fallback.
7116 * - Similarly, we don't need the fallback for certain combinations of
7117 * material sources anymore.
7118 * - Implement vertex blending and vertex tweening.
7119 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7120 * attribute arrays in load_tex_coords().
7121 * - Per-vertex point sizes. */
7122 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7124 glsl_vertex_pipe_vp_enable,
7125 glsl_vertex_pipe_vp_get_caps,
7126 glsl_vertex_pipe_vp_alloc,
7127 glsl_vertex_pipe_vp_free,
7128 glsl_vertex_pipe_vp_states,
7131 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7133 /* Nothing to do. */
7136 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7138 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7139 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7140 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7141 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7142 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7143 | WINED3DTEXOPCAPS_SELECTARG1
7144 | WINED3DTEXOPCAPS_SELECTARG2
7145 | WINED3DTEXOPCAPS_MODULATE4X
7146 | WINED3DTEXOPCAPS_MODULATE2X
7147 | WINED3DTEXOPCAPS_MODULATE
7148 | WINED3DTEXOPCAPS_ADDSIGNED2X
7149 | WINED3DTEXOPCAPS_ADDSIGNED
7150 | WINED3DTEXOPCAPS_ADD
7151 | WINED3DTEXOPCAPS_SUBTRACT
7152 | WINED3DTEXOPCAPS_ADDSMOOTH
7153 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7154 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7155 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7156 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7157 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7158 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7159 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7160 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7161 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7162 | WINED3DTEXOPCAPS_DOTPRODUCT3
7163 | WINED3DTEXOPCAPS_MULTIPLYADD
7164 | WINED3DTEXOPCAPS_LERP
7165 | WINED3DTEXOPCAPS_BUMPENVMAP
7166 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7167 caps->MaxTextureBlendStages = 8;
7168 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7171 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7173 struct shader_glsl_priv *priv;
7175 if (shader_backend == &glsl_shader_backend)
7177 priv = shader_priv;
7179 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7181 ERR("Failed to initialize rbtree.\n");
7182 return NULL;
7185 return priv;
7188 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7190 return NULL;
7193 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7195 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7196 struct glsl_ffp_fragment_shader, entry.entry);
7197 struct glsl_shader_prog_link *program, *program2;
7198 struct glsl_ffp_destroy_ctx *ctx = context;
7200 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7201 struct glsl_shader_prog_link, ps.shader_entry)
7203 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7205 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7206 HeapFree(GetProcessHeap(), 0, shader);
7209 /* Context activation is done by the caller. */
7210 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7212 struct shader_glsl_priv *priv = device->fragment_priv;
7213 struct glsl_ffp_destroy_ctx ctx;
7215 ctx.priv = priv;
7216 ctx.gl_info = &device->adapter->gl_info;
7217 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7220 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7221 const struct wined3d_state *state, DWORD state_id)
7223 context->last_was_pshader = use_ps(state);
7225 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7228 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7229 const struct wined3d_state *state, DWORD state_id)
7231 BOOL use_vshader = use_vs(state);
7232 enum fogsource new_source;
7233 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7234 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7236 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7238 if (!state->render_states[WINED3D_RS_FOGENABLE])
7239 return;
7241 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7243 if (use_vshader)
7244 new_source = FOGSOURCE_VS;
7245 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7246 new_source = FOGSOURCE_COORD;
7247 else
7248 new_source = FOGSOURCE_FFP;
7250 else
7252 new_source = FOGSOURCE_FFP;
7255 if (new_source != context->fog_source || fogstart == fogend)
7257 context->fog_source = new_source;
7258 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7262 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7263 const struct wined3d_state *state, DWORD state_id)
7265 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7268 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7269 const struct wined3d_state *state, DWORD state_id)
7271 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7274 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7276 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7277 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7278 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7279 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7280 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7281 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7282 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7283 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7284 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7285 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7286 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7287 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7288 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7289 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7290 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7291 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7292 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7293 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7294 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7295 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7296 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7297 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7298 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7299 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7300 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7301 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7302 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7303 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7304 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7305 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7306 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7307 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7308 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7309 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7310 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7311 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7312 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7313 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7314 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7315 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7316 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7317 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7318 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7319 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7320 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7321 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7322 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7323 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7324 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7325 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7326 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7327 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7328 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7329 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7330 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7331 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7332 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7333 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7334 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7335 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7336 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7337 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7338 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7339 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7340 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7341 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7342 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7343 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7344 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7345 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7346 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7347 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7348 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7349 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7350 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7351 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7352 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7353 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7354 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7355 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7356 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7357 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7358 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7359 {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 },
7360 {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 },
7361 {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 },
7362 {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 },
7363 {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 },
7364 {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 },
7365 {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 },
7366 {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 },
7367 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7368 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7369 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7370 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7371 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7372 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7373 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7374 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7375 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7376 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7379 const struct fragment_pipeline glsl_fragment_pipe =
7381 glsl_fragment_pipe_enable,
7382 glsl_fragment_pipe_get_caps,
7383 glsl_fragment_pipe_alloc,
7384 glsl_fragment_pipe_free,
7385 shader_glsl_color_fixup_supported,
7386 glsl_fragment_pipe_state_template,