wined3d: Recognize SM 4.1 ld2dms opcode.
[wine.git] / dlls / wined3d / glsl_shader.c
blob79eec10fc549101a3b2d793393caa46fbc86f27c
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 0x01
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x02
49 #define WINED3D_GLSL_SAMPLE_LOD 0x04
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x08
51 #define WINED3D_GLSL_SAMPLE_LOAD 0x10
52 #define WINED3D_GLSL_SAMPLE_OFFSET 0x20
54 struct glsl_dst_param
56 char reg_name[150];
57 char mask_str[6];
60 struct glsl_src_param
62 char reg_name[150];
63 char param_str[200];
66 struct glsl_sample_function
68 struct wined3d_string_buffer *name;
69 DWORD coord_mask;
70 enum wined3d_data_type data_type;
71 BOOL output_single_component;
72 unsigned int offset_size;
75 enum heap_node_op
77 HEAP_NODE_TRAVERSE_LEFT,
78 HEAP_NODE_TRAVERSE_RIGHT,
79 HEAP_NODE_POP,
82 struct constant_entry
84 unsigned int idx;
85 unsigned int version;
88 struct constant_heap
90 struct constant_entry *entries;
91 BOOL *contained;
92 unsigned int *positions;
93 unsigned int size;
96 /* GLSL shader private data */
97 struct shader_glsl_priv {
98 struct wined3d_string_buffer shader_buffer;
99 struct wined3d_string_buffer_list string_buffers;
100 struct wine_rb_tree program_lookup;
101 struct constant_heap vconst_heap;
102 struct constant_heap pconst_heap;
103 unsigned char *stack;
104 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
105 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
106 UINT next_constant_version;
108 const struct wined3d_vertex_pipe_ops *vertex_pipe;
109 const struct fragment_pipeline *fragment_pipe;
110 struct wine_rb_tree ffp_vertex_shaders;
111 struct wine_rb_tree ffp_fragment_shaders;
112 BOOL ffp_proj_control;
113 BOOL legacy_lighting;
116 struct glsl_vs_program
118 struct list shader_entry;
119 GLuint id;
120 GLenum vertex_color_clamp;
121 GLint *uniform_f_locations;
122 GLint uniform_i_locations[MAX_CONST_I];
123 GLint uniform_b_locations[MAX_CONST_B];
124 GLint pos_fixup_location;
126 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
127 GLint projection_matrix_location;
128 GLint normal_matrix_location;
129 GLint texture_matrix_location[MAX_TEXTURES];
130 GLint material_ambient_location;
131 GLint material_diffuse_location;
132 GLint material_specular_location;
133 GLint material_emissive_location;
134 GLint material_shininess_location;
135 GLint light_ambient_location;
136 struct
138 GLint diffuse;
139 GLint specular;
140 GLint ambient;
141 GLint position;
142 GLint direction;
143 GLint range;
144 GLint falloff;
145 GLint c_att;
146 GLint l_att;
147 GLint q_att;
148 GLint cos_htheta;
149 GLint cos_hphi;
150 } light_location[MAX_ACTIVE_LIGHTS];
151 GLint pointsize_location;
152 GLint pointsize_min_location;
153 GLint pointsize_max_location;
154 GLint pointsize_c_att_location;
155 GLint pointsize_l_att_location;
156 GLint pointsize_q_att_location;
159 struct glsl_gs_program
161 struct list shader_entry;
162 GLuint id;
165 struct glsl_ps_program
167 struct list shader_entry;
168 GLuint id;
169 GLint *uniform_f_locations;
170 GLint uniform_i_locations[MAX_CONST_I];
171 GLint uniform_b_locations[MAX_CONST_B];
172 GLint bumpenv_mat_location[MAX_TEXTURES];
173 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
174 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
175 GLint tss_constant_location[MAX_TEXTURES];
176 GLint tex_factor_location;
177 GLint specular_enable_location;
178 GLint fog_color_location;
179 GLint fog_density_location;
180 GLint fog_end_location;
181 GLint fog_scale_location;
182 GLint ycorrection_location;
183 GLint np2_fixup_location;
184 GLint color_key_location;
185 const struct ps_np2fixup_info *np2_fixup_info;
188 /* Struct to maintain data about a linked GLSL program */
189 struct glsl_shader_prog_link
191 struct wine_rb_entry program_lookup_entry;
192 struct glsl_vs_program vs;
193 struct glsl_gs_program gs;
194 struct glsl_ps_program ps;
195 GLuint id;
196 DWORD constant_update_mask;
197 UINT constant_version;
200 struct glsl_program_key
202 GLuint vs_id;
203 GLuint gs_id;
204 GLuint ps_id;
207 struct shader_glsl_ctx_priv {
208 const struct vs_compile_args *cur_vs_args;
209 const struct ps_compile_args *cur_ps_args;
210 struct ps_np2fixup_info *cur_np2fixup_info;
211 struct wined3d_string_buffer_list *string_buffers;
214 struct glsl_context_data
216 struct glsl_shader_prog_link *glsl_program;
219 struct glsl_ps_compiled_shader
221 struct ps_compile_args args;
222 struct ps_np2fixup_info np2fixup;
223 GLuint id;
226 struct glsl_vs_compiled_shader
228 struct vs_compile_args args;
229 GLuint id;
232 struct glsl_gs_compiled_shader
234 GLuint id;
237 struct glsl_shader_private
239 union
241 struct glsl_vs_compiled_shader *vs;
242 struct glsl_gs_compiled_shader *gs;
243 struct glsl_ps_compiled_shader *ps;
244 } gl_shaders;
245 UINT num_gl_shaders, shader_array_size;
248 struct glsl_ffp_vertex_shader
250 struct wined3d_ffp_vs_desc desc;
251 GLuint id;
252 struct list linked_programs;
255 struct glsl_ffp_fragment_shader
257 struct ffp_frag_desc entry;
258 GLuint id;
259 struct list linked_programs;
262 struct glsl_ffp_destroy_ctx
264 struct shader_glsl_priv *priv;
265 const struct wined3d_gl_info *gl_info;
268 static const char *debug_gl_shader_type(GLenum type)
270 switch (type)
272 #define WINED3D_TO_STR(u) case u: return #u
273 WINED3D_TO_STR(GL_VERTEX_SHADER);
274 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
275 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
276 #undef WINED3D_TO_STR
277 default:
278 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
282 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
284 switch (type)
286 case WINED3D_SHADER_TYPE_VERTEX:
287 return "vs";
289 case WINED3D_SHADER_TYPE_GEOMETRY:
290 return "gs";
292 case WINED3D_SHADER_TYPE_PIXEL:
293 return "ps";
295 default:
296 FIXME("Unhandled shader type %#x.\n", type);
297 return "unknown";
301 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
302 const struct wined3d_shader_version *version)
304 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
305 return "#version 150";
306 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
307 return "#version 130";
308 else
309 return "#version 120";
312 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
314 char str[4][17];
316 wined3d_ftoa(values[0], str[0]);
317 wined3d_ftoa(values[1], str[1]);
318 wined3d_ftoa(values[2], str[2]);
319 wined3d_ftoa(values[3], str[3]);
320 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
323 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
324 const int *values, unsigned int size)
326 int i;
328 if (!size || size > 4)
330 ERR("Invalid vector size %u.\n", size);
331 return;
334 if (size > 1)
335 shader_addline(buffer, "ivec%u(", size);
337 for (i = 0; i < size; ++i)
338 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
340 if (size > 1)
341 shader_addline(buffer, ")");
344 static const char *get_info_log_line(const char **ptr)
346 const char *p, *q;
348 p = *ptr;
349 if (!(q = strstr(p, "\n")))
351 if (!*p) return NULL;
352 *ptr += strlen(p);
353 return p;
355 *ptr = q + 1;
357 return p;
360 /* Context activation is done by the caller. */
361 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
363 int length = 0;
364 char *log;
366 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
367 return;
369 if (program)
370 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
371 else
372 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
374 /* A size of 1 is just a null-terminated string, so the log should be bigger than
375 * that if there are errors. */
376 if (length > 1)
378 const char *ptr, *line;
380 log = HeapAlloc(GetProcessHeap(), 0, length);
381 /* The info log is supposed to be zero-terminated, but at least some
382 * versions of fglrx don't terminate the string properly. The reported
383 * length does include the terminator, so explicitly set it to zero
384 * here. */
385 log[length - 1] = 0;
386 if (program)
387 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
388 else
389 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
391 ptr = log;
392 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
394 WARN("Info log received from GLSL shader #%u:\n", id);
395 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
397 else
399 FIXME("Info log received from GLSL shader #%u:\n", id);
400 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
402 HeapFree(GetProcessHeap(), 0, log);
406 /* Context activation is done by the caller. */
407 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
409 const char *ptr, *line;
411 TRACE("Compiling shader object %u.\n", shader);
413 if (TRACE_ON(d3d_shader))
415 ptr = src;
416 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
419 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
420 checkGLcall("glShaderSource");
421 GL_EXTCALL(glCompileShader(shader));
422 checkGLcall("glCompileShader");
423 print_glsl_info_log(gl_info, shader, FALSE);
426 /* Context activation is done by the caller. */
427 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
429 GLint i, shader_count, source_size = -1;
430 GLuint *shaders;
431 char *source = NULL;
433 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
434 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
435 if (!shaders)
437 ERR("Failed to allocate shader array memory.\n");
438 return;
441 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
442 for (i = 0; i < shader_count; ++i)
444 const char *ptr, *line;
445 GLint tmp;
447 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
449 if (source_size < tmp)
451 HeapFree(GetProcessHeap(), 0, source);
453 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
454 if (!source)
456 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
457 HeapFree(GetProcessHeap(), 0, shaders);
458 return;
460 source_size = tmp;
463 FIXME("Shader %u:\n", shaders[i]);
464 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
465 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
466 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
467 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
468 FIXME("\n");
470 ptr = source;
471 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
472 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
473 FIXME("\n");
476 HeapFree(GetProcessHeap(), 0, source);
477 HeapFree(GetProcessHeap(), 0, shaders);
480 /* Context activation is done by the caller. */
481 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
483 GLint tmp;
485 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
486 return;
488 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
489 if (!tmp)
491 FIXME("Program %u link status invalid.\n", program);
492 shader_glsl_dump_program_source(gl_info, program);
495 print_glsl_info_log(gl_info, program, TRUE);
498 /* Context activation is done by the caller. */
499 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
500 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
502 unsigned int mapped_unit;
503 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
504 const char *prefix;
505 unsigned int i, j;
506 GLint name_loc;
508 static const struct
510 enum wined3d_shader_type type;
511 unsigned int base_idx;
512 unsigned int count;
514 sampler_info[] =
516 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
517 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
520 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
522 prefix = shader_glsl_get_prefix(sampler_info[i].type);
524 for (j = 0; j < sampler_info[i].count; ++j)
526 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
527 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
528 if (name_loc == -1)
529 continue;
531 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
532 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
534 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
535 continue;
538 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
539 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
542 checkGLcall("glUniform1i");
543 string_buffer_release(&priv->string_buffers, sampler_name);
546 /* Context activation is done by the caller. */
547 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
548 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
550 unsigned int start = ~0U, end = 0;
551 int stack_idx = 0;
552 unsigned int heap_idx = 1;
553 unsigned int idx;
555 if (heap->entries[heap_idx].version <= version) return;
557 idx = heap->entries[heap_idx].idx;
558 if (constant_locations[idx] != -1)
559 start = end = idx;
560 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
562 while (stack_idx >= 0)
564 /* Note that we fall through to the next case statement. */
565 switch(stack[stack_idx])
567 case HEAP_NODE_TRAVERSE_LEFT:
569 unsigned int left_idx = heap_idx << 1;
570 if (left_idx < heap->size && heap->entries[left_idx].version > version)
572 heap_idx = left_idx;
573 idx = heap->entries[heap_idx].idx;
574 if (constant_locations[idx] != -1)
576 if (start > idx)
577 start = idx;
578 if (end < idx)
579 end = idx;
582 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
583 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
584 break;
588 case HEAP_NODE_TRAVERSE_RIGHT:
590 unsigned int right_idx = (heap_idx << 1) + 1;
591 if (right_idx < heap->size && heap->entries[right_idx].version > version)
593 heap_idx = right_idx;
594 idx = heap->entries[heap_idx].idx;
595 if (constant_locations[idx] != -1)
597 if (start > idx)
598 start = idx;
599 if (end < idx)
600 end = idx;
603 stack[stack_idx++] = HEAP_NODE_POP;
604 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
605 break;
609 case HEAP_NODE_POP:
610 heap_idx >>= 1;
611 --stack_idx;
612 break;
615 if (start <= end)
616 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
617 checkGLcall("walk_constant_heap()");
620 /* Context activation is done by the caller. */
621 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
623 GLfloat clamped_constant[4];
625 if (location == -1) return;
627 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
628 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
629 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
630 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
632 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
635 /* Context activation is done by the caller. */
636 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
637 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
639 int stack_idx = 0;
640 unsigned int heap_idx = 1;
641 unsigned int idx;
643 if (heap->entries[heap_idx].version <= version) return;
645 idx = heap->entries[heap_idx].idx;
646 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
647 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
649 while (stack_idx >= 0)
651 /* Note that we fall through to the next case statement. */
652 switch(stack[stack_idx])
654 case HEAP_NODE_TRAVERSE_LEFT:
656 unsigned int left_idx = heap_idx << 1;
657 if (left_idx < heap->size && heap->entries[left_idx].version > version)
659 heap_idx = left_idx;
660 idx = heap->entries[heap_idx].idx;
661 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
663 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
664 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
665 break;
669 case HEAP_NODE_TRAVERSE_RIGHT:
671 unsigned int right_idx = (heap_idx << 1) + 1;
672 if (right_idx < heap->size && heap->entries[right_idx].version > version)
674 heap_idx = right_idx;
675 idx = heap->entries[heap_idx].idx;
676 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
678 stack[stack_idx++] = HEAP_NODE_POP;
679 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
680 break;
684 case HEAP_NODE_POP:
685 heap_idx >>= 1;
686 --stack_idx;
687 break;
690 checkGLcall("walk_constant_heap_clamped()");
693 /* Context activation is done by the caller. */
694 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
695 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
696 unsigned char *stack, UINT version)
698 const struct wined3d_shader_lconst *lconst;
700 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
701 if (shader->reg_maps.shader_version.major == 1
702 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
703 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
704 else
705 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
707 if (!shader->load_local_constsF)
709 TRACE("No need to load local float constants for this shader\n");
710 return;
713 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
714 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
716 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
718 checkGLcall("glUniform4fv()");
721 /* Context activation is done by the caller. */
722 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
723 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
725 unsigned int i;
726 struct list* ptr;
728 for (i = 0; constants_set; constants_set >>= 1, ++i)
730 if (!(constants_set & 1)) continue;
732 /* We found this uniform name in the program - go ahead and send the data */
733 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
736 /* Load immediate constants */
737 ptr = list_head(&shader->constantsI);
738 while (ptr)
740 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
741 unsigned int idx = lconst->idx;
742 const GLint *values = (const GLint *)lconst->value;
744 /* We found this uniform name in the program - go ahead and send the data */
745 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
746 ptr = list_next(&shader->constantsI, ptr);
748 checkGLcall("glUniform4iv()");
751 /* Context activation is done by the caller. */
752 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
753 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
755 unsigned int i;
756 struct list* ptr;
758 for (i = 0; constants_set; constants_set >>= 1, ++i)
760 if (!(constants_set & 1)) continue;
762 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
765 /* Load immediate constants */
766 ptr = list_head(&shader->constantsB);
767 while (ptr)
769 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
770 unsigned int idx = lconst->idx;
771 const GLint *values = (const GLint *)lconst->value;
773 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
774 ptr = list_next(&shader->constantsB, ptr);
776 checkGLcall("glUniform1iv()");
779 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
781 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
784 /* Context activation is done by the caller (state handler). */
785 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
786 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
788 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
789 UINT fixup = ps->np2_fixup_info->active;
790 UINT i;
792 for (i = 0; fixup; fixup >>= 1, ++i)
794 const struct wined3d_texture *tex = state->textures[i];
795 unsigned char idx = ps->np2_fixup_info->idx[i];
796 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
798 if (!tex)
800 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
801 continue;
804 if (idx % 2)
806 tex_dim[2] = tex->pow2_matrix[0];
807 tex_dim[3] = tex->pow2_matrix[5];
809 else
811 tex_dim[0] = tex->pow2_matrix[0];
812 tex_dim[1] = tex->pow2_matrix[5];
816 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
819 /* Taken and adapted from Mesa. */
820 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
822 float pos, neg, t, det;
823 struct wined3d_matrix temp;
825 /* Calculate the determinant of upper left 3x3 submatrix and
826 * determine if the matrix is singular. */
827 pos = neg = 0.0f;
828 t = in->_11 * in->_22 * in->_33;
829 if (t >= 0.0f)
830 pos += t;
831 else
832 neg += t;
834 t = in->_21 * in->_32 * in->_13;
835 if (t >= 0.0f)
836 pos += t;
837 else
838 neg += t;
839 t = in->_31 * in->_12 * in->_23;
840 if (t >= 0.0f)
841 pos += t;
842 else
843 neg += t;
845 t = -in->_31 * in->_22 * in->_13;
846 if (t >= 0.0f)
847 pos += t;
848 else
849 neg += t;
850 t = -in->_21 * in->_12 * in->_33;
851 if (t >= 0.0f)
852 pos += t;
853 else
854 neg += t;
856 t = -in->_11 * in->_32 * in->_23;
857 if (t >= 0.0f)
858 pos += t;
859 else
860 neg += t;
862 det = pos + neg;
864 if (fabsf(det) < 1e-25f)
865 return FALSE;
867 det = 1.0f / det;
868 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
869 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
870 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
871 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
872 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
873 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
874 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
875 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
876 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
878 *out = temp;
879 return TRUE;
882 static void swap_rows(float **a, float **b)
884 float *tmp = *a;
886 *a = *b;
887 *b = tmp;
890 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
892 float wtmp[4][8];
893 float m0, m1, m2, m3, s;
894 float *r0, *r1, *r2, *r3;
896 r0 = wtmp[0];
897 r1 = wtmp[1];
898 r2 = wtmp[2];
899 r3 = wtmp[3];
901 r0[0] = m->_11;
902 r0[1] = m->_12;
903 r0[2] = m->_13;
904 r0[3] = m->_14;
905 r0[4] = 1.0f;
906 r0[5] = r0[6] = r0[7] = 0.0f;
908 r1[0] = m->_21;
909 r1[1] = m->_22;
910 r1[2] = m->_23;
911 r1[3] = m->_24;
912 r1[5] = 1.0f;
913 r1[4] = r1[6] = r1[7] = 0.0f;
915 r2[0] = m->_31;
916 r2[1] = m->_32;
917 r2[2] = m->_33;
918 r2[3] = m->_34;
919 r2[6] = 1.0f;
920 r2[4] = r2[5] = r2[7] = 0.0f;
922 r3[0] = m->_41;
923 r3[1] = m->_42;
924 r3[2] = m->_43;
925 r3[3] = m->_44;
926 r3[7] = 1.0f;
927 r3[4] = r3[5] = r3[6] = 0.0f;
929 /* Choose pivot - or die. */
930 if (fabsf(r3[0]) > fabsf(r2[0]))
931 swap_rows(&r3, &r2);
932 if (fabsf(r2[0]) > fabsf(r1[0]))
933 swap_rows(&r2, &r1);
934 if (fabsf(r1[0]) > fabsf(r0[0]))
935 swap_rows(&r1, &r0);
936 if (r0[0] == 0.0f)
937 return FALSE;
939 /* Eliminate first variable. */
940 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
941 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
942 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
943 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
944 s = r0[4];
945 if (s != 0.0f)
947 r1[4] -= m1 * s;
948 r2[4] -= m2 * s;
949 r3[4] -= m3 * s;
951 s = r0[5];
952 if (s != 0.0f)
954 r1[5] -= m1 * s;
955 r2[5] -= m2 * s;
956 r3[5] -= m3 * s;
958 s = r0[6];
959 if (s != 0.0f)
961 r1[6] -= m1 * s;
962 r2[6] -= m2 * s;
963 r3[6] -= m3 * s;
965 s = r0[7];
966 if (s != 0.0f)
968 r1[7] -= m1 * s;
969 r2[7] -= m2 * s;
970 r3[7] -= m3 * s;
973 /* Choose pivot - or die. */
974 if (fabsf(r3[1]) > fabsf(r2[1]))
975 swap_rows(&r3, &r2);
976 if (fabsf(r2[1]) > fabsf(r1[1]))
977 swap_rows(&r2, &r1);
978 if (r1[1] == 0.0f)
979 return FALSE;
981 /* Eliminate second variable. */
982 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
983 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
984 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
985 s = r1[4];
986 if (s != 0.0f)
988 r2[4] -= m2 * s;
989 r3[4] -= m3 * s;
991 s = r1[5];
992 if (s != 0.0f)
994 r2[5] -= m2 * s;
995 r3[5] -= m3 * s;
997 s = r1[6];
998 if (s != 0.0f)
1000 r2[6] -= m2 * s;
1001 r3[6] -= m3 * s;
1003 s = r1[7];
1004 if (s != 0.0f)
1006 r2[7] -= m2 * s;
1007 r3[7] -= m3 * s;
1010 /* Choose pivot - or die. */
1011 if (fabsf(r3[2]) > fabsf(r2[2]))
1012 swap_rows(&r3, &r2);
1013 if (r2[2] == 0.0f)
1014 return FALSE;
1016 /* Eliminate third variable. */
1017 m3 = r3[2] / r2[2];
1018 r3[3] -= m3 * r2[3];
1019 r3[4] -= m3 * r2[4];
1020 r3[5] -= m3 * r2[5];
1021 r3[6] -= m3 * r2[6];
1022 r3[7] -= m3 * r2[7];
1024 /* Last check. */
1025 if (r3[3] == 0.0f)
1026 return FALSE;
1028 /* Back substitute row 3. */
1029 s = 1.0f / r3[3];
1030 r3[4] *= s;
1031 r3[5] *= s;
1032 r3[6] *= s;
1033 r3[7] *= s;
1035 /* Back substitute row 2. */
1036 m2 = r2[3];
1037 s = 1.0f / r2[2];
1038 r2[4] = s * (r2[4] - r3[4] * m2);
1039 r2[5] = s * (r2[5] - r3[5] * m2);
1040 r2[6] = s * (r2[6] - r3[6] * m2);
1041 r2[7] = s * (r2[7] - r3[7] * m2);
1042 m1 = r1[3];
1043 r1[4] -= r3[4] * m1;
1044 r1[5] -= r3[5] * m1;
1045 r1[6] -= r3[6] * m1;
1046 r1[7] -= r3[7] * m1;
1047 m0 = r0[3];
1048 r0[4] -= r3[4] * m0;
1049 r0[5] -= r3[5] * m0;
1050 r0[6] -= r3[6] * m0;
1051 r0[7] -= r3[7] * m0;
1053 /* Back substitute row 1. */
1054 m1 = r1[2];
1055 s = 1.0f / r1[1];
1056 r1[4] = s * (r1[4] - r2[4] * m1);
1057 r1[5] = s * (r1[5] - r2[5] * m1);
1058 r1[6] = s * (r1[6] - r2[6] * m1);
1059 r1[7] = s * (r1[7] - r2[7] * m1);
1060 m0 = r0[2];
1061 r0[4] -= r2[4] * m0;
1062 r0[5] -= r2[5] * m0;
1063 r0[6] -= r2[6] * m0;
1064 r0[7] -= r2[7] * m0;
1066 /* Back substitute row 0. */
1067 m0 = r0[1];
1068 s = 1.0f / r0[0];
1069 r0[4] = s * (r0[4] - r1[4] * m0);
1070 r0[5] = s * (r0[5] - r1[5] * m0);
1071 r0[6] = s * (r0[6] - r1[6] * m0);
1072 r0[7] = s * (r0[7] - r1[7] * m0);
1074 out->_11 = r0[4];
1075 out->_12 = r0[5];
1076 out->_13 = r0[6];
1077 out->_14 = r0[7];
1078 out->_21 = r1[4];
1079 out->_22 = r1[5];
1080 out->_23 = r1[6];
1081 out->_24 = r1[7];
1082 out->_31 = r2[4];
1083 out->_32 = r2[5];
1084 out->_33 = r2[6];
1085 out->_34 = r2[7];
1086 out->_41 = r3[4];
1087 out->_42 = r3[5];
1088 out->_43 = r3[6];
1089 out->_44 = r3[7];
1091 return TRUE;
1094 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1095 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1097 const struct wined3d_gl_info *gl_info = context->gl_info;
1098 float mat[3 * 3];
1099 struct wined3d_matrix mv;
1100 unsigned int i, j;
1102 if (prog->vs.normal_matrix_location == -1)
1103 return;
1105 get_modelview_matrix(context, state, 0, &mv);
1106 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1107 invert_matrix_3d(&mv, &mv);
1108 else
1109 invert_matrix(&mv, &mv);
1110 /* Tests show that singular modelview matrices are used unchanged as normal
1111 * matrices on D3D3 and older. There seems to be no clearly consistent
1112 * behavior on newer D3D versions so always follow older ddraw behavior. */
1113 for (i = 0; i < 3; ++i)
1114 for (j = 0; j < 3; ++j)
1115 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1117 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1118 checkGLcall("glUniformMatrix3fv");
1121 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1122 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1124 const struct wined3d_gl_info *gl_info = context->gl_info;
1125 struct wined3d_matrix mat;
1127 if (tex >= MAX_TEXTURES)
1128 return;
1129 if (prog->vs.texture_matrix_location[tex] == -1)
1130 return;
1132 get_texture_matrix(context, state, tex, &mat);
1133 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1134 checkGLcall("glUniformMatrix4fv");
1137 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1138 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1140 const struct wined3d_gl_info *gl_info = context->gl_info;
1142 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1144 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1145 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1147 else
1149 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1151 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1153 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1154 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1155 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1156 checkGLcall("setting FFP material uniforms");
1159 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1160 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1162 const struct wined3d_gl_info *gl_info = context->gl_info;
1163 struct wined3d_color color;
1165 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1166 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1167 checkGLcall("glUniform3fv");
1170 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1171 const struct wined3d_matrix *src2)
1173 struct wined3d_vec4 temp;
1175 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1176 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1177 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1178 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1180 *dest = temp;
1183 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1184 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1186 const struct wined3d_gl_info *gl_info = context->gl_info;
1187 const struct wined3d_light_info *light_info = state->lights[light];
1188 struct wined3d_vec4 vec4;
1189 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1191 if (!light_info)
1192 return;
1194 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1195 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1196 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1198 switch (light_info->OriginalParms.type)
1200 case WINED3D_LIGHT_POINT:
1201 multiply_vector_matrix(&vec4, &light_info->position, view);
1202 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1203 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1204 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1205 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1206 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1207 break;
1209 case WINED3D_LIGHT_SPOT:
1210 multiply_vector_matrix(&vec4, &light_info->position, view);
1211 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1213 multiply_vector_matrix(&vec4, &light_info->direction, view);
1214 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1216 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1217 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1218 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1219 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1220 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1221 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1222 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1223 break;
1225 case WINED3D_LIGHT_DIRECTIONAL:
1226 multiply_vector_matrix(&vec4, &light_info->direction, view);
1227 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1228 break;
1230 case WINED3D_LIGHT_PARALLELPOINT:
1231 multiply_vector_matrix(&vec4, &light_info->position, view);
1232 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1233 break;
1235 default:
1236 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1238 checkGLcall("setting FFP lights uniforms");
1241 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1242 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1244 const struct wined3d_gl_info *gl_info = context->gl_info;
1245 float min, max;
1246 float size, att[3];
1248 get_pointsize_minmax(context, state, &min, &max);
1250 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1251 checkGLcall("glUniform1f");
1252 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1253 checkGLcall("glUniform1f");
1255 get_pointsize(context, state, &size, att);
1257 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1258 checkGLcall("glUniform1f");
1259 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1260 checkGLcall("glUniform1f");
1261 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1262 checkGLcall("glUniform1f");
1263 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1264 checkGLcall("glUniform1f");
1267 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1268 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1270 const struct wined3d_gl_info *gl_info = context->gl_info;
1271 struct wined3d_color color;
1272 float start, end, scale;
1273 union
1275 DWORD d;
1276 float f;
1277 } tmpvalue;
1279 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1280 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1281 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1282 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1283 get_fog_start_end(context, state, &start, &end);
1284 scale = 1.0f / (end - start);
1285 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1286 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1287 checkGLcall("fog emulation uniforms");
1290 /* Context activation is done by the caller (state handler). */
1291 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1292 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1294 struct wined3d_color float_key[2];
1295 const struct wined3d_texture *texture = state->textures[0];
1297 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1298 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1301 /* Context activation is done by the caller (state handler). */
1302 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1303 const struct wined3d_state *state)
1305 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1306 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1307 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1308 const struct wined3d_gl_info *gl_info = context->gl_info;
1309 struct shader_glsl_priv *priv = shader_priv;
1310 float position_fixup[4];
1311 DWORD update_mask;
1313 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1314 UINT constant_version;
1315 int i;
1317 if (!prog) {
1318 /* No GLSL program set - nothing to do. */
1319 return;
1321 constant_version = prog->constant_version;
1322 update_mask = context->constant_update_mask & prog->constant_update_mask;
1324 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1325 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1326 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1328 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1329 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1330 vshader->reg_maps.integer_constants);
1332 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1333 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1334 vshader->reg_maps.boolean_constants);
1336 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1337 shader_glsl_pointsize_uniform(context, state, prog);
1339 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1341 shader_get_position_fixup(context, state, position_fixup);
1342 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1343 checkGLcall("glUniform4fv");
1346 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1348 struct wined3d_matrix mat;
1350 get_modelview_matrix(context, state, 0, &mat);
1351 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1352 checkGLcall("glUniformMatrix4fv");
1354 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1357 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1359 struct wined3d_matrix mat;
1361 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1363 if (prog->vs.modelview_matrix_location[i] == -1)
1364 break;
1366 get_modelview_matrix(context, state, i, &mat);
1367 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1368 checkGLcall("glUniformMatrix4fv");
1372 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1374 struct wined3d_matrix projection;
1376 get_projection_matrix(context, state, &projection);
1377 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1378 checkGLcall("glUniformMatrix4fv");
1381 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1383 for (i = 0; i < MAX_TEXTURES; ++i)
1384 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1387 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1388 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1390 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1392 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1393 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1394 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1397 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1398 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1399 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1401 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1402 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1403 pshader->reg_maps.integer_constants);
1405 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1406 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1407 pshader->reg_maps.boolean_constants);
1409 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1411 for (i = 0; i < MAX_TEXTURES; ++i)
1413 if (prog->ps.bumpenv_mat_location[i] == -1)
1414 continue;
1416 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1417 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1419 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1421 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1422 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1423 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1424 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1428 checkGLcall("bump env uniforms");
1431 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1433 const struct wined3d_vec4 correction_params =
1435 /* Position is relative to the framebuffer, not the viewport. */
1436 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1437 context->render_offscreen ? 1.0f : -1.0f,
1438 0.0f,
1439 0.0f,
1442 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1445 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1446 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1447 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1448 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1450 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1452 struct wined3d_color color;
1454 if (prog->ps.tex_factor_location != -1)
1456 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1457 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1460 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1461 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1462 else
1463 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1465 for (i = 0; i < MAX_TEXTURES; ++i)
1467 if (prog->ps.tss_constant_location[i] == -1)
1468 continue;
1470 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1471 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1474 checkGLcall("fixed function uniforms");
1477 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1478 shader_glsl_load_fog_uniform(context, state, prog);
1480 if (priv->next_constant_version == UINT_MAX)
1482 TRACE("Max constant version reached, resetting to 0.\n");
1483 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1484 priv->next_constant_version = 1;
1486 else
1488 prog->constant_version = priv->next_constant_version++;
1492 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1494 struct constant_entry *entries = heap->entries;
1495 unsigned int *positions = heap->positions;
1496 unsigned int heap_idx, parent_idx;
1498 if (!heap->contained[idx])
1500 heap_idx = heap->size++;
1501 heap->contained[idx] = TRUE;
1503 else
1505 heap_idx = positions[idx];
1508 while (heap_idx > 1)
1510 parent_idx = heap_idx >> 1;
1512 if (new_version <= entries[parent_idx].version) break;
1514 entries[heap_idx] = entries[parent_idx];
1515 positions[entries[parent_idx].idx] = heap_idx;
1516 heap_idx = parent_idx;
1519 entries[heap_idx].version = new_version;
1520 entries[heap_idx].idx = idx;
1521 positions[idx] = heap_idx;
1524 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1526 struct shader_glsl_priv *priv = device->shader_priv;
1527 struct constant_heap *heap = &priv->vconst_heap;
1528 UINT i;
1530 for (i = start; i < count + start; ++i)
1532 update_heap_entry(heap, i, priv->next_constant_version);
1535 for (i = 0; i < device->context_count; ++i)
1537 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1541 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1543 struct shader_glsl_priv *priv = device->shader_priv;
1544 struct constant_heap *heap = &priv->pconst_heap;
1545 UINT i;
1547 for (i = start; i < count + start; ++i)
1549 update_heap_entry(heap, i, priv->next_constant_version);
1552 for (i = 0; i < device->context_count; ++i)
1554 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1558 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1560 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1561 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1562 if(shader_major > 3) return ret;
1564 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1565 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1566 return ret;
1569 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1571 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1574 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1575 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1577 va_list args;
1578 int ret;
1580 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1581 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1582 for (;;)
1584 va_start(args, format);
1585 ret = shader_vaddline(buffer, format, args);
1586 va_end(args);
1587 if (!ret)
1588 return;
1589 if (!string_buffer_resize(buffer, ret))
1590 return;
1594 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1595 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1597 va_list args;
1598 int ret;
1600 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1601 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1602 for (;;)
1604 va_start(args, format);
1605 ret = shader_vaddline(buffer, format, args);
1606 va_end(args);
1607 if (!ret)
1608 return;
1609 if (!string_buffer_resize(buffer, ret))
1610 return;
1614 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1616 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1617 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1618 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1619 unsigned int i;
1621 if (reg_maps->shader_version.major < 3)
1622 return input_reg_used[idx];
1624 for (i = 0; i < input_signature->element_count; ++i)
1626 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1628 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1629 continue;
1631 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1632 && input->semantic_idx == idx)
1634 if (input_reg_used[input->register_idx])
1635 return TRUE;
1636 else
1637 return FALSE;
1640 return FALSE;
1643 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1644 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1646 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1648 if (version->major >= 4)
1649 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1650 else
1651 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1654 /** Generate the variable & register declarations for the GLSL output target */
1655 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1656 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1657 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1659 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1660 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1661 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1662 const struct wined3d_gl_info *gl_info = context->gl_info;
1663 unsigned int i, extra_constants_needed = 0;
1664 const struct wined3d_shader_lconst *lconst;
1665 const char *prefix;
1666 DWORD map;
1668 prefix = shader_glsl_get_prefix(version->type);
1670 /* Prototype the subroutines */
1671 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1673 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1676 /* Declare the constants (aka uniforms) */
1677 if (shader->limits->constant_float > 0)
1679 unsigned max_constantsF;
1681 /* Unless the shader uses indirect addressing, always declare the
1682 * maximum array size and ignore that we need some uniforms privately.
1683 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1684 * and immediate values, still declare VC[256]. If the shader needs
1685 * more uniforms than we have it won't work in any case. If it uses
1686 * less, the compiler will figure out which uniforms are really used
1687 * and strip them out. This allows a shader to use c255 on a dx9 card,
1688 * as long as it doesn't also use all the other constants.
1690 * If the shader uses indirect addressing the compiler must assume
1691 * that all declared uniforms are used. In this case, declare only the
1692 * amount that we're assured to have.
1694 * Thus we run into problems in these two cases:
1695 * 1) The shader really uses more uniforms than supported.
1696 * 2) The shader uses indirect addressing, less constants than
1697 * supported, but uses a constant index > #supported consts. */
1698 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1700 /* No indirect addressing here. */
1701 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1703 else
1705 if (reg_maps->usesrelconstF)
1707 /* Subtract the other potential uniforms from the max
1708 * available (bools, ints, and 1 row of projection matrix).
1709 * Subtract another uniform for immediate values, which have
1710 * to be loaded via uniform by the driver as well. The shader
1711 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1712 * shader code, so one vec4 should be enough. (Unfortunately
1713 * the Nvidia driver doesn't store 128 and -128 in one float).
1715 * Writing gl_ClipVertex requires one uniform for each
1716 * clipplane as well. */
1717 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1718 if (vs_args->clip_enabled)
1719 max_constantsF -= gl_info->limits.clipplanes;
1720 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1721 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1722 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1723 * for now take this into account when calculating the number of available constants
1725 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1726 /* Set by driver quirks in directx.c */
1727 max_constantsF -= gl_info->reserved_glsl_constants;
1729 if (max_constantsF < shader->limits->constant_float)
1731 static unsigned int once;
1733 if (!once++)
1734 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1735 " it may not render correctly.\n");
1736 else
1737 WARN("The hardware does not support enough uniform components to run this shader.\n");
1740 else
1742 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1745 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1746 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1749 /* Always declare the full set of constants, the compiler can remove the
1750 * unused ones because d3d doesn't (yet) support indirect int and bool
1751 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1752 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1753 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1755 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1756 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1758 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1760 if (reg_maps->cb_sizes[i])
1761 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1762 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1765 /* Declare texture samplers */
1766 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1768 struct wined3d_shader_sampler_map_entry *entry;
1769 const char *sampler_type_prefix, *sampler_type;
1770 BOOL shadow_sampler, tex_rect;
1772 entry = &reg_maps->sampler_map.entries[i];
1774 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1776 ERR("Invalid resource index %u.\n", entry->resource_idx);
1777 continue;
1780 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1782 case WINED3D_DATA_FLOAT:
1783 case WINED3D_DATA_UNORM:
1784 case WINED3D_DATA_SNORM:
1785 sampler_type_prefix = "";
1786 break;
1788 case WINED3D_DATA_INT:
1789 sampler_type_prefix = "i";
1790 break;
1792 case WINED3D_DATA_UINT:
1793 sampler_type_prefix = "u";
1794 break;
1796 default:
1797 sampler_type_prefix = "";
1798 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1799 break;
1802 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
1803 switch (reg_maps->resource_info[entry->resource_idx].type)
1805 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1806 if (shadow_sampler)
1807 sampler_type = "sampler1DShadow";
1808 else
1809 sampler_type = "sampler1D";
1810 break;
1812 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1813 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1814 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1815 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1816 if (shadow_sampler)
1818 if (tex_rect)
1819 sampler_type = "sampler2DRectShadow";
1820 else
1821 sampler_type = "sampler2DShadow";
1823 else
1825 if (tex_rect)
1826 sampler_type = "sampler2DRect";
1827 else
1828 sampler_type = "sampler2D";
1830 break;
1832 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1833 if (shadow_sampler)
1834 FIXME("Unsupported 3D shadow sampler.\n");
1835 sampler_type = "sampler3D";
1836 break;
1838 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1839 if (shadow_sampler)
1840 FIXME("Unsupported Cube shadow sampler.\n");
1841 sampler_type = "samplerCube";
1842 break;
1844 default:
1845 sampler_type = "unsupported_sampler";
1846 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1847 break;
1849 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
1850 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
1853 /* Declare uniforms for NP2 texcoord fixup:
1854 * This is NOT done inside the loop that declares the texture samplers
1855 * since the NP2 fixup code is currently only used for the GeforceFX
1856 * series and when forcing the ARB_npot extension off. Modern cards just
1857 * skip the code anyway, so put it inside a separate loop. */
1858 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1860 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1861 UINT cur = 0;
1863 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1864 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1865 * samplerNP2Fixup stores texture dimensions and is updated through
1866 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1868 for (i = 0; i < shader->limits->sampler; ++i)
1870 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1871 continue;
1873 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1875 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1876 continue;
1879 fixup->idx[i] = cur++;
1882 fixup->num_consts = (cur + 1) >> 1;
1883 fixup->active = ps_args->np2_fixup;
1884 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1887 /* Declare address variables */
1888 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1890 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1893 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1895 for (i = 0; i < shader->input_signature.element_count; ++i)
1897 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1898 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1899 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1900 prefix, e->register_idx);
1901 else
1902 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1905 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1907 shader_addline(buffer, "uniform struct\n{\n");
1908 shader_addline(buffer, " float size;\n");
1909 shader_addline(buffer, " float size_min;\n");
1910 shader_addline(buffer, " float size_max;\n");
1911 shader_addline(buffer, "} ffp_point;\n");
1914 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1916 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1917 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
1918 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1919 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1922 shader_addline(buffer, "uniform vec4 posFixup;\n");
1923 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1925 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1927 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1929 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1931 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1933 shader_addline(buffer, "uniform struct\n{\n");
1934 shader_addline(buffer, " vec4 color;\n");
1935 shader_addline(buffer, " float density;\n");
1936 shader_addline(buffer, " float end;\n");
1937 shader_addline(buffer, " float scale;\n");
1938 shader_addline(buffer, "} ffp_fog;\n");
1940 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1942 if (glsl_is_color_reg_read(shader, 0))
1943 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
1944 if (glsl_is_color_reg_read(shader, 1))
1945 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
1946 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1947 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
1949 else
1951 if (glsl_is_color_reg_read(shader, 0))
1952 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1953 if (glsl_is_color_reg_read(shader, 1))
1954 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
1955 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1956 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1957 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1961 if (version->major >= 3)
1963 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1965 if (ps_args->vp_mode == vertexshader)
1966 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
1967 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1970 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1972 if (!(map & 1))
1973 continue;
1975 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1977 if (reg_maps->luminanceparams & (1u << i))
1979 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1980 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1981 extra_constants_needed++;
1984 extra_constants_needed++;
1987 if (ps_args->srgb_correction)
1989 shader_addline(buffer, "const vec4 srgb_const0 = ");
1990 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1991 shader_addline(buffer, ";\n");
1992 shader_addline(buffer, "const vec4 srgb_const1 = ");
1993 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1994 shader_addline(buffer, ";\n");
1996 if (reg_maps->vpos || reg_maps->usesdsy)
1998 ++extra_constants_needed;
1999 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2000 shader_addline(buffer, "vec4 vpos;\n");
2003 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2004 FIXME("Insufficient uniforms to run this shader.\n");
2007 /* Declare output register temporaries */
2008 if (shader->limits->packed_output)
2009 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2011 /* Declare temporary variables */
2012 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2014 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
2017 /* Declare loop registers aLx */
2018 if (version->major < 4)
2020 for (i = 0; i < reg_maps->loop_depth; ++i)
2022 shader_addline(buffer, "int aL%u;\n", i);
2023 shader_addline(buffer, "int tmpInt%u;\n", i);
2027 /* Temporary variables for matrix operations */
2028 shader_addline(buffer, "vec4 tmp0;\n");
2029 shader_addline(buffer, "vec4 tmp1;\n");
2031 if (!shader->load_local_constsF)
2033 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2035 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2036 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2037 shader_addline(buffer, ";\n");
2041 /* Start the main program. */
2042 shader_addline(buffer, "void main()\n{\n");
2044 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2045 * add approximately 0.5. This causes off-by-one problems as spotted by
2046 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2047 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2048 * causes precision troubles when we just subtract 0.5.
2050 * To deal with that, just floor() the position. This will eliminate the
2051 * fraction on all cards.
2053 * TODO: Test how this behaves with multisampling.
2055 * An advantage of floor is that it works even if the driver doesn't add
2056 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2057 * to return in gl_FragCoord, even though coordinates specify the pixel
2058 * centers instead of the pixel corners. This code will behave correctly
2059 * on drivers that returns integer values. */
2060 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
2062 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
2063 shader_addline(buffer,
2064 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2065 else
2066 shader_addline(buffer,
2067 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2071 /*****************************************************************************
2072 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2074 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2075 ****************************************************************************/
2077 /* Prototypes */
2078 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2079 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2081 /** Used for opcode modifiers - They multiply the result by the specified amount */
2082 static const char * const shift_glsl_tab[] = {
2083 "", /* 0 (none) */
2084 "2.0 * ", /* 1 (x2) */
2085 "4.0 * ", /* 2 (x4) */
2086 "8.0 * ", /* 3 (x8) */
2087 "16.0 * ", /* 4 (x16) */
2088 "32.0 * ", /* 5 (x32) */
2089 "", /* 6 (x64) */
2090 "", /* 7 (x128) */
2091 "", /* 8 (d256) */
2092 "", /* 9 (d128) */
2093 "", /* 10 (d64) */
2094 "", /* 11 (d32) */
2095 "0.0625 * ", /* 12 (d16) */
2096 "0.125 * ", /* 13 (d8) */
2097 "0.25 * ", /* 14 (d4) */
2098 "0.5 * " /* 15 (d2) */
2101 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2102 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2103 const char *in_reg, const char *in_regswizzle, char *out_str)
2105 switch (src_modifier)
2107 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2108 case WINED3DSPSM_DW:
2109 case WINED3DSPSM_NONE:
2110 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2111 break;
2112 case WINED3DSPSM_NEG:
2113 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2114 break;
2115 case WINED3DSPSM_NOT:
2116 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2117 break;
2118 case WINED3DSPSM_BIAS:
2119 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2120 break;
2121 case WINED3DSPSM_BIASNEG:
2122 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2123 break;
2124 case WINED3DSPSM_SIGN:
2125 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2126 break;
2127 case WINED3DSPSM_SIGNNEG:
2128 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2129 break;
2130 case WINED3DSPSM_COMP:
2131 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2132 break;
2133 case WINED3DSPSM_X2:
2134 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2135 break;
2136 case WINED3DSPSM_X2NEG:
2137 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2138 break;
2139 case WINED3DSPSM_ABS:
2140 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2141 break;
2142 case WINED3DSPSM_ABSNEG:
2143 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2144 break;
2145 default:
2146 FIXME("Unhandled modifier %u\n", src_modifier);
2147 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2151 /** Writes the GLSL variable name that corresponds to the register that the
2152 * DX opcode parameter is trying to access */
2153 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2154 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2156 /* oPos, oFog and oPts in D3D */
2157 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2159 const struct wined3d_shader *shader = ins->ctx->shader;
2160 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2161 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2162 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2163 const char *prefix = shader_glsl_get_prefix(version->type);
2164 struct glsl_src_param rel_param0, rel_param1;
2165 char imm_str[4][17];
2167 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2168 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2169 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2170 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2171 *is_color = FALSE;
2173 switch (reg->type)
2175 case WINED3DSPR_TEMP:
2176 sprintf(register_name, "R%u", reg->idx[0].offset);
2177 break;
2179 case WINED3DSPR_INPUT:
2180 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2182 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2184 if (reg->idx[0].rel_addr)
2185 FIXME("VS3+ input registers relative addressing.\n");
2186 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2187 *is_color = TRUE;
2188 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2189 break;
2192 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2194 if (reg->idx[0].rel_addr)
2196 if (reg->idx[1].rel_addr)
2197 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2198 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2199 else
2200 sprintf(register_name, "gs_in[%s + %u][%u]",
2201 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2203 else if (reg->idx[1].rel_addr)
2204 sprintf(register_name, "gs_in[%u][%s + %u]",
2205 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2206 else
2207 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2208 break;
2211 /* pixel shaders >= 3.0 */
2212 if (version->major >= 3)
2214 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2215 unsigned int in_count = vec4_varyings(version->major, gl_info);
2217 if (reg->idx[0].rel_addr)
2219 /* Removing a + 0 would be an obvious optimization, but
2220 * OS X doesn't see the NOP operation there. */
2221 if (idx)
2223 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2224 && shader->u.ps.declared_in_count > in_count)
2226 sprintf(register_name,
2227 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2228 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2229 prefix, rel_param0.param_str, idx);
2231 else
2233 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2236 else
2238 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2239 && shader->u.ps.declared_in_count > in_count)
2241 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2242 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2243 prefix, rel_param0.param_str);
2245 else
2247 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2251 else
2253 if (idx == in_count) sprintf(register_name, "gl_Color");
2254 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2255 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2258 else
2260 if (!reg->idx[0].offset)
2261 strcpy(register_name, "ffp_varying_diffuse");
2262 else
2263 strcpy(register_name, "ffp_varying_specular");
2264 break;
2266 break;
2268 case WINED3DSPR_CONST:
2270 /* Relative addressing */
2271 if (reg->idx[0].rel_addr)
2273 if (wined3d_settings.check_float_constants)
2274 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2275 rel_param0.param_str, reg->idx[0].offset,
2276 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2277 prefix, rel_param0.param_str, reg->idx[0].offset);
2278 else if (reg->idx[0].offset)
2279 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2280 else
2281 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2283 else
2285 if (shader_constant_is_local(shader, reg->idx[0].offset))
2286 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2287 else
2288 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2291 break;
2293 case WINED3DSPR_CONSTINT:
2294 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2295 break;
2297 case WINED3DSPR_CONSTBOOL:
2298 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2299 break;
2301 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2302 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2303 sprintf(register_name, "T%u", reg->idx[0].offset);
2304 else
2305 sprintf(register_name, "A%u", reg->idx[0].offset);
2306 break;
2308 case WINED3DSPR_LOOP:
2309 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2310 break;
2312 case WINED3DSPR_SAMPLER:
2313 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2314 break;
2316 case WINED3DSPR_COLOROUT:
2317 if (reg->idx[0].offset >= gl_info->limits.buffers)
2318 WARN("Write to render target %u, only %d supported.\n",
2319 reg->idx[0].offset, gl_info->limits.buffers);
2321 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
2322 break;
2324 case WINED3DSPR_RASTOUT:
2325 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2326 break;
2328 case WINED3DSPR_DEPTHOUT:
2329 sprintf(register_name, "gl_FragDepth");
2330 break;
2332 case WINED3DSPR_ATTROUT:
2333 if (!reg->idx[0].offset)
2334 sprintf(register_name, "%s_out[8]", prefix);
2335 else
2336 sprintf(register_name, "%s_out[9]", prefix);
2337 break;
2339 case WINED3DSPR_TEXCRDOUT:
2340 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2341 if (reg->idx[0].rel_addr)
2342 FIXME("VS3 output registers relative addressing.\n");
2343 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2344 break;
2346 case WINED3DSPR_MISCTYPE:
2347 if (!reg->idx[0].offset)
2349 /* vPos */
2350 sprintf(register_name, "vpos");
2352 else if (reg->idx[0].offset == 1)
2354 /* Note that gl_FrontFacing is a bool, while vFace is
2355 * a float for which the sign determines front/back */
2356 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2358 else
2360 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2361 sprintf(register_name, "unrecognized_register");
2363 break;
2365 case WINED3DSPR_IMMCONST:
2366 switch (reg->immconst_type)
2368 case WINED3D_IMMCONST_SCALAR:
2369 switch (reg->data_type)
2371 case WINED3D_DATA_FLOAT:
2372 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2373 break;
2374 case WINED3D_DATA_INT:
2375 sprintf(register_name, "%#x", reg->immconst_data[0]);
2376 break;
2377 case WINED3D_DATA_RESOURCE:
2378 case WINED3D_DATA_SAMPLER:
2379 case WINED3D_DATA_UINT:
2380 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2381 break;
2382 default:
2383 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2384 break;
2386 break;
2388 case WINED3D_IMMCONST_VEC4:
2389 switch (reg->data_type)
2391 case WINED3D_DATA_FLOAT:
2392 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2393 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2394 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2395 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2396 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2397 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2398 break;
2399 case WINED3D_DATA_INT:
2400 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2401 reg->immconst_data[0], reg->immconst_data[1],
2402 reg->immconst_data[2], reg->immconst_data[3]);
2403 break;
2404 case WINED3D_DATA_RESOURCE:
2405 case WINED3D_DATA_SAMPLER:
2406 case WINED3D_DATA_UINT:
2407 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2408 reg->immconst_data[0], reg->immconst_data[1],
2409 reg->immconst_data[2], reg->immconst_data[3]);
2410 break;
2411 default:
2412 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2413 break;
2415 break;
2417 default:
2418 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2419 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2421 break;
2423 case WINED3DSPR_CONSTBUFFER:
2424 if (reg->idx[1].rel_addr)
2425 sprintf(register_name, "%s_cb%u[%s + %u]",
2426 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2427 else
2428 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2429 break;
2431 case WINED3DSPR_IMMCONSTBUFFER:
2432 if (reg->idx[0].rel_addr)
2433 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2434 else
2435 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2436 break;
2438 case WINED3DSPR_PRIMID:
2439 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2440 break;
2442 default:
2443 FIXME("Unhandled register type %#x.\n", reg->type);
2444 sprintf(register_name, "unrecognized_register");
2445 break;
2449 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2451 *str++ = '.';
2452 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2453 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2454 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2455 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2456 *str = '\0';
2459 /* Get the GLSL write mask for the destination register */
2460 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2462 DWORD mask = param->write_mask;
2464 if (shader_is_scalar(&param->reg))
2466 mask = WINED3DSP_WRITEMASK_0;
2467 *write_mask = '\0';
2469 else
2471 shader_glsl_write_mask_to_str(mask, write_mask);
2474 return mask;
2477 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2478 unsigned int size = 0;
2480 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2481 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2482 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2483 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2485 return size;
2488 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2490 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2491 * but addressed as "rgba". To fix this we need to swap the register's x
2492 * and z components. */
2493 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2495 *str++ = '.';
2496 /* swizzle bits fields: wwzzyyxx */
2497 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2498 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2499 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2500 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2501 *str = '\0';
2504 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2505 BOOL fixup, DWORD mask, char *swizzle_str)
2507 if (shader_is_scalar(&param->reg))
2508 *swizzle_str = '\0';
2509 else
2510 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2513 /* From a given parameter token, generate the corresponding GLSL string.
2514 * Also, return the actual register name and swizzle in case the
2515 * caller needs this information as well. */
2516 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2517 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2519 BOOL is_color = FALSE;
2520 char swizzle_str[6];
2522 glsl_src->reg_name[0] = '\0';
2523 glsl_src->param_str[0] = '\0';
2524 swizzle_str[0] = '\0';
2526 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2527 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2529 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2531 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2533 else
2535 char reg_name[200];
2537 switch (wined3d_src->reg.data_type)
2539 case WINED3D_DATA_FLOAT:
2540 sprintf(reg_name, "%s", glsl_src->reg_name);
2541 break;
2542 case WINED3D_DATA_INT:
2543 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2544 break;
2545 case WINED3D_DATA_RESOURCE:
2546 case WINED3D_DATA_SAMPLER:
2547 case WINED3D_DATA_UINT:
2548 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2549 break;
2550 default:
2551 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2552 sprintf(reg_name, "%s", glsl_src->reg_name);
2553 break;
2556 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2560 /* From a given parameter token, generate the corresponding GLSL string.
2561 * Also, return the actual register name and swizzle in case the
2562 * caller needs this information as well. */
2563 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2564 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2566 BOOL is_color = FALSE;
2568 glsl_dst->mask_str[0] = '\0';
2569 glsl_dst->reg_name[0] = '\0';
2571 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2572 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2575 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2576 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2577 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2578 enum wined3d_data_type data_type)
2580 struct glsl_dst_param glsl_dst;
2581 DWORD mask;
2583 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2585 switch (data_type)
2587 case WINED3D_DATA_FLOAT:
2588 shader_addline(buffer, "%s%s = %s(",
2589 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2590 break;
2591 case WINED3D_DATA_INT:
2592 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2593 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2594 break;
2595 case WINED3D_DATA_RESOURCE:
2596 case WINED3D_DATA_SAMPLER:
2597 case WINED3D_DATA_UINT:
2598 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2599 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2600 break;
2601 default:
2602 FIXME("Unhandled data type %#x.\n", data_type);
2603 shader_addline(buffer, "%s%s = %s(",
2604 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2605 break;
2609 return mask;
2612 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2613 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2615 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2618 /** Process GLSL instruction modifiers */
2619 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2621 struct glsl_dst_param dst_param;
2622 DWORD modifiers;
2624 if (!ins->dst_count) return;
2626 modifiers = ins->dst[0].modifiers;
2627 if (!modifiers) return;
2629 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2631 if (modifiers & WINED3DSPDM_SATURATE)
2633 /* _SAT means to clamp the value of the register to between 0 and 1 */
2634 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2635 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2638 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2640 FIXME("_centroid modifier not handled\n");
2643 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2645 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2649 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2651 switch (op)
2653 case WINED3D_SHADER_REL_OP_GT: return ">";
2654 case WINED3D_SHADER_REL_OP_EQ: return "==";
2655 case WINED3D_SHADER_REL_OP_GE: return ">=";
2656 case WINED3D_SHADER_REL_OP_LT: return "<";
2657 case WINED3D_SHADER_REL_OP_NE: return "!=";
2658 case WINED3D_SHADER_REL_OP_LE: return "<=";
2659 default:
2660 FIXME("Unrecognized operator %#x.\n", op);
2661 return "(\?\?)";
2665 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2666 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
2668 static const struct
2670 unsigned int coord_size;
2671 unsigned int offset_size;
2672 const char *type_part;
2674 resource_types[] =
2676 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2677 {1, 0, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2678 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2679 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2680 {2, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2681 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2682 {3, 0, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2683 {2, 1, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2684 {3, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2685 {3, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2687 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2688 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2689 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2690 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
2691 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2692 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2693 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2694 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2695 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2696 const char *base = "texture", *type_part = "", *suffix = "";
2697 unsigned int coord_size;
2699 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2701 if (resource_type >= ARRAY_SIZE(resource_types))
2703 ERR("Unexpected resource type %#x.\n", resource_type);
2704 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2707 /* Note that there's no such thing as a projected cube texture. */
2708 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2709 projected = FALSE;
2711 if (needs_legacy_glsl_syntax(gl_info))
2713 if (shadow)
2714 base = "shadow";
2716 type_part = resource_types[resource_type].type_part;
2717 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2718 type_part = "2DRect";
2719 if (!type_part[0])
2720 FIXME("Unhandled resource type %#x.\n", resource_type);
2722 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2724 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2725 suffix = "ARB";
2726 else
2727 FIXME("Unsupported grad function.\n");
2731 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2733 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2734 if (flags & ~texel_fetch_flags)
2735 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
2737 base = "texelFetch";
2738 type_part = "";
2741 sample_function->offset_size = offset ? resource_types[resource_type].offset_size : 0;
2742 if (offset && !sample_function->offset_size)
2744 FIXME("Offset not supported for resource type %#x.\n", resource_type);
2745 offset = FALSE;
2748 sample_function->name = string_buffer_get(priv->string_buffers);
2749 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2750 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2752 coord_size = resource_types[resource_type].coord_size;
2753 if (shadow)
2754 ++coord_size;
2755 sample_function->coord_mask = (1u << coord_size) - 1;
2756 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2759 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2760 struct glsl_sample_function *sample_function)
2762 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2764 string_buffer_release(priv->string_buffers, sample_function->name);
2767 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2768 BOOL sign_fixup, enum fixup_channel_source channel_source)
2770 switch(channel_source)
2772 case CHANNEL_SOURCE_ZERO:
2773 strcat(arguments, "0.0");
2774 break;
2776 case CHANNEL_SOURCE_ONE:
2777 strcat(arguments, "1.0");
2778 break;
2780 case CHANNEL_SOURCE_X:
2781 strcat(arguments, reg_name);
2782 strcat(arguments, ".x");
2783 break;
2785 case CHANNEL_SOURCE_Y:
2786 strcat(arguments, reg_name);
2787 strcat(arguments, ".y");
2788 break;
2790 case CHANNEL_SOURCE_Z:
2791 strcat(arguments, reg_name);
2792 strcat(arguments, ".z");
2793 break;
2795 case CHANNEL_SOURCE_W:
2796 strcat(arguments, reg_name);
2797 strcat(arguments, ".w");
2798 break;
2800 default:
2801 FIXME("Unhandled channel source %#x\n", channel_source);
2802 strcat(arguments, "undefined");
2803 break;
2806 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2809 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2810 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2812 unsigned int mask_size, remaining;
2813 DWORD fixup_mask = 0;
2814 char arguments[256];
2815 char mask_str[6];
2817 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2818 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2819 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2820 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2821 if (!(mask &= fixup_mask))
2822 return;
2824 if (is_complex_fixup(fixup))
2826 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2827 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2828 return;
2831 shader_glsl_write_mask_to_str(mask, mask_str);
2832 mask_size = shader_glsl_get_write_mask_size(mask);
2834 arguments[0] = '\0';
2835 remaining = mask_size;
2836 if (mask & WINED3DSP_WRITEMASK_0)
2838 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2839 if (--remaining) strcat(arguments, ", ");
2841 if (mask & WINED3DSP_WRITEMASK_1)
2843 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2844 if (--remaining) strcat(arguments, ", ");
2846 if (mask & WINED3DSP_WRITEMASK_2)
2848 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2849 if (--remaining) strcat(arguments, ", ");
2851 if (mask & WINED3DSP_WRITEMASK_3)
2853 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2854 if (--remaining) strcat(arguments, ", ");
2857 if (mask_size > 1)
2858 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2859 else
2860 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2863 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2865 char reg_name[256];
2866 BOOL is_color;
2868 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2869 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2872 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2873 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
2874 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
2875 const char *coord_reg_fmt, ...)
2877 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2878 char dst_swizzle[6];
2879 struct color_fixup_desc fixup;
2880 BOOL np2_fixup = FALSE;
2881 va_list args;
2882 int ret;
2884 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2886 /* If ARB_texture_swizzle is supported we don't need to do anything here.
2887 * We actually rely on it for vertex shaders and SM4+. */
2888 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2890 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2891 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
2893 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
2894 np2_fixup = TRUE;
2896 else
2898 fixup = COLOR_FIXUP_IDENTITY;
2901 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2903 if (sample_function->output_single_component)
2904 shader_addline(ins->ctx->buffer, "vec4(");
2906 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2907 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
2909 for (;;)
2911 va_start(args, coord_reg_fmt);
2912 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2913 va_end(args);
2914 if (!ret)
2915 break;
2916 if (!string_buffer_resize(ins->ctx->buffer, ret))
2917 break;
2920 if (np2_fixup)
2922 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2923 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
2925 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2927 case 1:
2928 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2929 idx >> 1, (idx % 2) ? "z" : "x");
2930 break;
2931 case 2:
2932 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2933 idx >> 1, (idx % 2) ? "zw" : "xy");
2934 break;
2935 case 3:
2936 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2937 idx >> 1, (idx % 2) ? "zw" : "xy");
2938 break;
2939 case 4:
2940 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2941 idx >> 1, (idx % 2) ? "zw" : "xy");
2942 break;
2945 if (dx && dy)
2946 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
2947 else if (bias)
2948 shader_addline(ins->ctx->buffer, ", %s", bias);
2949 if (sample_function->offset_size)
2951 int offset_immdata[4] = {offset->u, offset->v, offset->w};
2952 shader_addline(ins->ctx->buffer, ", ");
2953 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
2955 shader_addline(ins->ctx->buffer, ")");
2957 if (sample_function->output_single_component)
2958 shader_addline(ins->ctx->buffer, ")");
2960 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2962 if (!is_identity_fixup(fixup))
2963 shader_glsl_color_correction(ins, fixup);
2966 /*****************************************************************************
2967 * Begin processing individual instruction opcodes
2968 ****************************************************************************/
2970 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2972 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2973 struct glsl_src_param src0_param;
2974 struct glsl_src_param src1_param;
2975 DWORD write_mask;
2976 const char *op;
2978 /* Determine the GLSL operator to use based on the opcode */
2979 switch (ins->handler_idx)
2981 case WINED3DSIH_ADD: op = "+"; break;
2982 case WINED3DSIH_AND: op = "&"; break;
2983 case WINED3DSIH_DIV: op = "/"; break;
2984 case WINED3DSIH_IADD: op = "+"; break;
2985 case WINED3DSIH_ISHL: op = "<<"; break;
2986 case WINED3DSIH_MUL: op = "*"; break;
2987 case WINED3DSIH_OR: op = "|"; break;
2988 case WINED3DSIH_SUB: op = "-"; break;
2989 case WINED3DSIH_USHR: op = ">>"; break;
2990 case WINED3DSIH_XOR: op = "^"; break;
2991 default:
2992 op = "<unhandled operator>";
2993 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
2994 break;
2997 write_mask = shader_glsl_append_dst(buffer, ins);
2998 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2999 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3000 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3003 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3005 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3006 struct glsl_src_param src0_param;
3007 struct glsl_src_param src1_param;
3008 unsigned int mask_size;
3009 DWORD write_mask;
3010 const char *op;
3012 write_mask = shader_glsl_append_dst(buffer, ins);
3013 mask_size = shader_glsl_get_write_mask_size(write_mask);
3014 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3015 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3017 if (mask_size > 1)
3019 switch (ins->handler_idx)
3021 case WINED3DSIH_EQ: op = "equal"; break;
3022 case WINED3DSIH_IEQ: op = "equal"; break;
3023 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3024 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3025 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3026 case WINED3DSIH_LT: op = "lessThan"; break;
3027 case WINED3DSIH_ILT: op = "lessThan"; break;
3028 case WINED3DSIH_NE: op = "notEqual"; break;
3029 case WINED3DSIH_INE: op = "notEqual"; break;
3030 default:
3031 op = "<unhandled operator>";
3032 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3033 break;
3036 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3037 mask_size, op, src0_param.param_str, src1_param.param_str);
3039 else
3041 switch (ins->handler_idx)
3043 case WINED3DSIH_EQ: op = "=="; break;
3044 case WINED3DSIH_IEQ: op = "=="; break;
3045 case WINED3DSIH_GE: op = ">="; break;
3046 case WINED3DSIH_IGE: op = ">="; break;
3047 case WINED3DSIH_UGE: op = ">="; break;
3048 case WINED3DSIH_LT: op = "<"; break;
3049 case WINED3DSIH_ILT: op = "<"; break;
3050 case WINED3DSIH_NE: op = "!="; break;
3051 case WINED3DSIH_INE: op = "!="; break;
3052 default:
3053 op = "<unhandled operator>";
3054 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3055 break;
3058 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3059 src0_param.param_str, op, src1_param.param_str);
3063 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3065 struct glsl_src_param src_param;
3066 DWORD write_mask;
3067 const char *op;
3069 switch (ins->handler_idx)
3071 case WINED3DSIH_INEG: op = "-"; break;
3072 case WINED3DSIH_NOT: op = "~"; break;
3073 default:
3074 op = "<unhandled operator>";
3075 ERR("Unhandled opcode %s.\n",
3076 debug_d3dshaderinstructionhandler(ins->handler_idx));
3077 break;
3080 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3081 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3082 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3085 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3087 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3088 struct glsl_src_param src0_param;
3089 struct glsl_src_param src1_param;
3090 DWORD write_mask;
3092 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3093 * not, we can emulate it. */
3094 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3095 FIXME("64-bit integer multiplies not implemented.\n");
3097 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3099 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3100 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3101 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3103 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3104 src0_param.param_str, src1_param.param_str);
3108 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3110 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3111 struct glsl_src_param src0_param, src1_param;
3112 DWORD write_mask;
3114 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3116 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3118 char dst_mask[6];
3120 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3121 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3122 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3123 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3124 dst_mask, src0_param.param_str, src1_param.param_str);
3126 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3127 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3128 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3129 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3131 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3132 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3134 else
3136 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3137 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3138 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3139 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3142 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3144 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3145 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3146 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3147 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3151 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3152 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3154 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3155 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3156 struct glsl_src_param src0_param;
3157 DWORD write_mask;
3159 write_mask = shader_glsl_append_dst(buffer, ins);
3160 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3162 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3163 * shader versions WINED3DSIO_MOVA is used for this. */
3164 if (ins->ctx->reg_maps->shader_version.major == 1
3165 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3166 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3168 /* This is a simple floor() */
3169 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3170 if (mask_size > 1) {
3171 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3172 } else {
3173 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3176 else if(ins->handler_idx == WINED3DSIH_MOVA)
3178 /* We need to *round* to the nearest int here. */
3179 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3181 if (gl_info->supported[EXT_GPU_SHADER4])
3183 if (mask_size > 1)
3184 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3185 else
3186 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3188 else
3190 if (mask_size > 1)
3191 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3192 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3193 else
3194 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3195 src0_param.param_str, src0_param.param_str);
3198 else
3200 shader_addline(buffer, "%s);\n", src0_param.param_str);
3204 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3205 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3207 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3208 struct glsl_src_param src0_param;
3209 struct glsl_src_param src1_param;
3210 DWORD dst_write_mask, src_write_mask;
3211 unsigned int dst_size;
3213 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3214 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3216 /* dp4 works on vec4, dp3 on vec3, etc. */
3217 if (ins->handler_idx == WINED3DSIH_DP4)
3218 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3219 else if (ins->handler_idx == WINED3DSIH_DP3)
3220 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3221 else
3222 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3224 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3225 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3227 if (dst_size > 1) {
3228 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3229 } else {
3230 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3234 /* Note that this instruction has some restrictions. The destination write mask
3235 * can't contain the w component, and the source swizzles have to be .xyzw */
3236 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3238 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3239 struct glsl_src_param src0_param;
3240 struct glsl_src_param src1_param;
3241 char dst_mask[6];
3243 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3244 shader_glsl_append_dst(ins->ctx->buffer, ins);
3245 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3246 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3247 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3250 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3252 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3255 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3256 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3257 * GLSL uses the value as-is. */
3258 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3260 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3261 struct glsl_src_param src0_param;
3262 struct glsl_src_param src1_param;
3263 DWORD dst_write_mask;
3264 unsigned int dst_size;
3266 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3267 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3269 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3270 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3272 if (dst_size > 1)
3274 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3275 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3277 else
3279 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3280 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3284 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3285 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3287 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3288 struct glsl_src_param src_param;
3289 const char *instruction;
3290 DWORD write_mask;
3291 unsigned i;
3293 /* Determine the GLSL function to use based on the opcode */
3294 /* TODO: Possibly make this a table for faster lookups */
3295 switch (ins->handler_idx)
3297 case WINED3DSIH_ABS: instruction = "abs"; break;
3298 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3299 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3300 case WINED3DSIH_FRC: instruction = "fract"; break;
3301 case WINED3DSIH_IMAX: instruction = "max"; break;
3302 case WINED3DSIH_IMIN: instruction = "min"; break;
3303 case WINED3DSIH_MAX: instruction = "max"; break;
3304 case WINED3DSIH_MIN: instruction = "min"; break;
3305 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3306 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3307 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3308 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3309 default: instruction = "";
3310 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3311 break;
3314 write_mask = shader_glsl_append_dst(buffer, ins);
3316 shader_addline(buffer, "%s(", instruction);
3318 if (ins->src_count)
3320 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3321 shader_addline(buffer, "%s", src_param.param_str);
3322 for (i = 1; i < ins->src_count; ++i)
3324 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3325 shader_addline(buffer, ", %s", src_param.param_str);
3329 shader_addline(buffer, "));\n");
3332 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3334 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3336 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3337 struct glsl_src_param src_param;
3338 unsigned int mask_size;
3339 DWORD write_mask;
3340 char dst_mask[6];
3342 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3343 mask_size = shader_glsl_get_write_mask_size(write_mask);
3344 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3346 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3347 src_param.param_str, src_param.param_str);
3348 shader_glsl_append_dst(buffer, ins);
3350 if (mask_size > 1)
3352 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3353 mask_size, src_param.param_str);
3355 else
3357 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3358 src_param.param_str);
3362 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3364 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3365 ins->ctx->reg_maps->shader_version.minor);
3366 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3367 struct glsl_src_param src0_param;
3368 const char *prefix, *suffix;
3369 unsigned int dst_size;
3370 DWORD dst_write_mask;
3372 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3373 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3375 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3376 dst_write_mask = WINED3DSP_WRITEMASK_3;
3378 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3380 switch (ins->handler_idx)
3382 case WINED3DSIH_EXP:
3383 case WINED3DSIH_EXPP:
3384 prefix = "exp2(";
3385 suffix = ")";
3386 break;
3388 case WINED3DSIH_LOG:
3389 case WINED3DSIH_LOGP:
3390 prefix = "log2(abs(";
3391 suffix = "))";
3392 break;
3394 case WINED3DSIH_RCP:
3395 prefix = "1.0 / ";
3396 suffix = "";
3397 break;
3399 case WINED3DSIH_RSQ:
3400 prefix = "inversesqrt(abs(";
3401 suffix = "))";
3402 break;
3404 default:
3405 prefix = "";
3406 suffix = "";
3407 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3408 break;
3411 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3412 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3413 else
3414 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3417 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3418 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3419 * dst.x = 2^(floor(src))
3420 * dst.y = src - floor(src)
3421 * dst.z = 2^src (partial precision is allowed, but optional)
3422 * dst.w = 1.0;
3423 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3424 * dst = 2^src; (partial precision is allowed, but optional)
3426 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3428 if (ins->ctx->reg_maps->shader_version.major < 2)
3430 struct glsl_src_param src_param;
3431 char dst_mask[6];
3433 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3435 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3436 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3437 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3438 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3440 shader_glsl_append_dst(ins->ctx->buffer, ins);
3441 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3442 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3443 return;
3446 shader_glsl_scalar_op(ins);
3449 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3450 const char *vector_constructor, const char *scalar_constructor)
3452 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3453 struct glsl_src_param src_param;
3454 unsigned int mask_size;
3455 DWORD write_mask;
3457 write_mask = shader_glsl_append_dst(buffer, ins);
3458 mask_size = shader_glsl_get_write_mask_size(write_mask);
3459 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3461 if (mask_size > 1)
3462 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3463 else
3464 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3467 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3469 shader_glsl_cast(ins, "ivec", "int");
3472 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3474 shader_glsl_cast(ins, "uvec", "uint");
3477 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3479 shader_glsl_cast(ins, "vec", "float");
3482 /** Process signed comparison opcodes in GLSL. */
3483 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3485 struct glsl_src_param src0_param;
3486 struct glsl_src_param src1_param;
3487 DWORD write_mask;
3488 unsigned int mask_size;
3490 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3491 mask_size = shader_glsl_get_write_mask_size(write_mask);
3492 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3493 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3495 if (mask_size > 1) {
3496 const char *compare;
3498 switch(ins->handler_idx)
3500 case WINED3DSIH_SLT: compare = "lessThan"; break;
3501 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3502 default: compare = "";
3503 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3506 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3507 src0_param.param_str, src1_param.param_str);
3508 } else {
3509 switch(ins->handler_idx)
3511 case WINED3DSIH_SLT:
3512 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3513 * to return 0.0 but step returns 1.0 because step is not < x
3514 * An alternative is a bvec compare padded with an unused second component.
3515 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3516 * issue. Playing with not() is not possible either because not() does not accept
3517 * a scalar.
3519 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3520 src0_param.param_str, src1_param.param_str);
3521 break;
3522 case WINED3DSIH_SGE:
3523 /* Here we can use the step() function and safe a conditional */
3524 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3525 break;
3526 default:
3527 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3533 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3535 const char *condition_prefix, *condition_suffix;
3536 struct wined3d_shader_dst_param dst;
3537 struct glsl_src_param src0_param;
3538 struct glsl_src_param src1_param;
3539 struct glsl_src_param src2_param;
3540 BOOL temp_destination = FALSE;
3541 DWORD cmp_channel = 0;
3542 unsigned int i, j;
3543 char mask_char[6];
3544 DWORD write_mask;
3546 switch (ins->handler_idx)
3548 case WINED3DSIH_CMP:
3549 condition_prefix = "";
3550 condition_suffix = " >= 0.0";
3551 break;
3553 case WINED3DSIH_CND:
3554 condition_prefix = "";
3555 condition_suffix = " > 0.5";
3556 break;
3558 case WINED3DSIH_MOVC:
3559 condition_prefix = "bool(";
3560 condition_suffix = ")";
3561 break;
3563 default:
3564 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3565 condition_prefix = "<unhandled prefix>";
3566 condition_suffix = "<unhandled suffix>";
3567 break;
3570 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3572 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3573 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3574 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3575 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3577 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3578 condition_prefix, src0_param.param_str, condition_suffix,
3579 src1_param.param_str, src2_param.param_str);
3580 return;
3583 dst = ins->dst[0];
3585 /* Splitting the instruction up in multiple lines imposes a problem:
3586 * The first lines may overwrite source parameters of the following lines.
3587 * Deal with that by using a temporary destination register if needed. */
3588 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3589 && ins->src[0].reg.type == dst.reg.type)
3590 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3591 && ins->src[1].reg.type == dst.reg.type)
3592 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3593 && ins->src[2].reg.type == dst.reg.type))
3594 temp_destination = TRUE;
3596 /* Cycle through all source0 channels. */
3597 for (i = 0; i < 4; ++i)
3599 write_mask = 0;
3600 /* Find the destination channels which use the current source0 channel. */
3601 for (j = 0; j < 4; ++j)
3603 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3605 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3606 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3609 dst.write_mask = ins->dst[0].write_mask & write_mask;
3611 if (temp_destination)
3613 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3614 continue;
3615 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3617 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3618 continue;
3620 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3621 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3622 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3624 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3625 condition_prefix, src0_param.param_str, condition_suffix,
3626 src1_param.param_str, src2_param.param_str);
3629 if (temp_destination)
3631 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3632 shader_glsl_append_dst(ins->ctx->buffer, ins);
3633 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3637 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3638 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3639 * the compare is done per component of src0. */
3640 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3642 struct glsl_src_param src0_param;
3643 struct glsl_src_param src1_param;
3644 struct glsl_src_param src2_param;
3645 DWORD write_mask;
3646 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3647 ins->ctx->reg_maps->shader_version.minor);
3649 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3651 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3652 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3653 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3654 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3656 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3657 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3658 else
3659 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3660 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3661 return;
3664 shader_glsl_conditional_move(ins);
3667 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3668 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3670 struct glsl_src_param src0_param;
3671 struct glsl_src_param src1_param;
3672 struct glsl_src_param src2_param;
3673 DWORD write_mask;
3675 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3676 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3677 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3678 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3679 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3680 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3683 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3684 Vertex shaders to GLSL codes */
3685 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3687 int i;
3688 int nComponents = 0;
3689 struct wined3d_shader_dst_param tmp_dst = {{0}};
3690 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3691 struct wined3d_shader_instruction tmp_ins;
3693 memset(&tmp_ins, 0, sizeof(tmp_ins));
3695 /* Set constants for the temporary argument */
3696 tmp_ins.ctx = ins->ctx;
3697 tmp_ins.dst_count = 1;
3698 tmp_ins.dst = &tmp_dst;
3699 tmp_ins.src_count = 2;
3700 tmp_ins.src = tmp_src;
3702 switch(ins->handler_idx)
3704 case WINED3DSIH_M4x4:
3705 nComponents = 4;
3706 tmp_ins.handler_idx = WINED3DSIH_DP4;
3707 break;
3708 case WINED3DSIH_M4x3:
3709 nComponents = 3;
3710 tmp_ins.handler_idx = WINED3DSIH_DP4;
3711 break;
3712 case WINED3DSIH_M3x4:
3713 nComponents = 4;
3714 tmp_ins.handler_idx = WINED3DSIH_DP3;
3715 break;
3716 case WINED3DSIH_M3x3:
3717 nComponents = 3;
3718 tmp_ins.handler_idx = WINED3DSIH_DP3;
3719 break;
3720 case WINED3DSIH_M3x2:
3721 nComponents = 2;
3722 tmp_ins.handler_idx = WINED3DSIH_DP3;
3723 break;
3724 default:
3725 break;
3728 tmp_dst = ins->dst[0];
3729 tmp_src[0] = ins->src[0];
3730 tmp_src[1] = ins->src[1];
3731 for (i = 0; i < nComponents; ++i)
3733 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3734 shader_glsl_dot(&tmp_ins);
3735 ++tmp_src[1].reg.idx[0].offset;
3740 The LRP instruction performs a component-wise linear interpolation
3741 between the second and third operands using the first operand as the
3742 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3743 This is equivalent to mix(src2, src1, src0);
3745 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3747 struct glsl_src_param src0_param;
3748 struct glsl_src_param src1_param;
3749 struct glsl_src_param src2_param;
3750 DWORD write_mask;
3752 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3754 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3755 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3756 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3758 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3759 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3762 /** Process the WINED3DSIO_LIT instruction in GLSL:
3763 * dst.x = dst.w = 1.0
3764 * dst.y = (src0.x > 0) ? src0.x
3765 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3766 * where src.w is clamped at +- 128
3768 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3770 struct glsl_src_param src0_param;
3771 struct glsl_src_param src1_param;
3772 struct glsl_src_param src3_param;
3773 char dst_mask[6];
3775 shader_glsl_append_dst(ins->ctx->buffer, ins);
3776 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3778 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3779 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3780 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3782 /* The sdk specifies the instruction like this
3783 * dst.x = 1.0;
3784 * if(src.x > 0.0) dst.y = src.x
3785 * else dst.y = 0.0.
3786 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3787 * else dst.z = 0.0;
3788 * dst.w = 1.0;
3789 * (where power = src.w clamped between -128 and 128)
3791 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3792 * dst.x = 1.0 ... No further explanation needed
3793 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3794 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3795 * dst.w = 1.0. ... Nothing fancy.
3797 * So we still have one conditional in there. So do this:
3798 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3800 * 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),
3801 * 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.
3802 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3804 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3805 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3806 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3808 shader_addline(ins->ctx->buffer,
3809 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3810 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3811 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3812 src0_param.param_str, src3_param.param_str, dst_mask);
3815 /** Process the WINED3DSIO_DST instruction in GLSL:
3816 * dst.x = 1.0
3817 * dst.y = src0.x * src0.y
3818 * dst.z = src0.z
3819 * dst.w = src1.w
3821 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3823 struct glsl_src_param src0y_param;
3824 struct glsl_src_param src0z_param;
3825 struct glsl_src_param src1y_param;
3826 struct glsl_src_param src1w_param;
3827 char dst_mask[6];
3829 shader_glsl_append_dst(ins->ctx->buffer, ins);
3830 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3832 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3833 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3834 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3835 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3837 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3838 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3841 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3842 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3843 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3845 * dst.x = cos(src0.?)
3846 * dst.y = sin(src0.?)
3847 * dst.z = dst.z
3848 * dst.w = dst.w
3850 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3852 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3853 struct glsl_src_param src0_param;
3854 DWORD write_mask;
3856 if (ins->ctx->reg_maps->shader_version.major < 4)
3858 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3860 write_mask = shader_glsl_append_dst(buffer, ins);
3861 switch (write_mask)
3863 case WINED3DSP_WRITEMASK_0:
3864 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3865 break;
3867 case WINED3DSP_WRITEMASK_1:
3868 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3869 break;
3871 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3872 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3873 src0_param.param_str, src0_param.param_str);
3874 break;
3876 default:
3877 ERR("Write mask should be .x, .y or .xy\n");
3878 break;
3881 return;
3884 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3887 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3889 char dst_mask[6];
3891 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3892 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3893 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3895 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3896 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3897 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3899 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3900 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3902 else
3904 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3905 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3906 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3909 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3911 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3912 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3913 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3917 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3918 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3919 * generate invalid code
3921 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3923 struct glsl_src_param src0_param;
3924 DWORD write_mask;
3926 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3927 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3929 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3932 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3933 * Start a for() loop where src1.y is the initial value of aL,
3934 * increment aL by src1.z for a total of src1.x iterations.
3935 * Need to use a temporary variable for this operation.
3937 /* FIXME: I don't think nested loops will work correctly this way. */
3938 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3940 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3941 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3942 const struct wined3d_shader *shader = ins->ctx->shader;
3943 const struct wined3d_shader_lconst *constant;
3944 struct glsl_src_param src1_param;
3945 const DWORD *control_values = NULL;
3947 if (ins->ctx->reg_maps->shader_version.major < 4)
3949 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3951 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3952 * class hardware doesn't support real varying indexing, but Microsoft
3953 * designed this feature for Shader model 2.x+. If the loop control is
3954 * known at compile time, the GLSL compiler can unroll the loop, and
3955 * replace indirect addressing with direct addressing. */
3956 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3958 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3960 if (constant->idx == ins->src[1].reg.idx[0].offset)
3962 control_values = constant->value;
3963 break;
3968 if (control_values)
3970 struct wined3d_shader_loop_control loop_control;
3971 loop_control.count = control_values[0];
3972 loop_control.start = control_values[1];
3973 loop_control.step = (int)control_values[2];
3975 if (loop_control.step > 0)
3977 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3978 loop_state->current_depth, loop_control.start,
3979 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3980 loop_state->current_depth, loop_control.step);
3982 else if (loop_control.step < 0)
3984 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3985 loop_state->current_depth, loop_control.start,
3986 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3987 loop_state->current_depth, loop_control.step);
3989 else
3991 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3992 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3993 loop_state->current_depth, loop_control.count,
3994 loop_state->current_depth);
3997 else
3999 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4000 loop_state->current_depth, loop_state->current_reg,
4001 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4002 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4005 ++loop_state->current_reg;
4007 else
4009 shader_addline(buffer, "for (;;)\n{\n");
4012 ++loop_state->current_depth;
4015 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4017 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4019 shader_addline(ins->ctx->buffer, "}\n");
4021 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4023 --loop_state->current_depth;
4024 --loop_state->current_reg;
4027 if (ins->handler_idx == WINED3DSIH_ENDREP)
4029 --loop_state->current_depth;
4033 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4035 const struct wined3d_shader *shader = ins->ctx->shader;
4036 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4037 const struct wined3d_shader_lconst *constant;
4038 struct glsl_src_param src0_param;
4039 const DWORD *control_values = NULL;
4041 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4042 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4044 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4046 if (constant->idx == ins->src[0].reg.idx[0].offset)
4048 control_values = constant->value;
4049 break;
4054 if (control_values)
4056 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4057 loop_state->current_depth, loop_state->current_depth,
4058 control_values[0], loop_state->current_depth);
4060 else
4062 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4063 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4064 loop_state->current_depth, loop_state->current_depth,
4065 src0_param.param_str, loop_state->current_depth);
4068 ++loop_state->current_depth;
4071 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4073 struct glsl_src_param src0_param;
4075 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4076 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
4079 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4081 struct glsl_src_param src0_param;
4082 struct glsl_src_param src1_param;
4084 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4085 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4087 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4088 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4091 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4093 shader_addline(ins->ctx->buffer, "} else {\n");
4096 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4098 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4101 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4103 shader_addline(ins->ctx->buffer, "break;\n");
4106 /* FIXME: According to MSDN the compare is done per component. */
4107 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4109 struct glsl_src_param src0_param;
4110 struct glsl_src_param src1_param;
4112 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4113 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4115 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4116 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4119 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4121 struct glsl_src_param src_param;
4123 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4124 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4127 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4129 shader_addline(ins->ctx->buffer, "}\n");
4130 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4133 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4135 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4138 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4140 struct glsl_src_param src1_param;
4142 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4143 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4144 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4147 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4149 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4150 * function only suppresses the unhandled instruction warning
4154 /*********************************************
4155 * Pixel Shader Specific Code begins here
4156 ********************************************/
4157 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4159 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4160 ins->ctx->reg_maps->shader_version.minor);
4161 struct glsl_sample_function sample_function;
4162 DWORD sample_flags = 0;
4163 DWORD resource_idx;
4164 DWORD mask = 0, swizzle;
4165 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4167 /* 1.0-1.4: Use destination register as sampler source.
4168 * 2.0+: Use provided sampler source. */
4169 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4170 resource_idx = ins->dst[0].reg.idx[0].offset;
4171 else
4172 resource_idx = ins->src[1].reg.idx[0].offset;
4174 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4176 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4177 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4178 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4180 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4181 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4183 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4184 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4186 case WINED3D_TTFF_COUNT1:
4187 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4188 break;
4189 case WINED3D_TTFF_COUNT2:
4190 mask = WINED3DSP_WRITEMASK_1;
4191 break;
4192 case WINED3D_TTFF_COUNT3:
4193 mask = WINED3DSP_WRITEMASK_2;
4194 break;
4195 case WINED3D_TTFF_COUNT4:
4196 case WINED3D_TTFF_DISABLE:
4197 mask = WINED3DSP_WRITEMASK_3;
4198 break;
4202 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4204 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4206 if (src_mod == WINED3DSPSM_DZ) {
4207 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4208 mask = WINED3DSP_WRITEMASK_2;
4209 } else if (src_mod == WINED3DSPSM_DW) {
4210 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4211 mask = WINED3DSP_WRITEMASK_3;
4214 else
4216 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4217 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4219 /* ps 2.0 texldp instruction always divides by the fourth component. */
4220 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4221 mask = WINED3DSP_WRITEMASK_3;
4225 if (priv->cur_ps_args->np2_fixup & (1u << resource_idx))
4226 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4228 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4229 mask |= sample_function.coord_mask;
4230 sample_function.coord_mask = mask;
4232 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4233 else swizzle = ins->src[1].swizzle;
4235 /* 1.0-1.3: Use destination register as coordinate source.
4236 1.4+: Use provided coordinate source register. */
4237 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4239 char coord_mask[6];
4240 shader_glsl_write_mask_to_str(mask, coord_mask);
4241 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4242 "T%u%s", resource_idx, coord_mask);
4244 else
4246 struct glsl_src_param coord_param;
4247 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4248 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4250 struct glsl_src_param bias;
4251 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4252 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4253 NULL, "%s", coord_param.param_str);
4254 } else {
4255 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4256 "%s", coord_param.param_str);
4259 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4262 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4264 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4265 struct glsl_src_param coord_param, dx_param, dy_param;
4266 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
4267 struct glsl_sample_function sample_function;
4268 DWORD sampler_idx;
4269 DWORD swizzle = ins->src[1].swizzle;
4270 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4272 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4274 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4275 shader_glsl_tex(ins);
4276 return;
4279 sampler_idx = ins->src[1].reg.idx[0].offset;
4280 if (priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4281 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4283 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, sample_flags, &sample_function);
4284 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4285 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4286 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4288 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4289 NULL, NULL, "%s", coord_param.param_str);
4290 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4293 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4295 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4296 struct glsl_src_param coord_param, lod_param;
4297 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4298 struct glsl_sample_function sample_function;
4299 DWORD sampler_idx;
4300 DWORD swizzle = ins->src[1].swizzle;
4301 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4303 sampler_idx = ins->src[1].reg.idx[0].offset;
4304 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4305 && priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4306 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4308 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, sample_flags, &sample_function);
4309 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4311 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4313 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4314 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4316 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4317 * However, the NVIDIA drivers allow them in fragment shaders as well,
4318 * even without the appropriate extension. */
4319 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4321 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4322 "%s", coord_param.param_str);
4323 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4326 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4327 unsigned int resource_idx, unsigned int sampler_idx)
4329 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4330 unsigned int i;
4332 for (i = 0; i < sampler_map->count; ++i)
4334 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4335 return entries[i].bind_idx;
4338 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4340 return ~0u;
4343 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4345 static const unsigned int texture_size_component_count[] =
4347 0, /* WINED3D_SHADER_RESOURCE_NONE */
4348 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4349 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4350 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4351 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4352 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4353 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4354 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4355 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4356 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4359 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4360 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4361 enum wined3d_shader_resource_type resource_type;
4362 unsigned int resource_idx, sampler_bind_idx, i;
4363 enum wined3d_data_type dst_data_type;
4364 struct glsl_src_param lod_param;
4365 char dst_swizzle[6];
4366 DWORD write_mask;
4368 dst_data_type = ins->dst[0].reg.data_type;
4369 if (ins->flags == WINED3DSI_RESINFO_UINT)
4370 dst_data_type = WINED3D_DATA_UINT;
4371 else if (ins->flags)
4372 FIXME("Unhandled flags %#x.\n", ins->flags);
4374 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4375 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4377 resource_idx = ins->src[1].reg.idx[0].offset;
4378 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4379 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4380 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4381 resource_idx, WINED3D_SAMPLER_DEFAULT);
4383 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4385 ERR("Unexpected resource type %#x.\n", resource_type);
4386 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4389 if (dst_data_type == WINED3D_DATA_UINT)
4390 shader_addline(ins->ctx->buffer, "uvec4(");
4391 else
4392 shader_addline(ins->ctx->buffer, "vec4(");
4394 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4395 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4397 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4398 shader_addline(ins->ctx->buffer, "0, ");
4400 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4402 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4403 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4405 else
4407 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4408 shader_addline(ins->ctx->buffer, "1");
4411 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4414 /* FIXME: The current implementation does not handle multisample textures correctly. */
4415 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4417 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4418 struct glsl_src_param coord_param, lod_param;
4419 struct glsl_sample_function sample_function;
4420 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4422 if (wined3d_shader_instruction_has_texel_offset(ins))
4423 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4425 resource_idx = ins->src[1].reg.idx[0].offset;
4426 sampler_idx = WINED3D_SAMPLER_DEFAULT;
4428 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4429 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4430 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4431 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4432 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4433 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4434 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4437 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4439 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4440 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4441 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4442 struct glsl_sample_function sample_function;
4443 DWORD flags = 0;
4445 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4446 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4447 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4448 flags |= WINED3D_GLSL_SAMPLE_LOD;
4449 if (wined3d_shader_instruction_has_texel_offset(ins))
4450 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4452 resource_idx = ins->src[1].reg.idx[0].offset;
4453 sampler_idx = ins->src[2].reg.idx[0].offset;
4455 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4456 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4458 switch (ins->handler_idx)
4460 case WINED3DSIH_SAMPLE:
4461 break;
4462 case WINED3DSIH_SAMPLE_B:
4463 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4464 lod_param_str = lod_param.param_str;
4465 break;
4466 case WINED3DSIH_SAMPLE_GRAD:
4467 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dx_param);
4468 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.coord_mask, &dy_param);
4469 dx_param_str = dx_param.param_str;
4470 dy_param_str = dy_param.param_str;
4471 break;
4472 case WINED3DSIH_SAMPLE_LOD:
4473 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4474 lod_param_str = lod_param.param_str;
4475 break;
4476 default:
4477 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4478 break;
4481 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4482 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4483 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4484 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4487 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
4489 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4490 struct glsl_src_param coord_param, compare_param;
4491 struct glsl_sample_function sample_function;
4492 const char *lod_param = NULL;
4493 DWORD flags = 0;
4494 UINT coord_size;
4496 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
4498 lod_param = "0";
4499 flags |= WINED3D_GLSL_SAMPLE_LOD;
4502 if (wined3d_shader_instruction_has_texel_offset(ins))
4503 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4505 resource_idx = ins->src[1].reg.idx[0].offset;
4506 sampler_idx = ins->src[2].reg.idx[0].offset;
4508 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4509 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4510 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
4511 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
4512 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4513 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4514 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
4515 coord_size, coord_param.param_str, compare_param.param_str);
4516 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4519 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4521 /* FIXME: Make this work for more than just 2D textures */
4522 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4523 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4525 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4527 char dst_mask[6];
4529 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4530 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4531 ins->dst[0].reg.idx[0].offset, dst_mask);
4533 else
4535 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4536 DWORD reg = ins->src[0].reg.idx[0].offset;
4537 char dst_swizzle[6];
4539 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4541 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4543 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4544 struct glsl_src_param div_param;
4545 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4547 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4549 if (mask_size > 1)
4550 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4551 else
4552 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4554 else
4556 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4561 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4562 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4563 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4564 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4566 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4567 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4568 struct glsl_sample_function sample_function;
4569 struct glsl_src_param src0_param;
4570 UINT mask_size;
4572 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4574 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4575 * scalar, and projected sampling would require 4.
4577 * It is a dependent read - not valid with conditional NP2 textures
4579 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4580 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4582 switch(mask_size)
4584 case 1:
4585 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4586 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4587 break;
4589 case 2:
4590 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4591 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4592 break;
4594 case 3:
4595 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4596 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4597 break;
4599 default:
4600 FIXME("Unexpected mask size %u\n", mask_size);
4601 break;
4603 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4606 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4607 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4608 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4610 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4611 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4612 struct glsl_src_param src0_param;
4613 DWORD dst_mask;
4614 unsigned int mask_size;
4616 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4617 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4618 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4620 if (mask_size > 1) {
4621 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4622 } else {
4623 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4627 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4628 * Calculate the depth as dst.x / dst.y */
4629 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4631 struct glsl_dst_param dst_param;
4633 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4635 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4636 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4637 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4638 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4639 * >= 1.0 or < 0.0
4641 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4642 dst_param.reg_name, dst_param.reg_name);
4645 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4646 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4647 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4648 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4650 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4652 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4653 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4654 struct glsl_src_param src0_param;
4656 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4658 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4659 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4662 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4663 * Calculate the 1st of a 2-row matrix multiplication. */
4664 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4666 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4667 DWORD reg = ins->dst[0].reg.idx[0].offset;
4668 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4669 struct glsl_src_param src0_param;
4671 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4672 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4675 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4676 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4677 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4679 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4680 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4681 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4682 DWORD reg = ins->dst[0].reg.idx[0].offset;
4683 struct glsl_src_param src0_param;
4685 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4686 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4687 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4690 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4692 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4693 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4694 struct glsl_sample_function sample_function;
4695 DWORD reg = ins->dst[0].reg.idx[0].offset;
4696 struct glsl_src_param src0_param;
4698 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4699 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4701 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4703 /* Sample the texture using the calculated coordinates */
4704 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
4705 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4708 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4709 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4710 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4712 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4713 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4714 struct glsl_sample_function sample_function;
4715 DWORD reg = ins->dst[0].reg.idx[0].offset;
4716 struct glsl_src_param src0_param;
4718 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4719 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4721 /* Dependent read, not valid with conditional NP2 */
4722 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4724 /* Sample the texture using the calculated coordinates */
4725 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
4726 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4728 tex_mx->current_row = 0;
4731 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4732 * Perform the 3rd row of a 3x3 matrix multiply */
4733 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4735 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4736 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4737 DWORD reg = ins->dst[0].reg.idx[0].offset;
4738 struct glsl_src_param src0_param;
4739 char dst_mask[6];
4741 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4743 shader_glsl_append_dst(ins->ctx->buffer, ins);
4744 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4745 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4747 tex_mx->current_row = 0;
4750 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4751 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4752 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4754 struct glsl_src_param src0_param;
4755 struct glsl_src_param src1_param;
4756 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4757 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4758 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4759 struct glsl_sample_function sample_function;
4760 DWORD reg = ins->dst[0].reg.idx[0].offset;
4761 char coord_mask[6];
4763 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4764 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4766 /* Perform the last matrix multiply operation */
4767 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4768 /* Reflection calculation */
4769 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4771 /* Dependent read, not valid with conditional NP2 */
4772 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4773 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4775 /* Sample the texture */
4776 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4777 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4778 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4780 tex_mx->current_row = 0;
4783 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4784 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4785 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4787 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4788 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4789 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4790 struct glsl_sample_function sample_function;
4791 DWORD reg = ins->dst[0].reg.idx[0].offset;
4792 struct glsl_src_param src0_param;
4793 char coord_mask[6];
4795 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4797 /* Perform the last matrix multiply operation */
4798 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4800 /* Construct the eye-ray vector from w coordinates */
4801 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4802 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4803 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4805 /* Dependent read, not valid with conditional NP2 */
4806 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4807 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4809 /* Sample the texture using the calculated coordinates */
4810 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4811 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4812 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4814 tex_mx->current_row = 0;
4817 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4818 * Apply a fake bump map transform.
4819 * texbem is pshader <= 1.3 only, this saves a few version checks
4821 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4823 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4824 struct glsl_sample_function sample_function;
4825 struct glsl_src_param coord_param;
4826 DWORD sampler_idx;
4827 DWORD mask;
4828 DWORD flags;
4829 char coord_mask[6];
4831 sampler_idx = ins->dst[0].reg.idx[0].offset;
4832 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4833 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4835 /* Dependent read, not valid with conditional NP2 */
4836 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4837 mask = sample_function.coord_mask;
4839 shader_glsl_write_mask_to_str(mask, coord_mask);
4841 /* With projected textures, texbem only divides the static texture coord,
4842 * not the displacement, so we can't let GL handle this. */
4843 if (flags & WINED3D_PSARGS_PROJECTED)
4845 DWORD div_mask=0;
4846 char coord_div_mask[3];
4847 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4849 case WINED3D_TTFF_COUNT1:
4850 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4851 break;
4852 case WINED3D_TTFF_COUNT2:
4853 div_mask = WINED3DSP_WRITEMASK_1;
4854 break;
4855 case WINED3D_TTFF_COUNT3:
4856 div_mask = WINED3DSP_WRITEMASK_2;
4857 break;
4858 case WINED3D_TTFF_COUNT4:
4859 case WINED3D_TTFF_DISABLE:
4860 div_mask = WINED3DSP_WRITEMASK_3;
4861 break;
4863 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4864 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4867 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4869 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4870 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4871 coord_param.param_str, coord_mask);
4873 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4875 struct glsl_src_param luminance_param;
4876 struct glsl_dst_param dst_param;
4878 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4879 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4881 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4882 dst_param.reg_name, dst_param.mask_str,
4883 luminance_param.param_str, sampler_idx, sampler_idx);
4885 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4888 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4890 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4891 struct glsl_src_param src0_param, src1_param;
4893 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4894 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4896 shader_glsl_append_dst(ins->ctx->buffer, ins);
4897 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4898 src0_param.param_str, sampler_idx, src1_param.param_str);
4901 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4902 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4903 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4905 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4906 struct glsl_sample_function sample_function;
4907 struct glsl_src_param src0_param;
4909 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4911 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4912 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4913 "%s.wx", src0_param.reg_name);
4914 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4917 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4918 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4919 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4921 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4922 struct glsl_sample_function sample_function;
4923 struct glsl_src_param src0_param;
4925 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4927 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4928 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4929 "%s.yz", src0_param.reg_name);
4930 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4933 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4934 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4935 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4937 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4938 struct glsl_sample_function sample_function;
4939 struct glsl_src_param src0_param;
4941 /* Dependent read, not valid with conditional NP2 */
4942 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4943 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4945 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4946 "%s", src0_param.param_str);
4947 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4950 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4951 * If any of the first 3 components are < 0, discard this pixel */
4952 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4954 if (ins->ctx->reg_maps->shader_version.major >= 4)
4956 struct glsl_src_param src_param;
4958 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4959 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4961 else
4963 struct glsl_dst_param dst_param;
4965 /* The argument is a destination parameter, and no writemasks are allowed */
4966 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4968 /* 2.0 shaders compare all 4 components in texkill. */
4969 if (ins->ctx->reg_maps->shader_version.major >= 2)
4970 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4971 /* 1.x shaders only compare the first 3 components, probably due to
4972 * the nature of the texkill instruction as a tex* instruction, and
4973 * phase, which kills all .w components. Even if all 4 components are
4974 * defined, only the first 3 are used. */
4975 else
4976 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4980 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4981 * dst = dot2(src0, src1) + src2 */
4982 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4984 struct glsl_src_param src0_param;
4985 struct glsl_src_param src1_param;
4986 struct glsl_src_param src2_param;
4987 DWORD write_mask;
4988 unsigned int mask_size;
4990 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4991 mask_size = shader_glsl_get_write_mask_size(write_mask);
4993 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4994 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4995 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4997 if (mask_size > 1) {
4998 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4999 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5000 } else {
5001 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5002 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5006 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5007 const struct wined3d_shader_signature *input_signature,
5008 const struct wined3d_shader_reg_maps *reg_maps,
5009 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5011 unsigned int i;
5013 for (i = 0; i < input_signature->element_count; ++i)
5015 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5016 const char *semantic_name;
5017 UINT semantic_idx;
5018 char reg_mask[6];
5020 /* Unused */
5021 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5022 continue;
5024 semantic_name = input->semantic_name;
5025 semantic_idx = input->semantic_idx;
5026 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5028 if (args->vp_mode == vertexshader)
5030 if (input->sysval_semantic == WINED3D_SV_POSITION)
5031 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5032 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5033 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5034 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5035 else
5036 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5037 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5038 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5040 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5042 if (args->pointsprite)
5043 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5044 shader->u.ps.input_reg_map[input->register_idx]);
5045 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5046 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5047 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5048 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5049 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5050 else
5051 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5052 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5054 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5056 if (!semantic_idx)
5057 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5058 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5059 else if (semantic_idx == 1)
5060 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5061 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5062 else
5063 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5064 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5066 else
5068 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5069 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5074 /*********************************************
5075 * Vertex Shader Specific Code begins here
5076 ********************************************/
5078 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5080 struct glsl_program_key key;
5082 key.vs_id = entry->vs.id;
5083 key.gs_id = entry->gs.id;
5084 key.ps_id = entry->ps.id;
5086 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5088 ERR("Failed to insert program entry.\n");
5092 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5093 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5095 struct wine_rb_entry *entry;
5096 struct glsl_program_key key;
5098 key.vs_id = vs_id;
5099 key.gs_id = gs_id;
5100 key.ps_id = ps_id;
5102 entry = wine_rb_get(&priv->program_lookup, &key);
5103 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5106 /* Context activation is done by the caller. */
5107 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5108 struct glsl_shader_prog_link *entry)
5110 struct glsl_program_key key;
5112 key.vs_id = entry->vs.id;
5113 key.gs_id = entry->gs.id;
5114 key.ps_id = entry->ps.id;
5115 wine_rb_remove(&priv->program_lookup, &key);
5117 GL_EXTCALL(glDeleteProgram(entry->id));
5118 if (entry->vs.id)
5119 list_remove(&entry->vs.shader_entry);
5120 if (entry->gs.id)
5121 list_remove(&entry->gs.shader_entry);
5122 if (entry->ps.id)
5123 list_remove(&entry->ps.shader_entry);
5124 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
5125 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
5126 HeapFree(GetProcessHeap(), 0, entry);
5129 static void handle_ps3_input(struct shader_glsl_priv *priv,
5130 const struct wined3d_gl_info *gl_info, const DWORD *map,
5131 const struct wined3d_shader_signature *input_signature,
5132 const struct wined3d_shader_reg_maps *reg_maps_in,
5133 const struct wined3d_shader_signature *output_signature,
5134 const struct wined3d_shader_reg_maps *reg_maps_out)
5136 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5137 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5138 unsigned int i, j;
5139 DWORD *set;
5140 DWORD in_idx;
5141 unsigned int in_count = vec4_varyings(3, gl_info);
5142 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5143 char reg_mask[6];
5144 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5146 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * max_varyings);
5148 for (i = 0; i < input_signature->element_count; ++i)
5150 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5152 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5153 continue;
5155 in_idx = map[input->register_idx];
5156 /* Declared, but not read register */
5157 if (in_idx == ~0u)
5158 continue;
5159 if (in_idx >= max_varyings)
5161 FIXME("More input varyings declared than supported, expect issues.\n");
5162 continue;
5165 if (in_idx == in_count)
5166 string_buffer_sprintf(destination, "gl_FrontColor");
5167 else if (in_idx == in_count + 1)
5168 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5169 else
5170 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
5172 if (!set[in_idx])
5173 set[in_idx] = ~0u;
5175 for (j = 0; j < output_signature->element_count; ++j)
5177 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5178 DWORD mask;
5180 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5181 || input->semantic_idx != output->semantic_idx
5182 || strcmp(input->semantic_name, output->semantic_name)
5183 || !(mask = input->mask & output->mask))
5184 continue;
5186 if (set[in_idx] == ~0u)
5187 set[in_idx] = 0;
5188 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5189 shader_glsl_write_mask_to_str(mask, reg_mask);
5191 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
5192 destination->buffer, reg_mask, output->register_idx, reg_mask);
5196 for (i = 0; i < max_varyings; ++i)
5198 unsigned int size;
5200 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5201 continue;
5203 if (set[i] == ~0U) set[i] = 0;
5205 size = 0;
5206 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
5207 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
5208 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
5209 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
5210 reg_mask[size] = '\0';
5212 if (i == in_count)
5213 string_buffer_sprintf(destination, "gl_FrontColor");
5214 else if (i == in_count + 1)
5215 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5216 else
5217 string_buffer_sprintf(destination, "ps_link[%u]", i);
5219 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5220 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5223 HeapFree(GetProcessHeap(), 0, set);
5224 string_buffer_release(&priv->string_buffers, destination);
5227 /* Context activation is done by the caller. */
5228 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
5229 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5230 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5232 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5233 GLuint ret;
5234 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5235 unsigned int i;
5236 const char *semantic_name;
5237 UINT semantic_idx;
5238 char reg_mask[6];
5239 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5241 string_buffer_clear(buffer);
5243 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5245 if (per_vertex_point_size)
5247 shader_addline(buffer, "uniform struct\n{\n");
5248 shader_addline(buffer, " float size_min;\n");
5249 shader_addline(buffer, " float size_max;\n");
5250 shader_addline(buffer, "} ffp_point;\n");
5253 if (ps_major < 3)
5255 DWORD colors_written_mask[2] = {0};
5256 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5258 if (!legacy_context)
5260 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5261 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5262 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5263 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5266 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5268 for (i = 0; i < vs->output_signature.element_count; ++i)
5270 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5271 DWORD write_mask;
5273 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5274 continue;
5276 semantic_name = output->semantic_name;
5277 semantic_idx = output->semantic_idx;
5278 write_mask = output->mask;
5279 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5281 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5283 if (legacy_context)
5284 shader_addline(buffer, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5285 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5286 else
5287 shader_addline(buffer, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5288 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5290 colors_written_mask[semantic_idx] = write_mask;
5292 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5294 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5295 reg_mask, output->register_idx, reg_mask);
5297 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5299 if (semantic_idx < MAX_TEXTURES)
5301 shader_addline(buffer, "%s[%u]%s = vs_out[%u]%s;\n",
5302 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5303 semantic_idx, reg_mask, output->register_idx, reg_mask);
5304 texcoords_written_mask[semantic_idx] = write_mask;
5307 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5309 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5310 output->register_idx, reg_mask[1]);
5312 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5314 shader_addline(buffer, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5315 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5316 output->register_idx, reg_mask[1]);
5320 for (i = 0; i < 2; ++i)
5322 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5324 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5325 if (!i)
5326 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5327 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5328 reg_mask, reg_mask);
5329 else
5330 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5331 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5332 reg_mask, reg_mask);
5335 for (i = 0; i < MAX_TEXTURES; ++i)
5337 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5338 continue;
5340 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5342 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5343 && !texcoords_written_mask[i])
5344 continue;
5346 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5347 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5348 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5352 else
5354 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5356 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5357 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5359 /* First, sort out position and point size. Those are not passed to the pixel shader */
5360 for (i = 0; i < vs->output_signature.element_count; ++i)
5362 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5364 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5365 continue;
5367 semantic_name = output->semantic_name;
5368 semantic_idx = output->semantic_idx;
5369 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5371 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5373 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5374 reg_mask, output->register_idx, reg_mask);
5376 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5378 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5379 output->register_idx, reg_mask[1]);
5383 /* Then, fix the pixel shader input */
5384 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5385 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
5388 shader_addline(buffer, "}\n");
5390 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5391 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5392 shader_glsl_compile(gl_info, ret, buffer->buffer);
5394 return ret;
5397 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
5399 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
5400 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5401 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
5402 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
5403 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
5404 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
5407 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
5409 switch (mode)
5411 case WINED3D_FFP_PS_FOG_OFF:
5412 return;
5414 case WINED3D_FFP_PS_FOG_LINEAR:
5415 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5416 break;
5418 case WINED3D_FFP_PS_FOG_EXP:
5419 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5420 break;
5422 case WINED3D_FFP_PS_FOG_EXP2:
5423 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5424 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5425 break;
5427 default:
5428 ERR("Invalid fog mode %#x.\n", mode);
5429 return;
5432 shader_addline(buffer, "gl_FragData[0].xyz = mix(ffp_fog.color.xyz, gl_FragData[0].xyz,"
5433 " clamp(fog, 0.0, 1.0));\n");
5436 /* Context activation is done by the caller. */
5437 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5438 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5439 const struct wined3d_shader *shader,
5440 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5442 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5443 const struct wined3d_gl_info *gl_info = context->gl_info;
5444 const DWORD *function = shader->function;
5445 struct shader_glsl_ctx_priv priv_ctx;
5446 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5448 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5449 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5451 memset(&priv_ctx, 0, sizeof(priv_ctx));
5452 priv_ctx.cur_ps_args = args;
5453 priv_ctx.cur_np2fixup_info = np2fixup_info;
5454 priv_ctx.string_buffers = string_buffers;
5456 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5458 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5459 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5460 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5461 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5462 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5463 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5464 /* The spec says that it doesn't have to be explicitly enabled, but the
5465 * nvidia drivers write a warning if we don't do so. */
5466 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5467 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5468 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5469 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5470 if (gl_info->supported[EXT_GPU_SHADER4])
5471 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5473 /* Base Declarations */
5474 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5476 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5478 unsigned int i;
5479 WORD map = reg_maps->texcoord;
5481 if (legacy_context)
5483 if (glsl_is_color_reg_read(shader, 0))
5484 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5485 if (glsl_is_color_reg_read(shader, 1))
5486 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5489 for (i = 0; map; map >>= 1, ++i)
5491 if (map & 1)
5493 if (args->pointsprite)
5494 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5495 else if (args->texcoords_initialized & (1u << i))
5496 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5497 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5498 else
5499 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5500 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5504 if (legacy_context)
5505 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5508 /* Pack 3.0 inputs */
5509 if (reg_maps->shader_version.major >= 3)
5510 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5512 /* Base Shader Body */
5513 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5515 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5516 if (reg_maps->shader_version.major < 2)
5518 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5519 shader_addline(buffer, "gl_FragData[0] = R0;\n");
5522 if (args->srgb_correction)
5523 shader_glsl_generate_srgb_write_correction(buffer);
5525 /* SM < 3 does not replace the fog stage. */
5526 if (reg_maps->shader_version.major < 3)
5527 shader_glsl_generate_fog_code(buffer, args->fog);
5529 shader_addline(buffer, "}\n");
5531 TRACE("Compiling shader object %u.\n", shader_id);
5532 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5534 return shader_id;
5537 /* Context activation is done by the caller. */
5538 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5539 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5540 const struct wined3d_shader *shader,
5541 const struct vs_compile_args *args)
5543 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5544 const struct wined3d_gl_info *gl_info = context->gl_info;
5545 const DWORD *function = shader->function;
5546 struct shader_glsl_ctx_priv priv_ctx;
5547 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5549 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5550 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5552 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5554 if (gl_info->supported[ARB_DRAW_INSTANCED])
5555 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5556 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5557 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5558 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5559 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5560 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5561 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5562 if (gl_info->supported[EXT_GPU_SHADER4])
5563 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5565 memset(&priv_ctx, 0, sizeof(priv_ctx));
5566 priv_ctx.cur_vs_args = args;
5567 priv_ctx.string_buffers = string_buffers;
5569 /* Base Declarations */
5570 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5572 /* Base Shader Body */
5573 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5575 /* Unpack outputs */
5576 shader_addline(buffer, "order_ps_input(vs_out);\n");
5578 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5579 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5580 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5581 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5583 if (reg_maps->shader_version.major < 3)
5585 if (args->fog_src == VS_FOG_Z)
5586 shader_addline(buffer, "%s = gl_Position.z;\n",
5587 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5588 else if (!reg_maps->fog)
5589 shader_addline(buffer, "%s = 0.0;\n",
5590 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5593 /* We always store the clipplanes without y inversion */
5594 if (args->clip_enabled)
5595 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5597 if (args->point_size && !args->per_vertex_point_size)
5598 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5600 /* Write the final position.
5602 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5603 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5604 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5605 * contains 1.0 to allow a mad.
5607 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5608 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5610 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5612 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5613 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5614 * which is the same as z = z * 2 - w.
5616 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5618 shader_addline(buffer, "}\n");
5620 TRACE("Compiling shader object %u.\n", shader_id);
5621 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5623 return shader_id;
5626 /* Context activation is done by the caller. */
5627 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5628 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5629 const struct wined3d_shader *shader)
5631 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5632 const struct wined3d_gl_info *gl_info = context->gl_info;
5633 const DWORD *function = shader->function;
5634 struct shader_glsl_ctx_priv priv_ctx;
5635 GLuint shader_id;
5637 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5639 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5641 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5642 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5643 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5644 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5645 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5646 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5647 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5648 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5649 if (gl_info->supported[EXT_GPU_SHADER4])
5650 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5652 memset(&priv_ctx, 0, sizeof(priv_ctx));
5653 priv_ctx.string_buffers = string_buffers;
5654 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5655 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5656 shader_addline(buffer, "}\n");
5658 TRACE("Compiling shader object %u.\n", shader_id);
5659 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5661 return shader_id;
5664 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5665 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5666 struct wined3d_shader *shader,
5667 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5669 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5670 struct glsl_shader_private *shader_data;
5671 struct ps_np2fixup_info *np2fixup;
5672 UINT i;
5673 DWORD new_size;
5674 GLuint ret;
5676 if (!shader->backend_data)
5678 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5679 if (!shader->backend_data)
5681 ERR("Failed to allocate backend data.\n");
5682 return 0;
5685 shader_data = shader->backend_data;
5686 gl_shaders = shader_data->gl_shaders.ps;
5688 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5689 * so a linear search is more performant than a hashmap or a binary search
5690 * (cache coherency etc)
5692 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5694 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5696 if (args->np2_fixup)
5697 *np2fixup_info = &gl_shaders[i].np2fixup;
5698 return gl_shaders[i].id;
5702 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5703 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5704 if (shader_data->num_gl_shaders)
5706 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5707 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5708 new_size * sizeof(*gl_shaders));
5710 else
5712 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5713 new_size = 1;
5716 if(!new_array) {
5717 ERR("Out of memory\n");
5718 return 0;
5720 shader_data->gl_shaders.ps = new_array;
5721 shader_data->shader_array_size = new_size;
5722 gl_shaders = new_array;
5725 gl_shaders[shader_data->num_gl_shaders].args = *args;
5727 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5728 memset(np2fixup, 0, sizeof(*np2fixup));
5729 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5731 pixelshader_update_resource_types(shader, args->tex_types);
5733 string_buffer_clear(buffer);
5734 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5735 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5737 return ret;
5740 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5741 const DWORD use_map)
5743 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5744 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5745 if (stored->point_size != new->point_size)
5746 return FALSE;
5747 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5748 return FALSE;
5749 if (stored->flatshading != new->flatshading)
5750 return FALSE;
5751 return stored->fog_src == new->fog_src;
5754 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5755 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5756 struct wined3d_shader *shader,
5757 const struct vs_compile_args *args)
5759 UINT i;
5760 DWORD new_size;
5761 DWORD use_map = context->stream_info.use_map;
5762 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5763 struct glsl_shader_private *shader_data;
5764 GLuint ret;
5766 if (!shader->backend_data)
5768 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5769 if (!shader->backend_data)
5771 ERR("Failed to allocate backend data.\n");
5772 return 0;
5775 shader_data = shader->backend_data;
5776 gl_shaders = shader_data->gl_shaders.vs;
5778 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5779 * so a linear search is more performant than a hashmap or a binary search
5780 * (cache coherency etc)
5782 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5784 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5785 return gl_shaders[i].id;
5788 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5790 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5791 if (shader_data->num_gl_shaders)
5793 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5794 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5795 new_size * sizeof(*gl_shaders));
5797 else
5799 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5800 new_size = 1;
5803 if(!new_array) {
5804 ERR("Out of memory\n");
5805 return 0;
5807 shader_data->gl_shaders.vs = new_array;
5808 shader_data->shader_array_size = new_size;
5809 gl_shaders = new_array;
5812 gl_shaders[shader_data->num_gl_shaders].args = *args;
5814 string_buffer_clear(buffer);
5815 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5816 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5818 return ret;
5821 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5822 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5823 struct wined3d_shader *shader)
5825 struct glsl_gs_compiled_shader *gl_shaders;
5826 struct glsl_shader_private *shader_data;
5827 GLuint ret;
5829 if (!shader->backend_data)
5831 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5833 ERR("Failed to allocate backend data.\n");
5834 return 0;
5837 shader_data = shader->backend_data;
5838 gl_shaders = shader_data->gl_shaders.gs;
5840 if (shader_data->num_gl_shaders)
5841 return gl_shaders[0].id;
5843 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5845 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5847 ERR("Failed to allocate GL shader array.\n");
5848 return 0;
5850 shader_data->shader_array_size = 1;
5851 gl_shaders = shader_data->gl_shaders.gs;
5853 string_buffer_clear(buffer);
5854 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5855 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5857 return ret;
5860 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5862 switch (mcs)
5864 case WINED3D_MCS_MATERIAL:
5865 return material;
5866 case WINED3D_MCS_COLOR1:
5867 return "ffp_attrib_diffuse";
5868 case WINED3D_MCS_COLOR2:
5869 return "ffp_attrib_specular";
5870 default:
5871 ERR("Invalid material color source %#x.\n", mcs);
5872 return "<invalid>";
5876 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5877 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5879 const char *diffuse, *specular, *emissive, *ambient;
5880 enum wined3d_light_type light_type;
5881 unsigned int i;
5883 if (!settings->lighting)
5885 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5886 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
5887 return;
5890 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5891 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5892 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5893 shader_addline(buffer, "vec3 dir, dst;\n");
5894 shader_addline(buffer, "float att, t;\n");
5896 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5897 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5898 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5899 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5901 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5903 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5904 switch (light_type)
5906 case WINED3D_LIGHT_POINT:
5907 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5908 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5909 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5910 shader_addline(buffer, "dst.x = 1.0;\n");
5911 if (legacy_lighting)
5913 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5914 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5916 else
5918 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5920 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5921 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5922 if (!legacy_lighting)
5923 shader_addline(buffer, "att = 1.0 / att;\n");
5924 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5925 if (!settings->normal)
5927 if (!legacy_lighting)
5928 shader_addline(buffer, "}\n");
5929 break;
5931 shader_addline(buffer, "dir = normalize(dir);\n");
5932 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5933 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5934 if (settings->localviewer)
5935 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5936 else
5937 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5938 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5939 " * ffp_light[%u].specular) * att;\n", i);
5940 if (!legacy_lighting)
5941 shader_addline(buffer, "}\n");
5942 break;
5944 case WINED3D_LIGHT_SPOT:
5945 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5946 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5947 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5948 shader_addline(buffer, "dst.x = 1.0;\n");
5949 if (legacy_lighting)
5951 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5952 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5954 else
5956 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5958 shader_addline(buffer, "dir = normalize(dir);\n");
5959 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5960 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5961 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5962 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5963 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5964 i, i, i, i);
5965 if (legacy_lighting)
5966 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5967 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5968 i, i, i);
5969 else
5970 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5971 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5972 i, i, i);
5973 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5974 if (!settings->normal)
5976 if (!legacy_lighting)
5977 shader_addline(buffer, "}\n");
5978 break;
5980 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5981 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5982 if (settings->localviewer)
5983 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5984 else
5985 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5986 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5987 " * ffp_light[%u].specular) * att;\n", i);
5988 if (!legacy_lighting)
5989 shader_addline(buffer, "}\n");
5990 break;
5992 case WINED3D_LIGHT_DIRECTIONAL:
5993 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5994 if (!settings->normal)
5995 break;
5996 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
5997 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5998 " * ffp_light[%u].diffuse.xyz;\n", i);
5999 /* TODO: In the non-local viewer case the halfvector is constant
6000 * and could be precomputed and stored in a uniform. */
6001 if (settings->localviewer)
6002 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6003 else
6004 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6005 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6006 " * ffp_light[%u].specular;\n", i);
6007 break;
6009 case WINED3D_LIGHT_PARALLELPOINT:
6010 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6011 if (!settings->normal)
6012 break;
6013 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
6014 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6015 " * ffp_light[%u].diffuse.xyz;\n", i);
6016 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6017 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6018 " * ffp_light[%u].specular;\n", i);
6019 break;
6021 default:
6022 if (light_type)
6023 FIXME("Unhandled light type %#x.\n", light_type);
6024 continue;
6028 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6029 ambient, diffuse, emissive);
6030 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6031 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6034 /* Context activation is done by the caller. */
6035 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6036 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6038 static const struct attrib_info
6040 const char type[6];
6041 const char name[24];
6043 attrib_info[] =
6045 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6046 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6047 /* TODO: Indexed vertex blending */
6048 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6049 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6050 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6051 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6052 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6054 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6055 BOOL legacy_lighting = priv->legacy_lighting;
6056 GLuint shader_obj;
6057 unsigned int i;
6058 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6059 BOOL output_legacy_fogcoord = legacy_context;
6061 string_buffer_clear(buffer);
6063 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6065 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6067 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6069 shader_addline(buffer, "attribute %s vs_in%u;\n", type, i);
6071 shader_addline(buffer, "\n");
6073 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6074 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6075 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6076 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6078 shader_addline(buffer, "uniform struct\n{\n");
6079 shader_addline(buffer, " vec4 emissive;\n");
6080 shader_addline(buffer, " vec4 ambient;\n");
6081 shader_addline(buffer, " vec4 diffuse;\n");
6082 shader_addline(buffer, " vec4 specular;\n");
6083 shader_addline(buffer, " float shininess;\n");
6084 shader_addline(buffer, "} ffp_material;\n");
6086 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6087 shader_addline(buffer, "uniform struct\n{\n");
6088 shader_addline(buffer, " vec4 diffuse;\n");
6089 shader_addline(buffer, " vec4 specular;\n");
6090 shader_addline(buffer, " vec4 ambient;\n");
6091 shader_addline(buffer, " vec4 position;\n");
6092 shader_addline(buffer, " vec3 direction;\n");
6093 shader_addline(buffer, " float range;\n");
6094 shader_addline(buffer, " float falloff;\n");
6095 shader_addline(buffer, " float c_att;\n");
6096 shader_addline(buffer, " float l_att;\n");
6097 shader_addline(buffer, " float q_att;\n");
6098 shader_addline(buffer, " float cos_htheta;\n");
6099 shader_addline(buffer, " float cos_hphi;\n");
6100 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6102 if (settings->point_size)
6104 shader_addline(buffer, "uniform struct\n{\n");
6105 shader_addline(buffer, " float size;\n");
6106 shader_addline(buffer, " float size_min;\n");
6107 shader_addline(buffer, " float size_max;\n");
6108 shader_addline(buffer, " float c_att;\n");
6109 shader_addline(buffer, " float l_att;\n");
6110 shader_addline(buffer, " float q_att;\n");
6111 shader_addline(buffer, "} ffp_point;\n");
6114 if (legacy_context)
6116 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6117 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6118 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6119 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6121 else
6123 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6124 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6125 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6126 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6129 shader_addline(buffer, "\nvoid main()\n{\n");
6130 shader_addline(buffer, "float m;\n");
6131 shader_addline(buffer, "vec3 r;\n");
6133 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6135 if (attrib_info[i].name[0])
6136 shader_addline(buffer, "%s %s = vs_in%u;\n",
6137 attrib_info[i].type, attrib_info[i].name, i);
6139 for (i = 0; i < MAX_TEXTURES; ++i)
6141 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6142 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU)
6144 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6148 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6150 if (settings->transformed)
6152 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6153 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6154 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6156 else
6158 for (i = 0; i < settings->vertexblends; ++i)
6159 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6161 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6162 for (i = 0; i < settings->vertexblends + 1; ++i)
6163 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6165 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6166 if (settings->clipping)
6167 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6168 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6171 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6172 if (settings->normal)
6174 if (!settings->vertexblends)
6176 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6178 else
6180 for (i = 0; i < settings->vertexblends + 1; ++i)
6181 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6184 if (settings->normalize)
6185 shader_addline(buffer, "normal = normalize(normal);\n");
6188 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6189 if (legacy_context)
6191 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6192 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6195 for (i = 0; i < MAX_TEXTURES; ++i)
6197 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6199 switch (settings->texgen[i] & 0xffff0000)
6201 case WINED3DTSS_TCI_PASSTHRU:
6202 if (settings->texcoords & (1u << i))
6203 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6204 i, i, i);
6205 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6206 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6207 else
6208 output_legacy_texcoord = FALSE;
6209 break;
6211 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6212 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6213 break;
6215 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6216 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6217 break;
6219 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6220 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6221 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6222 break;
6224 case WINED3DTSS_TCI_SPHEREMAP:
6225 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6226 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6227 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6228 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6229 break;
6231 default:
6232 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6233 break;
6235 if (output_legacy_texcoord)
6236 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6239 switch (settings->fog_mode)
6241 case WINED3D_FFP_VS_FOG_OFF:
6242 output_legacy_fogcoord = FALSE;
6243 break;
6245 case WINED3D_FFP_VS_FOG_FOGCOORD:
6246 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6247 break;
6249 case WINED3D_FFP_VS_FOG_RANGE:
6250 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6251 break;
6253 case WINED3D_FFP_VS_FOG_DEPTH:
6254 if (settings->ortho_fog)
6255 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6256 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6257 else if (settings->transformed)
6258 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6259 else
6260 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6261 break;
6263 default:
6264 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6265 break;
6267 if (output_legacy_fogcoord)
6268 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6270 if (settings->point_size)
6272 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6273 " + ffp_point.l_att * length(ec_pos.xyz)"
6274 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6275 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6276 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6279 shader_addline(buffer, "}\n");
6281 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6282 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6284 return shader_obj;
6287 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6288 DWORD argnum, unsigned int stage, DWORD arg)
6290 const char *ret;
6292 if (arg == ARG_UNUSED)
6293 return "<unused arg>";
6295 switch (arg & WINED3DTA_SELECTMASK)
6297 case WINED3DTA_DIFFUSE:
6298 ret = "ffp_varying_diffuse";
6299 break;
6301 case WINED3DTA_CURRENT:
6302 ret = "ret";
6303 break;
6305 case WINED3DTA_TEXTURE:
6306 switch (stage)
6308 case 0: ret = "tex0"; break;
6309 case 1: ret = "tex1"; break;
6310 case 2: ret = "tex2"; break;
6311 case 3: ret = "tex3"; break;
6312 case 4: ret = "tex4"; break;
6313 case 5: ret = "tex5"; break;
6314 case 6: ret = "tex6"; break;
6315 case 7: ret = "tex7"; break;
6316 default:
6317 ret = "<invalid texture>";
6318 break;
6320 break;
6322 case WINED3DTA_TFACTOR:
6323 ret = "tex_factor";
6324 break;
6326 case WINED3DTA_SPECULAR:
6327 ret = "ffp_varying_specular";
6328 break;
6330 case WINED3DTA_TEMP:
6331 ret = "temp_reg";
6332 break;
6334 case WINED3DTA_CONSTANT:
6335 switch (stage)
6337 case 0: ret = "tss_const0"; break;
6338 case 1: ret = "tss_const1"; break;
6339 case 2: ret = "tss_const2"; break;
6340 case 3: ret = "tss_const3"; break;
6341 case 4: ret = "tss_const4"; break;
6342 case 5: ret = "tss_const5"; break;
6343 case 6: ret = "tss_const6"; break;
6344 case 7: ret = "tss_const7"; break;
6345 default:
6346 ret = "<invalid constant>";
6347 break;
6349 break;
6351 default:
6352 return "<unhandled arg>";
6355 if (arg & WINED3DTA_COMPLEMENT)
6357 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6358 if (argnum == 0)
6359 ret = "arg0";
6360 else if (argnum == 1)
6361 ret = "arg1";
6362 else if (argnum == 2)
6363 ret = "arg2";
6366 if (arg & WINED3DTA_ALPHAREPLICATE)
6368 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6369 if (argnum == 0)
6370 ret = "arg0";
6371 else if (argnum == 1)
6372 ret = "arg1";
6373 else if (argnum == 2)
6374 ret = "arg2";
6377 return ret;
6380 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6381 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6383 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6385 if (color && alpha)
6386 dstmask = "";
6387 else if (color)
6388 dstmask = ".xyz";
6389 else
6390 dstmask = ".w";
6392 if (dst == tempreg)
6393 dstreg = "temp_reg";
6394 else
6395 dstreg = "ret";
6397 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6398 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6399 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6401 switch (op)
6403 case WINED3D_TOP_DISABLE:
6404 break;
6406 case WINED3D_TOP_SELECT_ARG1:
6407 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6408 break;
6410 case WINED3D_TOP_SELECT_ARG2:
6411 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6412 break;
6414 case WINED3D_TOP_MODULATE:
6415 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6416 break;
6418 case WINED3D_TOP_MODULATE_4X:
6419 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6420 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6421 break;
6423 case WINED3D_TOP_MODULATE_2X:
6424 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6425 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6426 break;
6428 case WINED3D_TOP_ADD:
6429 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6430 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6431 break;
6433 case WINED3D_TOP_ADD_SIGNED:
6434 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6435 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6436 break;
6438 case WINED3D_TOP_ADD_SIGNED_2X:
6439 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6440 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6441 break;
6443 case WINED3D_TOP_SUBTRACT:
6444 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6445 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6446 break;
6448 case WINED3D_TOP_ADD_SMOOTH:
6449 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6450 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6451 break;
6453 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6454 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6455 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6456 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6457 break;
6459 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6460 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6461 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6462 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6463 break;
6465 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6466 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6467 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6468 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6469 break;
6471 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6472 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6473 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6474 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6475 break;
6477 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6478 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6479 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6480 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6481 break;
6483 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6484 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6485 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6486 break;
6488 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6489 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6490 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6491 break;
6493 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6494 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6495 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6496 break;
6497 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6498 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6499 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6500 break;
6502 case WINED3D_TOP_BUMPENVMAP:
6503 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6504 /* These are handled in the first pass, nothing to do. */
6505 break;
6507 case WINED3D_TOP_DOTPRODUCT3:
6508 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6509 dstreg, dstmask, arg1, arg2, dstmask);
6510 break;
6512 case WINED3D_TOP_MULTIPLY_ADD:
6513 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6514 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6515 break;
6517 case WINED3D_TOP_LERP:
6518 /* MSDN isn't quite right here. */
6519 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6520 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6521 break;
6523 default:
6524 FIXME("Unhandled operation %#x.\n", op);
6525 break;
6529 /* Context activation is done by the caller. */
6530 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6531 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6533 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6534 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6535 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6536 UINT lowest_disabled_stage;
6537 GLuint shader_id;
6538 DWORD arg0, arg1, arg2;
6539 unsigned int stage;
6540 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6541 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6543 string_buffer_clear(buffer);
6545 /* Find out which textures are read */
6546 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6548 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6549 break;
6551 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6552 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6553 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6555 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6556 || (stage == 0 && settings->color_key_enabled))
6557 tex_map |= 1u << stage;
6558 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6559 tfactor_used = TRUE;
6560 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6561 tempreg_used = TRUE;
6562 if (settings->op[stage].dst == tempreg)
6563 tempreg_used = TRUE;
6564 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6565 tss_const_map |= 1u << stage;
6567 switch (settings->op[stage].cop)
6569 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6570 lum_map |= 1u << stage;
6571 /* fall through */
6572 case WINED3D_TOP_BUMPENVMAP:
6573 bump_map |= 1u << stage;
6574 /* fall through */
6575 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6576 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6577 tex_map |= 1u << stage;
6578 break;
6580 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6581 tfactor_used = TRUE;
6582 break;
6584 default:
6585 break;
6588 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6589 continue;
6591 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6592 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6593 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6595 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6596 tex_map |= 1u << stage;
6597 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6598 tfactor_used = TRUE;
6599 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6600 tempreg_used = TRUE;
6601 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6602 tss_const_map |= 1u << stage;
6604 lowest_disabled_stage = stage;
6606 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6608 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6609 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6611 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6612 shader_addline(buffer, "vec4 ret;\n");
6613 if (tempreg_used || settings->sRGB_write)
6614 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6615 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6617 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6619 if (tss_const_map & (1u << stage))
6620 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6622 if (!(tex_map & (1u << stage)))
6623 continue;
6625 switch (settings->op[stage].tex_type)
6627 case WINED3D_GL_RES_TYPE_TEX_1D:
6628 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6629 break;
6630 case WINED3D_GL_RES_TYPE_TEX_2D:
6631 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6632 break;
6633 case WINED3D_GL_RES_TYPE_TEX_3D:
6634 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6635 break;
6636 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6637 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6638 break;
6639 case WINED3D_GL_RES_TYPE_TEX_RECT:
6640 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6641 break;
6642 default:
6643 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6644 break;
6647 shader_addline(buffer, "vec4 tex%u;\n", stage);
6649 if (!(bump_map & (1u << stage)))
6650 continue;
6651 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6653 if (!(lum_map & (1u << stage)))
6654 continue;
6655 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6656 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6658 if (tfactor_used)
6659 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6660 if (settings->color_key_enabled)
6661 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6662 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6664 if (settings->sRGB_write)
6666 shader_addline(buffer, "const vec4 srgb_const0 = ");
6667 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6668 shader_addline(buffer, ";\n");
6669 shader_addline(buffer, "const vec4 srgb_const1 = ");
6670 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6671 shader_addline(buffer, ";\n");
6674 shader_addline(buffer, "uniform struct\n{\n");
6675 shader_addline(buffer, " vec4 color;\n");
6676 shader_addline(buffer, " float density;\n");
6677 shader_addline(buffer, " float end;\n");
6678 shader_addline(buffer, " float scale;\n");
6679 shader_addline(buffer, "} ffp_fog;\n");
6681 if (legacy_context)
6683 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6684 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6685 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6686 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6687 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6689 else
6691 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6692 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6693 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6694 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6695 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6698 shader_addline(buffer, "void main()\n{\n");
6700 if (legacy_context)
6702 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6703 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6706 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6708 if (tex_map & (1u << stage))
6710 if (settings->pointsprite)
6711 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6712 else if (settings->texcoords_initialized & (1u << stage))
6713 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6714 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6715 else
6716 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6720 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6721 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6723 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6724 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6726 /* Generate texture sampling instructions */
6727 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6729 const char *texture_function, *coord_mask;
6730 BOOL proj;
6732 if (!(tex_map & (1u << stage)))
6733 continue;
6735 if (settings->op[stage].projected == proj_none)
6737 proj = FALSE;
6739 else if (settings->op[stage].projected == proj_count4
6740 || settings->op[stage].projected == proj_count3)
6742 proj = TRUE;
6744 else
6746 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6747 proj = TRUE;
6750 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
6751 proj = FALSE;
6753 switch (settings->op[stage].tex_type)
6755 case WINED3D_GL_RES_TYPE_TEX_1D:
6756 if (proj)
6758 texture_function = "texture1DProj";
6759 coord_mask = "xw";
6761 else
6763 texture_function = "texture1D";
6764 coord_mask = "x";
6766 break;
6767 case WINED3D_GL_RES_TYPE_TEX_2D:
6768 if (proj)
6770 texture_function = "texture2DProj";
6771 coord_mask = "xyw";
6773 else
6775 texture_function = "texture2D";
6776 coord_mask = "xy";
6778 break;
6779 case WINED3D_GL_RES_TYPE_TEX_3D:
6780 if (proj)
6782 texture_function = "texture3DProj";
6783 coord_mask = "xyzw";
6785 else
6787 texture_function = "texture3D";
6788 coord_mask = "xyz";
6790 break;
6791 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6792 texture_function = "textureCube";
6793 coord_mask = "xyz";
6794 break;
6795 case WINED3D_GL_RES_TYPE_TEX_RECT:
6796 if (proj)
6798 texture_function = "texture2DRectProj";
6799 coord_mask = "xyw";
6801 else
6803 texture_function = "texture2DRect";
6804 coord_mask = "xy";
6806 break;
6807 default:
6808 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6809 texture_function = "";
6810 coord_mask = "xyzw";
6811 break;
6813 if (!needs_legacy_glsl_syntax(gl_info))
6814 texture_function = proj ? "textureProj" : "texture";
6816 if (stage > 0
6817 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6818 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6820 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6822 /* With projective textures, texbem only divides the static
6823 * texture coord, not the displacement, so multiply the
6824 * displacement with the dividing parameter before passing it to
6825 * TXP. */
6826 if (settings->op[stage].projected != proj_none)
6828 if (settings->op[stage].projected == proj_count4)
6830 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6831 stage, stage);
6832 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
6834 else
6836 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6837 stage, stage);
6838 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
6841 else
6843 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
6846 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6847 stage, texture_function, stage, coord_mask);
6849 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6850 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6851 stage, stage - 1, stage - 1, stage - 1);
6853 else if (settings->op[stage].projected == proj_count3)
6855 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6856 stage, texture_function, stage, stage);
6858 else
6860 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6861 stage, texture_function, stage, stage, coord_mask);
6864 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6865 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6866 settings->op[stage].color_fixup);
6869 if (settings->color_key_enabled)
6871 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6872 shader_addline(buffer, " discard;\n");
6875 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
6877 /* Generate the main shader */
6878 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6880 BOOL op_equal;
6882 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6883 break;
6885 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6886 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6887 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6888 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6889 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6890 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6891 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6892 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6893 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6894 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6895 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6896 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6897 else
6898 op_equal = settings->op[stage].aop == settings->op[stage].cop
6899 && settings->op[stage].carg0 == settings->op[stage].aarg0
6900 && settings->op[stage].carg1 == settings->op[stage].aarg1
6901 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6903 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6905 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6906 settings->op[stage].cop, settings->op[stage].carg0,
6907 settings->op[stage].carg1, settings->op[stage].carg2);
6909 else if (op_equal)
6911 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6912 settings->op[stage].cop, settings->op[stage].carg0,
6913 settings->op[stage].carg1, settings->op[stage].carg2);
6915 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
6916 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6918 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6919 settings->op[stage].cop, settings->op[stage].carg0,
6920 settings->op[stage].carg1, settings->op[stage].carg2);
6921 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6922 settings->op[stage].aop, settings->op[stage].aarg0,
6923 settings->op[stage].aarg1, settings->op[stage].aarg2);
6927 shader_addline(buffer, "gl_FragData[0] = ffp_varying_specular * specular_enable + ret;\n");
6929 if (settings->sRGB_write)
6930 shader_glsl_generate_srgb_write_correction(buffer);
6932 shader_glsl_generate_fog_code(buffer, settings->fog);
6934 shader_addline(buffer, "}\n");
6936 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6937 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6939 string_buffer_release(&priv->string_buffers, tex_reg_name);
6940 return shader_id;
6943 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6944 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6946 struct glsl_ffp_vertex_shader *shader;
6947 const struct wine_rb_entry *entry;
6949 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6950 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6952 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6953 return NULL;
6955 shader->desc.settings = *settings;
6956 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
6957 list_init(&shader->linked_programs);
6958 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6959 ERR("Failed to insert ffp vertex shader.\n");
6961 return shader;
6964 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6965 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6967 struct glsl_ffp_fragment_shader *glsl_desc;
6968 const struct ffp_frag_desc *desc;
6970 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6971 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6973 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6974 return NULL;
6976 glsl_desc->entry.settings = *args;
6977 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
6978 list_init(&glsl_desc->linked_programs);
6979 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6981 return glsl_desc;
6985 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6986 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6988 unsigned int i;
6989 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6991 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6992 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6993 for (i = 0; i < vs_c_count; ++i)
6995 string_buffer_sprintf(name, "vs_c[%u]", i);
6996 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6998 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6999 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
7001 for (i = 0; i < MAX_CONST_I; ++i)
7003 string_buffer_sprintf(name, "vs_i[%u]", i);
7004 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7007 for (i = 0; i < MAX_CONST_B; ++i)
7009 string_buffer_sprintf(name, "vs_b[%u]", i);
7010 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7013 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
7015 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7017 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7018 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7020 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7021 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7022 for (i = 0; i < MAX_TEXTURES; ++i)
7024 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7025 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7027 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7028 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7029 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7030 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7031 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7032 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7033 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7035 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7036 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7037 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7038 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7039 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7040 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7041 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7042 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7043 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7044 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7045 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7046 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7047 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7048 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7049 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7050 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7051 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7052 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7053 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7054 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7055 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7056 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7057 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7058 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7060 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7061 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7062 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7063 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7064 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7065 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7067 string_buffer_release(&priv->string_buffers, name);
7070 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7071 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7073 unsigned int i;
7074 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7076 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
7077 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
7078 for (i = 0; i < ps_c_count; ++i)
7080 string_buffer_sprintf(name, "ps_c[%u]", i);
7081 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7083 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
7084 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
7086 for (i = 0; i < MAX_CONST_I; ++i)
7088 string_buffer_sprintf(name, "ps_i[%u]", i);
7089 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7092 for (i = 0; i < MAX_CONST_B; ++i)
7094 string_buffer_sprintf(name, "ps_b[%u]", i);
7095 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7098 for (i = 0; i < MAX_TEXTURES; ++i)
7100 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7101 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7102 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7103 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7104 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7105 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7106 string_buffer_sprintf(name, "tss_const%u", i);
7107 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7110 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7111 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7113 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7114 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7115 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7116 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7118 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7119 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7120 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7122 string_buffer_release(&priv->string_buffers, name);
7125 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7126 struct shader_glsl_priv *priv, GLuint program_id,
7127 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
7129 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7130 GLuint block_idx;
7131 unsigned int i;
7132 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7134 for (i = 0; i < count; ++i)
7136 if (!reg_maps->cb_sizes[i])
7137 continue;
7139 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7140 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7141 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7143 checkGLcall("glUniformBlockBinding");
7144 string_buffer_release(&priv->string_buffers, name);
7147 /* Context activation is done by the caller. */
7148 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7149 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7151 const struct wined3d_gl_info *gl_info = context->gl_info;
7152 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7153 const struct ps_np2fixup_info *np2fixup_info = NULL;
7154 struct glsl_shader_prog_link *entry = NULL;
7155 struct wined3d_shader *vshader = NULL;
7156 struct wined3d_shader *gshader = NULL;
7157 struct wined3d_shader *pshader = NULL;
7158 GLuint program_id;
7159 GLuint reorder_shader_id = 0;
7160 unsigned int i;
7161 GLuint vs_id = 0;
7162 GLuint gs_id = 0;
7163 GLuint ps_id = 0;
7164 struct list *ps_list, *vs_list;
7165 WORD attribs_map;
7166 struct wined3d_string_buffer *tmp_name;
7168 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7170 vs_id = ctx_data->glsl_program->vs.id;
7171 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7173 if (use_vs(state))
7175 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7176 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7178 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7179 && ctx_data->glsl_program->gs.id)
7180 gs_id = ctx_data->glsl_program->gs.id;
7181 else if (gshader)
7182 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7185 else if (use_vs(state))
7187 struct vs_compile_args vs_compile_args;
7189 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7191 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7192 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
7193 vs_list = &vshader->linked_programs;
7195 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
7196 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7198 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7200 struct glsl_ffp_vertex_shader *ffp_shader;
7201 struct wined3d_ffp_vs_settings settings;
7203 wined3d_ffp_get_vs_settings(context, state, &settings);
7204 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7205 vs_id = ffp_shader->id;
7206 vs_list = &ffp_shader->linked_programs;
7209 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7211 ps_id = ctx_data->glsl_program->ps.id;
7212 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7214 if (use_ps(state))
7215 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7217 else if (use_ps(state))
7219 struct ps_compile_args ps_compile_args;
7220 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7221 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7222 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7223 pshader, &ps_compile_args, &np2fixup_info);
7224 ps_list = &pshader->linked_programs;
7226 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7228 struct glsl_ffp_fragment_shader *ffp_shader;
7229 struct ffp_frag_settings settings;
7231 gen_ffp_frag_op(context, state, &settings, FALSE);
7232 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7233 ps_id = ffp_shader->id;
7234 ps_list = &ffp_shader->linked_programs;
7237 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7239 ctx_data->glsl_program = entry;
7240 return;
7243 /* If we get to this point, then no matching program exists, so we create one */
7244 program_id = GL_EXTCALL(glCreateProgram());
7245 TRACE("Created new GLSL shader program %u.\n", program_id);
7247 /* Create the entry */
7248 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7249 entry->id = program_id;
7250 entry->vs.id = vs_id;
7251 entry->gs.id = gs_id;
7252 entry->ps.id = ps_id;
7253 entry->constant_version = 0;
7254 entry->ps.np2_fixup_info = np2fixup_info;
7255 /* Add the hash table entry */
7256 add_glsl_program_entry(priv, entry);
7258 /* Set the current program */
7259 ctx_data->glsl_program = entry;
7261 /* Attach GLSL vshader */
7262 if (vs_id)
7264 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7265 GL_EXTCALL(glAttachShader(program_id, vs_id));
7266 checkGLcall("glAttachShader");
7268 list_add_head(vs_list, &entry->vs.shader_entry);
7271 if (vshader)
7273 attribs_map = vshader->reg_maps.input_registers;
7274 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
7275 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7276 d3d_info->emulated_flatshading
7277 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7278 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7279 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7280 checkGLcall("glAttachShader");
7281 /* Flag the reorder function for deletion, then it will be freed automatically when the program
7282 * is destroyed
7284 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7286 else
7288 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7291 /* Bind vertex attributes to a corresponding index number to match
7292 * the same index numbers as ARB_vertex_programs (makes loading
7293 * vertex attributes simpler). With this method, we can use the
7294 * exact same code to load the attributes later for both ARB and
7295 * GLSL shaders.
7297 * We have to do this here because we need to know the Program ID
7298 * in order to make the bindings work, and it has to be done prior
7299 * to linking the GLSL program. */
7300 tmp_name = string_buffer_get(&priv->string_buffers);
7301 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7303 if (!(attribs_map & 1))
7304 continue;
7306 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7307 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7309 checkGLcall("glBindAttribLocation");
7310 string_buffer_release(&priv->string_buffers, tmp_name);
7312 if (gshader)
7314 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7315 GL_EXTCALL(glAttachShader(program_id, gs_id));
7316 checkGLcall("glAttachShader");
7318 TRACE("input type %s, output type %s, vertices out %u.\n",
7319 debug_d3dprimitivetype(gshader->u.gs.input_type),
7320 debug_d3dprimitivetype(gshader->u.gs.output_type),
7321 gshader->u.gs.vertices_out);
7322 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7323 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7324 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7325 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7326 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7327 gshader->u.gs.vertices_out));
7328 checkGLcall("glProgramParameteriARB");
7330 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7333 /* Attach GLSL pshader */
7334 if (ps_id)
7336 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7337 GL_EXTCALL(glAttachShader(program_id, ps_id));
7338 checkGLcall("glAttachShader");
7340 list_add_head(ps_list, &entry->ps.shader_entry);
7343 /* Link the program */
7344 TRACE("Linking GLSL shader program %u.\n", program_id);
7345 GL_EXTCALL(glLinkProgram(program_id));
7346 shader_glsl_validate_link(gl_info, program_id);
7348 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7349 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
7350 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7351 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
7352 checkGLcall("Find glsl program uniform locations");
7354 if (pshader && pshader->reg_maps.shader_version.major >= 3
7355 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7357 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7358 entry->vs.vertex_color_clamp = GL_FALSE;
7360 else
7362 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7365 /* Set the shader to allow uniform loading on it */
7366 GL_EXTCALL(glUseProgram(program_id));
7367 checkGLcall("glUseProgram");
7369 /* Texture unit mapping is set up to be the same each time the shader
7370 * program is used so we can hardcode the sampler uniform values. */
7371 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7373 entry->constant_update_mask = 0;
7374 if (vshader)
7376 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7377 if (vshader->reg_maps.integer_constants)
7378 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7379 if (vshader->reg_maps.boolean_constants)
7380 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7381 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7383 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7384 0, gl_info->limits.vertex_uniform_blocks);
7386 else
7388 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7389 | WINED3D_SHADER_CONST_FFP_PROJ;
7391 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7393 if (entry->vs.modelview_matrix_location[i] != -1)
7395 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7396 break;
7400 for (i = 0; i < MAX_TEXTURES; ++i)
7402 if (entry->vs.texture_matrix_location[i] != -1)
7404 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7405 break;
7408 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7409 || entry->vs.material_specular_location != -1
7410 || entry->vs.material_emissive_location != -1
7411 || entry->vs.material_shininess_location != -1)
7412 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7413 if (entry->vs.light_ambient_location != -1)
7414 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7416 if (entry->vs.pointsize_min_location != -1)
7417 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7419 if (gshader)
7420 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7421 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7423 if (ps_id)
7425 if (pshader)
7427 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7428 if (pshader->reg_maps.integer_constants)
7429 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7430 if (pshader->reg_maps.boolean_constants)
7431 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7432 if (entry->ps.ycorrection_location != -1)
7433 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7435 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7436 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7437 gl_info->limits.fragment_uniform_blocks);
7439 else
7441 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7444 for (i = 0; i < MAX_TEXTURES; ++i)
7446 if (entry->ps.bumpenv_mat_location[i] != -1)
7448 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7449 break;
7453 if (entry->ps.fog_color_location != -1)
7454 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7455 if (entry->ps.np2_fixup_location != -1)
7456 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7457 if (entry->ps.color_key_location != -1)
7458 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7462 /* Context activation is done by the caller. */
7463 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7464 BOOL masked)
7466 GLuint program_id;
7467 GLuint vshader_id, pshader_id;
7468 const char *blt_pshader;
7470 static const char blt_vshader[] =
7471 "#version 120\n"
7472 "void main(void)\n"
7473 "{\n"
7474 " gl_Position = gl_Vertex;\n"
7475 " gl_FrontColor = vec4(1.0);\n"
7476 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7477 "}\n";
7479 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7481 /* WINED3D_GL_RES_TYPE_TEX_1D */
7482 NULL,
7483 /* WINED3D_GL_RES_TYPE_TEX_2D */
7484 "#version 120\n"
7485 "uniform sampler2D sampler;\n"
7486 "void main(void)\n"
7487 "{\n"
7488 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7489 "}\n",
7490 /* WINED3D_GL_RES_TYPE_TEX_3D */
7491 NULL,
7492 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7493 "#version 120\n"
7494 "uniform samplerCube sampler;\n"
7495 "void main(void)\n"
7496 "{\n"
7497 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7498 "}\n",
7499 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7500 "#version 120\n"
7501 "#extension GL_ARB_texture_rectangle : enable\n"
7502 "uniform sampler2DRect sampler;\n"
7503 "void main(void)\n"
7504 "{\n"
7505 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7506 "}\n",
7507 /* WINED3D_GL_RES_TYPE_BUFFER */
7508 NULL,
7509 /* WINED3D_GL_RES_TYPE_RB */
7510 NULL,
7513 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7515 /* WINED3D_GL_RES_TYPE_TEX_1D */
7516 NULL,
7517 /* WINED3D_GL_RES_TYPE_TEX_2D */
7518 "#version 120\n"
7519 "uniform sampler2D sampler;\n"
7520 "uniform vec4 mask;\n"
7521 "void main(void)\n"
7522 "{\n"
7523 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7524 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7525 "}\n",
7526 /* WINED3D_GL_RES_TYPE_TEX_3D */
7527 NULL,
7528 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7529 "#version 120\n"
7530 "uniform samplerCube sampler;\n"
7531 "uniform vec4 mask;\n"
7532 "void main(void)\n"
7533 "{\n"
7534 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7535 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7536 "}\n",
7537 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7538 "#version 120\n"
7539 "#extension GL_ARB_texture_rectangle : enable\n"
7540 "uniform sampler2DRect sampler;\n"
7541 "uniform vec4 mask;\n"
7542 "void main(void)\n"
7543 "{\n"
7544 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7545 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7546 "}\n",
7547 /* WINED3D_GL_RES_TYPE_BUFFER */
7548 NULL,
7549 /* WINED3D_GL_RES_TYPE_RB */
7550 NULL,
7553 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7554 if (!blt_pshader)
7556 FIXME("tex_type %#x not supported\n", tex_type);
7557 return 0;
7560 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7561 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7563 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7564 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7566 program_id = GL_EXTCALL(glCreateProgram());
7567 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7568 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7569 GL_EXTCALL(glLinkProgram(program_id));
7571 shader_glsl_validate_link(gl_info, program_id);
7573 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7574 * is destroyed
7576 GL_EXTCALL(glDeleteShader(vshader_id));
7577 GL_EXTCALL(glDeleteShader(pshader_id));
7578 return program_id;
7581 /* Context activation is done by the caller. */
7582 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7583 const struct wined3d_state *state)
7585 struct glsl_context_data *ctx_data = context->shader_backend_data;
7586 const struct wined3d_gl_info *gl_info = context->gl_info;
7587 struct shader_glsl_priv *priv = shader_priv;
7588 GLuint program_id = 0, prev_id = 0;
7589 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7591 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7592 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7594 if (ctx_data->glsl_program)
7596 prev_id = ctx_data->glsl_program->id;
7597 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7599 else
7601 prev_id = 0;
7602 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7605 set_glsl_shader_program(context, state, priv, ctx_data);
7607 if (ctx_data->glsl_program)
7609 program_id = ctx_data->glsl_program->id;
7610 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7612 else
7614 program_id = 0;
7615 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7618 if (old_vertex_color_clamp != current_vertex_color_clamp)
7620 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7622 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7623 checkGLcall("glClampColorARB");
7625 else
7627 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7631 TRACE("Using GLSL program %u.\n", program_id);
7633 if (prev_id != program_id)
7635 GL_EXTCALL(glUseProgram(program_id));
7636 checkGLcall("glUseProgram");
7638 if (program_id)
7639 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7643 /* "context" is not necessarily the currently active context. */
7644 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7646 struct glsl_context_data *ctx_data = context->shader_backend_data;
7648 ctx_data->glsl_program = NULL;
7649 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7650 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7651 | (1u << WINED3D_SHADER_TYPE_GEOMETRY);
7654 /* Context activation is done by the caller. */
7655 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7657 const struct wined3d_gl_info *gl_info = context->gl_info;
7658 struct shader_glsl_priv *priv = shader_priv;
7660 shader_glsl_invalidate_current_program(context);
7661 GL_EXTCALL(glUseProgram(0));
7662 checkGLcall("glUseProgram");
7664 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7665 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7667 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7669 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7670 checkGLcall("glClampColorARB");
7674 /* Context activation is done by the caller. */
7675 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7676 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7678 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7679 struct shader_glsl_priv *priv = shader_priv;
7680 GLuint *blt_program;
7681 GLint loc;
7683 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7684 if (!*blt_program)
7686 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7687 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7688 GL_EXTCALL(glUseProgram(*blt_program));
7689 GL_EXTCALL(glUniform1i(loc, 0));
7691 else
7693 GL_EXTCALL(glUseProgram(*blt_program));
7696 if (masked)
7698 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7699 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7703 /* Context activation is done by the caller. */
7704 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7706 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7707 GLuint program_id;
7709 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7710 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7712 GL_EXTCALL(glUseProgram(program_id));
7713 checkGLcall("glUseProgram");
7716 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7717 const struct glsl_shader_prog_link *program)
7719 const struct glsl_context_data *ctx_data;
7720 struct wined3d_context *context;
7721 unsigned int i;
7723 for (i = 0; i < device->context_count; ++i)
7725 context = device->contexts[i];
7726 ctx_data = context->shader_backend_data;
7728 if (ctx_data->glsl_program == program)
7729 shader_glsl_invalidate_current_program(context);
7733 static void shader_glsl_destroy(struct wined3d_shader *shader)
7735 struct glsl_shader_private *shader_data = shader->backend_data;
7736 struct wined3d_device *device = shader->device;
7737 struct shader_glsl_priv *priv = device->shader_priv;
7738 const struct wined3d_gl_info *gl_info;
7739 const struct list *linked_programs;
7740 struct wined3d_context *context;
7742 if (!shader_data || !shader_data->num_gl_shaders)
7744 HeapFree(GetProcessHeap(), 0, shader_data);
7745 shader->backend_data = NULL;
7746 return;
7749 context = context_acquire(device, NULL);
7750 gl_info = context->gl_info;
7752 TRACE("Deleting linked programs.\n");
7753 linked_programs = &shader->linked_programs;
7754 if (linked_programs->next)
7756 struct glsl_shader_prog_link *entry, *entry2;
7757 UINT i;
7759 switch (shader->reg_maps.shader_version.type)
7761 case WINED3D_SHADER_TYPE_PIXEL:
7763 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7765 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7767 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7768 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7769 checkGLcall("glDeleteShader");
7771 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7773 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7774 struct glsl_shader_prog_link, ps.shader_entry)
7776 shader_glsl_invalidate_contexts_program(device, entry);
7777 delete_glsl_program_entry(priv, gl_info, entry);
7780 break;
7783 case WINED3D_SHADER_TYPE_VERTEX:
7785 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7787 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7789 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7790 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7791 checkGLcall("glDeleteShader");
7793 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7795 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7796 struct glsl_shader_prog_link, vs.shader_entry)
7798 shader_glsl_invalidate_contexts_program(device, entry);
7799 delete_glsl_program_entry(priv, gl_info, entry);
7802 break;
7805 case WINED3D_SHADER_TYPE_GEOMETRY:
7807 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7809 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7811 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7812 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7813 checkGLcall("glDeleteShader");
7815 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7817 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7818 struct glsl_shader_prog_link, gs.shader_entry)
7820 shader_glsl_invalidate_contexts_program(device, entry);
7821 delete_glsl_program_entry(priv, gl_info, entry);
7824 break;
7827 default:
7828 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7829 break;
7833 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7834 shader->backend_data = NULL;
7836 context_release(context);
7839 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7841 const struct glsl_program_key *k = key;
7842 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7843 const struct glsl_shader_prog_link, program_lookup_entry);
7845 if (k->vs_id > prog->vs.id) return 1;
7846 else if (k->vs_id < prog->vs.id) return -1;
7848 if (k->gs_id > prog->gs.id) return 1;
7849 else if (k->gs_id < prog->gs.id) return -1;
7851 if (k->ps_id > prog->ps.id) return 1;
7852 else if (k->ps_id < prog->ps.id) return -1;
7854 return 0;
7857 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7859 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7860 + constant_count * sizeof(*heap->contained)
7861 + constant_count * sizeof(*heap->positions);
7862 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7864 if (!mem)
7866 ERR("Failed to allocate memory\n");
7867 return FALSE;
7870 heap->entries = mem;
7871 heap->entries[1].version = 0;
7872 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7873 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7874 heap->positions = (unsigned int *)(heap->contained + constant_count);
7875 heap->size = 1;
7877 return TRUE;
7880 static void constant_heap_free(struct constant_heap *heap)
7882 HeapFree(GetProcessHeap(), 0, heap->entries);
7885 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7887 wined3d_rb_alloc,
7888 wined3d_rb_realloc,
7889 wined3d_rb_free,
7890 glsl_program_key_compare,
7893 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7894 const struct fragment_pipeline *fragment_pipe)
7896 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7897 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7898 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7899 gl_info->limits.glsl_ps_float_constants)) + 1;
7900 struct fragment_caps fragment_caps;
7901 void *vertex_priv, *fragment_priv;
7903 string_buffer_list_init(&priv->string_buffers);
7905 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7907 ERR("Failed to initialize vertex pipe.\n");
7908 HeapFree(GetProcessHeap(), 0, priv);
7909 return E_FAIL;
7912 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7914 ERR("Failed to initialize fragment pipe.\n");
7915 vertex_pipe->vp_free(device);
7916 HeapFree(GetProcessHeap(), 0, priv);
7917 return E_FAIL;
7920 if (!string_buffer_init(&priv->shader_buffer))
7922 ERR("Failed to initialize shader buffer.\n");
7923 goto fail;
7926 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7927 if (!priv->stack)
7929 ERR("Failed to allocate memory.\n");
7930 goto fail;
7933 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7935 ERR("Failed to initialize vertex shader constant heap\n");
7936 goto fail;
7939 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7941 ERR("Failed to initialize pixel shader constant heap\n");
7942 goto fail;
7945 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7947 ERR("Failed to initialize rbtree.\n");
7948 goto fail;
7951 priv->next_constant_version = 1;
7952 priv->vertex_pipe = vertex_pipe;
7953 priv->fragment_pipe = fragment_pipe;
7954 fragment_pipe->get_caps(gl_info, &fragment_caps);
7955 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7956 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7958 device->vertex_priv = vertex_priv;
7959 device->fragment_priv = fragment_priv;
7960 device->shader_priv = priv;
7962 return WINED3D_OK;
7964 fail:
7965 constant_heap_free(&priv->pconst_heap);
7966 constant_heap_free(&priv->vconst_heap);
7967 HeapFree(GetProcessHeap(), 0, priv->stack);
7968 string_buffer_free(&priv->shader_buffer);
7969 fragment_pipe->free_private(device);
7970 vertex_pipe->vp_free(device);
7971 HeapFree(GetProcessHeap(), 0, priv);
7972 return E_OUTOFMEMORY;
7975 /* Context activation is done by the caller. */
7976 static void shader_glsl_free(struct wined3d_device *device)
7978 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7979 struct shader_glsl_priv *priv = device->shader_priv;
7980 int i;
7982 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7984 if (priv->depth_blt_program_full[i])
7986 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7988 if (priv->depth_blt_program_masked[i])
7990 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7994 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7995 constant_heap_free(&priv->pconst_heap);
7996 constant_heap_free(&priv->vconst_heap);
7997 HeapFree(GetProcessHeap(), 0, priv->stack);
7998 string_buffer_list_cleanup(&priv->string_buffers);
7999 string_buffer_free(&priv->shader_buffer);
8000 priv->fragment_pipe->free_private(device);
8001 priv->vertex_pipe->vp_free(device);
8003 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8004 device->shader_priv = NULL;
8007 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8009 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8010 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8013 static void shader_glsl_free_context_data(struct wined3d_context *context)
8015 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8018 static void shader_glsl_init_context_state(struct wined3d_context *context)
8020 const struct wined3d_gl_info *gl_info = context->gl_info;
8022 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8023 checkGLcall("GL_PROGRAM_POINT_SIZE");
8026 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8028 UINT shader_model;
8030 /* FIXME: Check for the specific extensions required for SM5 support
8031 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8032 * soon as we introduce them, adjusting the GL / GLSL version checks
8033 * accordingly. */
8034 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3])
8035 shader_model = 5;
8036 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8037 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8038 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8039 shader_model = 4;
8040 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
8041 * texldd and texldl instructions. */
8042 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
8043 shader_model = 3;
8044 else
8045 shader_model = 2;
8046 TRACE("Shader model %u.\n", shader_model);
8048 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8049 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8050 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8052 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
8053 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
8054 caps->varying_count = gl_info->limits.glsl_varyings;
8056 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8057 * Direct3D minimum requirement.
8059 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8060 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8062 * The problem is that the refrast clamps temporary results in the shader to
8063 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8064 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8065 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8066 * offer a way to query this.
8068 if (shader_model >= 4)
8069 caps->ps_1x_max_value = FLT_MAX;
8070 else
8071 caps->ps_1x_max_value = 1024.0f;
8073 /* Ideally we'd only set caps like sRGB writes here if supported by both
8074 * the shader backend and the fragment pipe, but we can get called before
8075 * shader_glsl_alloc(). */
8076 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8077 | WINED3D_SHADER_CAP_SRGB_WRITE;
8080 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8082 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8084 TRACE("Checking support for fixup:\n");
8085 dump_color_fixup_desc(fixup);
8088 /* We support everything except YUV conversions. */
8089 if (!is_complex_fixup(fixup))
8091 TRACE("[OK]\n");
8092 return TRUE;
8095 TRACE("[FAILED]\n");
8096 return FALSE;
8099 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8101 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8102 /* WINED3DSIH_ADD */ shader_glsl_binop,
8103 /* WINED3DSIH_AND */ shader_glsl_binop,
8104 /* WINED3DSIH_BEM */ shader_glsl_bem,
8105 /* WINED3DSIH_BREAK */ shader_glsl_break,
8106 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8107 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8108 /* WINED3DSIH_CALL */ shader_glsl_call,
8109 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8110 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8111 /* WINED3DSIH_CND */ shader_glsl_cnd,
8112 /* WINED3DSIH_CRS */ shader_glsl_cross,
8113 /* WINED3DSIH_CUT */ shader_glsl_cut,
8114 /* WINED3DSIH_DCL */ shader_glsl_nop,
8115 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8116 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8117 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ NULL,
8118 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8119 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8120 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8121 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8122 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8123 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8124 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8125 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8126 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8127 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8128 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8129 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8130 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8131 /* WINED3DSIH_DEF */ shader_glsl_nop,
8132 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8133 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8134 /* WINED3DSIH_DIV */ shader_glsl_binop,
8135 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8136 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8137 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8138 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8139 /* WINED3DSIH_DST */ shader_glsl_dst,
8140 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8141 /* WINED3DSIH_DSX_COARSE */ NULL,
8142 /* WINED3DSIH_DSX_FINE */ NULL,
8143 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8144 /* WINED3DSIH_DSY_COARSE */ NULL,
8145 /* WINED3DSIH_DSY_FINE */ NULL,
8146 /* WINED3DSIH_ELSE */ shader_glsl_else,
8147 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8148 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8149 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8150 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8151 /* WINED3DSIH_EQ */ shader_glsl_relop,
8152 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8153 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8154 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8155 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8156 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8157 /* WINED3DSIH_GE */ shader_glsl_relop,
8158 /* WINED3DSIH_IADD */ shader_glsl_binop,
8159 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8160 /* WINED3DSIH_IF */ shader_glsl_if,
8161 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8162 /* WINED3DSIH_IGE */ shader_glsl_relop,
8163 /* WINED3DSIH_ILT */ shader_glsl_relop,
8164 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8165 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8166 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8167 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8168 /* WINED3DSIH_INE */ shader_glsl_relop,
8169 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8170 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8171 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8172 /* WINED3DSIH_LABEL */ shader_glsl_label,
8173 /* WINED3DSIH_LD */ shader_glsl_ld,
8174 /* WINED3DSIH_LD2DMS */ NULL,
8175 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8176 /* WINED3DSIH_LIT */ shader_glsl_lit,
8177 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8178 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8179 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8180 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8181 /* WINED3DSIH_LT */ shader_glsl_relop,
8182 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8183 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8184 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8185 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8186 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8187 /* WINED3DSIH_MAD */ shader_glsl_mad,
8188 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8189 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8190 /* WINED3DSIH_MOV */ shader_glsl_mov,
8191 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8192 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8193 /* WINED3DSIH_MUL */ shader_glsl_binop,
8194 /* WINED3DSIH_NE */ shader_glsl_relop,
8195 /* WINED3DSIH_NOP */ shader_glsl_nop,
8196 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8197 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8198 /* WINED3DSIH_OR */ shader_glsl_binop,
8199 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8200 /* WINED3DSIH_POW */ shader_glsl_pow,
8201 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8202 /* WINED3DSIH_REP */ shader_glsl_rep,
8203 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8204 /* WINED3DSIH_RET */ shader_glsl_ret,
8205 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8206 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8207 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8208 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8209 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8210 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8211 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
8212 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
8213 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8214 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8215 /* WINED3DSIH_SETP */ NULL,
8216 /* WINED3DSIH_SGE */ shader_glsl_compare,
8217 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8218 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8219 /* WINED3DSIH_SLT */ shader_glsl_compare,
8220 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8221 /* WINED3DSIH_SUB */ shader_glsl_binop,
8222 /* WINED3DSIH_TEX */ shader_glsl_tex,
8223 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8224 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8225 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8226 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8227 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8228 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8229 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8230 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8231 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8232 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8233 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8234 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8235 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8236 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8237 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8238 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8239 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8240 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8241 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8242 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8243 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8244 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8245 /* WINED3DSIH_UGE */ shader_glsl_relop,
8246 /* WINED3DSIH_USHR */ shader_glsl_binop,
8247 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8248 /* WINED3DSIH_XOR */ shader_glsl_binop,
8251 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8252 SHADER_HANDLER hw_fct;
8254 /* Select handler */
8255 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8257 /* Unhandled opcode */
8258 if (!hw_fct)
8260 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8261 return;
8263 hw_fct(ins);
8265 shader_glsl_add_instruction_modifiers(ins);
8268 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8270 struct shader_glsl_priv *priv = shader_priv;
8272 return priv->ffp_proj_control;
8275 const struct wined3d_shader_backend_ops glsl_shader_backend =
8277 shader_glsl_handle_instruction,
8278 shader_glsl_select,
8279 shader_glsl_disable,
8280 shader_glsl_select_depth_blt,
8281 shader_glsl_deselect_depth_blt,
8282 shader_glsl_update_float_vertex_constants,
8283 shader_glsl_update_float_pixel_constants,
8284 shader_glsl_load_constants,
8285 shader_glsl_destroy,
8286 shader_glsl_alloc,
8287 shader_glsl_free,
8288 shader_glsl_allocate_context_data,
8289 shader_glsl_free_context_data,
8290 shader_glsl_init_context_state,
8291 shader_glsl_get_caps,
8292 shader_glsl_color_fixup_supported,
8293 shader_glsl_has_ffp_proj_control,
8296 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8298 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8300 caps->xyzrhw = TRUE;
8301 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8302 caps->ffp_generic_attributes = TRUE;
8303 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8304 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8305 caps->max_vertex_blend_matrix_index = 0;
8306 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8307 | WINED3DVTXPCAPS_MATERIALSOURCE7
8308 | WINED3DVTXPCAPS_VERTEXFOG
8309 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8310 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8311 | WINED3DVTXPCAPS_LOCALVIEWER
8312 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8313 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8314 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8315 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8318 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8320 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8321 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8322 return 0;
8325 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8327 struct shader_glsl_priv *priv;
8329 if (shader_backend == &glsl_shader_backend)
8331 priv = shader_priv;
8333 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8335 ERR("Failed to initialize rbtree.\n");
8336 return NULL;
8339 return priv;
8342 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8344 return NULL;
8347 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8349 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8350 struct glsl_ffp_vertex_shader, desc.entry);
8351 struct glsl_shader_prog_link *program, *program2;
8352 struct glsl_ffp_destroy_ctx *ctx = context;
8354 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8355 struct glsl_shader_prog_link, vs.shader_entry)
8357 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8359 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8360 HeapFree(GetProcessHeap(), 0, shader);
8363 /* Context activation is done by the caller. */
8364 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8366 struct shader_glsl_priv *priv = device->vertex_priv;
8367 struct glsl_ffp_destroy_ctx ctx;
8369 ctx.priv = priv;
8370 ctx.gl_info = &device->adapter->gl_info;
8371 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8374 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8375 const struct wined3d_state *state, DWORD state_id) {}
8377 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8378 const struct wined3d_state *state, DWORD state_id)
8380 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8383 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8384 const struct wined3d_state *state, DWORD state_id)
8386 const struct wined3d_gl_info *gl_info = context->gl_info;
8387 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8388 BOOL transformed = context->stream_info.position_transformed;
8389 BOOL wasrhw = context->last_was_rhw;
8390 unsigned int i;
8392 context->last_was_rhw = transformed;
8394 /* If the vertex declaration contains a transformed position attribute,
8395 * the draw uses the fixed function vertex pipeline regardless of any
8396 * vertex shader set by the application. */
8397 if (transformed != wasrhw)
8398 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8400 if (!use_vs(state))
8402 if (context->last_was_vshader)
8404 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8405 clipplane(context, state, STATE_CLIPPLANE(i));
8408 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8410 /* Because of settings->texcoords, we have to regenerate the vertex
8411 * shader on a vdecl change if there aren't enough varyings to just
8412 * always output all the texture coordinates. */
8413 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
8414 || normal != context->last_was_normal)
8415 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8417 if (use_ps(state)
8418 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8419 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8420 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8422 else
8424 if (!context->last_was_vshader)
8426 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8427 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8428 clipplane(context, state, STATE_CLIPPLANE(i));
8432 context->last_was_vshader = use_vs(state);
8433 context->last_was_normal = normal;
8436 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8437 const struct wined3d_state *state, DWORD state_id)
8439 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8440 /* Different vertex shaders potentially require a different vertex attributes setup. */
8441 if (!isStateDirty(context, STATE_VDECL))
8442 context_apply_state(context, state, STATE_VDECL);
8445 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8446 const struct wined3d_state *state, DWORD state_id)
8448 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8451 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8452 const struct wined3d_state *state, DWORD state_id)
8454 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8457 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8459 const struct wined3d_gl_info *gl_info = context->gl_info;
8460 unsigned int k;
8462 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8463 | WINED3D_SHADER_CONST_FFP_LIGHTS
8464 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8466 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8468 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8469 clipplane(context, state, STATE_CLIPPLANE(k));
8473 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8474 const struct wined3d_state *state, DWORD state_id)
8476 /* Table fog behavior depends on the projection matrix. */
8477 if (state->render_states[WINED3D_RS_FOGENABLE]
8478 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8479 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8480 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8483 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8484 const struct wined3d_state *state, DWORD state_id)
8486 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8487 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8488 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8489 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8490 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8491 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
8494 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8495 const struct wined3d_state *state, DWORD state_id)
8497 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8500 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8501 const struct wined3d_state *state, DWORD state_id)
8503 DWORD sampler = state_id - STATE_SAMPLER(0);
8504 const struct wined3d_texture *texture = state->textures[sampler];
8505 BOOL np2;
8507 if (!texture)
8508 return;
8510 if (sampler >= MAX_TEXTURES)
8511 return;
8513 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8514 || context->lastWasPow2Texture & (1u << sampler))
8516 if (np2)
8517 context->lastWasPow2Texture |= 1u << sampler;
8518 else
8519 context->lastWasPow2Texture &= ~(1u << sampler);
8521 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8525 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8526 const struct wined3d_state *state, DWORD state_id)
8528 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8531 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8532 const struct wined3d_state *state, DWORD state_id)
8534 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8537 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8538 const struct wined3d_state *state, DWORD state_id)
8540 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8543 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8544 const struct wined3d_state *state, DWORD state_id)
8546 if (!use_vs(state))
8547 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8550 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
8551 const struct wined3d_state *state, DWORD state_id)
8553 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
8556 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8558 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8559 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8560 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8561 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8562 /* Clip planes */
8563 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8564 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8565 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8566 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8567 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8568 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8569 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8570 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8571 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8572 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8573 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8574 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8575 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8576 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8577 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8578 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8579 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8580 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8581 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8582 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8583 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8584 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8585 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8586 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8587 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8588 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8589 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8590 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8591 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8592 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8593 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8594 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8595 /* Lights */
8596 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8597 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8598 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8599 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8600 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8601 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8602 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8603 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8604 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8605 /* Viewport */
8606 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8607 /* Transform states */
8608 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8609 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8610 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8611 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8612 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8613 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8614 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8615 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8616 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8617 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8618 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8619 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8620 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8621 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8622 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8623 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8624 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8625 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8626 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8627 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8628 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8629 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8630 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8631 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8632 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8633 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8634 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8635 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8636 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8637 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8638 /* Fog */
8639 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8640 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8641 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8642 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8643 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8644 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8645 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8646 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8647 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8648 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8649 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8650 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8651 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8652 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8653 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8654 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8655 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8656 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8657 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8658 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
8659 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8660 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8661 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8662 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8663 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8664 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8665 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8666 /* NP2 texture matrix fixups. They are not needed if
8667 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8668 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8669 * matrix. */
8670 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8671 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8672 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8673 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8674 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8675 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8676 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8677 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8678 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8679 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8680 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8681 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8682 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8683 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8684 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8685 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8686 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8687 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8688 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8689 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8690 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8691 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8692 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8693 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8694 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8695 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
8696 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
8697 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8700 /* TODO:
8701 * - Implement vertex tweening. */
8702 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8704 glsl_vertex_pipe_vp_enable,
8705 glsl_vertex_pipe_vp_get_caps,
8706 glsl_vertex_pipe_vp_get_emul_mask,
8707 glsl_vertex_pipe_vp_alloc,
8708 glsl_vertex_pipe_vp_free,
8709 glsl_vertex_pipe_vp_states,
8712 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8714 /* Nothing to do. */
8717 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8719 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8720 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8721 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8722 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8723 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8724 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8725 | WINED3DTEXOPCAPS_SELECTARG1
8726 | WINED3DTEXOPCAPS_SELECTARG2
8727 | WINED3DTEXOPCAPS_MODULATE4X
8728 | WINED3DTEXOPCAPS_MODULATE2X
8729 | WINED3DTEXOPCAPS_MODULATE
8730 | WINED3DTEXOPCAPS_ADDSIGNED2X
8731 | WINED3DTEXOPCAPS_ADDSIGNED
8732 | WINED3DTEXOPCAPS_ADD
8733 | WINED3DTEXOPCAPS_SUBTRACT
8734 | WINED3DTEXOPCAPS_ADDSMOOTH
8735 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8736 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8737 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8738 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8739 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8740 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8741 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8742 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8743 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8744 | WINED3DTEXOPCAPS_DOTPRODUCT3
8745 | WINED3DTEXOPCAPS_MULTIPLYADD
8746 | WINED3DTEXOPCAPS_LERP
8747 | WINED3DTEXOPCAPS_BUMPENVMAP
8748 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8749 caps->MaxTextureBlendStages = 8;
8750 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8753 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
8755 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8756 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8757 return 0;
8760 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8762 struct shader_glsl_priv *priv;
8764 if (shader_backend == &glsl_shader_backend)
8766 priv = shader_priv;
8768 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8770 ERR("Failed to initialize rbtree.\n");
8771 return NULL;
8774 return priv;
8777 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8779 return NULL;
8782 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8784 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8785 struct glsl_ffp_fragment_shader, entry.entry);
8786 struct glsl_shader_prog_link *program, *program2;
8787 struct glsl_ffp_destroy_ctx *ctx = context;
8789 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8790 struct glsl_shader_prog_link, ps.shader_entry)
8792 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8794 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8795 HeapFree(GetProcessHeap(), 0, shader);
8798 /* Context activation is done by the caller. */
8799 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8801 struct shader_glsl_priv *priv = device->fragment_priv;
8802 struct glsl_ffp_destroy_ctx ctx;
8804 ctx.priv = priv;
8805 ctx.gl_info = &device->adapter->gl_info;
8806 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8809 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8810 const struct wined3d_state *state, DWORD state_id)
8812 context->last_was_pshader = use_ps(state);
8814 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8817 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8818 const struct wined3d_state *state, DWORD state_id)
8820 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8823 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8824 const struct wined3d_state *state, DWORD state_id)
8826 BOOL use_vshader = use_vs(state);
8827 enum fogsource new_source;
8828 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8829 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8831 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8833 if (!state->render_states[WINED3D_RS_FOGENABLE])
8834 return;
8836 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8838 if (use_vshader)
8839 new_source = FOGSOURCE_VS;
8840 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8841 new_source = FOGSOURCE_COORD;
8842 else
8843 new_source = FOGSOURCE_FFP;
8845 else
8847 new_source = FOGSOURCE_FFP;
8850 if (new_source != context->fog_source || fogstart == fogend)
8852 context->fog_source = new_source;
8853 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8857 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8858 const struct wined3d_state *state, DWORD state_id)
8860 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8861 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8862 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8864 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8865 glsl_fragment_pipe_fog(context, state, state_id);
8868 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
8869 const struct wined3d_state *state, DWORD state_id)
8871 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8872 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8873 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8876 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8877 const struct wined3d_state *state, DWORD state_id)
8879 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8882 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8883 const struct wined3d_state *state, DWORD state_id)
8885 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8888 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8889 const struct wined3d_state *state, DWORD state_id)
8891 const struct wined3d_gl_info *gl_info = context->gl_info;
8892 int glParm;
8893 float ref;
8895 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8897 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8899 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8900 checkGLcall("glEnable GL_ALPHA_TEST");
8902 else
8904 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8905 checkGLcall("glDisable GL_ALPHA_TEST");
8906 return;
8909 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8910 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8912 if (glParm)
8914 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8915 checkGLcall("glAlphaFunc");
8919 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8920 const struct wined3d_state *state, DWORD state_id)
8922 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8925 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
8926 const struct wined3d_state *state, DWORD state_id)
8928 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8931 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8933 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8934 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
8935 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8936 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8937 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8938 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8939 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8940 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8941 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8942 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8943 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8944 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8945 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8946 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8947 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8948 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8949 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8950 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8951 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8952 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8953 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8954 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8955 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8956 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8957 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8958 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8959 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8960 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8961 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8962 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8963 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8964 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8965 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8966 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8967 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8968 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8969 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8970 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8971 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8972 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8973 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8974 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8975 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8976 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8977 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8978 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8979 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8980 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8981 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8982 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8983 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8984 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8985 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8986 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8987 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8988 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8989 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8990 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8991 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8992 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8993 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8994 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8995 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8996 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8997 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8998 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8999 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9000 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9001 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9002 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9003 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9004 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9005 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9006 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9007 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9008 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9009 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9010 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9011 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
9012 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9013 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9014 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9015 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9016 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9017 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9018 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9019 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9020 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9021 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9022 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9023 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9024 {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 },
9025 {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 },
9026 {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 },
9027 {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 },
9028 {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 },
9029 {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 },
9030 {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 },
9031 {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 },
9032 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9033 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9034 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9035 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9036 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9037 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9038 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9039 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9040 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9041 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9042 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9043 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9044 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9047 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9049 return TRUE;
9052 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9056 const struct fragment_pipeline glsl_fragment_pipe =
9058 glsl_fragment_pipe_enable,
9059 glsl_fragment_pipe_get_caps,
9060 glsl_fragment_pipe_get_emul_mask,
9061 glsl_fragment_pipe_alloc,
9062 glsl_fragment_pipe_free,
9063 glsl_fragment_pipe_alloc_context_data,
9064 glsl_fragment_pipe_free_context_data,
9065 shader_glsl_color_fixup_supported,
9066 glsl_fragment_pipe_state_template,