wined3d: Store the directional light direction in the lightDirn field.
[wine.git] / dlls / wined3d / glsl_shader.c
blobe49aff7f4c6ceefd0185cb40dadbae1b8956052d
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
32 #include "config.h"
33 #include "wine/port.h"
35 #include <limits.h>
36 #include <stdio.h>
37 #ifdef HAVE_FLOAT_H
38 # include <float.h>
39 #endif
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
49 #define WINED3D_GLSL_SAMPLE_LOD 0x4
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
52 struct glsl_dst_param
54 char reg_name[150];
55 char mask_str[6];
58 struct glsl_src_param
60 char reg_name[150];
61 char param_str[200];
64 struct glsl_sample_function
66 const char *name;
67 DWORD coord_mask;
68 enum wined3d_data_type data_type;
71 enum heap_node_op
73 HEAP_NODE_TRAVERSE_LEFT,
74 HEAP_NODE_TRAVERSE_RIGHT,
75 HEAP_NODE_POP,
78 struct constant_entry
80 unsigned int idx;
81 unsigned int version;
84 struct constant_heap
86 struct constant_entry *entries;
87 BOOL *contained;
88 unsigned int *positions;
89 unsigned int size;
92 /* GLSL shader private data */
93 struct shader_glsl_priv {
94 struct wined3d_string_buffer shader_buffer;
95 struct wined3d_string_buffer_list string_buffers;
96 struct wine_rb_tree program_lookup;
97 struct constant_heap vconst_heap;
98 struct constant_heap pconst_heap;
99 unsigned char *stack;
100 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
101 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
102 UINT next_constant_version;
104 const struct wined3d_vertex_pipe_ops *vertex_pipe;
105 const struct fragment_pipeline *fragment_pipe;
106 struct wine_rb_tree ffp_vertex_shaders;
107 struct wine_rb_tree ffp_fragment_shaders;
108 BOOL ffp_proj_control;
111 struct glsl_vs_program
113 struct list shader_entry;
114 GLuint id;
115 GLenum vertex_color_clamp;
116 GLint *uniform_f_locations;
117 GLint uniform_i_locations[MAX_CONST_I];
118 GLint uniform_b_locations[MAX_CONST_B];
119 GLint pos_fixup_location;
121 GLint modelview_matrix_location;
122 GLint projection_matrix_location;
123 GLint normal_matrix_location;
124 GLint texture_matrix_location[MAX_TEXTURES];
125 GLint material_ambient_location;
126 GLint material_diffuse_location;
127 GLint material_specular_location;
128 GLint material_emission_location;
129 GLint material_shininess_location;
132 struct glsl_gs_program
134 struct list shader_entry;
135 GLuint id;
138 struct glsl_ps_program
140 struct list shader_entry;
141 GLuint id;
142 GLint *uniform_f_locations;
143 GLint uniform_i_locations[MAX_CONST_I];
144 GLint uniform_b_locations[MAX_CONST_B];
145 GLint bumpenv_mat_location[MAX_TEXTURES];
146 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
147 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
148 GLint tss_constant_location[MAX_TEXTURES];
149 GLint tex_factor_location;
150 GLint specular_enable_location;
151 GLint ycorrection_location;
152 GLint np2_fixup_location;
153 GLint color_key_location;
154 const struct ps_np2fixup_info *np2_fixup_info;
157 /* Struct to maintain data about a linked GLSL program */
158 struct glsl_shader_prog_link
160 struct wine_rb_entry program_lookup_entry;
161 struct glsl_vs_program vs;
162 struct glsl_gs_program gs;
163 struct glsl_ps_program ps;
164 GLuint id;
165 DWORD constant_update_mask;
166 UINT constant_version;
169 struct glsl_program_key
171 GLuint vs_id;
172 GLuint gs_id;
173 GLuint ps_id;
176 struct shader_glsl_ctx_priv {
177 const struct vs_compile_args *cur_vs_args;
178 const struct ps_compile_args *cur_ps_args;
179 struct ps_np2fixup_info *cur_np2fixup_info;
182 struct glsl_context_data
184 struct glsl_shader_prog_link *glsl_program;
187 struct glsl_ps_compiled_shader
189 struct ps_compile_args args;
190 struct ps_np2fixup_info np2fixup;
191 GLuint id;
194 struct glsl_vs_compiled_shader
196 struct vs_compile_args args;
197 GLuint id;
200 struct glsl_gs_compiled_shader
202 GLuint id;
205 struct glsl_shader_private
207 union
209 struct glsl_vs_compiled_shader *vs;
210 struct glsl_gs_compiled_shader *gs;
211 struct glsl_ps_compiled_shader *ps;
212 } gl_shaders;
213 UINT num_gl_shaders, shader_array_size;
216 struct glsl_ffp_vertex_shader
218 struct wined3d_ffp_vs_desc desc;
219 GLuint id;
220 struct list linked_programs;
223 struct glsl_ffp_fragment_shader
225 struct ffp_frag_desc entry;
226 GLuint id;
227 struct list linked_programs;
230 struct glsl_ffp_destroy_ctx
232 struct shader_glsl_priv *priv;
233 const struct wined3d_gl_info *gl_info;
236 static const char *debug_gl_shader_type(GLenum type)
238 switch (type)
240 #define WINED3D_TO_STR(u) case u: return #u
241 WINED3D_TO_STR(GL_VERTEX_SHADER);
242 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
243 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
244 #undef WINED3D_TO_STR
245 default:
246 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
250 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
252 switch (type)
254 case WINED3D_SHADER_TYPE_VERTEX:
255 return "vs";
257 case WINED3D_SHADER_TYPE_GEOMETRY:
258 return "gs";
260 case WINED3D_SHADER_TYPE_PIXEL:
261 return "ps";
263 default:
264 FIXME("Unhandled shader type %#x.\n", type);
265 return "unknown";
269 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
270 const struct wined3d_shader_version *version)
272 if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version->major >= 4)
273 return "#version 130";
274 else
275 return "#version 120";
278 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
280 char str[4][17];
282 wined3d_ftoa(values[0], str[0]);
283 wined3d_ftoa(values[1], str[1]);
284 wined3d_ftoa(values[2], str[2]);
285 wined3d_ftoa(values[3], str[3]);
286 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
289 static const char *get_info_log_line(const char **ptr)
291 const char *p, *q;
293 p = *ptr;
294 if (!(q = strstr(p, "\n")))
296 if (!*p) return NULL;
297 *ptr += strlen(p);
298 return p;
300 *ptr = q + 1;
302 return p;
305 /* Context activation is done by the caller. */
306 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
308 int length = 0;
309 char *log;
311 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
312 return;
314 if (program)
315 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
316 else
317 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
319 /* A size of 1 is just a null-terminated string, so the log should be bigger than
320 * that if there are errors. */
321 if (length > 1)
323 const char *ptr, *line;
325 log = HeapAlloc(GetProcessHeap(), 0, length);
326 /* The info log is supposed to be zero-terminated, but at least some
327 * versions of fglrx don't terminate the string properly. The reported
328 * length does include the terminator, so explicitly set it to zero
329 * here. */
330 log[length - 1] = 0;
331 if (program)
332 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
333 else
334 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
336 ptr = log;
337 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
339 WARN("Info log received from GLSL shader #%u:\n", id);
340 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
342 else
344 FIXME("Info log received from GLSL shader #%u:\n", id);
345 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
347 HeapFree(GetProcessHeap(), 0, log);
351 /* Context activation is done by the caller. */
352 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
354 const char *ptr, *line;
356 TRACE("Compiling shader object %u.\n", shader);
358 if (TRACE_ON(d3d_shader))
360 ptr = src;
361 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
364 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
365 checkGLcall("glShaderSource");
366 GL_EXTCALL(glCompileShader(shader));
367 checkGLcall("glCompileShader");
368 print_glsl_info_log(gl_info, shader, FALSE);
371 /* Context activation is done by the caller. */
372 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
374 GLint i, shader_count, source_size = -1;
375 GLuint *shaders;
376 char *source = NULL;
378 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
379 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
380 if (!shaders)
382 ERR("Failed to allocate shader array memory.\n");
383 return;
386 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
387 for (i = 0; i < shader_count; ++i)
389 const char *ptr, *line;
390 GLint tmp;
392 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
394 if (source_size < tmp)
396 HeapFree(GetProcessHeap(), 0, source);
398 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
399 if (!source)
401 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
402 HeapFree(GetProcessHeap(), 0, shaders);
403 return;
405 source_size = tmp;
408 FIXME("Shader %u:\n", shaders[i]);
409 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
410 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
411 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
412 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
413 FIXME("\n");
415 ptr = source;
416 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
417 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
418 FIXME("\n");
421 HeapFree(GetProcessHeap(), 0, source);
422 HeapFree(GetProcessHeap(), 0, shaders);
425 /* Context activation is done by the caller. */
426 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
428 GLint tmp;
430 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
431 return;
433 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
434 if (!tmp)
436 FIXME("Program %u link status invalid.\n", program);
437 shader_glsl_dump_program_source(gl_info, program);
440 print_glsl_info_log(gl_info, program, TRUE);
443 /* Context activation is done by the caller. */
444 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
445 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
447 unsigned int mapped_unit;
448 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
449 const char *prefix;
450 unsigned int i, j;
451 GLint name_loc;
453 static const struct
455 enum wined3d_shader_type type;
456 unsigned int base_idx;
457 unsigned int count;
459 sampler_info[] =
461 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
462 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
465 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
467 prefix = shader_glsl_get_prefix(sampler_info[i].type);
469 for (j = 0; j < sampler_info[i].count; ++j)
471 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
472 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
473 if (name_loc == -1)
474 continue;
476 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
477 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
479 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
480 continue;
483 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
484 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
487 checkGLcall("glUniform1i");
488 string_buffer_release(&priv->string_buffers, sampler_name);
491 /* Context activation is done by the caller. */
492 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
493 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
495 unsigned int start = ~0U, end = 0;
496 int stack_idx = 0;
497 unsigned int heap_idx = 1;
498 unsigned int idx;
500 if (heap->entries[heap_idx].version <= version) return;
502 idx = heap->entries[heap_idx].idx;
503 if (constant_locations[idx] != -1)
504 start = end = idx;
505 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
507 while (stack_idx >= 0)
509 /* Note that we fall through to the next case statement. */
510 switch(stack[stack_idx])
512 case HEAP_NODE_TRAVERSE_LEFT:
514 unsigned int left_idx = heap_idx << 1;
515 if (left_idx < heap->size && heap->entries[left_idx].version > version)
517 heap_idx = left_idx;
518 idx = heap->entries[heap_idx].idx;
519 if (constant_locations[idx] != -1)
521 if (start > idx)
522 start = idx;
523 if (end < idx)
524 end = idx;
527 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
528 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
529 break;
533 case HEAP_NODE_TRAVERSE_RIGHT:
535 unsigned int right_idx = (heap_idx << 1) + 1;
536 if (right_idx < heap->size && heap->entries[right_idx].version > version)
538 heap_idx = right_idx;
539 idx = heap->entries[heap_idx].idx;
540 if (constant_locations[idx] != -1)
542 if (start > idx)
543 start = idx;
544 if (end < idx)
545 end = idx;
548 stack[stack_idx++] = HEAP_NODE_POP;
549 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
550 break;
554 case HEAP_NODE_POP:
555 heap_idx >>= 1;
556 --stack_idx;
557 break;
560 if (start <= end)
561 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
562 checkGLcall("walk_constant_heap()");
565 /* Context activation is done by the caller. */
566 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
568 GLfloat clamped_constant[4];
570 if (location == -1) return;
572 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
573 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
574 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
575 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
577 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
580 /* Context activation is done by the caller. */
581 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
582 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
584 int stack_idx = 0;
585 unsigned int heap_idx = 1;
586 unsigned int idx;
588 if (heap->entries[heap_idx].version <= version) return;
590 idx = heap->entries[heap_idx].idx;
591 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
592 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
594 while (stack_idx >= 0)
596 /* Note that we fall through to the next case statement. */
597 switch(stack[stack_idx])
599 case HEAP_NODE_TRAVERSE_LEFT:
601 unsigned int left_idx = heap_idx << 1;
602 if (left_idx < heap->size && heap->entries[left_idx].version > version)
604 heap_idx = left_idx;
605 idx = heap->entries[heap_idx].idx;
606 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
608 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
609 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
610 break;
614 case HEAP_NODE_TRAVERSE_RIGHT:
616 unsigned int right_idx = (heap_idx << 1) + 1;
617 if (right_idx < heap->size && heap->entries[right_idx].version > version)
619 heap_idx = right_idx;
620 idx = heap->entries[heap_idx].idx;
621 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
623 stack[stack_idx++] = HEAP_NODE_POP;
624 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
625 break;
629 case HEAP_NODE_POP:
630 heap_idx >>= 1;
631 --stack_idx;
632 break;
635 checkGLcall("walk_constant_heap_clamped()");
638 /* Context activation is done by the caller. */
639 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
640 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
641 unsigned char *stack, UINT version)
643 const struct wined3d_shader_lconst *lconst;
645 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
646 if (shader->reg_maps.shader_version.major == 1
647 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
648 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
649 else
650 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
652 if (!shader->load_local_constsF)
654 TRACE("No need to load local float constants for this shader\n");
655 return;
658 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
659 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
661 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
663 checkGLcall("glUniform4fv()");
666 /* Context activation is done by the caller. */
667 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
668 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
670 unsigned int i;
671 struct list* ptr;
673 for (i = 0; constants_set; constants_set >>= 1, ++i)
675 if (!(constants_set & 1)) continue;
677 /* We found this uniform name in the program - go ahead and send the data */
678 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
681 /* Load immediate constants */
682 ptr = list_head(&shader->constantsI);
683 while (ptr)
685 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
686 unsigned int idx = lconst->idx;
687 const GLint *values = (const GLint *)lconst->value;
689 /* We found this uniform name in the program - go ahead and send the data */
690 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
691 ptr = list_next(&shader->constantsI, ptr);
693 checkGLcall("glUniform4iv()");
696 /* Context activation is done by the caller. */
697 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
698 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
700 unsigned int i;
701 struct list* ptr;
703 for (i = 0; constants_set; constants_set >>= 1, ++i)
705 if (!(constants_set & 1)) continue;
707 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
710 /* Load immediate constants */
711 ptr = list_head(&shader->constantsB);
712 while (ptr)
714 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
715 unsigned int idx = lconst->idx;
716 const GLint *values = (const GLint *)lconst->value;
718 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
719 ptr = list_next(&shader->constantsB, ptr);
721 checkGLcall("glUniform1iv()");
724 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
726 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
729 /* Context activation is done by the caller (state handler). */
730 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
731 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
733 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
734 UINT fixup = ps->np2_fixup_info->active;
735 UINT i;
737 for (i = 0; fixup; fixup >>= 1, ++i)
739 const struct wined3d_texture *tex = state->textures[i];
740 unsigned char idx = ps->np2_fixup_info->idx[i];
741 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
743 if (!tex)
745 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
746 continue;
749 if (idx % 2)
751 tex_dim[2] = tex->pow2_matrix[0];
752 tex_dim[3] = tex->pow2_matrix[5];
754 else
756 tex_dim[0] = tex->pow2_matrix[0];
757 tex_dim[1] = tex->pow2_matrix[5];
761 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
764 /* Taken and adapted from Mesa. */
765 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
767 float pos, neg, t, det;
768 struct wined3d_matrix temp;
770 /* Calculate the determinant of upper left 3x3 submatrix and
771 * determine if the matrix is singular. */
772 pos = neg = 0.0f;
773 t = in->_11 * in->_22 * in->_33;
774 if (t >= 0.0f)
775 pos += t;
776 else
777 neg += t;
779 t = in->_21 * in->_32 * in->_13;
780 if (t >= 0.0f)
781 pos += t;
782 else
783 neg += t;
784 t = in->_31 * in->_12 * in->_23;
785 if (t >= 0.0f)
786 pos += t;
787 else
788 neg += t;
790 t = -in->_31 * in->_22 * in->_13;
791 if (t >= 0.0f)
792 pos += t;
793 else
794 neg += t;
795 t = -in->_21 * in->_12 * in->_33;
796 if (t >= 0.0f)
797 pos += t;
798 else
799 neg += t;
801 t = -in->_11 * in->_32 * in->_23;
802 if (t >= 0.0f)
803 pos += t;
804 else
805 neg += t;
807 det = pos + neg;
809 if (fabsf(det) < 1e-25f)
810 return FALSE;
812 det = 1.0f / det;
813 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
814 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
815 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
816 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
817 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
818 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
819 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
820 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
821 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
823 *out = temp;
824 return TRUE;
827 static void swap_rows(float **a, float **b)
829 float *tmp = *a;
831 *a = *b;
832 *b = tmp;
835 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
837 float wtmp[4][8];
838 float m0, m1, m2, m3, s;
839 float *r0, *r1, *r2, *r3;
841 r0 = wtmp[0];
842 r1 = wtmp[1];
843 r2 = wtmp[2];
844 r3 = wtmp[3];
846 r0[0] = m->_11;
847 r0[1] = m->_12;
848 r0[2] = m->_13;
849 r0[3] = m->_14;
850 r0[4] = 1.0f;
851 r0[5] = r0[6] = r0[7] = 0.0f;
853 r1[0] = m->_21;
854 r1[1] = m->_22;
855 r1[2] = m->_23;
856 r1[3] = m->_24;
857 r1[5] = 1.0f;
858 r1[4] = r1[6] = r1[7] = 0.0f;
860 r2[0] = m->_31;
861 r2[1] = m->_32;
862 r2[2] = m->_33;
863 r2[3] = m->_34;
864 r2[6] = 1.0f;
865 r2[4] = r2[5] = r2[7] = 0.0f;
867 r3[0] = m->_41;
868 r3[1] = m->_42;
869 r3[2] = m->_43;
870 r3[3] = m->_44;
871 r3[7] = 1.0f;
872 r3[4] = r3[5] = r3[6] = 0.0f;
874 /* Choose pivot - or die. */
875 if (fabsf(r3[0]) > fabsf(r2[0]))
876 swap_rows(&r3, &r2);
877 if (fabsf(r2[0]) > fabsf(r1[0]))
878 swap_rows(&r2, &r1);
879 if (fabsf(r1[0]) > fabsf(r0[0]))
880 swap_rows(&r1, &r0);
881 if (r0[0] == 0.0f)
882 return FALSE;
884 /* Eliminate first variable. */
885 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
886 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
887 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
888 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
889 s = r0[4];
890 if (s != 0.0f)
892 r1[4] -= m1 * s;
893 r2[4] -= m2 * s;
894 r3[4] -= m3 * s;
896 s = r0[5];
897 if (s != 0.0f)
899 r1[5] -= m1 * s;
900 r2[5] -= m2 * s;
901 r3[5] -= m3 * s;
903 s = r0[6];
904 if (s != 0.0f)
906 r1[6] -= m1 * s;
907 r2[6] -= m2 * s;
908 r3[6] -= m3 * s;
910 s = r0[7];
911 if (s != 0.0f)
913 r1[7] -= m1 * s;
914 r2[7] -= m2 * s;
915 r3[7] -= m3 * s;
918 /* Choose pivot - or die. */
919 if (fabsf(r3[1]) > fabsf(r2[1]))
920 swap_rows(&r3, &r2);
921 if (fabsf(r2[1]) > fabsf(r1[1]))
922 swap_rows(&r2, &r1);
923 if (r1[1] == 0.0f)
924 return FALSE;
926 /* Eliminate second variable. */
927 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
928 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
929 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
930 s = r1[4];
931 if (s != 0.0f)
933 r2[4] -= m2 * s;
934 r3[4] -= m3 * s;
936 s = r1[5];
937 if (s != 0.0f)
939 r2[5] -= m2 * s;
940 r3[5] -= m3 * s;
942 s = r1[6];
943 if (s != 0.0f)
945 r2[6] -= m2 * s;
946 r3[6] -= m3 * s;
948 s = r1[7];
949 if (s != 0.0f)
951 r2[7] -= m2 * s;
952 r3[7] -= m3 * s;
955 /* Choose pivot - or die. */
956 if (fabsf(r3[2]) > fabsf(r2[2]))
957 swap_rows(&r3, &r2);
958 if (r2[2] == 0.0f)
959 return FALSE;
961 /* Eliminate third variable. */
962 m3 = r3[2] / r2[2];
963 r3[3] -= m3 * r2[3];
964 r3[4] -= m3 * r2[4];
965 r3[5] -= m3 * r2[5];
966 r3[6] -= m3 * r2[6];
967 r3[7] -= m3 * r2[7];
969 /* Last check. */
970 if (r3[3] == 0.0f)
971 return FALSE;
973 /* Back substitute row 3. */
974 s = 1.0f / r3[3];
975 r3[4] *= s;
976 r3[5] *= s;
977 r3[6] *= s;
978 r3[7] *= s;
980 /* Back substitute row 2. */
981 m2 = r2[3];
982 s = 1.0f / r2[2];
983 r2[4] = s * (r2[4] - r3[4] * m2);
984 r2[5] = s * (r2[5] - r3[5] * m2);
985 r2[6] = s * (r2[6] - r3[6] * m2);
986 r2[7] = s * (r2[7] - r3[7] * m2);
987 m1 = r1[3];
988 r1[4] -= r3[4] * m1;
989 r1[5] -= r3[5] * m1;
990 r1[6] -= r3[6] * m1;
991 r1[7] -= r3[7] * m1;
992 m0 = r0[3];
993 r0[4] -= r3[4] * m0;
994 r0[5] -= r3[5] * m0;
995 r0[6] -= r3[6] * m0;
996 r0[7] -= r3[7] * m0;
998 /* Back substitute row 1. */
999 m1 = r1[2];
1000 s = 1.0f / r1[1];
1001 r1[4] = s * (r1[4] - r2[4] * m1);
1002 r1[5] = s * (r1[5] - r2[5] * m1);
1003 r1[6] = s * (r1[6] - r2[6] * m1);
1004 r1[7] = s * (r1[7] - r2[7] * m1);
1005 m0 = r0[2];
1006 r0[4] -= r2[4] * m0;
1007 r0[5] -= r2[5] * m0;
1008 r0[6] -= r2[6] * m0;
1009 r0[7] -= r2[7] * m0;
1011 /* Back substitute row 0. */
1012 m0 = r0[1];
1013 s = 1.0f / r0[0];
1014 r0[4] = s * (r0[4] - r1[4] * m0);
1015 r0[5] = s * (r0[5] - r1[5] * m0);
1016 r0[6] = s * (r0[6] - r1[6] * m0);
1017 r0[7] = s * (r0[7] - r1[7] * m0);
1019 out->_11 = r0[4];
1020 out->_12 = r0[5];
1021 out->_13 = r0[6];
1022 out->_14 = r0[7];
1023 out->_21 = r1[4];
1024 out->_22 = r1[5];
1025 out->_23 = r1[6];
1026 out->_24 = r1[7];
1027 out->_31 = r2[4];
1028 out->_32 = r2[5];
1029 out->_33 = r2[6];
1030 out->_34 = r2[7];
1031 out->_41 = r3[4];
1032 out->_42 = r3[5];
1033 out->_43 = r3[6];
1034 out->_44 = r3[7];
1036 return TRUE;
1039 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1040 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1042 const struct wined3d_gl_info *gl_info = context->gl_info;
1043 float mat[3 * 3];
1044 struct wined3d_matrix mv;
1045 unsigned int i, j;
1047 get_modelview_matrix(context, state, &mv);
1048 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1049 invert_matrix_3d(&mv, &mv);
1050 else
1051 invert_matrix(&mv, &mv);
1052 /* Tests show that singular modelview matrices are used unchanged as normal
1053 * matrices on D3D3 and older. There seems to be no clearly consistent
1054 * behavior on newer D3D versions so always follow older ddraw behavior. */
1055 for (i = 0; i < 3; ++i)
1056 for (j = 0; j < 3; ++j)
1057 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1059 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1060 checkGLcall("glUniformMatrix3fv");
1063 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1064 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1066 const struct wined3d_gl_info *gl_info = context->gl_info;
1067 struct wined3d_matrix mat;
1069 if (tex >= MAX_TEXTURES)
1070 return;
1071 if (prog->vs.texture_matrix_location[tex] == -1)
1072 return;
1074 get_texture_matrix(context, state, tex, &mat);
1075 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1076 checkGLcall("glUniformMatrix4fv");
1079 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1080 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1082 const struct wined3d_gl_info *gl_info = context->gl_info;
1084 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1086 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1087 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1089 else
1091 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1093 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1095 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1096 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1097 GL_EXTCALL(glUniform4fv(prog->vs.material_emission_location, 1, &state->material.emissive.r));
1098 checkGLcall("setting FFP material uniforms");
1101 /* Context activation is done by the caller (state handler). */
1102 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1103 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1105 struct wined3d_color float_key;
1106 const struct wined3d_texture *texture = state->textures[0];
1108 wined3d_format_convert_color_to_float(texture->resource.format, NULL,
1109 texture->async.src_blt_color_key.color_space_high_value, &float_key);
1110 GL_EXTCALL(glUniform4fv(ps->color_key_location, 1, &float_key.r));
1113 /* Context activation is done by the caller (state handler). */
1114 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1115 const struct wined3d_state *state)
1117 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1118 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1119 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1120 const struct wined3d_gl_info *gl_info = context->gl_info;
1121 struct shader_glsl_priv *priv = shader_priv;
1122 float position_fixup[4];
1123 DWORD update_mask = 0;
1125 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1126 UINT constant_version;
1127 int i;
1129 if (!prog) {
1130 /* No GLSL program set - nothing to do. */
1131 return;
1133 constant_version = prog->constant_version;
1134 update_mask = context->constant_update_mask & prog->constant_update_mask;
1136 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1137 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1138 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1140 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1141 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1142 vshader->reg_maps.integer_constants);
1144 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1145 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1146 vshader->reg_maps.boolean_constants);
1148 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1150 shader_get_position_fixup(context, state, position_fixup);
1151 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1152 checkGLcall("glUniform4fv");
1155 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1157 struct wined3d_matrix mat;
1159 get_modelview_matrix(context, state, &mat);
1160 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location, 1, FALSE, &mat._11));
1161 checkGLcall("glUniformMatrix4fv");
1163 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1166 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1168 struct wined3d_matrix projection;
1170 get_projection_matrix(context, state, &projection);
1171 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1172 checkGLcall("glUniformMatrix4fv");
1175 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1177 for (i = 0; i < MAX_TEXTURES; ++i)
1178 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1181 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1182 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1184 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1185 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1186 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1188 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1189 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1190 pshader->reg_maps.integer_constants);
1192 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1193 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1194 pshader->reg_maps.boolean_constants);
1196 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1198 for (i = 0; i < MAX_TEXTURES; ++i)
1200 if (prog->ps.bumpenv_mat_location[i] == -1)
1201 continue;
1203 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1204 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1206 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1208 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1209 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1210 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1211 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1215 checkGLcall("bump env uniforms");
1218 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1220 float correction_params[] =
1222 /* position is window relative, not viewport relative */
1223 context->render_offscreen ? 0.0f : (float)context->current_rt->resource.height,
1224 context->render_offscreen ? 1.0f : -1.0f,
1225 0.0f,
1226 0.0f,
1229 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
1232 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1233 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1234 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1235 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1237 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1239 float col[4];
1241 if (prog->ps.tex_factor_location != -1)
1243 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
1244 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
1247 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1248 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1249 else
1250 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1252 for (i = 0; i < MAX_TEXTURES; ++i)
1254 if (prog->ps.tss_constant_location[i] == -1)
1255 continue;
1257 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
1258 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
1261 checkGLcall("fixed function uniforms");
1264 if (priv->next_constant_version == UINT_MAX)
1266 TRACE("Max constant version reached, resetting to 0.\n");
1267 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1268 priv->next_constant_version = 1;
1270 else
1272 prog->constant_version = priv->next_constant_version++;
1276 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1278 struct constant_entry *entries = heap->entries;
1279 unsigned int *positions = heap->positions;
1280 unsigned int heap_idx, parent_idx;
1282 if (!heap->contained[idx])
1284 heap_idx = heap->size++;
1285 heap->contained[idx] = TRUE;
1287 else
1289 heap_idx = positions[idx];
1292 while (heap_idx > 1)
1294 parent_idx = heap_idx >> 1;
1296 if (new_version <= entries[parent_idx].version) break;
1298 entries[heap_idx] = entries[parent_idx];
1299 positions[entries[parent_idx].idx] = heap_idx;
1300 heap_idx = parent_idx;
1303 entries[heap_idx].version = new_version;
1304 entries[heap_idx].idx = idx;
1305 positions[idx] = heap_idx;
1308 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1310 struct shader_glsl_priv *priv = device->shader_priv;
1311 struct constant_heap *heap = &priv->vconst_heap;
1312 UINT i;
1314 for (i = start; i < count + start; ++i)
1316 update_heap_entry(heap, i, priv->next_constant_version);
1319 for (i = 0; i < device->context_count; ++i)
1321 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1325 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1327 struct shader_glsl_priv *priv = device->shader_priv;
1328 struct constant_heap *heap = &priv->pconst_heap;
1329 UINT i;
1331 for (i = start; i < count + start; ++i)
1333 update_heap_entry(heap, i, priv->next_constant_version);
1336 for (i = 0; i < device->context_count; ++i)
1338 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1342 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1344 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1345 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1346 if(shader_major > 3) return ret;
1348 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1349 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1350 return ret;
1353 /** Generate the variable & register declarations for the GLSL output target */
1354 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1355 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1356 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1358 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1359 const struct wined3d_state *state = &shader->device->state;
1360 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1361 const struct wined3d_gl_info *gl_info = context->gl_info;
1362 const struct wined3d_fb_state *fb = &shader->device->fb;
1363 unsigned int i, extra_constants_needed = 0;
1364 const struct wined3d_shader_lconst *lconst;
1365 const char *prefix;
1366 DWORD map;
1368 prefix = shader_glsl_get_prefix(version->type);
1370 /* Prototype the subroutines */
1371 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1373 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1376 /* Declare the constants (aka uniforms) */
1377 if (shader->limits->constant_float > 0)
1379 unsigned max_constantsF;
1381 /* Unless the shader uses indirect addressing, always declare the
1382 * maximum array size and ignore that we need some uniforms privately.
1383 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1384 * and immediate values, still declare VC[256]. If the shader needs
1385 * more uniforms than we have it won't work in any case. If it uses
1386 * less, the compiler will figure out which uniforms are really used
1387 * and strip them out. This allows a shader to use c255 on a dx9 card,
1388 * as long as it doesn't also use all the other constants.
1390 * If the shader uses indirect addressing the compiler must assume
1391 * that all declared uniforms are used. In this case, declare only the
1392 * amount that we're assured to have.
1394 * Thus we run into problems in these two cases:
1395 * 1) The shader really uses more uniforms than supported.
1396 * 2) The shader uses indirect addressing, less constants than
1397 * supported, but uses a constant index > #supported consts. */
1398 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1400 /* No indirect addressing here. */
1401 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1403 else
1405 if (reg_maps->usesrelconstF)
1407 /* Subtract the other potential uniforms from the max
1408 * available (bools, ints, and 1 row of projection matrix).
1409 * Subtract another uniform for immediate values, which have
1410 * to be loaded via uniform by the driver as well. The shader
1411 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1412 * shader code, so one vec4 should be enough. (Unfortunately
1413 * the Nvidia driver doesn't store 128 and -128 in one float).
1415 * Writing gl_ClipVertex requires one uniform for each
1416 * clipplane as well. */
1417 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1418 if(ctx_priv->cur_vs_args->clip_enabled)
1420 max_constantsF -= gl_info->limits.clipplanes;
1422 max_constantsF -= count_bits(reg_maps->integer_constants);
1423 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1424 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1425 * for now take this into account when calculating the number of available constants
1427 max_constantsF -= count_bits(reg_maps->boolean_constants);
1428 /* Set by driver quirks in directx.c */
1429 max_constantsF -= gl_info->reserved_glsl_constants;
1431 if (max_constantsF < shader->limits->constant_float)
1433 static unsigned int once;
1435 if (!once++)
1436 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1437 " it may not render correctly.\n");
1438 else
1439 WARN("The hardware does not support enough uniform components to run this shader.\n");
1442 else
1444 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1447 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1448 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1451 /* Always declare the full set of constants, the compiler can remove the
1452 * unused ones because d3d doesn't (yet) support indirect int and bool
1453 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1454 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1455 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1457 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1458 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1460 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1462 if (reg_maps->cb_sizes[i])
1463 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1464 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1467 /* Declare texture samplers */
1468 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1470 struct wined3d_shader_sampler_map_entry *entry;
1471 BOOL shadow_sampler, tex_rect;
1472 const char *sampler_type;
1474 entry = &reg_maps->sampler_map.entries[i];
1476 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1478 ERR("Invalid resource index %u.\n", entry->resource_idx);
1479 continue;
1482 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << entry->sampler_idx));
1483 switch (reg_maps->resource_info[entry->resource_idx].type)
1485 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1486 if (shadow_sampler)
1487 sampler_type = "sampler1DShadow";
1488 else
1489 sampler_type = "sampler1D";
1490 break;
1492 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1493 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1494 && (ps_args->np2_fixup & (1 << entry->resource_idx))
1495 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1496 if (shadow_sampler)
1498 if (tex_rect)
1499 sampler_type = "sampler2DRectShadow";
1500 else
1501 sampler_type = "sampler2DShadow";
1503 else
1505 if (tex_rect)
1506 sampler_type = "sampler2DRect";
1507 else
1508 sampler_type = "sampler2D";
1510 break;
1512 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1513 if (shadow_sampler)
1514 FIXME("Unsupported 3D shadow sampler.\n");
1515 sampler_type = "sampler3D";
1516 break;
1518 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1519 if (shadow_sampler)
1520 FIXME("Unsupported Cube shadow sampler.\n");
1521 sampler_type = "samplerCube";
1522 break;
1524 default:
1525 sampler_type = "unsupported_sampler";
1526 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1527 break;
1529 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1532 /* Declare uniforms for NP2 texcoord fixup:
1533 * This is NOT done inside the loop that declares the texture samplers
1534 * since the NP2 fixup code is currently only used for the GeforceFX
1535 * series and when forcing the ARB_npot extension off. Modern cards just
1536 * skip the code anyway, so put it inside a separate loop. */
1537 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1539 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1540 UINT cur = 0;
1542 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1543 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1544 * samplerNP2Fixup stores texture dimensions and is updated through
1545 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1547 for (i = 0; i < shader->limits->sampler; ++i)
1549 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1 << i)))
1550 continue;
1552 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1554 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1555 continue;
1558 fixup->idx[i] = cur++;
1561 fixup->num_consts = (cur + 1) >> 1;
1562 fixup->active = ps_args->np2_fixup;
1563 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1566 /* Declare address variables */
1567 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1569 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1572 /* Declare texture coordinate temporaries and initialize them */
1573 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1575 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1578 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1580 for (i = 0; i < shader->input_signature.element_count; ++i)
1582 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1583 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1584 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1585 prefix, e->register_idx);
1586 else
1587 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1590 shader_addline(buffer, "uniform vec4 posFixup;\n");
1591 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1593 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1595 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1597 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1599 if (version->major >= 3)
1601 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1603 if (use_vs(state))
1604 shader_addline(buffer, "varying vec4 %s_link[%u];\n", prefix, in_count);
1605 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1608 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1610 if (!(map & 1))
1611 continue;
1613 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1615 if (reg_maps->luminanceparams & (1 << i))
1617 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1618 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1619 extra_constants_needed++;
1622 extra_constants_needed++;
1625 if (ps_args->srgb_correction)
1627 shader_addline(buffer, "const vec4 srgb_const0 = ");
1628 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1629 shader_addline(buffer, ";\n");
1630 shader_addline(buffer, "const vec4 srgb_const1 = ");
1631 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1632 shader_addline(buffer, ";\n");
1634 if (reg_maps->vpos || reg_maps->usesdsy)
1636 if (shader->limits->constant_float + extra_constants_needed
1637 + 1 < gl_info->limits.glsl_ps_float_constants)
1639 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1640 extra_constants_needed++;
1642 else
1644 float ycorrection[] =
1646 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1647 context->render_offscreen ? 1.0f : -1.0f,
1648 0.0f,
1649 0.0f,
1652 /* This happens because we do not have proper tracking of the
1653 * constant registers that are actually used, only the max
1654 * limit of the shader version. */
1655 FIXME("Cannot find a free uniform for vpos correction params\n");
1656 shader_addline(buffer, "const vec4 ycorrection = ");
1657 shader_glsl_append_imm_vec4(buffer, ycorrection);
1658 shader_addline(buffer, ";\n");
1660 shader_addline(buffer, "vec4 vpos;\n");
1664 /* Declare output register temporaries */
1665 if (shader->limits->packed_output)
1666 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1668 /* Declare temporary variables */
1669 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1671 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1674 /* Declare loop registers aLx */
1675 if (version->major < 4)
1677 for (i = 0; i < reg_maps->loop_depth; ++i)
1679 shader_addline(buffer, "int aL%u;\n", i);
1680 shader_addline(buffer, "int tmpInt%u;\n", i);
1684 /* Temporary variables for matrix operations */
1685 shader_addline(buffer, "vec4 tmp0;\n");
1686 shader_addline(buffer, "vec4 tmp1;\n");
1688 if (!shader->load_local_constsF)
1690 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1692 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1693 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1694 shader_addline(buffer, ";\n");
1698 /* Start the main program. */
1699 shader_addline(buffer, "void main()\n{\n");
1701 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1702 * add approximately 0.5. This causes off-by-one problems as spotted by
1703 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1704 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1705 * causes precision troubles when we just subtract 0.5.
1707 * To deal with that, just floor() the position. This will eliminate the
1708 * fraction on all cards.
1710 * TODO: Test how this behaves with multisampling.
1712 * An advantage of floor is that it works even if the driver doesn't add
1713 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1714 * to return in gl_FragCoord, even though coordinates specify the pixel
1715 * centers instead of the pixel corners. This code will behave correctly
1716 * on drivers that returns integer values. */
1717 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1719 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
1720 shader_addline(buffer,
1721 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1722 else
1723 shader_addline(buffer,
1724 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
1728 /*****************************************************************************
1729 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1731 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1732 ****************************************************************************/
1734 /* Prototypes */
1735 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1736 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1738 /** Used for opcode modifiers - They multiply the result by the specified amount */
1739 static const char * const shift_glsl_tab[] = {
1740 "", /* 0 (none) */
1741 "2.0 * ", /* 1 (x2) */
1742 "4.0 * ", /* 2 (x4) */
1743 "8.0 * ", /* 3 (x8) */
1744 "16.0 * ", /* 4 (x16) */
1745 "32.0 * ", /* 5 (x32) */
1746 "", /* 6 (x64) */
1747 "", /* 7 (x128) */
1748 "", /* 8 (d256) */
1749 "", /* 9 (d128) */
1750 "", /* 10 (d64) */
1751 "", /* 11 (d32) */
1752 "0.0625 * ", /* 12 (d16) */
1753 "0.125 * ", /* 13 (d8) */
1754 "0.25 * ", /* 14 (d4) */
1755 "0.5 * " /* 15 (d2) */
1758 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1759 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1760 const char *in_reg, const char *in_regswizzle, char *out_str)
1762 out_str[0] = 0;
1764 switch (src_modifier)
1766 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1767 case WINED3DSPSM_DW:
1768 case WINED3DSPSM_NONE:
1769 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1770 break;
1771 case WINED3DSPSM_NEG:
1772 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1773 break;
1774 case WINED3DSPSM_NOT:
1775 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1776 break;
1777 case WINED3DSPSM_BIAS:
1778 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1779 break;
1780 case WINED3DSPSM_BIASNEG:
1781 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1782 break;
1783 case WINED3DSPSM_SIGN:
1784 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1785 break;
1786 case WINED3DSPSM_SIGNNEG:
1787 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1788 break;
1789 case WINED3DSPSM_COMP:
1790 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1791 break;
1792 case WINED3DSPSM_X2:
1793 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1794 break;
1795 case WINED3DSPSM_X2NEG:
1796 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1797 break;
1798 case WINED3DSPSM_ABS:
1799 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1800 break;
1801 case WINED3DSPSM_ABSNEG:
1802 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1803 break;
1804 default:
1805 FIXME("Unhandled modifier %u\n", src_modifier);
1806 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1810 /** Writes the GLSL variable name that corresponds to the register that the
1811 * DX opcode parameter is trying to access */
1812 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1813 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1815 /* oPos, oFog and oPts in D3D */
1816 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1818 const struct wined3d_shader *shader = ins->ctx->shader;
1819 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1820 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1821 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1822 const char *prefix = shader_glsl_get_prefix(version->type);
1823 struct glsl_src_param rel_param0, rel_param1;
1824 char imm_str[4][17];
1826 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1827 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1828 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1829 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1830 *is_color = FALSE;
1832 switch (reg->type)
1834 case WINED3DSPR_TEMP:
1835 sprintf(register_name, "R%u", reg->idx[0].offset);
1836 break;
1838 case WINED3DSPR_INPUT:
1839 /* vertex shaders */
1840 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1842 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1843 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1844 *is_color = TRUE;
1845 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1846 break;
1849 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1851 if (reg->idx[0].rel_addr)
1853 if (reg->idx[1].rel_addr)
1854 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1855 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1856 else
1857 sprintf(register_name, "gs_in[%s + %u][%u]",
1858 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1860 else if (reg->idx[1].rel_addr)
1861 sprintf(register_name, "gs_in[%u][%s + %u]",
1862 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1863 else
1864 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1865 break;
1868 /* pixel shaders >= 3.0 */
1869 if (version->major >= 3)
1871 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1872 unsigned int in_count = vec4_varyings(version->major, gl_info);
1874 if (reg->idx[0].rel_addr)
1876 /* Removing a + 0 would be an obvious optimization, but
1877 * OS X doesn't see the NOP operation there. */
1878 if (idx)
1880 if (shader->u.ps.declared_in_count > in_count)
1882 sprintf(register_name,
1883 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1884 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1885 prefix, rel_param0.param_str, idx);
1887 else
1889 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1892 else
1894 if (shader->u.ps.declared_in_count > in_count)
1896 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1897 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1898 prefix, rel_param0.param_str);
1900 else
1902 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1906 else
1908 if (idx == in_count) sprintf(register_name, "gl_Color");
1909 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1910 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1913 else
1915 if (!reg->idx[0].offset)
1916 strcpy(register_name, "gl_Color");
1917 else
1918 strcpy(register_name, "gl_SecondaryColor");
1919 break;
1921 break;
1923 case WINED3DSPR_CONST:
1925 /* Relative addressing */
1926 if (reg->idx[0].rel_addr)
1928 if (reg->idx[0].offset)
1929 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1930 else
1931 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1933 else
1935 if (shader_constant_is_local(shader, reg->idx[0].offset))
1936 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1937 else
1938 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1941 break;
1943 case WINED3DSPR_CONSTINT:
1944 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1945 break;
1947 case WINED3DSPR_CONSTBOOL:
1948 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1949 break;
1951 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1952 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1953 sprintf(register_name, "T%u", reg->idx[0].offset);
1954 else
1955 sprintf(register_name, "A%u", reg->idx[0].offset);
1956 break;
1958 case WINED3DSPR_LOOP:
1959 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1960 break;
1962 case WINED3DSPR_SAMPLER:
1963 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1964 break;
1966 case WINED3DSPR_COLOROUT:
1967 if (reg->idx[0].offset >= gl_info->limits.buffers)
1968 WARN("Write to render target %u, only %d supported.\n",
1969 reg->idx[0].offset, gl_info->limits.buffers);
1971 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1972 break;
1974 case WINED3DSPR_RASTOUT:
1975 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1976 break;
1978 case WINED3DSPR_DEPTHOUT:
1979 sprintf(register_name, "gl_FragDepth");
1980 break;
1982 case WINED3DSPR_ATTROUT:
1983 if (!reg->idx[0].offset)
1984 sprintf(register_name, "%s_out[8]", prefix);
1985 else
1986 sprintf(register_name, "%s_out[9]", prefix);
1987 break;
1989 case WINED3DSPR_TEXCRDOUT:
1990 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1991 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1992 break;
1994 case WINED3DSPR_MISCTYPE:
1995 if (!reg->idx[0].offset)
1997 /* vPos */
1998 sprintf(register_name, "vpos");
2000 else if (reg->idx[0].offset == 1)
2002 /* Note that gl_FrontFacing is a bool, while vFace is
2003 * a float for which the sign determines front/back */
2004 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2006 else
2008 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2009 sprintf(register_name, "unrecognized_register");
2011 break;
2013 case WINED3DSPR_IMMCONST:
2014 switch (reg->immconst_type)
2016 case WINED3D_IMMCONST_SCALAR:
2017 switch (reg->data_type)
2019 case WINED3D_DATA_FLOAT:
2020 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2021 break;
2022 case WINED3D_DATA_INT:
2023 sprintf(register_name, "%#x", reg->immconst_data[0]);
2024 break;
2025 case WINED3D_DATA_RESOURCE:
2026 case WINED3D_DATA_SAMPLER:
2027 case WINED3D_DATA_UINT:
2028 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2029 break;
2030 default:
2031 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2032 break;
2034 break;
2036 case WINED3D_IMMCONST_VEC4:
2037 switch (reg->data_type)
2039 case WINED3D_DATA_FLOAT:
2040 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2041 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2042 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2043 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2044 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2045 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2046 break;
2047 case WINED3D_DATA_INT:
2048 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2049 reg->immconst_data[0], reg->immconst_data[1],
2050 reg->immconst_data[2], reg->immconst_data[3]);
2051 break;
2052 case WINED3D_DATA_RESOURCE:
2053 case WINED3D_DATA_SAMPLER:
2054 case WINED3D_DATA_UINT:
2055 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2056 reg->immconst_data[0], reg->immconst_data[1],
2057 reg->immconst_data[2], reg->immconst_data[3]);
2058 break;
2059 default:
2060 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2061 break;
2063 break;
2065 default:
2066 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2067 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2069 break;
2071 case WINED3DSPR_CONSTBUFFER:
2072 if (reg->idx[1].rel_addr)
2073 sprintf(register_name, "%s_cb%u[%s + %u]",
2074 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2075 else
2076 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2077 break;
2079 case WINED3DSPR_PRIMID:
2080 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2081 break;
2083 default:
2084 FIXME("Unhandled register type %#x.\n", reg->type);
2085 sprintf(register_name, "unrecognized_register");
2086 break;
2090 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2092 *str++ = '.';
2093 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2094 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2095 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2096 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2097 *str = '\0';
2100 /* Get the GLSL write mask for the destination register */
2101 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2103 DWORD mask = param->write_mask;
2105 if (shader_is_scalar(&param->reg))
2107 mask = WINED3DSP_WRITEMASK_0;
2108 *write_mask = '\0';
2110 else
2112 shader_glsl_write_mask_to_str(mask, write_mask);
2115 return mask;
2118 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2119 unsigned int size = 0;
2121 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2122 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2123 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2124 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2126 return size;
2129 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2131 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2132 * but addressed as "rgba". To fix this we need to swap the register's x
2133 * and z components. */
2134 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2136 *str++ = '.';
2137 /* swizzle bits fields: wwzzyyxx */
2138 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2139 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2140 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2141 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2142 *str = '\0';
2145 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2146 BOOL fixup, DWORD mask, char *swizzle_str)
2148 if (shader_is_scalar(&param->reg))
2149 *swizzle_str = '\0';
2150 else
2151 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2154 /* From a given parameter token, generate the corresponding GLSL string.
2155 * Also, return the actual register name and swizzle in case the
2156 * caller needs this information as well. */
2157 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2158 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2160 BOOL is_color = FALSE;
2161 char swizzle_str[6];
2163 glsl_src->reg_name[0] = '\0';
2164 glsl_src->param_str[0] = '\0';
2165 swizzle_str[0] = '\0';
2167 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2168 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2170 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2172 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2174 else
2176 char reg_name[200];
2178 switch (wined3d_src->reg.data_type)
2180 case WINED3D_DATA_FLOAT:
2181 sprintf(reg_name, "%s", glsl_src->reg_name);
2182 break;
2183 case WINED3D_DATA_INT:
2184 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2185 break;
2186 case WINED3D_DATA_RESOURCE:
2187 case WINED3D_DATA_SAMPLER:
2188 case WINED3D_DATA_UINT:
2189 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2190 break;
2191 default:
2192 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2193 sprintf(reg_name, "%s", glsl_src->reg_name);
2194 break;
2197 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2201 /* From a given parameter token, generate the corresponding GLSL string.
2202 * Also, return the actual register name and swizzle in case the
2203 * caller needs this information as well. */
2204 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2205 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2207 BOOL is_color = FALSE;
2209 glsl_dst->mask_str[0] = '\0';
2210 glsl_dst->reg_name[0] = '\0';
2212 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2213 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2216 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2217 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2218 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2219 enum wined3d_data_type data_type)
2221 struct glsl_dst_param glsl_dst;
2222 DWORD mask;
2224 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2226 switch (data_type)
2228 case WINED3D_DATA_FLOAT:
2229 shader_addline(buffer, "%s%s = %s(",
2230 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2231 break;
2232 case WINED3D_DATA_INT:
2233 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2234 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2235 break;
2236 case WINED3D_DATA_RESOURCE:
2237 case WINED3D_DATA_SAMPLER:
2238 case WINED3D_DATA_UINT:
2239 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2240 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2241 break;
2242 default:
2243 FIXME("Unhandled data type %#x.\n", data_type);
2244 shader_addline(buffer, "%s%s = %s(",
2245 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2246 break;
2250 return mask;
2253 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2254 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2256 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2259 /** Process GLSL instruction modifiers */
2260 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2262 struct glsl_dst_param dst_param;
2263 DWORD modifiers;
2265 if (!ins->dst_count) return;
2267 modifiers = ins->dst[0].modifiers;
2268 if (!modifiers) return;
2270 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2272 if (modifiers & WINED3DSPDM_SATURATE)
2274 /* _SAT means to clamp the value of the register to between 0 and 1 */
2275 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2276 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2279 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2281 FIXME("_centroid modifier not handled\n");
2284 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2286 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2290 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2292 switch (op)
2294 case WINED3D_SHADER_REL_OP_GT: return ">";
2295 case WINED3D_SHADER_REL_OP_EQ: return "==";
2296 case WINED3D_SHADER_REL_OP_GE: return ">=";
2297 case WINED3D_SHADER_REL_OP_LT: return "<";
2298 case WINED3D_SHADER_REL_OP_NE: return "!=";
2299 case WINED3D_SHADER_REL_OP_LE: return "<=";
2300 default:
2301 FIXME("Unrecognized operator %#x.\n", op);
2302 return "(\?\?)";
2306 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2307 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2309 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2310 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2311 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2312 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << resource_idx));
2313 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2314 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2315 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2316 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2318 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2320 /* Note that there's no such thing as a projected cube texture. */
2321 switch (resource_type)
2323 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2324 if (shadow)
2326 if (lod)
2328 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
2330 else if (grad)
2332 if (gl_info->supported[EXT_GPU_SHADER4])
2333 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
2334 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2335 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
2336 else
2338 FIXME("Unsupported 1D shadow grad function.\n");
2339 sample_function->name = "unsupported1DGrad";
2342 else
2344 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
2346 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2348 else
2350 if (lod)
2352 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
2354 else if (grad)
2356 if (gl_info->supported[EXT_GPU_SHADER4])
2357 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
2358 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2359 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
2360 else
2362 FIXME("Unsupported 1D grad function.\n");
2363 sample_function->name = "unsupported1DGrad";
2366 else
2368 sample_function->name = projected ? "texture1DProj" : "texture1D";
2370 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
2372 break;
2374 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2375 if (shadow)
2377 if (texrect)
2379 if (lod)
2381 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
2383 else if (grad)
2385 if (gl_info->supported[EXT_GPU_SHADER4])
2386 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
2387 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2388 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
2389 else
2391 FIXME("Unsupported RECT shadow grad function.\n");
2392 sample_function->name = "unsupported2DRectGrad";
2395 else
2397 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
2400 else
2402 if (lod)
2404 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
2406 else if (grad)
2408 if (gl_info->supported[EXT_GPU_SHADER4])
2409 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2410 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2411 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2412 else
2414 FIXME("Unsupported 2D shadow grad function.\n");
2415 sample_function->name = "unsupported2DGrad";
2418 else
2420 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2423 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2425 else
2427 if (texrect)
2429 if (lod)
2431 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2433 else if (grad)
2435 if (gl_info->supported[EXT_GPU_SHADER4])
2436 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2437 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2438 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2439 else
2441 FIXME("Unsupported RECT grad function.\n");
2442 sample_function->name = "unsupported2DRectGrad";
2445 else
2447 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2450 else
2452 if (lod)
2454 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2456 else if (grad)
2458 if (gl_info->supported[EXT_GPU_SHADER4])
2459 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2460 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2461 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2462 else
2464 FIXME("Unsupported 2D grad function.\n");
2465 sample_function->name = "unsupported2DGrad";
2468 else
2470 sample_function->name = projected ? "texture2DProj" : "texture2D";
2473 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2475 break;
2477 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2478 if (shadow)
2480 FIXME("Unsupported 3D shadow function.\n");
2481 sample_function->name = "unsupported3DShadow";
2482 sample_function->coord_mask = 0;
2484 else
2486 if (lod)
2488 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2490 else if (grad)
2492 if (gl_info->supported[EXT_GPU_SHADER4])
2493 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2494 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2495 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2496 else
2498 FIXME("Unsupported 3D grad function.\n");
2499 sample_function->name = "unsupported3DGrad";
2502 else
2504 sample_function->name = projected ? "texture3DProj" : "texture3D";
2506 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2508 break;
2510 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2511 if (shadow)
2513 FIXME("Unsupported Cube shadow function.\n");
2514 sample_function->name = "unsupportedCubeShadow";
2515 sample_function->coord_mask = 0;
2517 else
2519 if (lod)
2521 sample_function->name = "textureCubeLod";
2523 else if (grad)
2525 if (gl_info->supported[EXT_GPU_SHADER4])
2526 sample_function->name = "textureCubeGrad";
2527 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2528 sample_function->name = "textureCubeGradARB";
2529 else
2531 FIXME("Unsupported Cube grad function.\n");
2532 sample_function->name = "unsupportedCubeGrad";
2535 else
2537 sample_function->name = "textureCube";
2539 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2541 break;
2543 default:
2544 sample_function->name = "";
2545 sample_function->coord_mask = 0;
2546 FIXME("Unhandled resource type %#x.\n", resource_type);
2547 break;
2551 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2552 BOOL sign_fixup, enum fixup_channel_source channel_source)
2554 switch(channel_source)
2556 case CHANNEL_SOURCE_ZERO:
2557 strcat(arguments, "0.0");
2558 break;
2560 case CHANNEL_SOURCE_ONE:
2561 strcat(arguments, "1.0");
2562 break;
2564 case CHANNEL_SOURCE_X:
2565 strcat(arguments, reg_name);
2566 strcat(arguments, ".x");
2567 break;
2569 case CHANNEL_SOURCE_Y:
2570 strcat(arguments, reg_name);
2571 strcat(arguments, ".y");
2572 break;
2574 case CHANNEL_SOURCE_Z:
2575 strcat(arguments, reg_name);
2576 strcat(arguments, ".z");
2577 break;
2579 case CHANNEL_SOURCE_W:
2580 strcat(arguments, reg_name);
2581 strcat(arguments, ".w");
2582 break;
2584 default:
2585 FIXME("Unhandled channel source %#x\n", channel_source);
2586 strcat(arguments, "undefined");
2587 break;
2590 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2593 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2594 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2596 unsigned int mask_size, remaining;
2597 DWORD fixup_mask = 0;
2598 char arguments[256];
2599 char mask_str[6];
2601 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2602 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2603 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2604 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2605 if (!(mask &= fixup_mask))
2606 return;
2608 if (is_complex_fixup(fixup))
2610 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2611 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2612 return;
2615 shader_glsl_write_mask_to_str(mask, mask_str);
2616 mask_size = shader_glsl_get_write_mask_size(mask);
2618 arguments[0] = '\0';
2619 remaining = mask_size;
2620 if (mask & WINED3DSP_WRITEMASK_0)
2622 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2623 if (--remaining) strcat(arguments, ", ");
2625 if (mask & WINED3DSP_WRITEMASK_1)
2627 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2628 if (--remaining) strcat(arguments, ", ");
2630 if (mask & WINED3DSP_WRITEMASK_2)
2632 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2633 if (--remaining) strcat(arguments, ", ");
2635 if (mask & WINED3DSP_WRITEMASK_3)
2637 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2638 if (--remaining) strcat(arguments, ", ");
2641 if (mask_size > 1)
2642 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2643 else
2644 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2647 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2649 char reg_name[256];
2650 BOOL is_color;
2652 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2653 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2656 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2657 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2658 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2660 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2661 char dst_swizzle[6];
2662 struct color_fixup_desc fixup;
2663 BOOL np2_fixup = FALSE;
2664 va_list args;
2666 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2668 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2670 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2671 fixup = priv->cur_ps_args->color_fixup[sampler];
2673 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2674 if(bias) {
2675 FIXME("Biased sampling from NP2 textures is unsupported\n");
2676 } else {
2677 np2_fixup = TRUE;
2681 else
2683 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2686 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2688 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2689 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2691 va_start(args, coord_reg_fmt);
2692 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2693 va_end(args);
2695 if(bias) {
2696 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2697 } else {
2698 if (np2_fixup) {
2699 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2700 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2702 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2703 (idx % 2) ? "zw" : "xy", dst_swizzle);
2704 } else if(dx && dy) {
2705 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2706 } else {
2707 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2711 if(!is_identity_fixup(fixup)) {
2712 shader_glsl_color_correction(ins, fixup);
2716 /*****************************************************************************
2717 * Begin processing individual instruction opcodes
2718 ****************************************************************************/
2720 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2722 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2723 struct glsl_src_param src0_param;
2724 struct glsl_src_param src1_param;
2725 DWORD write_mask;
2726 const char *op;
2728 /* Determine the GLSL operator to use based on the opcode */
2729 switch (ins->handler_idx)
2731 case WINED3DSIH_ADD: op = "+"; break;
2732 case WINED3DSIH_AND: op = "&"; break;
2733 case WINED3DSIH_DIV: op = "/"; break;
2734 case WINED3DSIH_IADD: op = "+"; break;
2735 case WINED3DSIH_ISHL: op = "<<"; break;
2736 case WINED3DSIH_MUL: op = "*"; break;
2737 case WINED3DSIH_OR: op = "|"; break;
2738 case WINED3DSIH_SUB: op = "-"; break;
2739 case WINED3DSIH_USHR: op = ">>"; break;
2740 case WINED3DSIH_XOR: op = "^"; break;
2741 default:
2742 op = "<unhandled operator>";
2743 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2744 break;
2747 write_mask = shader_glsl_append_dst(buffer, ins);
2748 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2749 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2750 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2753 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2755 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2756 struct glsl_src_param src0_param;
2757 struct glsl_src_param src1_param;
2758 unsigned int mask_size;
2759 DWORD write_mask;
2760 const char *op;
2762 write_mask = shader_glsl_append_dst(buffer, ins);
2763 mask_size = shader_glsl_get_write_mask_size(write_mask);
2764 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2765 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2767 if (mask_size > 1)
2769 switch (ins->handler_idx)
2771 case WINED3DSIH_EQ: op = "equal"; break;
2772 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2773 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2774 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2775 case WINED3DSIH_LT: op = "lessThan"; break;
2776 case WINED3DSIH_NE: op = "notEqual"; break;
2777 default:
2778 op = "<unhandled operator>";
2779 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2780 break;
2783 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2784 mask_size, op, src0_param.param_str, src1_param.param_str);
2786 else
2788 switch (ins->handler_idx)
2790 case WINED3DSIH_EQ: op = "=="; break;
2791 case WINED3DSIH_GE: op = ">="; break;
2792 case WINED3DSIH_IGE: op = ">="; break;
2793 case WINED3DSIH_UGE: op = ">="; break;
2794 case WINED3DSIH_LT: op = "<"; break;
2795 case WINED3DSIH_NE: op = "!="; break;
2796 default:
2797 op = "<unhandled operator>";
2798 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2799 break;
2802 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2803 src0_param.param_str, op, src1_param.param_str);
2807 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2809 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2810 struct glsl_src_param src0_param;
2811 struct glsl_src_param src1_param;
2812 DWORD write_mask;
2814 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2815 * not, we can emulate it. */
2816 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2817 FIXME("64-bit integer multiplies not implemented.\n");
2819 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2821 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2822 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2823 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2825 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2826 src0_param.param_str, src1_param.param_str);
2830 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2832 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2833 struct glsl_src_param src0_param, src1_param;
2834 DWORD write_mask;
2836 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2839 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2841 char dst_mask[6];
2843 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2844 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2845 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2846 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2847 dst_mask, src0_param.param_str, src1_param.param_str);
2849 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2850 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2851 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2852 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2854 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2855 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2857 else
2859 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2860 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2861 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2862 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2865 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2867 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2868 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2869 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2870 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2874 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2875 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2877 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2878 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2879 struct glsl_src_param src0_param;
2880 DWORD write_mask;
2882 write_mask = shader_glsl_append_dst(buffer, ins);
2883 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2885 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2886 * shader versions WINED3DSIO_MOVA is used for this. */
2887 if (ins->ctx->reg_maps->shader_version.major == 1
2888 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2889 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2891 /* This is a simple floor() */
2892 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2893 if (mask_size > 1) {
2894 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2895 } else {
2896 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2899 else if(ins->handler_idx == WINED3DSIH_MOVA)
2901 /* We need to *round* to the nearest int here. */
2902 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2904 if (gl_info->supported[EXT_GPU_SHADER4])
2906 if (mask_size > 1)
2907 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2908 else
2909 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2911 else
2913 if (mask_size > 1)
2914 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2915 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2916 else
2917 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2918 src0_param.param_str, src0_param.param_str);
2921 else
2923 shader_addline(buffer, "%s);\n", src0_param.param_str);
2927 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2928 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2930 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2931 struct glsl_src_param src0_param;
2932 struct glsl_src_param src1_param;
2933 DWORD dst_write_mask, src_write_mask;
2934 unsigned int dst_size = 0;
2936 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2937 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2939 /* dp4 works on vec4, dp3 on vec3, etc. */
2940 if (ins->handler_idx == WINED3DSIH_DP4)
2941 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2942 else if (ins->handler_idx == WINED3DSIH_DP3)
2943 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2944 else
2945 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2947 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2948 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2950 if (dst_size > 1) {
2951 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2952 } else {
2953 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2957 /* Note that this instruction has some restrictions. The destination write mask
2958 * can't contain the w component, and the source swizzles have to be .xyzw */
2959 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2961 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2962 struct glsl_src_param src0_param;
2963 struct glsl_src_param src1_param;
2964 char dst_mask[6];
2966 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2967 shader_glsl_append_dst(ins->ctx->buffer, ins);
2968 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2969 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2970 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2973 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2975 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2978 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2979 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2980 * GLSL uses the value as-is. */
2981 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2983 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2984 struct glsl_src_param src0_param;
2985 struct glsl_src_param src1_param;
2986 DWORD dst_write_mask;
2987 unsigned int dst_size;
2989 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2990 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2992 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2993 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2995 if (dst_size > 1)
2997 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2998 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3000 else
3002 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3003 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3007 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3008 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3010 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3011 struct glsl_src_param src_param;
3012 const char *instruction;
3013 DWORD write_mask;
3014 unsigned i;
3016 /* Determine the GLSL function to use based on the opcode */
3017 /* TODO: Possibly make this a table for faster lookups */
3018 switch (ins->handler_idx)
3020 case WINED3DSIH_MIN: instruction = "min"; break;
3021 case WINED3DSIH_MAX: instruction = "max"; break;
3022 case WINED3DSIH_ABS: instruction = "abs"; break;
3023 case WINED3DSIH_FRC: instruction = "fract"; break;
3024 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3025 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3026 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3027 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3028 default: instruction = "";
3029 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
3030 break;
3033 write_mask = shader_glsl_append_dst(buffer, ins);
3035 shader_addline(buffer, "%s(", instruction);
3037 if (ins->src_count)
3039 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3040 shader_addline(buffer, "%s", src_param.param_str);
3041 for (i = 1; i < ins->src_count; ++i)
3043 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3044 shader_addline(buffer, ", %s", src_param.param_str);
3048 shader_addline(buffer, "));\n");
3051 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3053 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3055 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3056 struct glsl_src_param src_param;
3057 unsigned int mask_size;
3058 DWORD write_mask;
3059 char dst_mask[6];
3061 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3062 mask_size = shader_glsl_get_write_mask_size(write_mask);
3063 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3065 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3066 src_param.param_str, src_param.param_str);
3067 shader_glsl_append_dst(buffer, ins);
3069 if (mask_size > 1)
3071 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3072 mask_size, src_param.param_str);
3074 else
3076 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3077 src_param.param_str);
3081 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3083 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3084 struct glsl_src_param src0_param;
3085 const char *prefix, *suffix;
3086 unsigned int dst_size;
3087 DWORD dst_write_mask;
3089 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3090 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3092 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
3094 switch (ins->handler_idx)
3096 case WINED3DSIH_EXP:
3097 case WINED3DSIH_EXPP:
3098 prefix = "exp2(";
3099 suffix = ")";
3100 break;
3102 case WINED3DSIH_LOG:
3103 case WINED3DSIH_LOGP:
3104 prefix = "log2(abs(";
3105 suffix = "))";
3106 break;
3108 case WINED3DSIH_RCP:
3109 prefix = "1.0 / ";
3110 suffix = "";
3111 break;
3113 case WINED3DSIH_RSQ:
3114 prefix = "inversesqrt(abs(";
3115 suffix = "))";
3116 break;
3118 default:
3119 prefix = "";
3120 suffix = "";
3121 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3122 break;
3125 if (dst_size > 1)
3126 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3127 else
3128 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3131 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3132 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3133 * dst.x = 2^(floor(src))
3134 * dst.y = src - floor(src)
3135 * dst.z = 2^src (partial precision is allowed, but optional)
3136 * dst.w = 1.0;
3137 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3138 * dst = 2^src; (partial precision is allowed, but optional)
3140 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3142 if (ins->ctx->reg_maps->shader_version.major < 2)
3144 struct glsl_src_param src_param;
3145 char dst_mask[6];
3147 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3149 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3150 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3151 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3152 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3154 shader_glsl_append_dst(ins->ctx->buffer, ins);
3155 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3156 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3157 return;
3160 shader_glsl_scalar_op(ins);
3163 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3165 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3166 struct glsl_src_param src_param;
3167 unsigned int mask_size;
3168 DWORD write_mask;
3170 write_mask = shader_glsl_append_dst(buffer, ins);
3171 mask_size = shader_glsl_get_write_mask_size(write_mask);
3172 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3174 if (mask_size > 1)
3175 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
3176 else
3177 shader_addline(buffer, "int(%s));\n", src_param.param_str);
3180 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3182 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3183 struct glsl_src_param src_param;
3184 unsigned int mask_size;
3185 DWORD write_mask;
3187 write_mask = shader_glsl_append_dst(buffer, ins);
3188 mask_size = shader_glsl_get_write_mask_size(write_mask);
3189 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3191 if (mask_size > 1)
3192 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
3193 else
3194 shader_addline(buffer, "float(%s));\n", src_param.param_str);
3197 /** Process signed comparison opcodes in GLSL. */
3198 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3200 struct glsl_src_param src0_param;
3201 struct glsl_src_param src1_param;
3202 DWORD write_mask;
3203 unsigned int mask_size;
3205 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3206 mask_size = shader_glsl_get_write_mask_size(write_mask);
3207 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3208 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3210 if (mask_size > 1) {
3211 const char *compare;
3213 switch(ins->handler_idx)
3215 case WINED3DSIH_SLT: compare = "lessThan"; break;
3216 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3217 default: compare = "";
3218 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3221 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3222 src0_param.param_str, src1_param.param_str);
3223 } else {
3224 switch(ins->handler_idx)
3226 case WINED3DSIH_SLT:
3227 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3228 * to return 0.0 but step returns 1.0 because step is not < x
3229 * An alternative is a bvec compare padded with an unused second component.
3230 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3231 * issue. Playing with not() is not possible either because not() does not accept
3232 * a scalar.
3234 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3235 src0_param.param_str, src1_param.param_str);
3236 break;
3237 case WINED3DSIH_SGE:
3238 /* Here we can use the step() function and safe a conditional */
3239 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3240 break;
3241 default:
3242 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3248 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3250 const char *condition_prefix, *condition_suffix;
3251 struct wined3d_shader_dst_param dst;
3252 struct glsl_src_param src0_param;
3253 struct glsl_src_param src1_param;
3254 struct glsl_src_param src2_param;
3255 BOOL temp_destination = FALSE;
3256 DWORD cmp_channel = 0;
3257 unsigned int i, j;
3258 char mask_char[6];
3259 DWORD write_mask;
3261 switch (ins->handler_idx)
3263 case WINED3DSIH_CMP:
3264 condition_prefix = "";
3265 condition_suffix = " >= 0.0";
3266 break;
3268 case WINED3DSIH_CND:
3269 condition_prefix = "";
3270 condition_suffix = " > 0.5";
3271 break;
3273 case WINED3DSIH_MOVC:
3274 condition_prefix = "bool(";
3275 condition_suffix = ")";
3276 break;
3278 default:
3279 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3280 condition_prefix = "<unhandled prefix>";
3281 condition_suffix = "<unhandled suffix>";
3282 break;
3285 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3287 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3288 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3289 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3290 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3292 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3293 condition_prefix, src0_param.param_str, condition_suffix,
3294 src1_param.param_str, src2_param.param_str);
3295 return;
3298 dst = ins->dst[0];
3300 /* Splitting the instruction up in multiple lines imposes a problem:
3301 * The first lines may overwrite source parameters of the following lines.
3302 * Deal with that by using a temporary destination register if needed. */
3303 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3304 && ins->src[0].reg.type == dst.reg.type)
3305 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3306 && ins->src[1].reg.type == dst.reg.type)
3307 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3308 && ins->src[2].reg.type == dst.reg.type))
3309 temp_destination = TRUE;
3311 /* Cycle through all source0 channels. */
3312 for (i = 0; i < 4; ++i)
3314 write_mask = 0;
3315 /* Find the destination channels which use the current source0 channel. */
3316 for (j = 0; j < 4; ++j)
3318 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3320 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3321 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3324 dst.write_mask = ins->dst[0].write_mask & write_mask;
3326 if (temp_destination)
3328 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3329 continue;
3330 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3332 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3333 continue;
3335 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3336 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3337 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3339 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3340 condition_prefix, src0_param.param_str, condition_suffix,
3341 src1_param.param_str, src2_param.param_str);
3344 if (temp_destination)
3346 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3347 shader_glsl_append_dst(ins->ctx->buffer, ins);
3348 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3352 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3353 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3354 * the compare is done per component of src0. */
3355 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3357 struct glsl_src_param src0_param;
3358 struct glsl_src_param src1_param;
3359 struct glsl_src_param src2_param;
3360 DWORD write_mask;
3361 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3362 ins->ctx->reg_maps->shader_version.minor);
3364 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3366 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3367 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3368 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3369 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3371 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3372 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3373 else
3374 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3375 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3376 return;
3379 shader_glsl_conditional_move(ins);
3382 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3383 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3385 struct glsl_src_param src0_param;
3386 struct glsl_src_param src1_param;
3387 struct glsl_src_param src2_param;
3388 DWORD write_mask;
3390 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3391 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3392 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3393 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3394 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3395 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3398 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3399 Vertex shaders to GLSL codes */
3400 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3402 int i;
3403 int nComponents = 0;
3404 struct wined3d_shader_dst_param tmp_dst = {{0}};
3405 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3406 struct wined3d_shader_instruction tmp_ins;
3408 memset(&tmp_ins, 0, sizeof(tmp_ins));
3410 /* Set constants for the temporary argument */
3411 tmp_ins.ctx = ins->ctx;
3412 tmp_ins.dst_count = 1;
3413 tmp_ins.dst = &tmp_dst;
3414 tmp_ins.src_count = 2;
3415 tmp_ins.src = tmp_src;
3417 switch(ins->handler_idx)
3419 case WINED3DSIH_M4x4:
3420 nComponents = 4;
3421 tmp_ins.handler_idx = WINED3DSIH_DP4;
3422 break;
3423 case WINED3DSIH_M4x3:
3424 nComponents = 3;
3425 tmp_ins.handler_idx = WINED3DSIH_DP4;
3426 break;
3427 case WINED3DSIH_M3x4:
3428 nComponents = 4;
3429 tmp_ins.handler_idx = WINED3DSIH_DP3;
3430 break;
3431 case WINED3DSIH_M3x3:
3432 nComponents = 3;
3433 tmp_ins.handler_idx = WINED3DSIH_DP3;
3434 break;
3435 case WINED3DSIH_M3x2:
3436 nComponents = 2;
3437 tmp_ins.handler_idx = WINED3DSIH_DP3;
3438 break;
3439 default:
3440 break;
3443 tmp_dst = ins->dst[0];
3444 tmp_src[0] = ins->src[0];
3445 tmp_src[1] = ins->src[1];
3446 for (i = 0; i < nComponents; ++i)
3448 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3449 shader_glsl_dot(&tmp_ins);
3450 ++tmp_src[1].reg.idx[0].offset;
3455 The LRP instruction performs a component-wise linear interpolation
3456 between the second and third operands using the first operand as the
3457 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3458 This is equivalent to mix(src2, src1, src0);
3460 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3462 struct glsl_src_param src0_param;
3463 struct glsl_src_param src1_param;
3464 struct glsl_src_param src2_param;
3465 DWORD write_mask;
3467 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3469 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3470 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3471 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3473 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3474 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3477 /** Process the WINED3DSIO_LIT instruction in GLSL:
3478 * dst.x = dst.w = 1.0
3479 * dst.y = (src0.x > 0) ? src0.x
3480 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3481 * where src.w is clamped at +- 128
3483 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3485 struct glsl_src_param src0_param;
3486 struct glsl_src_param src1_param;
3487 struct glsl_src_param src3_param;
3488 char dst_mask[6];
3490 shader_glsl_append_dst(ins->ctx->buffer, ins);
3491 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3493 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3494 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3495 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3497 /* The sdk specifies the instruction like this
3498 * dst.x = 1.0;
3499 * if(src.x > 0.0) dst.y = src.x
3500 * else dst.y = 0.0.
3501 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3502 * else dst.z = 0.0;
3503 * dst.w = 1.0;
3504 * (where power = src.w clamped between -128 and 128)
3506 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3507 * dst.x = 1.0 ... No further explanation needed
3508 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3509 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3510 * dst.w = 1.0. ... Nothing fancy.
3512 * So we still have one conditional in there. So do this:
3513 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3515 * 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),
3516 * 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.
3517 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3519 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3520 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3521 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3523 shader_addline(ins->ctx->buffer,
3524 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3525 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3526 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3527 src0_param.param_str, src3_param.param_str, dst_mask);
3530 /** Process the WINED3DSIO_DST instruction in GLSL:
3531 * dst.x = 1.0
3532 * dst.y = src0.x * src0.y
3533 * dst.z = src0.z
3534 * dst.w = src1.w
3536 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3538 struct glsl_src_param src0y_param;
3539 struct glsl_src_param src0z_param;
3540 struct glsl_src_param src1y_param;
3541 struct glsl_src_param src1w_param;
3542 char dst_mask[6];
3544 shader_glsl_append_dst(ins->ctx->buffer, ins);
3545 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3547 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3548 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3549 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3550 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3552 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3553 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3556 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3557 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3558 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3560 * dst.x = cos(src0.?)
3561 * dst.y = sin(src0.?)
3562 * dst.z = dst.z
3563 * dst.w = dst.w
3565 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3567 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3568 struct glsl_src_param src0_param;
3569 DWORD write_mask;
3571 if (ins->ctx->reg_maps->shader_version.major < 4)
3573 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3575 write_mask = shader_glsl_append_dst(buffer, ins);
3576 switch (write_mask)
3578 case WINED3DSP_WRITEMASK_0:
3579 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3580 break;
3582 case WINED3DSP_WRITEMASK_1:
3583 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3584 break;
3586 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3587 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3588 src0_param.param_str, src0_param.param_str);
3589 break;
3591 default:
3592 ERR("Write mask should be .x, .y or .xy\n");
3593 break;
3596 return;
3599 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3602 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3604 char dst_mask[6];
3606 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3607 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3608 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3610 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3611 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3612 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3614 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3615 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3617 else
3619 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3620 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3621 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3624 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3626 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3627 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3628 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3632 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3633 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3634 * generate invalid code
3636 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3638 struct glsl_src_param src0_param;
3639 DWORD write_mask;
3641 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3642 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3644 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3647 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3648 * Start a for() loop where src1.y is the initial value of aL,
3649 * increment aL by src1.z for a total of src1.x iterations.
3650 * Need to use a temporary variable for this operation.
3652 /* FIXME: I don't think nested loops will work correctly this way. */
3653 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3655 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3656 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3657 const struct wined3d_shader *shader = ins->ctx->shader;
3658 const struct wined3d_shader_lconst *constant;
3659 struct glsl_src_param src1_param;
3660 const DWORD *control_values = NULL;
3662 if (ins->ctx->reg_maps->shader_version.major < 4)
3664 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3666 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3667 * class hardware doesn't support real varying indexing, but Microsoft
3668 * designed this feature for Shader model 2.x+. If the loop control is
3669 * known at compile time, the GLSL compiler can unroll the loop, and
3670 * replace indirect addressing with direct addressing. */
3671 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3673 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3675 if (constant->idx == ins->src[1].reg.idx[0].offset)
3677 control_values = constant->value;
3678 break;
3683 if (control_values)
3685 struct wined3d_shader_loop_control loop_control;
3686 loop_control.count = control_values[0];
3687 loop_control.start = control_values[1];
3688 loop_control.step = (int)control_values[2];
3690 if (loop_control.step > 0)
3692 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3693 loop_state->current_depth, loop_control.start,
3694 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3695 loop_state->current_depth, loop_control.step);
3697 else if (loop_control.step < 0)
3699 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3700 loop_state->current_depth, loop_control.start,
3701 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3702 loop_state->current_depth, loop_control.step);
3704 else
3706 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3707 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3708 loop_state->current_depth, loop_control.count,
3709 loop_state->current_depth);
3712 else
3714 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3715 loop_state->current_depth, loop_state->current_reg,
3716 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3717 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3720 ++loop_state->current_reg;
3722 else
3724 shader_addline(buffer, "for (;;)\n{\n");
3727 ++loop_state->current_depth;
3730 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3732 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3734 shader_addline(ins->ctx->buffer, "}\n");
3736 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3738 --loop_state->current_depth;
3739 --loop_state->current_reg;
3742 if (ins->handler_idx == WINED3DSIH_ENDREP)
3744 --loop_state->current_depth;
3748 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3750 const struct wined3d_shader *shader = ins->ctx->shader;
3751 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3752 const struct wined3d_shader_lconst *constant;
3753 struct glsl_src_param src0_param;
3754 const DWORD *control_values = NULL;
3756 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3757 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3759 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3761 if (constant->idx == ins->src[0].reg.idx[0].offset)
3763 control_values = constant->value;
3764 break;
3769 if (control_values)
3771 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3772 loop_state->current_depth, loop_state->current_depth,
3773 control_values[0], loop_state->current_depth);
3775 else
3777 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3778 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3779 loop_state->current_depth, loop_state->current_depth,
3780 src0_param.param_str, loop_state->current_depth);
3783 ++loop_state->current_depth;
3786 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3788 struct glsl_src_param src0_param;
3790 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3791 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3794 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3796 struct glsl_src_param src0_param;
3797 struct glsl_src_param src1_param;
3799 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3800 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3802 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3803 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3806 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3808 shader_addline(ins->ctx->buffer, "} else {\n");
3811 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3813 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3816 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3818 shader_addline(ins->ctx->buffer, "break;\n");
3821 /* FIXME: According to MSDN the compare is done per component. */
3822 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3824 struct glsl_src_param src0_param;
3825 struct glsl_src_param src1_param;
3827 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3828 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3830 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3831 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3834 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3836 struct glsl_src_param src_param;
3838 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3839 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3842 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3844 shader_addline(ins->ctx->buffer, "}\n");
3845 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3848 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3850 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3853 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3855 struct glsl_src_param src1_param;
3857 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3858 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3859 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3862 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3864 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3865 * function only suppresses the unhandled instruction warning
3869 /*********************************************
3870 * Pixel Shader Specific Code begins here
3871 ********************************************/
3872 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3874 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3875 ins->ctx->reg_maps->shader_version.minor);
3876 struct glsl_sample_function sample_function;
3877 DWORD sample_flags = 0;
3878 DWORD resource_idx;
3879 DWORD mask = 0, swizzle;
3880 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3882 /* 1.0-1.4: Use destination register as sampler source.
3883 * 2.0+: Use provided sampler source. */
3884 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3885 resource_idx = ins->dst[0].reg.idx[0].offset;
3886 else
3887 resource_idx = ins->src[1].reg.idx[0].offset;
3889 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3891 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3892 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3893 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3895 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3896 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3898 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3899 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3901 case WINED3D_TTFF_COUNT1:
3902 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3903 break;
3904 case WINED3D_TTFF_COUNT2:
3905 mask = WINED3DSP_WRITEMASK_1;
3906 break;
3907 case WINED3D_TTFF_COUNT3:
3908 mask = WINED3DSP_WRITEMASK_2;
3909 break;
3910 case WINED3D_TTFF_COUNT4:
3911 case WINED3D_TTFF_DISABLE:
3912 mask = WINED3DSP_WRITEMASK_3;
3913 break;
3917 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3919 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3921 if (src_mod == WINED3DSPSM_DZ) {
3922 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3923 mask = WINED3DSP_WRITEMASK_2;
3924 } else if (src_mod == WINED3DSPSM_DW) {
3925 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3926 mask = WINED3DSP_WRITEMASK_3;
3929 else
3931 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3932 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3934 /* ps 2.0 texldp instruction always divides by the fourth component. */
3935 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3936 mask = WINED3DSP_WRITEMASK_3;
3940 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
3941 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3943 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
3944 mask |= sample_function.coord_mask;
3946 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3947 else swizzle = ins->src[1].swizzle;
3949 /* 1.0-1.3: Use destination register as coordinate source.
3950 1.4+: Use provided coordinate source register. */
3951 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3953 char coord_mask[6];
3954 shader_glsl_write_mask_to_str(mask, coord_mask);
3955 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3956 "T%u%s", resource_idx, coord_mask);
3958 else
3960 struct glsl_src_param coord_param;
3961 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3962 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3964 struct glsl_src_param bias;
3965 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3966 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3967 "%s", coord_param.param_str);
3968 } else {
3969 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3970 "%s", coord_param.param_str);
3975 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3977 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3978 struct glsl_src_param coord_param, dx_param, dy_param;
3979 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3980 struct glsl_sample_function sample_function;
3981 DWORD sampler_idx;
3982 DWORD swizzle = ins->src[1].swizzle;
3983 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3985 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3987 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3988 shader_glsl_tex(ins);
3989 return;
3992 sampler_idx = ins->src[1].reg.idx[0].offset;
3993 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3994 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3996 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3997 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3998 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3999 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4001 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
4002 "%s", coord_param.param_str);
4005 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4007 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4008 struct glsl_src_param coord_param, lod_param;
4009 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4010 struct glsl_sample_function sample_function;
4011 DWORD sampler_idx;
4012 DWORD swizzle = ins->src[1].swizzle;
4013 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4015 sampler_idx = ins->src[1].reg.idx[0].offset;
4016 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4017 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
4018 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4020 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4021 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4023 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4025 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4026 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4028 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4029 * However, the NVIDIA drivers allow them in fragment shaders as well,
4030 * even without the appropriate extension. */
4031 WARN("Using %s in fragment shader.\n", sample_function.name);
4033 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
4034 "%s", coord_param.param_str);
4037 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4038 unsigned int resource_idx, unsigned int sampler_idx)
4040 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4041 unsigned int i;
4043 for (i = 0; i < sampler_map->count; ++i)
4045 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4046 return entries[i].bind_idx;
4049 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4051 return ~0u;
4054 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4056 struct glsl_sample_function sample_function;
4057 struct glsl_src_param coord_param;
4058 unsigned int sampler_idx;
4060 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
4061 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4062 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4063 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
4064 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4065 NULL, NULL, NULL, "%s", coord_param.param_str);
4068 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4070 /* FIXME: Make this work for more than just 2D textures */
4071 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4072 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4074 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4076 char dst_mask[6];
4078 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4079 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
4080 ins->dst[0].reg.idx[0].offset, dst_mask);
4082 else
4084 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4085 DWORD reg = ins->src[0].reg.idx[0].offset;
4086 char dst_swizzle[6];
4088 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4090 if (src_mod == WINED3DSPSM_DZ)
4092 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4093 struct glsl_src_param div_param;
4095 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
4097 if (mask_size > 1) {
4098 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4099 } else {
4100 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4103 else if (src_mod == WINED3DSPSM_DW)
4105 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4106 struct glsl_src_param div_param;
4108 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
4110 if (mask_size > 1) {
4111 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4112 } else {
4113 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4115 } else {
4116 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
4121 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4122 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4123 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4124 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4126 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4127 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4128 struct glsl_sample_function sample_function;
4129 struct glsl_src_param src0_param;
4130 UINT mask_size;
4132 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4134 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4135 * scalar, and projected sampling would require 4.
4137 * It is a dependent read - not valid with conditional NP2 textures
4139 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4140 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4142 switch(mask_size)
4144 case 1:
4145 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4146 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4147 break;
4149 case 2:
4150 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4151 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4152 break;
4154 case 3:
4155 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4156 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4157 break;
4159 default:
4160 FIXME("Unexpected mask size %u\n", mask_size);
4161 break;
4165 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4166 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4167 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4169 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4170 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4171 struct glsl_src_param src0_param;
4172 DWORD dst_mask;
4173 unsigned int mask_size;
4175 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4176 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4177 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4179 if (mask_size > 1) {
4180 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4181 } else {
4182 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4186 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4187 * Calculate the depth as dst.x / dst.y */
4188 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4190 struct glsl_dst_param dst_param;
4192 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4194 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4195 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4196 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4197 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4198 * >= 1.0 or < 0.0
4200 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4201 dst_param.reg_name, dst_param.reg_name);
4204 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4205 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4206 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4207 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4209 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4211 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4212 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4213 struct glsl_src_param src0_param;
4215 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4217 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4218 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4221 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4222 * Calculate the 1st of a 2-row matrix multiplication. */
4223 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4225 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4226 DWORD reg = ins->dst[0].reg.idx[0].offset;
4227 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4228 struct glsl_src_param src0_param;
4230 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4231 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4234 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4235 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4236 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4238 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4239 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4240 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4241 DWORD reg = ins->dst[0].reg.idx[0].offset;
4242 struct glsl_src_param src0_param;
4244 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4245 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4246 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4249 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4251 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4252 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4253 struct glsl_sample_function sample_function;
4254 DWORD reg = ins->dst[0].reg.idx[0].offset;
4255 struct glsl_src_param src0_param;
4257 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4258 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4260 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4262 /* Sample the texture using the calculated coordinates */
4263 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
4266 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4267 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4268 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4270 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4271 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4272 struct glsl_sample_function sample_function;
4273 DWORD reg = ins->dst[0].reg.idx[0].offset;
4274 struct glsl_src_param src0_param;
4276 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4277 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4279 /* Dependent read, not valid with conditional NP2 */
4280 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4282 /* Sample the texture using the calculated coordinates */
4283 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
4285 tex_mx->current_row = 0;
4288 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4289 * Perform the 3rd row of a 3x3 matrix multiply */
4290 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4292 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4293 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4294 DWORD reg = ins->dst[0].reg.idx[0].offset;
4295 struct glsl_src_param src0_param;
4296 char dst_mask[6];
4298 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4300 shader_glsl_append_dst(ins->ctx->buffer, ins);
4301 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4302 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4304 tex_mx->current_row = 0;
4307 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4308 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4309 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4311 struct glsl_src_param src0_param;
4312 struct glsl_src_param src1_param;
4313 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4314 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4315 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4316 struct glsl_sample_function sample_function;
4317 DWORD reg = ins->dst[0].reg.idx[0].offset;
4318 char coord_mask[6];
4320 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4321 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4323 /* Perform the last matrix multiply operation */
4324 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4325 /* Reflection calculation */
4326 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4328 /* Dependent read, not valid with conditional NP2 */
4329 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4330 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4332 /* Sample the texture */
4333 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4334 NULL, NULL, NULL, "tmp0%s", coord_mask);
4336 tex_mx->current_row = 0;
4339 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4340 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4341 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4343 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4344 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4345 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4346 struct glsl_sample_function sample_function;
4347 DWORD reg = ins->dst[0].reg.idx[0].offset;
4348 struct glsl_src_param src0_param;
4349 char coord_mask[6];
4351 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4353 /* Perform the last matrix multiply operation */
4354 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4356 /* Construct the eye-ray vector from w coordinates */
4357 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
4358 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4359 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4361 /* Dependent read, not valid with conditional NP2 */
4362 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4363 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4365 /* Sample the texture using the calculated coordinates */
4366 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4367 NULL, NULL, NULL, "tmp0%s", coord_mask);
4369 tex_mx->current_row = 0;
4372 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4373 * Apply a fake bump map transform.
4374 * texbem is pshader <= 1.3 only, this saves a few version checks
4376 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4378 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4379 struct glsl_sample_function sample_function;
4380 struct glsl_src_param coord_param;
4381 DWORD sampler_idx;
4382 DWORD mask;
4383 DWORD flags;
4384 char coord_mask[6];
4386 sampler_idx = ins->dst[0].reg.idx[0].offset;
4387 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4388 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4390 /* Dependent read, not valid with conditional NP2 */
4391 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4392 mask = sample_function.coord_mask;
4394 shader_glsl_write_mask_to_str(mask, coord_mask);
4396 /* With projected textures, texbem only divides the static texture coord,
4397 * not the displacement, so we can't let GL handle this. */
4398 if (flags & WINED3D_PSARGS_PROJECTED)
4400 DWORD div_mask=0;
4401 char coord_div_mask[3];
4402 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4404 case WINED3D_TTFF_COUNT1:
4405 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4406 break;
4407 case WINED3D_TTFF_COUNT2:
4408 div_mask = WINED3DSP_WRITEMASK_1;
4409 break;
4410 case WINED3D_TTFF_COUNT3:
4411 div_mask = WINED3DSP_WRITEMASK_2;
4412 break;
4413 case WINED3D_TTFF_COUNT4:
4414 case WINED3D_TTFF_DISABLE:
4415 div_mask = WINED3DSP_WRITEMASK_3;
4416 break;
4418 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4419 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4422 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4424 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4425 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4426 coord_param.param_str, coord_mask);
4428 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4430 struct glsl_src_param luminance_param;
4431 struct glsl_dst_param dst_param;
4433 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4434 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4436 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4437 dst_param.reg_name, dst_param.mask_str,
4438 luminance_param.param_str, sampler_idx, sampler_idx);
4442 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4444 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4445 struct glsl_src_param src0_param, src1_param;
4447 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4448 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4450 shader_glsl_append_dst(ins->ctx->buffer, ins);
4451 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4452 src0_param.param_str, sampler_idx, src1_param.param_str);
4455 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4456 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4457 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4459 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4460 struct glsl_sample_function sample_function;
4461 struct glsl_src_param src0_param;
4463 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4465 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4466 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4467 "%s.wx", src0_param.reg_name);
4470 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4471 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4472 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4474 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4475 struct glsl_sample_function sample_function;
4476 struct glsl_src_param src0_param;
4478 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4480 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4481 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4482 "%s.yz", src0_param.reg_name);
4485 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4486 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4487 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4489 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4490 struct glsl_sample_function sample_function;
4491 struct glsl_src_param src0_param;
4493 /* Dependent read, not valid with conditional NP2 */
4494 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4495 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4497 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4498 "%s", src0_param.param_str);
4501 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4502 * If any of the first 3 components are < 0, discard this pixel */
4503 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4505 struct glsl_dst_param dst_param;
4507 /* The argument is a destination parameter, and no writemasks are allowed */
4508 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4509 if (ins->ctx->reg_maps->shader_version.major >= 2)
4511 if (ins->ctx->reg_maps->shader_version.major >= 4)
4512 FIXME("SM4 discard not implemented.\n");
4513 /* 2.0 shaders compare all 4 components in texkill */
4514 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4515 } else {
4516 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4517 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4518 * 4 components are defined, only the first 3 are used
4520 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4524 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4525 * dst = dot2(src0, src1) + src2 */
4526 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4528 struct glsl_src_param src0_param;
4529 struct glsl_src_param src1_param;
4530 struct glsl_src_param src2_param;
4531 DWORD write_mask;
4532 unsigned int mask_size;
4534 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4535 mask_size = shader_glsl_get_write_mask_size(write_mask);
4537 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4538 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4539 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4541 if (mask_size > 1) {
4542 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4543 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4544 } else {
4545 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4546 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4550 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
4551 const struct wined3d_shader_signature *input_signature,
4552 const struct wined3d_shader_reg_maps *reg_maps,
4553 enum vertexprocessing_mode vertexprocessing)
4555 unsigned int i;
4557 for (i = 0; i < input_signature->element_count; ++i)
4559 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4560 const char *semantic_name;
4561 UINT semantic_idx;
4562 char reg_mask[6];
4564 /* Unused */
4565 if (!(reg_maps->input_registers & (1 << input->register_idx)))
4566 continue;
4568 semantic_name = input->semantic_name;
4569 semantic_idx = input->semantic_idx;
4570 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4572 if (vertexprocessing == vertexshader)
4574 if (input->sysval_semantic == WINED3D_SV_POSITION)
4575 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4576 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4577 else
4578 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4579 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4580 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4582 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4584 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4585 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4586 shader->u.ps.input_reg_map[input->register_idx], reg_mask, semantic_idx, reg_mask);
4587 else
4588 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4589 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4591 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4593 if (!semantic_idx)
4594 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4595 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4596 else if (semantic_idx == 1)
4597 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4598 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4599 else
4600 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4601 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4603 else
4605 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4606 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4611 /*********************************************
4612 * Vertex Shader Specific Code begins here
4613 ********************************************/
4615 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4617 struct glsl_program_key key;
4619 key.vs_id = entry->vs.id;
4620 key.gs_id = entry->gs.id;
4621 key.ps_id = entry->ps.id;
4623 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4625 ERR("Failed to insert program entry.\n");
4629 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4630 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4632 struct wine_rb_entry *entry;
4633 struct glsl_program_key key;
4635 key.vs_id = vs_id;
4636 key.gs_id = gs_id;
4637 key.ps_id = ps_id;
4639 entry = wine_rb_get(&priv->program_lookup, &key);
4640 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4643 /* Context activation is done by the caller. */
4644 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4645 struct glsl_shader_prog_link *entry)
4647 struct glsl_program_key key;
4649 key.vs_id = entry->vs.id;
4650 key.gs_id = entry->gs.id;
4651 key.ps_id = entry->ps.id;
4652 wine_rb_remove(&priv->program_lookup, &key);
4654 GL_EXTCALL(glDeleteProgram(entry->id));
4655 if (entry->vs.id)
4656 list_remove(&entry->vs.shader_entry);
4657 if (entry->gs.id)
4658 list_remove(&entry->gs.shader_entry);
4659 if (entry->ps.id)
4660 list_remove(&entry->ps.shader_entry);
4661 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4662 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4663 HeapFree(GetProcessHeap(), 0, entry);
4666 static void handle_ps3_input(struct wined3d_string_buffer *buffer,
4667 const struct wined3d_gl_info *gl_info, const DWORD *map,
4668 const struct wined3d_shader_signature *input_signature,
4669 const struct wined3d_shader_reg_maps *reg_maps_in,
4670 const struct wined3d_shader_signature *output_signature,
4671 const struct wined3d_shader_reg_maps *reg_maps_out)
4673 unsigned int i, j;
4674 DWORD *set;
4675 DWORD in_idx;
4676 unsigned int in_count = vec4_varyings(3, gl_info);
4677 char reg_mask[6];
4678 char destination[50];
4680 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4682 for (i = 0; i < input_signature->element_count; ++i)
4684 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4686 if (!(reg_maps_in->input_registers & (1 << input->register_idx)))
4687 continue;
4689 in_idx = map[input->register_idx];
4690 /* Declared, but not read register */
4691 if (in_idx == ~0u)
4692 continue;
4693 if (in_idx >= (in_count + 2))
4695 FIXME("More input varyings declared than supported, expect issues.\n");
4696 continue;
4699 if (in_idx == in_count)
4700 sprintf(destination, "gl_FrontColor");
4701 else if (in_idx == in_count + 1)
4702 sprintf(destination, "gl_FrontSecondaryColor");
4703 else
4704 sprintf(destination, "ps_link[%u]", in_idx);
4706 if (!set[in_idx])
4707 set[in_idx] = ~0u;
4709 for (j = 0; j < output_signature->element_count; ++j)
4711 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4712 DWORD mask;
4714 if (!(reg_maps_out->output_registers & (1 << output->register_idx))
4715 || input->semantic_idx != output->semantic_idx
4716 || strcmp(input->semantic_name, output->semantic_name)
4717 || !(mask = input->mask & output->mask))
4718 continue;
4720 if (set[in_idx] == ~0u)
4721 set[in_idx] = mask;
4722 else
4723 set[in_idx] |= mask;
4724 shader_glsl_write_mask_to_str(mask, reg_mask);
4726 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4727 destination, reg_mask, output->register_idx, reg_mask);
4731 for (i = 0; i < in_count + 2; ++i)
4733 unsigned int size;
4735 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4736 continue;
4738 if (set[i] == ~0U) set[i] = 0;
4740 size = 0;
4741 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4742 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4743 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4744 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4745 reg_mask[size] = '\0';
4747 if (i == in_count)
4748 sprintf(destination, "gl_FrontColor");
4749 else if (i == in_count + 1)
4750 sprintf(destination, "gl_FrontSecondaryColor");
4751 else
4752 sprintf(destination, "ps_link[%u]", i);
4754 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4755 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4758 HeapFree(GetProcessHeap(), 0, set);
4761 /* Context activation is done by the caller. */
4762 static GLuint generate_param_reorder_function(struct wined3d_string_buffer *buffer,
4763 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4764 const struct wined3d_gl_info *gl_info)
4766 GLuint ret = 0;
4767 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4768 unsigned int i;
4769 const char *semantic_name;
4770 UINT semantic_idx;
4771 char reg_mask[6];
4773 string_buffer_clear(buffer);
4775 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
4777 if (ps_major < 3)
4779 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4781 for (i = 0; i < vs->output_signature.element_count; ++i)
4783 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4784 DWORD write_mask;
4786 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4787 continue;
4789 semantic_name = output->semantic_name;
4790 semantic_idx = output->semantic_idx;
4791 write_mask = output->mask;
4792 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4794 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4796 if (!semantic_idx)
4797 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4798 reg_mask, output->register_idx, reg_mask);
4799 else if (semantic_idx == 1)
4800 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4801 reg_mask, output->register_idx, reg_mask);
4803 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4805 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4806 reg_mask, output->register_idx, reg_mask);
4808 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4810 if (semantic_idx < 8)
4812 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4813 write_mask |= WINED3DSP_WRITEMASK_3;
4815 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4816 semantic_idx, reg_mask, output->register_idx, reg_mask);
4817 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4818 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4821 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4823 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4825 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4827 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
4828 output->register_idx, reg_mask[1]);
4831 shader_addline(buffer, "}\n");
4833 else
4835 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4836 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4837 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4838 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4840 /* First, sort out position and point size. Those are not passed to the pixel shader */
4841 for (i = 0; i < vs->output_signature.element_count; ++i)
4843 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4845 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4846 continue;
4848 semantic_name = output->semantic_name;
4849 semantic_idx = output->semantic_idx;
4850 shader_glsl_write_mask_to_str(output->mask, reg_mask);
4852 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4854 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4855 reg_mask, output->register_idx, reg_mask);
4857 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4859 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4863 /* Then, fix the pixel shader input */
4864 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
4865 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
4867 shader_addline(buffer, "}\n");
4870 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4871 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4872 shader_glsl_compile(gl_info, ret, buffer->buffer);
4874 return ret;
4877 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
4879 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4880 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4881 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4882 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4883 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4884 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4887 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4889 switch (mode)
4891 case WINED3D_FFP_PS_FOG_OFF:
4892 return;
4894 case WINED3D_FFP_PS_FOG_LINEAR:
4895 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4896 break;
4898 case WINED3D_FFP_PS_FOG_EXP:
4899 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4900 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4901 break;
4903 case WINED3D_FFP_PS_FOG_EXP2:
4904 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4905 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4906 break;
4908 default:
4909 ERR("Invalid fog mode %#x.\n", mode);
4910 return;
4913 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4916 /* Context activation is done by the caller. */
4917 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4918 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
4919 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4921 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4922 const struct wined3d_gl_info *gl_info = context->gl_info;
4923 const DWORD *function = shader->function;
4924 struct shader_glsl_ctx_priv priv_ctx;
4926 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4927 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
4929 memset(&priv_ctx, 0, sizeof(priv_ctx));
4930 priv_ctx.cur_ps_args = args;
4931 priv_ctx.cur_np2fixup_info = np2fixup_info;
4933 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
4935 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4936 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4937 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4938 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4939 /* The spec says that it doesn't have to be explicitly enabled, but the
4940 * nvidia drivers write a warning if we don't do so. */
4941 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4942 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4943 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4944 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4945 if (gl_info->supported[EXT_GPU_SHADER4])
4946 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4948 /* Base Declarations */
4949 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4951 /* Pack 3.0 inputs */
4952 if (reg_maps->shader_version.major >= 3)
4953 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args->vp_mode);
4955 /* Base Shader Body */
4956 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4958 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4959 if (reg_maps->shader_version.major < 2)
4961 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4962 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4965 if (args->srgb_correction)
4966 shader_glsl_generate_srgb_write_correction(buffer);
4968 /* SM < 3 does not replace the fog stage. */
4969 if (reg_maps->shader_version.major < 3)
4970 shader_glsl_generate_fog_code(buffer, args->fog);
4972 shader_addline(buffer, "}\n");
4974 TRACE("Compiling shader object %u.\n", shader_id);
4975 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4977 return shader_id;
4980 /* Context activation is done by the caller. */
4981 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4982 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
4983 const struct vs_compile_args *args)
4985 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4986 const struct wined3d_gl_info *gl_info = context->gl_info;
4987 const DWORD *function = shader->function;
4988 struct shader_glsl_ctx_priv priv_ctx;
4990 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4991 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4993 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
4995 if (gl_info->supported[ARB_DRAW_INSTANCED])
4996 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
4997 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4998 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4999 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5000 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5001 if (gl_info->supported[EXT_GPU_SHADER4])
5002 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5004 memset(&priv_ctx, 0, sizeof(priv_ctx));
5005 priv_ctx.cur_vs_args = args;
5007 /* Base Declarations */
5008 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5010 /* Base Shader Body */
5011 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5013 /* Unpack outputs */
5014 shader_addline(buffer, "order_ps_input(vs_out);\n");
5016 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5017 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5018 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5019 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5021 if (args->fog_src == VS_FOG_Z)
5022 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
5023 else if (!reg_maps->fog)
5024 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
5026 /* We always store the clipplanes without y inversion */
5027 if (args->clip_enabled)
5028 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5030 /* Write the final position.
5032 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5033 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5034 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5035 * contains 1.0 to allow a mad.
5037 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5038 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5040 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5042 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5043 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5044 * which is the same as z = z * 2 - w.
5046 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5048 shader_addline(buffer, "}\n");
5050 TRACE("Compiling shader object %u.\n", shader_id);
5051 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5053 return shader_id;
5056 /* Context activation is done by the caller. */
5057 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5058 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader)
5060 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5061 const struct wined3d_gl_info *gl_info = context->gl_info;
5062 const DWORD *function = shader->function;
5063 struct shader_glsl_ctx_priv priv_ctx;
5064 GLuint shader_id;
5066 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5068 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5070 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5071 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5072 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5073 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5074 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5075 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5076 if (gl_info->supported[EXT_GPU_SHADER4])
5077 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5079 memset(&priv_ctx, 0, sizeof(priv_ctx));
5080 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5081 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5082 shader_addline(buffer, "}\n");
5084 TRACE("Compiling shader object %u.\n", shader_id);
5085 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5087 return shader_id;
5090 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5091 struct wined3d_string_buffer *buffer, struct wined3d_shader *shader,
5092 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5094 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5095 struct glsl_shader_private *shader_data;
5096 struct ps_np2fixup_info *np2fixup;
5097 UINT i;
5098 DWORD new_size;
5099 GLuint ret;
5101 if (!shader->backend_data)
5103 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5104 if (!shader->backend_data)
5106 ERR("Failed to allocate backend data.\n");
5107 return 0;
5110 shader_data = shader->backend_data;
5111 gl_shaders = shader_data->gl_shaders.ps;
5113 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5114 * so a linear search is more performant than a hashmap or a binary search
5115 * (cache coherency etc)
5117 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5119 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5121 if (args->np2_fixup)
5122 *np2fixup_info = &gl_shaders[i].np2fixup;
5123 return gl_shaders[i].id;
5127 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5128 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5129 if (shader_data->num_gl_shaders)
5131 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5132 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5133 new_size * sizeof(*gl_shaders));
5135 else
5137 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5138 new_size = 1;
5141 if(!new_array) {
5142 ERR("Out of memory\n");
5143 return 0;
5145 shader_data->gl_shaders.ps = new_array;
5146 shader_data->shader_array_size = new_size;
5147 gl_shaders = new_array;
5150 gl_shaders[shader_data->num_gl_shaders].args = *args;
5152 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5153 memset(np2fixup, 0, sizeof(*np2fixup));
5154 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5156 pixelshader_update_resource_types(shader, args->tex_types);
5158 string_buffer_clear(buffer);
5159 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
5160 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5162 return ret;
5165 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5166 const DWORD use_map) {
5167 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5168 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5169 return stored->fog_src == new->fog_src;
5172 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5173 struct wined3d_string_buffer *buffer, struct wined3d_shader *shader,
5174 const struct vs_compile_args *args)
5176 UINT i;
5177 DWORD new_size;
5178 DWORD use_map = context->stream_info.use_map;
5179 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5180 struct glsl_shader_private *shader_data;
5181 GLuint ret;
5183 if (!shader->backend_data)
5185 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5186 if (!shader->backend_data)
5188 ERR("Failed to allocate backend data.\n");
5189 return 0;
5192 shader_data = shader->backend_data;
5193 gl_shaders = shader_data->gl_shaders.vs;
5195 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5196 * so a linear search is more performant than a hashmap or a binary search
5197 * (cache coherency etc)
5199 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5201 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5202 return gl_shaders[i].id;
5205 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5207 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5208 if (shader_data->num_gl_shaders)
5210 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5211 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5212 new_size * sizeof(*gl_shaders));
5214 else
5216 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5217 new_size = 1;
5220 if(!new_array) {
5221 ERR("Out of memory\n");
5222 return 0;
5224 shader_data->gl_shaders.vs = new_array;
5225 shader_data->shader_array_size = new_size;
5226 gl_shaders = new_array;
5229 gl_shaders[shader_data->num_gl_shaders].args = *args;
5231 string_buffer_clear(buffer);
5232 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
5233 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5235 return ret;
5238 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5239 struct wined3d_string_buffer *buffer, struct wined3d_shader *shader)
5241 struct glsl_gs_compiled_shader *gl_shaders;
5242 struct glsl_shader_private *shader_data;
5243 GLuint ret;
5245 if (!shader->backend_data)
5247 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5249 ERR("Failed to allocate backend data.\n");
5250 return 0;
5253 shader_data = shader->backend_data;
5254 gl_shaders = shader_data->gl_shaders.gs;
5256 if (shader_data->num_gl_shaders)
5257 return gl_shaders[0].id;
5259 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5261 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5263 ERR("Failed to allocate GL shader array.\n");
5264 return 0;
5266 shader_data->shader_array_size = 1;
5267 gl_shaders = shader_data->gl_shaders.gs;
5269 string_buffer_clear(buffer);
5270 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
5271 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5273 return ret;
5276 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5278 switch (mcs)
5280 case WINED3D_MCS_MATERIAL:
5281 return material;
5282 case WINED3D_MCS_COLOR1:
5283 return "gl_Color";
5284 case WINED3D_MCS_COLOR2:
5285 return "gl_SecondaryColor";
5286 default:
5287 ERR("Invalid material color source %#x.\n", mcs);
5288 return "<invalid>";
5292 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5293 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5295 const char *diffuse, *specular, *emission, *ambient;
5296 enum wined3d_light_type light_type;
5297 unsigned int i;
5299 if (!settings->lighting)
5301 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
5302 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
5303 return;
5306 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
5307 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5308 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5309 shader_addline(buffer, "vec3 dir, dst;\n");
5310 shader_addline(buffer, "float att, t;\n");
5312 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5313 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5314 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5315 emission = shader_glsl_ffp_mcs(settings->emission_source, "ffp_material.emission");
5317 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5319 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5320 switch (light_type)
5322 case WINED3D_LIGHT_POINT:
5323 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
5324 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5325 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5326 shader_addline(buffer, "dst.x = 1.0;\n");
5327 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
5328 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
5329 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
5330 if (!settings->normal)
5331 break;
5332 shader_addline(buffer, "dir = normalize(dir);\n");
5333 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5334 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
5335 if (settings->localviewer)
5336 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5337 else
5338 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5339 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5340 " * gl_LightSource[%u].specular) / att;\n", i);
5341 break;
5343 case WINED3D_LIGHT_SPOT:
5344 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
5345 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5346 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5347 shader_addline(buffer, "dst.x = 1.0;\n");
5348 shader_addline(buffer, "dir = normalize(dir);\n");
5349 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
5350 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
5351 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
5352 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
5353 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
5354 i, i, i, i);
5355 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
5356 if (!settings->normal)
5357 break;
5358 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5359 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
5360 if (settings->localviewer)
5361 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5362 else
5363 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5364 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5365 " * gl_LightSource[%u].specular) * att;\n", i);
5366 break;
5368 case WINED3D_LIGHT_DIRECTIONAL:
5369 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
5370 if (!settings->normal)
5371 break;
5372 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
5373 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5374 " * gl_LightSource[%u].diffuse.xyz;\n", i);
5375 /* TODO: In the non-local viewer case the halfvector is constant
5376 * and could be precomputed and stored in a uniform. */
5377 if (settings->localviewer)
5378 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5379 else
5380 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5381 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5382 " * gl_LightSource[%u].specular;\n", i);
5383 break;
5385 default:
5386 if (light_type)
5387 FIXME("Unhandled light type %#x.\n", light_type);
5388 continue;
5392 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5393 ambient, diffuse, emission);
5394 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
5395 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
5398 /* Context activation is done by the caller. */
5399 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_string_buffer *buffer,
5400 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5402 GLuint shader_obj;
5403 unsigned int i;
5405 string_buffer_clear(buffer);
5407 shader_addline(buffer, "#version 120\n");
5408 shader_addline(buffer, "\n");
5410 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix;\n");
5411 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5412 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5413 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
5415 shader_addline(buffer, "uniform struct\n{\n");
5416 shader_addline(buffer, " vec4 emission;\n");
5417 shader_addline(buffer, " vec4 ambient;\n");
5418 shader_addline(buffer, " vec4 diffuse;\n");
5419 shader_addline(buffer, " vec4 specular;\n");
5420 shader_addline(buffer, " float shininess;\n");
5421 shader_addline(buffer, "} ffp_material;\n");
5423 shader_addline(buffer, "\nvoid main()\n{\n");
5424 shader_addline(buffer, "float m;\n");
5425 shader_addline(buffer, "vec3 r;\n");
5427 if (settings->transformed)
5429 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
5430 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5431 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
5433 else
5435 shader_addline(buffer, "vec4 ec_pos = ffp_modelview_matrix * gl_Vertex;\n");
5436 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5437 if (settings->clipping)
5438 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5439 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5442 if (!settings->normal)
5443 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5444 else if (settings->normalize)
5445 shader_addline(buffer, "vec3 normal = normalize(ffp_normal_matrix * gl_Normal);\n");
5446 else
5447 shader_addline(buffer, "vec3 normal = ffp_normal_matrix * gl_Normal;\n");
5449 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
5451 for (i = 0; i < MAX_TEXTURES; ++i)
5453 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
5455 case WINED3DTSS_TCI_PASSTHRU:
5456 if (settings->texcoords & (1 << i))
5457 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * gl_MultiTexCoord%d;\n",
5458 i, i, i);
5459 break;
5461 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5462 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
5463 break;
5465 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5466 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
5467 break;
5469 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5470 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5471 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5472 break;
5474 case WINED3DTSS_TCI_SPHEREMAP:
5475 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5476 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5477 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5478 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5479 break;
5481 default:
5482 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5483 break;
5487 switch (settings->fog_mode)
5489 case WINED3D_FFP_VS_FOG_OFF:
5490 break;
5492 case WINED3D_FFP_VS_FOG_FOGCOORD:
5493 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5494 break;
5496 case WINED3D_FFP_VS_FOG_RANGE:
5497 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5498 break;
5500 case WINED3D_FFP_VS_FOG_DEPTH:
5501 if (settings->ortho_fog)
5502 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5503 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5504 else if (settings->transformed)
5505 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5506 else
5507 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5508 break;
5510 default:
5511 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5512 break;
5515 if (settings->point_size)
5517 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5518 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5519 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5520 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5523 shader_addline(buffer, "}\n");
5525 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5526 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5528 return shader_obj;
5531 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
5532 DWORD argnum, unsigned int stage, DWORD arg)
5534 const char *ret;
5536 if (arg == ARG_UNUSED)
5537 return "<unused arg>";
5539 switch (arg & WINED3DTA_SELECTMASK)
5541 case WINED3DTA_DIFFUSE:
5542 ret = "gl_Color";
5543 break;
5545 case WINED3DTA_CURRENT:
5546 if (!stage)
5547 ret = "gl_Color";
5548 else
5549 ret = "ret";
5550 break;
5552 case WINED3DTA_TEXTURE:
5553 switch (stage)
5555 case 0: ret = "tex0"; break;
5556 case 1: ret = "tex1"; break;
5557 case 2: ret = "tex2"; break;
5558 case 3: ret = "tex3"; break;
5559 case 4: ret = "tex4"; break;
5560 case 5: ret = "tex5"; break;
5561 case 6: ret = "tex6"; break;
5562 case 7: ret = "tex7"; break;
5563 default:
5564 ret = "<invalid texture>";
5565 break;
5567 break;
5569 case WINED3DTA_TFACTOR:
5570 ret = "tex_factor";
5571 break;
5573 case WINED3DTA_SPECULAR:
5574 ret = "gl_SecondaryColor";
5575 break;
5577 case WINED3DTA_TEMP:
5578 ret = "temp_reg";
5579 break;
5581 case WINED3DTA_CONSTANT:
5582 switch (stage)
5584 case 0: ret = "tss_const0"; break;
5585 case 1: ret = "tss_const1"; break;
5586 case 2: ret = "tss_const2"; break;
5587 case 3: ret = "tss_const3"; break;
5588 case 4: ret = "tss_const4"; break;
5589 case 5: ret = "tss_const5"; break;
5590 case 6: ret = "tss_const6"; break;
5591 case 7: ret = "tss_const7"; break;
5592 default:
5593 ret = "<invalid constant>";
5594 break;
5596 break;
5598 default:
5599 return "<unhandled arg>";
5602 if (arg & WINED3DTA_COMPLEMENT)
5604 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5605 if (argnum == 0)
5606 ret = "arg0";
5607 else if (argnum == 1)
5608 ret = "arg1";
5609 else if (argnum == 2)
5610 ret = "arg2";
5613 if (arg & WINED3DTA_ALPHAREPLICATE)
5615 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5616 if (argnum == 0)
5617 ret = "arg0";
5618 else if (argnum == 1)
5619 ret = "arg1";
5620 else if (argnum == 2)
5621 ret = "arg2";
5624 return ret;
5627 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
5628 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5630 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5632 if (color && alpha)
5633 dstmask = "";
5634 else if (color)
5635 dstmask = ".xyz";
5636 else
5637 dstmask = ".w";
5639 if (dst == tempreg)
5640 dstreg = "temp_reg";
5641 else
5642 dstreg = "ret";
5644 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5645 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5646 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5648 switch (op)
5650 case WINED3D_TOP_DISABLE:
5651 if (!stage)
5652 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5653 break;
5655 case WINED3D_TOP_SELECT_ARG1:
5656 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5657 break;
5659 case WINED3D_TOP_SELECT_ARG2:
5660 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5661 break;
5663 case WINED3D_TOP_MODULATE:
5664 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5665 break;
5667 case WINED3D_TOP_MODULATE_4X:
5668 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5669 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5670 break;
5672 case WINED3D_TOP_MODULATE_2X:
5673 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5674 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5675 break;
5677 case WINED3D_TOP_ADD:
5678 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5679 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5680 break;
5682 case WINED3D_TOP_ADD_SIGNED:
5683 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5684 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5685 break;
5687 case WINED3D_TOP_ADD_SIGNED_2X:
5688 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5689 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5690 break;
5692 case WINED3D_TOP_SUBTRACT:
5693 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5694 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5695 break;
5697 case WINED3D_TOP_ADD_SMOOTH:
5698 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5699 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5700 break;
5702 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5703 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5704 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5705 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5706 break;
5708 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5709 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5710 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5711 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5712 break;
5714 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5715 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5716 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5717 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5718 break;
5720 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5721 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5722 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5723 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5724 break;
5726 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5727 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5728 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5729 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5730 break;
5732 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5733 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5734 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5735 break;
5737 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5738 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5739 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5740 break;
5742 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5743 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5744 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5745 break;
5746 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5747 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5748 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5749 break;
5751 case WINED3D_TOP_BUMPENVMAP:
5752 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5753 /* These are handled in the first pass, nothing to do. */
5754 break;
5756 case WINED3D_TOP_DOTPRODUCT3:
5757 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5758 dstreg, dstmask, arg1, arg2, dstmask);
5759 break;
5761 case WINED3D_TOP_MULTIPLY_ADD:
5762 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5763 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5764 break;
5766 case WINED3D_TOP_LERP:
5767 /* MSDN isn't quite right here. */
5768 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5769 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5770 break;
5772 default:
5773 FIXME("Unhandled operation %#x.\n", op);
5774 break;
5778 /* Context activation is done by the caller. */
5779 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_string_buffer *buffer,
5780 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5782 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5783 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5784 const char *final_combiner_src = "ret";
5785 UINT lowest_disabled_stage;
5786 GLuint shader_id;
5787 DWORD arg0, arg1, arg2;
5788 unsigned int stage;
5790 string_buffer_clear(buffer);
5792 /* Find out which textures are read */
5793 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5795 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5796 break;
5798 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5799 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5800 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5802 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
5803 || (stage == 0 && settings->color_key_enabled))
5804 tex_map |= 1 << stage;
5805 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5806 tfactor_used = TRUE;
5807 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5808 tempreg_used = TRUE;
5809 if (settings->op[stage].dst == tempreg)
5810 tempreg_used = TRUE;
5811 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5812 tss_const_map |= 1 << stage;
5814 switch (settings->op[stage].cop)
5816 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5817 lum_map |= 1 << stage;
5818 /* fall through */
5819 case WINED3D_TOP_BUMPENVMAP:
5820 bump_map |= 1 << stage;
5821 /* fall through */
5822 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5823 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5824 tex_map |= 1 << stage;
5825 break;
5827 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5828 tfactor_used = TRUE;
5829 break;
5831 default:
5832 break;
5835 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5836 continue;
5838 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5839 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5840 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5842 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5843 tex_map |= 1 << stage;
5844 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5845 tfactor_used = TRUE;
5846 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5847 tempreg_used = TRUE;
5848 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5849 tss_const_map |= 1 << stage;
5851 lowest_disabled_stage = stage;
5853 shader_addline(buffer, "#version 120\n");
5855 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5856 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5858 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5859 shader_addline(buffer, "vec4 ret;\n");
5860 if (tempreg_used || settings->sRGB_write)
5861 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
5862 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5864 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5866 if (tss_const_map & (1 << stage))
5867 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5869 if (!(tex_map & (1 << stage)))
5870 continue;
5872 switch (settings->op[stage].tex_type)
5874 case WINED3D_GL_RES_TYPE_TEX_1D:
5875 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5876 break;
5877 case WINED3D_GL_RES_TYPE_TEX_2D:
5878 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5879 break;
5880 case WINED3D_GL_RES_TYPE_TEX_3D:
5881 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5882 break;
5883 case WINED3D_GL_RES_TYPE_TEX_CUBE:
5884 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5885 break;
5886 case WINED3D_GL_RES_TYPE_TEX_RECT:
5887 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5888 break;
5889 default:
5890 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5891 break;
5894 shader_addline(buffer, "vec4 tex%u;\n", stage);
5896 if (!(bump_map & (1 << stage)))
5897 continue;
5898 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5900 if (!(lum_map & (1 << stage)))
5901 continue;
5902 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5903 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5905 if (tfactor_used)
5906 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5907 if (settings->color_key_enabled)
5908 shader_addline(buffer, "uniform vec4 color_key;\n");
5909 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5911 if (settings->sRGB_write)
5913 shader_addline(buffer, "const vec4 srgb_const0 = ");
5914 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5915 shader_addline(buffer, ";\n");
5916 shader_addline(buffer, "const vec4 srgb_const1 = ");
5917 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5918 shader_addline(buffer, ";\n");
5921 shader_addline(buffer, "void main()\n{\n");
5923 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5924 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5926 /* Generate texture sampling instructions) */
5927 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5929 const char *texture_function, *coord_mask;
5930 char tex_reg_name[8];
5931 BOOL proj;
5933 if (!(tex_map & (1 << stage)))
5934 continue;
5936 if (settings->op[stage].projected == proj_none)
5938 proj = FALSE;
5940 else if (settings->op[stage].projected == proj_count4
5941 || settings->op[stage].projected == proj_count3)
5943 proj = TRUE;
5945 else
5947 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5948 proj = TRUE;
5951 switch (settings->op[stage].tex_type)
5953 case WINED3D_GL_RES_TYPE_TEX_1D:
5954 if (proj)
5956 texture_function = "texture1DProj";
5957 coord_mask = "xw";
5959 else
5961 texture_function = "texture1D";
5962 coord_mask = "x";
5964 break;
5965 case WINED3D_GL_RES_TYPE_TEX_2D:
5966 if (proj)
5968 texture_function = "texture2DProj";
5969 coord_mask = "xyw";
5971 else
5973 texture_function = "texture2D";
5974 coord_mask = "xy";
5976 break;
5977 case WINED3D_GL_RES_TYPE_TEX_3D:
5978 if (proj)
5980 texture_function = "texture3DProj";
5981 coord_mask = "xyzw";
5983 else
5985 texture_function = "texture3D";
5986 coord_mask = "xyz";
5988 break;
5989 case WINED3D_GL_RES_TYPE_TEX_CUBE:
5990 texture_function = "textureCube";
5991 coord_mask = "xyz";
5992 break;
5993 case WINED3D_GL_RES_TYPE_TEX_RECT:
5994 if (proj)
5996 texture_function = "texture2DRectProj";
5997 coord_mask = "xyw";
5999 else
6001 texture_function = "texture2DRect";
6002 coord_mask = "xy";
6004 break;
6005 default:
6006 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6007 texture_function = "";
6008 coord_mask = "xyzw";
6009 break;
6012 if (stage > 0
6013 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6014 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6016 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6018 /* With projective textures, texbem only divides the static
6019 * texture coord, not the displacement, so multiply the
6020 * displacement with the dividing parameter before passing it to
6021 * TXP. */
6022 if (settings->op[stage].projected != proj_none)
6024 if (settings->op[stage].projected == proj_count4)
6026 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
6027 stage, stage);
6028 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
6030 else
6032 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
6033 stage, stage);
6034 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
6037 else
6039 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
6042 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6043 stage, texture_function, stage, coord_mask);
6045 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6046 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6047 stage, stage - 1, stage - 1, stage - 1);
6049 else if (settings->op[stage].projected == proj_count3)
6051 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
6052 stage, texture_function, stage, stage);
6054 else
6056 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
6057 stage, texture_function, stage, stage, coord_mask);
6060 sprintf(tex_reg_name, "tex%u", stage);
6061 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
6062 settings->op[stage].color_fixup);
6065 if (settings->color_key_enabled)
6066 shader_addline(buffer, "if (all(equal(tex0, color_key))) discard;\n");
6068 /* Generate the main shader */
6069 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6071 BOOL op_equal;
6073 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6075 if (!stage)
6076 final_combiner_src = "gl_Color";
6077 break;
6080 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6081 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6082 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6083 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6084 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6085 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6086 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6087 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6088 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6089 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6090 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6091 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6092 else
6093 op_equal = settings->op[stage].aop == settings->op[stage].cop
6094 && settings->op[stage].carg0 == settings->op[stage].aarg0
6095 && settings->op[stage].carg1 == settings->op[stage].aarg1
6096 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6098 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6100 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6101 settings->op[stage].cop, settings->op[stage].carg0,
6102 settings->op[stage].carg1, settings->op[stage].carg2);
6103 if (!stage)
6104 shader_addline(buffer, "ret.w = gl_Color.w;\n");
6106 else if (op_equal)
6108 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6109 settings->op[stage].cop, settings->op[stage].carg0,
6110 settings->op[stage].carg1, settings->op[stage].carg2);
6112 else
6114 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6115 settings->op[stage].cop, settings->op[stage].carg0,
6116 settings->op[stage].carg1, settings->op[stage].carg2);
6117 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6118 settings->op[stage].aop, settings->op[stage].aarg0,
6119 settings->op[stage].aarg1, settings->op[stage].aarg2);
6123 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
6125 if (settings->sRGB_write)
6126 shader_glsl_generate_srgb_write_correction(buffer);
6128 shader_glsl_generate_fog_code(buffer, settings->fog);
6130 shader_addline(buffer, "}\n");
6132 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6133 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6134 return shader_id;
6137 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6138 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6140 struct glsl_ffp_vertex_shader *shader;
6141 const struct wine_rb_entry *entry;
6143 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6144 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6146 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6147 return NULL;
6149 shader->desc.settings = *settings;
6150 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
6151 list_init(&shader->linked_programs);
6152 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6153 ERR("Failed to insert ffp vertex shader.\n");
6155 return shader;
6158 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6159 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6161 struct glsl_ffp_fragment_shader *glsl_desc;
6162 const struct ffp_frag_desc *desc;
6164 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6165 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6167 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6168 return NULL;
6170 glsl_desc->entry.settings = *args;
6171 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
6172 list_init(&glsl_desc->linked_programs);
6173 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6175 return glsl_desc;
6179 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6180 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6182 unsigned int i;
6183 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6185 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6186 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6187 for (i = 0; i < vs_c_count; ++i)
6189 string_buffer_sprintf(name, "vs_c[%u]", i);
6190 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6192 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6193 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
6195 for (i = 0; i < MAX_CONST_I; ++i)
6197 string_buffer_sprintf(name, "vs_i[%u]", i);
6198 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6201 for (i = 0; i < MAX_CONST_B; ++i)
6203 string_buffer_sprintf(name, "vs_b[%u]", i);
6204 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6207 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
6209 vs->modelview_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_modelview_matrix"));
6210 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
6211 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
6212 for (i = 0; i < MAX_TEXTURES; ++i)
6214 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
6215 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6217 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
6218 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
6219 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
6220 vs->material_emission_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emission"));
6221 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
6223 string_buffer_release(&priv->string_buffers, name);
6226 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
6227 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
6229 unsigned int i;
6230 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6232 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6233 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
6234 for (i = 0; i < ps_c_count; ++i)
6236 string_buffer_sprintf(name, "ps_c[%u]", i);
6237 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6239 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
6240 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
6242 for (i = 0; i < MAX_CONST_I; ++i)
6244 string_buffer_sprintf(name, "ps_i[%u]", i);
6245 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6248 for (i = 0; i < MAX_CONST_B; ++i)
6250 string_buffer_sprintf(name, "ps_b[%u]", i);
6251 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6254 for (i = 0; i < MAX_TEXTURES; ++i)
6256 string_buffer_sprintf(name, "bumpenv_mat%u", i);
6257 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6258 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
6259 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6260 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
6261 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6262 string_buffer_sprintf(name, "tss_const%u", i);
6263 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6266 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
6267 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
6268 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
6269 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
6270 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
6272 string_buffer_release(&priv->string_buffers, name);
6275 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
6276 struct shader_glsl_priv *priv, GLuint program_id,
6277 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
6279 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6280 GLuint block_idx;
6281 unsigned int i;
6282 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6284 for (i = 0; i < count; ++i)
6286 if (!reg_maps->cb_sizes[i])
6287 continue;
6289 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
6290 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
6291 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
6293 checkGLcall("glUniformBlockBinding");
6294 string_buffer_release(&priv->string_buffers, name);
6297 /* Context activation is done by the caller. */
6298 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
6299 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
6301 const struct wined3d_gl_info *gl_info = context->gl_info;
6302 const struct ps_np2fixup_info *np2fixup_info = NULL;
6303 struct glsl_shader_prog_link *entry = NULL;
6304 struct wined3d_shader *vshader = NULL;
6305 struct wined3d_shader *gshader = NULL;
6306 struct wined3d_shader *pshader = NULL;
6307 GLuint program_id = 0;
6308 GLuint reorder_shader_id = 0;
6309 unsigned int i;
6310 GLuint vs_id = 0;
6311 GLuint gs_id = 0;
6312 GLuint ps_id = 0;
6313 struct list *ps_list, *vs_list;
6315 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
6317 vs_id = ctx_data->glsl_program->vs.id;
6318 vs_list = &ctx_data->glsl_program->vs.shader_entry;
6320 if (use_vs(state))
6322 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6323 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
6325 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
6326 && ctx_data->glsl_program->gs.id)
6327 gs_id = ctx_data->glsl_program->gs.id;
6328 else if (gshader)
6329 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
6332 else if (use_vs(state))
6334 struct vs_compile_args vs_compile_args;
6335 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6337 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
6338 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
6339 vs_list = &vshader->linked_programs;
6341 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
6342 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
6344 else if (priv->vertex_pipe == &glsl_vertex_pipe)
6346 struct glsl_ffp_vertex_shader *ffp_shader;
6347 struct wined3d_ffp_vs_settings settings;
6349 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
6350 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
6351 vs_id = ffp_shader->id;
6352 vs_list = &ffp_shader->linked_programs;
6355 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
6357 ps_id = ctx_data->glsl_program->ps.id;
6358 ps_list = &ctx_data->glsl_program->ps.shader_entry;
6360 if (use_ps(state))
6361 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6363 else if (use_ps(state))
6365 struct ps_compile_args ps_compile_args;
6366 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6367 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
6368 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
6369 pshader, &ps_compile_args, &np2fixup_info);
6370 ps_list = &pshader->linked_programs;
6372 else if (priv->fragment_pipe == &glsl_fragment_pipe)
6374 struct glsl_ffp_fragment_shader *ffp_shader;
6375 struct ffp_frag_settings settings;
6377 gen_ffp_frag_op(context, state, &settings, FALSE);
6378 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
6379 ps_id = ffp_shader->id;
6380 ps_list = &ffp_shader->linked_programs;
6383 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
6385 ctx_data->glsl_program = entry;
6386 return;
6389 /* If we get to this point, then no matching program exists, so we create one */
6390 program_id = GL_EXTCALL(glCreateProgram());
6391 TRACE("Created new GLSL shader program %u.\n", program_id);
6393 /* Create the entry */
6394 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
6395 entry->id = program_id;
6396 entry->vs.id = vs_id;
6397 entry->gs.id = gs_id;
6398 entry->ps.id = ps_id;
6399 entry->constant_version = 0;
6400 entry->ps.np2_fixup_info = np2fixup_info;
6401 /* Add the hash table entry */
6402 add_glsl_program_entry(priv, entry);
6404 /* Set the current program */
6405 ctx_data->glsl_program = entry;
6407 /* Attach GLSL vshader */
6408 if (vs_id)
6410 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
6411 GL_EXTCALL(glAttachShader(program_id, vs_id));
6412 checkGLcall("glAttachShader");
6414 list_add_head(vs_list, &entry->vs.shader_entry);
6417 if (vshader)
6419 WORD map = vshader->reg_maps.input_registers;
6420 struct wined3d_string_buffer *tmp_name = string_buffer_get(&priv->string_buffers);
6422 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
6423 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
6424 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
6425 checkGLcall("glAttachShader");
6426 /* Flag the reorder function for deletion, then it will be freed automatically when the program
6427 * is destroyed
6429 GL_EXTCALL(glDeleteShader(reorder_shader_id));
6431 /* Bind vertex attributes to a corresponding index number to match
6432 * the same index numbers as ARB_vertex_programs (makes loading
6433 * vertex attributes simpler). With this method, we can use the
6434 * exact same code to load the attributes later for both ARB and
6435 * GLSL shaders.
6437 * We have to do this here because we need to know the Program ID
6438 * in order to make the bindings work, and it has to be done prior
6439 * to linking the GLSL program. */
6440 for (i = 0; map; map >>= 1, ++i)
6442 if (!(map & 1)) continue;
6444 string_buffer_sprintf(tmp_name, "vs_in%u", i);
6445 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
6447 checkGLcall("glBindAttribLocation");
6448 string_buffer_release(&priv->string_buffers, tmp_name);
6451 if (gshader)
6453 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
6454 GL_EXTCALL(glAttachShader(program_id, gs_id));
6455 checkGLcall("glAttachShader");
6457 TRACE("input type %s, output type %s, vertices out %u.\n",
6458 debug_d3dprimitivetype(gshader->u.gs.input_type),
6459 debug_d3dprimitivetype(gshader->u.gs.output_type),
6460 gshader->u.gs.vertices_out);
6461 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
6462 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
6463 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
6464 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
6465 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
6466 gshader->u.gs.vertices_out));
6467 checkGLcall("glProgramParameteriARB");
6469 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
6472 /* Attach GLSL pshader */
6473 if (ps_id)
6475 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
6476 GL_EXTCALL(glAttachShader(program_id, ps_id));
6477 checkGLcall("glAttachShader");
6479 list_add_head(ps_list, &entry->ps.shader_entry);
6482 /* Link the program */
6483 TRACE("Linking GLSL shader program %u.\n", program_id);
6484 GL_EXTCALL(glLinkProgram(program_id));
6485 shader_glsl_validate_link(gl_info, program_id);
6487 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
6488 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6489 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
6490 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6491 checkGLcall("Find glsl program uniform locations");
6493 if (pshader && pshader->reg_maps.shader_version.major >= 3
6494 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6496 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6497 entry->vs.vertex_color_clamp = GL_FALSE;
6499 else
6501 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6504 /* Set the shader to allow uniform loading on it */
6505 GL_EXTCALL(glUseProgram(program_id));
6506 checkGLcall("glUseProgram");
6508 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6509 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6510 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6511 * vertex shader with fixed function pixel processing is used we make sure that the card
6512 * supports enough samplers to allow the max number of vertex samplers with all possible
6513 * fixed function fragment processing setups. So once the program is linked these samplers
6514 * won't change. */
6515 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
6517 entry->constant_update_mask = 0;
6518 if (vshader)
6520 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6521 if (vshader->reg_maps.integer_constants)
6522 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6523 if (vshader->reg_maps.boolean_constants)
6524 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6525 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6527 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
6528 0, gl_info->limits.vertex_uniform_blocks);
6530 else
6532 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
6533 | WINED3D_SHADER_CONST_FFP_PROJ;
6535 for (i = 0; i < MAX_TEXTURES; ++i)
6537 if (entry->vs.texture_matrix_location[i] != -1)
6539 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
6540 break;
6543 if (entry->vs.material_ambient_location != -1)
6544 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
6547 if (gshader)
6548 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
6549 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6551 if (ps_id)
6553 if (pshader)
6555 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6556 if (pshader->reg_maps.integer_constants)
6557 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6558 if (pshader->reg_maps.boolean_constants)
6559 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6560 if (entry->ps.ycorrection_location != -1)
6561 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6563 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
6564 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6565 gl_info->limits.fragment_uniform_blocks);
6567 else
6569 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6572 for (i = 0; i < MAX_TEXTURES; ++i)
6574 if (entry->ps.bumpenv_mat_location[i] != -1)
6576 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6577 break;
6581 if (entry->ps.np2_fixup_location != -1)
6582 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6583 if (entry->ps.color_key_location != -1)
6584 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
6588 /* Context activation is done by the caller. */
6589 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
6590 BOOL masked)
6592 GLuint program_id;
6593 GLuint vshader_id, pshader_id;
6594 const char *blt_pshader;
6596 static const char blt_vshader[] =
6597 "#version 120\n"
6598 "void main(void)\n"
6599 "{\n"
6600 " gl_Position = gl_Vertex;\n"
6601 " gl_FrontColor = vec4(1.0);\n"
6602 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6603 "}\n";
6605 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
6607 /* WINED3D_GL_RES_TYPE_TEX_1D */
6608 NULL,
6609 /* WINED3D_GL_RES_TYPE_TEX_2D */
6610 "#version 120\n"
6611 "uniform sampler2D sampler;\n"
6612 "void main(void)\n"
6613 "{\n"
6614 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6615 "}\n",
6616 /* WINED3D_GL_RES_TYPE_TEX_3D */
6617 NULL,
6618 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6619 "#version 120\n"
6620 "uniform samplerCube sampler;\n"
6621 "void main(void)\n"
6622 "{\n"
6623 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6624 "}\n",
6625 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6626 "#version 120\n"
6627 "#extension GL_ARB_texture_rectangle : enable\n"
6628 "uniform sampler2DRect sampler;\n"
6629 "void main(void)\n"
6630 "{\n"
6631 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6632 "}\n",
6635 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
6637 /* WINED3D_GL_RES_TYPE_TEX_1D */
6638 NULL,
6639 /* WINED3D_GL_RES_TYPE_TEX_2D */
6640 "#version 120\n"
6641 "uniform sampler2D sampler;\n"
6642 "uniform vec4 mask;\n"
6643 "void main(void)\n"
6644 "{\n"
6645 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6646 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6647 "}\n",
6648 /* WINED3D_GL_RES_TYPE_TEX_3D */
6649 NULL,
6650 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6651 "#version 120\n"
6652 "uniform samplerCube sampler;\n"
6653 "uniform vec4 mask;\n"
6654 "void main(void)\n"
6655 "{\n"
6656 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6657 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6658 "}\n",
6659 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6660 "#version 120\n"
6661 "#extension GL_ARB_texture_rectangle : enable\n"
6662 "uniform sampler2DRect sampler;\n"
6663 "uniform vec4 mask;\n"
6664 "void main(void)\n"
6665 "{\n"
6666 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6667 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6668 "}\n",
6671 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6672 if (!blt_pshader)
6674 FIXME("tex_type %#x not supported\n", tex_type);
6675 return 0;
6678 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6679 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6681 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6682 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6684 program_id = GL_EXTCALL(glCreateProgram());
6685 GL_EXTCALL(glAttachShader(program_id, vshader_id));
6686 GL_EXTCALL(glAttachShader(program_id, pshader_id));
6687 GL_EXTCALL(glLinkProgram(program_id));
6689 shader_glsl_validate_link(gl_info, program_id);
6691 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6692 * is destroyed
6694 GL_EXTCALL(glDeleteShader(vshader_id));
6695 GL_EXTCALL(glDeleteShader(pshader_id));
6696 return program_id;
6699 /* Context activation is done by the caller. */
6700 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6701 const struct wined3d_state *state)
6703 struct glsl_context_data *ctx_data = context->shader_backend_data;
6704 const struct wined3d_gl_info *gl_info = context->gl_info;
6705 struct shader_glsl_priv *priv = shader_priv;
6706 GLuint program_id = 0, prev_id = 0;
6707 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6709 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6710 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6712 if (ctx_data->glsl_program)
6714 prev_id = ctx_data->glsl_program->id;
6715 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6717 else
6719 prev_id = 0;
6720 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6723 set_glsl_shader_program(context, state, priv, ctx_data);
6725 if (ctx_data->glsl_program)
6727 program_id = ctx_data->glsl_program->id;
6728 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6730 else
6732 program_id = 0;
6733 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6736 if (old_vertex_color_clamp != current_vertex_color_clamp)
6738 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6740 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6741 checkGLcall("glClampColorARB");
6743 else
6745 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6749 TRACE("Using GLSL program %u.\n", program_id);
6751 if (prev_id != program_id)
6753 GL_EXTCALL(glUseProgram(program_id));
6754 checkGLcall("glUseProgram");
6756 if (program_id)
6757 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6761 /* "context" is not necessarily the currently active context. */
6762 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6764 struct glsl_context_data *ctx_data = context->shader_backend_data;
6766 ctx_data->glsl_program = NULL;
6767 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6768 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6769 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6772 /* Context activation is done by the caller. */
6773 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6775 const struct wined3d_gl_info *gl_info = context->gl_info;
6776 struct shader_glsl_priv *priv = shader_priv;
6778 shader_glsl_invalidate_current_program(context);
6779 GL_EXTCALL(glUseProgram(0));
6780 checkGLcall("glUseProgram");
6782 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6783 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6785 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6787 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6788 checkGLcall("glClampColorARB");
6792 /* Context activation is done by the caller. */
6793 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6794 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
6796 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6797 struct shader_glsl_priv *priv = shader_priv;
6798 GLuint *blt_program;
6799 GLint loc;
6801 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6802 if (!*blt_program)
6804 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6805 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
6806 GL_EXTCALL(glUseProgram(*blt_program));
6807 GL_EXTCALL(glUniform1i(loc, 0));
6809 else
6811 GL_EXTCALL(glUseProgram(*blt_program));
6814 if (masked)
6816 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
6817 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6821 /* Context activation is done by the caller. */
6822 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6824 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6825 GLuint program_id;
6827 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
6828 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6830 GL_EXTCALL(glUseProgram(program_id));
6831 checkGLcall("glUseProgram");
6834 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6835 const struct glsl_shader_prog_link *program)
6837 const struct glsl_context_data *ctx_data;
6838 struct wined3d_context *context;
6839 unsigned int i;
6841 for (i = 0; i < device->context_count; ++i)
6843 context = device->contexts[i];
6844 ctx_data = context->shader_backend_data;
6846 if (ctx_data->glsl_program == program)
6847 shader_glsl_invalidate_current_program(context);
6851 static void shader_glsl_destroy(struct wined3d_shader *shader)
6853 struct glsl_shader_private *shader_data = shader->backend_data;
6854 struct wined3d_device *device = shader->device;
6855 struct shader_glsl_priv *priv = device->shader_priv;
6856 const struct wined3d_gl_info *gl_info;
6857 const struct list *linked_programs;
6858 struct wined3d_context *context;
6860 if (!shader_data || !shader_data->num_gl_shaders)
6862 HeapFree(GetProcessHeap(), 0, shader_data);
6863 shader->backend_data = NULL;
6864 return;
6867 context = context_acquire(device, NULL);
6868 gl_info = context->gl_info;
6870 TRACE("Deleting linked programs.\n");
6871 linked_programs = &shader->linked_programs;
6872 if (linked_programs->next)
6874 struct glsl_shader_prog_link *entry, *entry2;
6875 UINT i;
6877 switch (shader->reg_maps.shader_version.type)
6879 case WINED3D_SHADER_TYPE_PIXEL:
6881 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6883 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6885 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
6886 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6887 checkGLcall("glDeleteShader");
6889 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6891 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6892 struct glsl_shader_prog_link, ps.shader_entry)
6894 shader_glsl_invalidate_contexts_program(device, entry);
6895 delete_glsl_program_entry(priv, gl_info, entry);
6898 break;
6901 case WINED3D_SHADER_TYPE_VERTEX:
6903 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6905 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6907 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
6908 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6909 checkGLcall("glDeleteShader");
6911 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6913 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6914 struct glsl_shader_prog_link, vs.shader_entry)
6916 shader_glsl_invalidate_contexts_program(device, entry);
6917 delete_glsl_program_entry(priv, gl_info, entry);
6920 break;
6923 case WINED3D_SHADER_TYPE_GEOMETRY:
6925 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6927 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6929 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6930 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
6931 checkGLcall("glDeleteShader");
6933 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6935 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6936 struct glsl_shader_prog_link, gs.shader_entry)
6938 shader_glsl_invalidate_contexts_program(device, entry);
6939 delete_glsl_program_entry(priv, gl_info, entry);
6942 break;
6945 default:
6946 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6947 break;
6951 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6952 shader->backend_data = NULL;
6954 context_release(context);
6957 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6959 const struct glsl_program_key *k = key;
6960 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6961 const struct glsl_shader_prog_link, program_lookup_entry);
6963 if (k->vs_id > prog->vs.id) return 1;
6964 else if (k->vs_id < prog->vs.id) return -1;
6966 if (k->gs_id > prog->gs.id) return 1;
6967 else if (k->gs_id < prog->gs.id) return -1;
6969 if (k->ps_id > prog->ps.id) return 1;
6970 else if (k->ps_id < prog->ps.id) return -1;
6972 return 0;
6975 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6977 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6978 + constant_count * sizeof(*heap->contained)
6979 + constant_count * sizeof(*heap->positions);
6980 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6982 if (!mem)
6984 ERR("Failed to allocate memory\n");
6985 return FALSE;
6988 heap->entries = mem;
6989 heap->entries[1].version = 0;
6990 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6991 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6992 heap->positions = (unsigned int *)(heap->contained + constant_count);
6993 heap->size = 1;
6995 return TRUE;
6998 static void constant_heap_free(struct constant_heap *heap)
7000 HeapFree(GetProcessHeap(), 0, heap->entries);
7003 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7005 wined3d_rb_alloc,
7006 wined3d_rb_realloc,
7007 wined3d_rb_free,
7008 glsl_program_key_compare,
7011 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7012 const struct fragment_pipeline *fragment_pipe)
7014 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7015 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7016 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7017 gl_info->limits.glsl_ps_float_constants)) + 1;
7018 struct fragment_caps fragment_caps;
7019 void *vertex_priv, *fragment_priv;
7021 string_buffer_list_init(&priv->string_buffers);
7023 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7025 ERR("Failed to initialize vertex pipe.\n");
7026 HeapFree(GetProcessHeap(), 0, priv);
7027 return E_FAIL;
7030 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7032 ERR("Failed to initialize fragment pipe.\n");
7033 vertex_pipe->vp_free(device);
7034 HeapFree(GetProcessHeap(), 0, priv);
7035 return E_FAIL;
7038 if (!string_buffer_init(&priv->shader_buffer))
7040 ERR("Failed to initialize shader buffer.\n");
7041 goto fail;
7044 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7045 if (!priv->stack)
7047 ERR("Failed to allocate memory.\n");
7048 goto fail;
7051 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7053 ERR("Failed to initialize vertex shader constant heap\n");
7054 goto fail;
7057 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7059 ERR("Failed to initialize pixel shader constant heap\n");
7060 goto fail;
7063 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7065 ERR("Failed to initialize rbtree.\n");
7066 goto fail;
7069 priv->next_constant_version = 1;
7070 priv->vertex_pipe = vertex_pipe;
7071 priv->fragment_pipe = fragment_pipe;
7072 fragment_pipe->get_caps(gl_info, &fragment_caps);
7073 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7075 device->vertex_priv = vertex_priv;
7076 device->fragment_priv = fragment_priv;
7077 device->shader_priv = priv;
7079 return WINED3D_OK;
7081 fail:
7082 constant_heap_free(&priv->pconst_heap);
7083 constant_heap_free(&priv->vconst_heap);
7084 HeapFree(GetProcessHeap(), 0, priv->stack);
7085 string_buffer_free(&priv->shader_buffer);
7086 fragment_pipe->free_private(device);
7087 vertex_pipe->vp_free(device);
7088 HeapFree(GetProcessHeap(), 0, priv);
7089 return E_OUTOFMEMORY;
7092 /* Context activation is done by the caller. */
7093 static void shader_glsl_free(struct wined3d_device *device)
7095 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7096 struct shader_glsl_priv *priv = device->shader_priv;
7097 int i;
7099 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7101 if (priv->depth_blt_program_full[i])
7103 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7105 if (priv->depth_blt_program_masked[i])
7107 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7111 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7112 constant_heap_free(&priv->pconst_heap);
7113 constant_heap_free(&priv->vconst_heap);
7114 HeapFree(GetProcessHeap(), 0, priv->stack);
7115 string_buffer_list_cleanup(&priv->string_buffers);
7116 string_buffer_free(&priv->shader_buffer);
7117 priv->fragment_pipe->free_private(device);
7118 priv->vertex_pipe->vp_free(device);
7120 HeapFree(GetProcessHeap(), 0, device->shader_priv);
7121 device->shader_priv = NULL;
7124 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
7126 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
7127 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
7130 static void shader_glsl_free_context_data(struct wined3d_context *context)
7132 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
7135 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
7137 UINT shader_model;
7139 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
7140 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
7141 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
7142 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
7143 shader_model = 4;
7144 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7145 * texldd and texldl instructions. */
7146 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
7147 shader_model = 3;
7148 else
7149 shader_model = 2;
7150 TRACE("Shader model %u.\n", shader_model);
7152 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
7153 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
7154 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
7156 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
7157 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
7159 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7160 * Direct3D minimum requirement.
7162 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7163 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7165 * The problem is that the refrast clamps temporary results in the shader to
7166 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7167 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7168 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7169 * offer a way to query this.
7171 if (shader_model >= 4)
7172 caps->ps_1x_max_value = FLT_MAX;
7173 else
7174 caps->ps_1x_max_value = 1024.0f;
7176 /* Ideally we'd only set caps like sRGB writes here if supported by both
7177 * the shader backend and the fragment pipe, but we can get called before
7178 * shader_glsl_alloc(). */
7179 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
7180 | WINED3D_SHADER_CAP_SRGB_WRITE;
7183 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
7185 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7187 TRACE("Checking support for fixup:\n");
7188 dump_color_fixup_desc(fixup);
7191 /* We support everything except YUV conversions. */
7192 if (!is_complex_fixup(fixup))
7194 TRACE("[OK]\n");
7195 return TRUE;
7198 TRACE("[FAILED]\n");
7199 return FALSE;
7202 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
7204 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
7205 /* WINED3DSIH_ADD */ shader_glsl_binop,
7206 /* WINED3DSIH_AND */ shader_glsl_binop,
7207 /* WINED3DSIH_BEM */ shader_glsl_bem,
7208 /* WINED3DSIH_BREAK */ shader_glsl_break,
7209 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
7210 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
7211 /* WINED3DSIH_CALL */ shader_glsl_call,
7212 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
7213 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
7214 /* WINED3DSIH_CND */ shader_glsl_cnd,
7215 /* WINED3DSIH_CRS */ shader_glsl_cross,
7216 /* WINED3DSIH_CUT */ shader_glsl_cut,
7217 /* WINED3DSIH_DCL */ shader_glsl_nop,
7218 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
7219 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
7220 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
7221 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
7222 /* WINED3DSIH_DEF */ shader_glsl_nop,
7223 /* WINED3DSIH_DEFB */ shader_glsl_nop,
7224 /* WINED3DSIH_DEFI */ shader_glsl_nop,
7225 /* WINED3DSIH_DIV */ shader_glsl_binop,
7226 /* WINED3DSIH_DP2 */ shader_glsl_dot,
7227 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
7228 /* WINED3DSIH_DP3 */ shader_glsl_dot,
7229 /* WINED3DSIH_DP4 */ shader_glsl_dot,
7230 /* WINED3DSIH_DST */ shader_glsl_dst,
7231 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
7232 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
7233 /* WINED3DSIH_ELSE */ shader_glsl_else,
7234 /* WINED3DSIH_EMIT */ shader_glsl_emit,
7235 /* WINED3DSIH_ENDIF */ shader_glsl_end,
7236 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
7237 /* WINED3DSIH_ENDREP */ shader_glsl_end,
7238 /* WINED3DSIH_EQ */ shader_glsl_relop,
7239 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
7240 /* WINED3DSIH_EXPP */ shader_glsl_expp,
7241 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
7242 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
7243 /* WINED3DSIH_GE */ shader_glsl_relop,
7244 /* WINED3DSIH_IADD */ shader_glsl_binop,
7245 /* WINED3DSIH_IEQ */ NULL,
7246 /* WINED3DSIH_IF */ shader_glsl_if,
7247 /* WINED3DSIH_IFC */ shader_glsl_ifc,
7248 /* WINED3DSIH_IGE */ shader_glsl_relop,
7249 /* WINED3DSIH_IMUL */ shader_glsl_imul,
7250 /* WINED3DSIH_ISHL */ shader_glsl_binop,
7251 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
7252 /* WINED3DSIH_LABEL */ shader_glsl_label,
7253 /* WINED3DSIH_LD */ NULL,
7254 /* WINED3DSIH_LIT */ shader_glsl_lit,
7255 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
7256 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
7257 /* WINED3DSIH_LOOP */ shader_glsl_loop,
7258 /* WINED3DSIH_LRP */ shader_glsl_lrp,
7259 /* WINED3DSIH_LT */ shader_glsl_relop,
7260 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
7261 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
7262 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
7263 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
7264 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
7265 /* WINED3DSIH_MAD */ shader_glsl_mad,
7266 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
7267 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
7268 /* WINED3DSIH_MOV */ shader_glsl_mov,
7269 /* WINED3DSIH_MOVA */ shader_glsl_mov,
7270 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
7271 /* WINED3DSIH_MUL */ shader_glsl_binop,
7272 /* WINED3DSIH_NE */ shader_glsl_relop,
7273 /* WINED3DSIH_NOP */ shader_glsl_nop,
7274 /* WINED3DSIH_NRM */ shader_glsl_nrm,
7275 /* WINED3DSIH_OR */ shader_glsl_binop,
7276 /* WINED3DSIH_PHASE */ shader_glsl_nop,
7277 /* WINED3DSIH_POW */ shader_glsl_pow,
7278 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
7279 /* WINED3DSIH_REP */ shader_glsl_rep,
7280 /* WINED3DSIH_RET */ shader_glsl_ret,
7281 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
7282 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
7283 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
7284 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
7285 /* WINED3DSIH_SAMPLE_LOD */ NULL,
7286 /* WINED3DSIH_SETP */ NULL,
7287 /* WINED3DSIH_SGE */ shader_glsl_compare,
7288 /* WINED3DSIH_SGN */ shader_glsl_sgn,
7289 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
7290 /* WINED3DSIH_SLT */ shader_glsl_compare,
7291 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
7292 /* WINED3DSIH_SUB */ shader_glsl_binop,
7293 /* WINED3DSIH_TEX */ shader_glsl_tex,
7294 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
7295 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
7296 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
7297 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
7298 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
7299 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
7300 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
7301 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
7302 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
7303 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
7304 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
7305 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
7306 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
7307 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
7308 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
7309 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
7310 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
7311 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
7312 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
7313 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
7314 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
7315 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
7316 /* WINED3DSIH_UGE */ shader_glsl_relop,
7317 /* WINED3DSIH_USHR */ shader_glsl_binop,
7318 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
7319 /* WINED3DSIH_XOR */ shader_glsl_binop,
7322 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
7323 SHADER_HANDLER hw_fct;
7325 /* Select handler */
7326 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
7328 /* Unhandled opcode */
7329 if (!hw_fct)
7331 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
7332 return;
7334 hw_fct(ins);
7336 shader_glsl_add_instruction_modifiers(ins);
7339 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
7341 struct shader_glsl_priv *priv = shader_priv;
7343 return priv->ffp_proj_control;
7346 const struct wined3d_shader_backend_ops glsl_shader_backend =
7348 shader_glsl_handle_instruction,
7349 shader_glsl_select,
7350 shader_glsl_disable,
7351 shader_glsl_select_depth_blt,
7352 shader_glsl_deselect_depth_blt,
7353 shader_glsl_update_float_vertex_constants,
7354 shader_glsl_update_float_pixel_constants,
7355 shader_glsl_load_constants,
7356 shader_glsl_destroy,
7357 shader_glsl_alloc,
7358 shader_glsl_free,
7359 shader_glsl_allocate_context_data,
7360 shader_glsl_free_context_data,
7361 shader_glsl_get_caps,
7362 shader_glsl_color_fixup_supported,
7363 shader_glsl_has_ffp_proj_control,
7366 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7368 if (enable)
7369 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7370 else
7371 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7372 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
7375 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
7377 caps->xyzrhw = TRUE;
7378 caps->max_active_lights = gl_info->limits.lights;
7379 caps->max_vertex_blend_matrices = 1;
7380 caps->max_vertex_blend_matrix_index = 0;
7381 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
7382 | WINED3DVTXPCAPS_MATERIALSOURCE7
7383 | WINED3DVTXPCAPS_VERTEXFOG
7384 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7385 | WINED3DVTXPCAPS_POSITIONALLIGHTS
7386 | WINED3DVTXPCAPS_LOCALVIEWER
7387 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
7388 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
7389 caps->max_user_clip_planes = gl_info->limits.clipplanes;
7390 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
7393 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7395 struct shader_glsl_priv *priv;
7397 if (shader_backend == &glsl_shader_backend)
7399 priv = shader_priv;
7401 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
7403 ERR("Failed to initialize rbtree.\n");
7404 return NULL;
7407 return priv;
7410 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
7412 return NULL;
7415 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
7417 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7418 struct glsl_ffp_vertex_shader, desc.entry);
7419 struct glsl_shader_prog_link *program, *program2;
7420 struct glsl_ffp_destroy_ctx *ctx = context;
7422 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7423 struct glsl_shader_prog_link, vs.shader_entry)
7425 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7427 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7428 HeapFree(GetProcessHeap(), 0, shader);
7431 /* Context activation is done by the caller. */
7432 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
7434 struct shader_glsl_priv *priv = device->vertex_priv;
7435 struct glsl_ffp_destroy_ctx ctx;
7437 ctx.priv = priv;
7438 ctx.gl_info = &device->adapter->gl_info;
7439 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
7442 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
7443 const struct wined3d_state *state, DWORD state_id)
7445 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7448 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
7449 const struct wined3d_state *state, DWORD state_id)
7451 const struct wined3d_gl_info *gl_info = context->gl_info;
7452 BOOL transformed = context->stream_info.position_transformed;
7453 BOOL wasrhw = context->last_was_rhw;
7454 unsigned int i;
7456 context->last_was_rhw = transformed;
7458 if (!use_vs(state))
7460 if (context->last_was_vshader)
7462 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7463 clipplane(context, state, STATE_CLIPPLANE(i));
7466 if (transformed != wasrhw)
7467 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7469 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7471 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_LIGHTING)))
7472 state_lighting(context, state, STATE_RENDER(WINED3D_RS_LIGHTING));
7473 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS)))
7474 state_normalize(context, state, STATE_RENDER(WINED3D_RS_NORMALIZENORMALS));
7476 /* Because of settings->texcoords, we have to always regenerate the
7477 * vertex shader on a vdecl change.
7478 * TODO: Just always output all the texcoords when there are enough
7479 * varyings available to drop the dependency. */
7480 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7482 if (use_ps(state)
7483 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
7484 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
7485 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7487 else
7489 if (!context->last_was_vshader)
7491 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
7492 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7493 clipplane(context, state, STATE_CLIPPLANE(i));
7497 context->last_was_vshader = use_vs(state);
7500 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
7501 const struct wined3d_state *state, DWORD state_id)
7503 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7504 /* Different vertex shaders potentially require a different vertex attributes setup. */
7505 if (!isStateDirty(context, STATE_VDECL))
7506 context_apply_state(context, state, STATE_VDECL);
7509 static void glsl_vertex_pipe_world(struct wined3d_context *context,
7510 const struct wined3d_state *state, DWORD state_id)
7512 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7515 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
7517 const struct wined3d_gl_info *gl_info = context->gl_info;
7518 const struct wined3d_light_info *light = NULL;
7519 unsigned int k;
7521 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7523 /* Light settings are affected by the ModelView transform in OpenGL, the View transform in Direct3D. */
7524 gl_info->gl_ops.gl.p_glMatrixMode(GL_MODELVIEW);
7525 gl_info->gl_ops.gl.p_glPushMatrix();
7526 gl_info->gl_ops.gl.p_glLoadMatrixf(&state->transforms[WINED3D_TS_VIEW]._11);
7528 for (k = 0; k < gl_info->limits.lights; ++k)
7530 if (!(light = state->lights[k]))
7531 continue;
7532 if (light->OriginalParms.type == WINED3D_LIGHT_DIRECTIONAL)
7533 gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_POSITION, light->lightDirn);
7534 else
7535 gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_POSITION, light->lightPosn);
7536 checkGLcall("glLightfv posn");
7537 gl_info->gl_ops.gl.p_glLightfv(GL_LIGHT0 + light->glIndex, GL_SPOT_DIRECTION, light->lightDirn);
7538 checkGLcall("glLightfv dirn");
7541 gl_info->gl_ops.gl.p_glPopMatrix();
7543 for (k = 0; k < gl_info->limits.clipplanes; ++k)
7545 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
7546 clipplane(context, state, STATE_CLIPPLANE(k));
7549 if (context->swapchain->device->vertexBlendUsed)
7551 static int warned;
7553 if (!warned++)
7554 FIXME("Vertex blending emulation.\n");
7558 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
7559 const struct wined3d_state *state, DWORD state_id)
7561 /* Table fog behavior depends on the projection matrix. */
7562 if (state->render_states[WINED3D_RS_FOGENABLE]
7563 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
7564 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7565 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7568 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
7569 const struct wined3d_state *state, DWORD state_id)
7571 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
7572 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
7573 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
7574 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
7575 state_pscale(context, state, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE));
7576 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7579 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
7580 const struct wined3d_state *state, DWORD state_id)
7582 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7585 static void glsl_vertex_pipe_material(struct wined3d_context *context,
7586 const struct wined3d_state *state, DWORD state_id)
7588 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7591 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
7593 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7594 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
7595 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
7596 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
7597 /* Clip planes */
7598 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
7599 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
7600 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
7601 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
7602 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
7603 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
7604 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
7605 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
7606 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
7607 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
7608 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
7609 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
7610 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
7611 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
7612 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
7613 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
7614 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
7615 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
7616 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
7617 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
7618 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
7619 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
7620 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
7621 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
7622 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
7623 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
7624 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
7625 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
7626 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
7627 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
7628 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
7629 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
7630 /* Lights */
7631 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7632 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
7633 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
7634 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
7635 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
7636 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
7637 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
7638 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
7639 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
7640 /* Viewport */
7641 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
7642 /* Transform states */
7643 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
7644 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
7645 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7646 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7647 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7648 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7649 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7650 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7651 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7652 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7653 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
7654 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7655 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7656 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7657 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7658 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7659 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7660 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7661 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7662 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7663 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7664 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7665 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7666 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7667 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7668 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7669 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7670 /* Fog */
7671 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7672 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7673 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7674 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7675 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7676 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7677 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7678 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
7679 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7680 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7681 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7682 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7683 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7684 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7685 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7686 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7687 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7688 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7689 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7690 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7691 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7692 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7693 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7694 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7695 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7696 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7697 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7698 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7699 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7700 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7701 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7702 /* NP2 texture matrix fixups. They are not needed if
7703 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
7704 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
7705 * matrix. */
7706 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7707 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7708 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7709 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7710 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7711 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7712 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7713 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7714 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7715 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7716 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7717 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7718 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7719 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7720 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7721 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7722 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7723 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7724 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7725 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7726 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7727 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7728 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7729 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7730 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7731 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7734 /* TODO:
7735 * - This currently depends on GL fixed function functions to set things
7736 * like light parameters. Ideally we'd use regular uniforms for that.
7737 * - In part because of the previous point, much of this is modelled after
7738 * GL fixed function, and has much of the same limitations. For example,
7739 * D3D spot lights are slightly different from GL spot lights.
7740 * - We can now implement drawing transformed vertices using the GLSL pipe,
7741 * instead of using the immediate mode fallback.
7742 * - Similarly, we don't need the fallback for certain combinations of
7743 * material sources anymore.
7744 * - Implement vertex blending and vertex tweening.
7745 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7746 * attribute arrays in load_tex_coords().
7747 * - Per-vertex point sizes. */
7748 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7750 glsl_vertex_pipe_vp_enable,
7751 glsl_vertex_pipe_vp_get_caps,
7752 glsl_vertex_pipe_vp_alloc,
7753 glsl_vertex_pipe_vp_free,
7754 glsl_vertex_pipe_vp_states,
7757 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7759 /* Nothing to do. */
7762 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7764 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7765 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
7766 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
7767 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7768 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7769 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7770 | WINED3DTEXOPCAPS_SELECTARG1
7771 | WINED3DTEXOPCAPS_SELECTARG2
7772 | WINED3DTEXOPCAPS_MODULATE4X
7773 | WINED3DTEXOPCAPS_MODULATE2X
7774 | WINED3DTEXOPCAPS_MODULATE
7775 | WINED3DTEXOPCAPS_ADDSIGNED2X
7776 | WINED3DTEXOPCAPS_ADDSIGNED
7777 | WINED3DTEXOPCAPS_ADD
7778 | WINED3DTEXOPCAPS_SUBTRACT
7779 | WINED3DTEXOPCAPS_ADDSMOOTH
7780 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7781 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7782 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7783 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7784 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7785 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7786 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7787 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7788 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7789 | WINED3DTEXOPCAPS_DOTPRODUCT3
7790 | WINED3DTEXOPCAPS_MULTIPLYADD
7791 | WINED3DTEXOPCAPS_LERP
7792 | WINED3DTEXOPCAPS_BUMPENVMAP
7793 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7794 caps->MaxTextureBlendStages = 8;
7795 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7798 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7800 struct shader_glsl_priv *priv;
7802 if (shader_backend == &glsl_shader_backend)
7804 priv = shader_priv;
7806 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7808 ERR("Failed to initialize rbtree.\n");
7809 return NULL;
7812 return priv;
7815 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7817 return NULL;
7820 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7822 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7823 struct glsl_ffp_fragment_shader, entry.entry);
7824 struct glsl_shader_prog_link *program, *program2;
7825 struct glsl_ffp_destroy_ctx *ctx = context;
7827 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7828 struct glsl_shader_prog_link, ps.shader_entry)
7830 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7832 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7833 HeapFree(GetProcessHeap(), 0, shader);
7836 /* Context activation is done by the caller. */
7837 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7839 struct shader_glsl_priv *priv = device->fragment_priv;
7840 struct glsl_ffp_destroy_ctx ctx;
7842 ctx.priv = priv;
7843 ctx.gl_info = &device->adapter->gl_info;
7844 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7847 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7848 const struct wined3d_state *state, DWORD state_id)
7850 context->last_was_pshader = use_ps(state);
7852 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7855 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7856 const struct wined3d_state *state, DWORD state_id)
7858 BOOL use_vshader = use_vs(state);
7859 enum fogsource new_source;
7860 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7861 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7863 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7865 if (!state->render_states[WINED3D_RS_FOGENABLE])
7866 return;
7868 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7870 if (use_vshader)
7871 new_source = FOGSOURCE_VS;
7872 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
7873 new_source = FOGSOURCE_COORD;
7874 else
7875 new_source = FOGSOURCE_FFP;
7877 else
7879 new_source = FOGSOURCE_FFP;
7882 if (new_source != context->fog_source || fogstart == fogend)
7884 context->fog_source = new_source;
7885 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7889 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
7890 const struct wined3d_state *state, DWORD state_id)
7892 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
7893 glsl_fragment_pipe_fog(context, state, state_id);
7896 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7897 const struct wined3d_state *state, DWORD state_id)
7899 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7902 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7903 const struct wined3d_state *state, DWORD state_id)
7905 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7908 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
7909 const struct wined3d_state *state, DWORD state_id)
7911 const struct wined3d_gl_info *gl_info = context->gl_info;
7912 int glParm;
7913 float ref;
7915 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
7917 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
7919 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
7920 checkGLcall("glEnable GL_ALPHA_TEST");
7922 else
7924 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
7925 checkGLcall("glDisable GL_ALPHA_TEST");
7926 return;
7929 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
7930 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
7932 if (glParm)
7934 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
7935 checkGLcall("glAlphaFunc");
7939 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
7940 const struct wined3d_state *state, DWORD state_id)
7942 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7945 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7947 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7948 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7949 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7950 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7951 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7952 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7953 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7954 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7955 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7956 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7957 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7958 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7959 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7960 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7961 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7962 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7963 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7964 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7965 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7966 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7967 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7968 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7969 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7970 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7971 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7972 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7973 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7974 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7975 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7976 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7977 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7978 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7979 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7980 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7981 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7982 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7983 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7984 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7985 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7986 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7987 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7988 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7989 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7990 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7991 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7992 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7993 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7994 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7995 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7996 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7997 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7998 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7999 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8000 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8001 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8002 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8003 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8004 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8005 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8006 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8007 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8008 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8009 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8010 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8011 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8012 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8013 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8014 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8015 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8016 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8017 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8018 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8019 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8020 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8021 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8022 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8023 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8024 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
8025 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8026 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
8027 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
8028 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8029 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8030 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
8031 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
8032 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
8033 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8034 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
8035 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
8036 {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 },
8037 {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 },
8038 {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 },
8039 {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 },
8040 {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 },
8041 {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 },
8042 {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 },
8043 {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 },
8044 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8045 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8046 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8047 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8048 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8049 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8050 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8051 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8052 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8053 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
8056 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
8058 return TRUE;
8061 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
8065 const struct fragment_pipeline glsl_fragment_pipe =
8067 glsl_fragment_pipe_enable,
8068 glsl_fragment_pipe_get_caps,
8069 glsl_fragment_pipe_alloc,
8070 glsl_fragment_pipe_free,
8071 glsl_fragment_pipe_alloc_context_data,
8072 glsl_fragment_pipe_free_context_data,
8073 shader_glsl_color_fixup_supported,
8074 glsl_fragment_pipe_state_template,