wined3d: Implement aoffimmi modifier for SM4 sample instructions in GLSL backend.
[wine.git] / dlls / wined3d / glsl_shader.c
blob85244c81673aa954677180ae521667d12c651086
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 const char *fmt = "%#x";
327 char str[4][17] = {{0}};
328 int i;
330 if (1 > size || size > 4)
332 ERR("Invalid vector size %u.\n", size);
333 return;
336 for (i = size - 1; i >= 0; --i)
338 sprintf(str[i], fmt, values[i]);
339 fmt = "%#x, ";
342 if (size > 1)
343 shader_addline(buffer, "ivec%u(%s%s%s%s)", size, str[0], str[1], str[2], str[3]);
344 else
345 shader_addline(buffer, str[0]);
348 static const char *get_info_log_line(const char **ptr)
350 const char *p, *q;
352 p = *ptr;
353 if (!(q = strstr(p, "\n")))
355 if (!*p) return NULL;
356 *ptr += strlen(p);
357 return p;
359 *ptr = q + 1;
361 return p;
364 /* Context activation is done by the caller. */
365 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
367 int length = 0;
368 char *log;
370 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
371 return;
373 if (program)
374 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
375 else
376 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
378 /* A size of 1 is just a null-terminated string, so the log should be bigger than
379 * that if there are errors. */
380 if (length > 1)
382 const char *ptr, *line;
384 log = HeapAlloc(GetProcessHeap(), 0, length);
385 /* The info log is supposed to be zero-terminated, but at least some
386 * versions of fglrx don't terminate the string properly. The reported
387 * length does include the terminator, so explicitly set it to zero
388 * here. */
389 log[length - 1] = 0;
390 if (program)
391 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
392 else
393 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
395 ptr = log;
396 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
398 WARN("Info log received from GLSL shader #%u:\n", id);
399 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
401 else
403 FIXME("Info log received from GLSL shader #%u:\n", id);
404 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
406 HeapFree(GetProcessHeap(), 0, log);
410 /* Context activation is done by the caller. */
411 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
413 const char *ptr, *line;
415 TRACE("Compiling shader object %u.\n", shader);
417 if (TRACE_ON(d3d_shader))
419 ptr = src;
420 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
423 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
424 checkGLcall("glShaderSource");
425 GL_EXTCALL(glCompileShader(shader));
426 checkGLcall("glCompileShader");
427 print_glsl_info_log(gl_info, shader, FALSE);
430 /* Context activation is done by the caller. */
431 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
433 GLint i, shader_count, source_size = -1;
434 GLuint *shaders;
435 char *source = NULL;
437 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
438 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
439 if (!shaders)
441 ERR("Failed to allocate shader array memory.\n");
442 return;
445 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
446 for (i = 0; i < shader_count; ++i)
448 const char *ptr, *line;
449 GLint tmp;
451 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
453 if (source_size < tmp)
455 HeapFree(GetProcessHeap(), 0, source);
457 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
458 if (!source)
460 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
461 HeapFree(GetProcessHeap(), 0, shaders);
462 return;
464 source_size = tmp;
467 FIXME("Shader %u:\n", shaders[i]);
468 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
469 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
470 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
471 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
472 FIXME("\n");
474 ptr = source;
475 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
476 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
477 FIXME("\n");
480 HeapFree(GetProcessHeap(), 0, source);
481 HeapFree(GetProcessHeap(), 0, shaders);
484 /* Context activation is done by the caller. */
485 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
487 GLint tmp;
489 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
490 return;
492 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
493 if (!tmp)
495 FIXME("Program %u link status invalid.\n", program);
496 shader_glsl_dump_program_source(gl_info, program);
499 print_glsl_info_log(gl_info, program, TRUE);
502 /* Context activation is done by the caller. */
503 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
504 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
506 unsigned int mapped_unit;
507 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
508 const char *prefix;
509 unsigned int i, j;
510 GLint name_loc;
512 static const struct
514 enum wined3d_shader_type type;
515 unsigned int base_idx;
516 unsigned int count;
518 sampler_info[] =
520 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
521 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
524 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
526 prefix = shader_glsl_get_prefix(sampler_info[i].type);
528 for (j = 0; j < sampler_info[i].count; ++j)
530 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
531 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
532 if (name_loc == -1)
533 continue;
535 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
536 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
538 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
539 continue;
542 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
543 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
546 checkGLcall("glUniform1i");
547 string_buffer_release(&priv->string_buffers, sampler_name);
550 /* Context activation is done by the caller. */
551 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
552 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
554 unsigned int start = ~0U, end = 0;
555 int stack_idx = 0;
556 unsigned int heap_idx = 1;
557 unsigned int idx;
559 if (heap->entries[heap_idx].version <= version) return;
561 idx = heap->entries[heap_idx].idx;
562 if (constant_locations[idx] != -1)
563 start = end = idx;
564 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
566 while (stack_idx >= 0)
568 /* Note that we fall through to the next case statement. */
569 switch(stack[stack_idx])
571 case HEAP_NODE_TRAVERSE_LEFT:
573 unsigned int left_idx = heap_idx << 1;
574 if (left_idx < heap->size && heap->entries[left_idx].version > version)
576 heap_idx = left_idx;
577 idx = heap->entries[heap_idx].idx;
578 if (constant_locations[idx] != -1)
580 if (start > idx)
581 start = idx;
582 if (end < idx)
583 end = idx;
586 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
587 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
588 break;
592 case HEAP_NODE_TRAVERSE_RIGHT:
594 unsigned int right_idx = (heap_idx << 1) + 1;
595 if (right_idx < heap->size && heap->entries[right_idx].version > version)
597 heap_idx = right_idx;
598 idx = heap->entries[heap_idx].idx;
599 if (constant_locations[idx] != -1)
601 if (start > idx)
602 start = idx;
603 if (end < idx)
604 end = idx;
607 stack[stack_idx++] = HEAP_NODE_POP;
608 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
609 break;
613 case HEAP_NODE_POP:
614 heap_idx >>= 1;
615 --stack_idx;
616 break;
619 if (start <= end)
620 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
621 checkGLcall("walk_constant_heap()");
624 /* Context activation is done by the caller. */
625 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
627 GLfloat clamped_constant[4];
629 if (location == -1) return;
631 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
632 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
633 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
634 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
636 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
639 /* Context activation is done by the caller. */
640 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
641 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
643 int stack_idx = 0;
644 unsigned int heap_idx = 1;
645 unsigned int idx;
647 if (heap->entries[heap_idx].version <= version) return;
649 idx = heap->entries[heap_idx].idx;
650 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
651 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
653 while (stack_idx >= 0)
655 /* Note that we fall through to the next case statement. */
656 switch(stack[stack_idx])
658 case HEAP_NODE_TRAVERSE_LEFT:
660 unsigned int left_idx = heap_idx << 1;
661 if (left_idx < heap->size && heap->entries[left_idx].version > version)
663 heap_idx = left_idx;
664 idx = heap->entries[heap_idx].idx;
665 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
667 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
668 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
669 break;
673 case HEAP_NODE_TRAVERSE_RIGHT:
675 unsigned int right_idx = (heap_idx << 1) + 1;
676 if (right_idx < heap->size && heap->entries[right_idx].version > version)
678 heap_idx = right_idx;
679 idx = heap->entries[heap_idx].idx;
680 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
682 stack[stack_idx++] = HEAP_NODE_POP;
683 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
684 break;
688 case HEAP_NODE_POP:
689 heap_idx >>= 1;
690 --stack_idx;
691 break;
694 checkGLcall("walk_constant_heap_clamped()");
697 /* Context activation is done by the caller. */
698 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
699 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
700 unsigned char *stack, UINT version)
702 const struct wined3d_shader_lconst *lconst;
704 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
705 if (shader->reg_maps.shader_version.major == 1
706 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
707 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
708 else
709 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
711 if (!shader->load_local_constsF)
713 TRACE("No need to load local float constants for this shader\n");
714 return;
717 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
718 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
720 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
722 checkGLcall("glUniform4fv()");
725 /* Context activation is done by the caller. */
726 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
727 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
729 unsigned int i;
730 struct list* ptr;
732 for (i = 0; constants_set; constants_set >>= 1, ++i)
734 if (!(constants_set & 1)) continue;
736 /* We found this uniform name in the program - go ahead and send the data */
737 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
740 /* Load immediate constants */
741 ptr = list_head(&shader->constantsI);
742 while (ptr)
744 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
745 unsigned int idx = lconst->idx;
746 const GLint *values = (const GLint *)lconst->value;
748 /* We found this uniform name in the program - go ahead and send the data */
749 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
750 ptr = list_next(&shader->constantsI, ptr);
752 checkGLcall("glUniform4iv()");
755 /* Context activation is done by the caller. */
756 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
757 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
759 unsigned int i;
760 struct list* ptr;
762 for (i = 0; constants_set; constants_set >>= 1, ++i)
764 if (!(constants_set & 1)) continue;
766 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
769 /* Load immediate constants */
770 ptr = list_head(&shader->constantsB);
771 while (ptr)
773 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
774 unsigned int idx = lconst->idx;
775 const GLint *values = (const GLint *)lconst->value;
777 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
778 ptr = list_next(&shader->constantsB, ptr);
780 checkGLcall("glUniform1iv()");
783 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
785 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
788 /* Context activation is done by the caller (state handler). */
789 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
790 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
792 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
793 UINT fixup = ps->np2_fixup_info->active;
794 UINT i;
796 for (i = 0; fixup; fixup >>= 1, ++i)
798 const struct wined3d_texture *tex = state->textures[i];
799 unsigned char idx = ps->np2_fixup_info->idx[i];
800 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
802 if (!tex)
804 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
805 continue;
808 if (idx % 2)
810 tex_dim[2] = tex->pow2_matrix[0];
811 tex_dim[3] = tex->pow2_matrix[5];
813 else
815 tex_dim[0] = tex->pow2_matrix[0];
816 tex_dim[1] = tex->pow2_matrix[5];
820 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
823 /* Taken and adapted from Mesa. */
824 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
826 float pos, neg, t, det;
827 struct wined3d_matrix temp;
829 /* Calculate the determinant of upper left 3x3 submatrix and
830 * determine if the matrix is singular. */
831 pos = neg = 0.0f;
832 t = in->_11 * in->_22 * in->_33;
833 if (t >= 0.0f)
834 pos += t;
835 else
836 neg += t;
838 t = in->_21 * in->_32 * in->_13;
839 if (t >= 0.0f)
840 pos += t;
841 else
842 neg += t;
843 t = in->_31 * in->_12 * in->_23;
844 if (t >= 0.0f)
845 pos += t;
846 else
847 neg += t;
849 t = -in->_31 * in->_22 * in->_13;
850 if (t >= 0.0f)
851 pos += t;
852 else
853 neg += t;
854 t = -in->_21 * in->_12 * in->_33;
855 if (t >= 0.0f)
856 pos += t;
857 else
858 neg += t;
860 t = -in->_11 * in->_32 * in->_23;
861 if (t >= 0.0f)
862 pos += t;
863 else
864 neg += t;
866 det = pos + neg;
868 if (fabsf(det) < 1e-25f)
869 return FALSE;
871 det = 1.0f / det;
872 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
873 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
874 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
875 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
876 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
877 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
878 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
879 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
880 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
882 *out = temp;
883 return TRUE;
886 static void swap_rows(float **a, float **b)
888 float *tmp = *a;
890 *a = *b;
891 *b = tmp;
894 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
896 float wtmp[4][8];
897 float m0, m1, m2, m3, s;
898 float *r0, *r1, *r2, *r3;
900 r0 = wtmp[0];
901 r1 = wtmp[1];
902 r2 = wtmp[2];
903 r3 = wtmp[3];
905 r0[0] = m->_11;
906 r0[1] = m->_12;
907 r0[2] = m->_13;
908 r0[3] = m->_14;
909 r0[4] = 1.0f;
910 r0[5] = r0[6] = r0[7] = 0.0f;
912 r1[0] = m->_21;
913 r1[1] = m->_22;
914 r1[2] = m->_23;
915 r1[3] = m->_24;
916 r1[5] = 1.0f;
917 r1[4] = r1[6] = r1[7] = 0.0f;
919 r2[0] = m->_31;
920 r2[1] = m->_32;
921 r2[2] = m->_33;
922 r2[3] = m->_34;
923 r2[6] = 1.0f;
924 r2[4] = r2[5] = r2[7] = 0.0f;
926 r3[0] = m->_41;
927 r3[1] = m->_42;
928 r3[2] = m->_43;
929 r3[3] = m->_44;
930 r3[7] = 1.0f;
931 r3[4] = r3[5] = r3[6] = 0.0f;
933 /* Choose pivot - or die. */
934 if (fabsf(r3[0]) > fabsf(r2[0]))
935 swap_rows(&r3, &r2);
936 if (fabsf(r2[0]) > fabsf(r1[0]))
937 swap_rows(&r2, &r1);
938 if (fabsf(r1[0]) > fabsf(r0[0]))
939 swap_rows(&r1, &r0);
940 if (r0[0] == 0.0f)
941 return FALSE;
943 /* Eliminate first variable. */
944 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
945 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
946 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
947 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
948 s = r0[4];
949 if (s != 0.0f)
951 r1[4] -= m1 * s;
952 r2[4] -= m2 * s;
953 r3[4] -= m3 * s;
955 s = r0[5];
956 if (s != 0.0f)
958 r1[5] -= m1 * s;
959 r2[5] -= m2 * s;
960 r3[5] -= m3 * s;
962 s = r0[6];
963 if (s != 0.0f)
965 r1[6] -= m1 * s;
966 r2[6] -= m2 * s;
967 r3[6] -= m3 * s;
969 s = r0[7];
970 if (s != 0.0f)
972 r1[7] -= m1 * s;
973 r2[7] -= m2 * s;
974 r3[7] -= m3 * s;
977 /* Choose pivot - or die. */
978 if (fabsf(r3[1]) > fabsf(r2[1]))
979 swap_rows(&r3, &r2);
980 if (fabsf(r2[1]) > fabsf(r1[1]))
981 swap_rows(&r2, &r1);
982 if (r1[1] == 0.0f)
983 return FALSE;
985 /* Eliminate second variable. */
986 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
987 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
988 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
989 s = r1[4];
990 if (s != 0.0f)
992 r2[4] -= m2 * s;
993 r3[4] -= m3 * s;
995 s = r1[5];
996 if (s != 0.0f)
998 r2[5] -= m2 * s;
999 r3[5] -= m3 * s;
1001 s = r1[6];
1002 if (s != 0.0f)
1004 r2[6] -= m2 * s;
1005 r3[6] -= m3 * s;
1007 s = r1[7];
1008 if (s != 0.0f)
1010 r2[7] -= m2 * s;
1011 r3[7] -= m3 * s;
1014 /* Choose pivot - or die. */
1015 if (fabsf(r3[2]) > fabsf(r2[2]))
1016 swap_rows(&r3, &r2);
1017 if (r2[2] == 0.0f)
1018 return FALSE;
1020 /* Eliminate third variable. */
1021 m3 = r3[2] / r2[2];
1022 r3[3] -= m3 * r2[3];
1023 r3[4] -= m3 * r2[4];
1024 r3[5] -= m3 * r2[5];
1025 r3[6] -= m3 * r2[6];
1026 r3[7] -= m3 * r2[7];
1028 /* Last check. */
1029 if (r3[3] == 0.0f)
1030 return FALSE;
1032 /* Back substitute row 3. */
1033 s = 1.0f / r3[3];
1034 r3[4] *= s;
1035 r3[5] *= s;
1036 r3[6] *= s;
1037 r3[7] *= s;
1039 /* Back substitute row 2. */
1040 m2 = r2[3];
1041 s = 1.0f / r2[2];
1042 r2[4] = s * (r2[4] - r3[4] * m2);
1043 r2[5] = s * (r2[5] - r3[5] * m2);
1044 r2[6] = s * (r2[6] - r3[6] * m2);
1045 r2[7] = s * (r2[7] - r3[7] * m2);
1046 m1 = r1[3];
1047 r1[4] -= r3[4] * m1;
1048 r1[5] -= r3[5] * m1;
1049 r1[6] -= r3[6] * m1;
1050 r1[7] -= r3[7] * m1;
1051 m0 = r0[3];
1052 r0[4] -= r3[4] * m0;
1053 r0[5] -= r3[5] * m0;
1054 r0[6] -= r3[6] * m0;
1055 r0[7] -= r3[7] * m0;
1057 /* Back substitute row 1. */
1058 m1 = r1[2];
1059 s = 1.0f / r1[1];
1060 r1[4] = s * (r1[4] - r2[4] * m1);
1061 r1[5] = s * (r1[5] - r2[5] * m1);
1062 r1[6] = s * (r1[6] - r2[6] * m1);
1063 r1[7] = s * (r1[7] - r2[7] * m1);
1064 m0 = r0[2];
1065 r0[4] -= r2[4] * m0;
1066 r0[5] -= r2[5] * m0;
1067 r0[6] -= r2[6] * m0;
1068 r0[7] -= r2[7] * m0;
1070 /* Back substitute row 0. */
1071 m0 = r0[1];
1072 s = 1.0f / r0[0];
1073 r0[4] = s * (r0[4] - r1[4] * m0);
1074 r0[5] = s * (r0[5] - r1[5] * m0);
1075 r0[6] = s * (r0[6] - r1[6] * m0);
1076 r0[7] = s * (r0[7] - r1[7] * m0);
1078 out->_11 = r0[4];
1079 out->_12 = r0[5];
1080 out->_13 = r0[6];
1081 out->_14 = r0[7];
1082 out->_21 = r1[4];
1083 out->_22 = r1[5];
1084 out->_23 = r1[6];
1085 out->_24 = r1[7];
1086 out->_31 = r2[4];
1087 out->_32 = r2[5];
1088 out->_33 = r2[6];
1089 out->_34 = r2[7];
1090 out->_41 = r3[4];
1091 out->_42 = r3[5];
1092 out->_43 = r3[6];
1093 out->_44 = r3[7];
1095 return TRUE;
1098 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1099 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1101 const struct wined3d_gl_info *gl_info = context->gl_info;
1102 float mat[3 * 3];
1103 struct wined3d_matrix mv;
1104 unsigned int i, j;
1106 if (prog->vs.normal_matrix_location == -1)
1107 return;
1109 get_modelview_matrix(context, state, 0, &mv);
1110 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1111 invert_matrix_3d(&mv, &mv);
1112 else
1113 invert_matrix(&mv, &mv);
1114 /* Tests show that singular modelview matrices are used unchanged as normal
1115 * matrices on D3D3 and older. There seems to be no clearly consistent
1116 * behavior on newer D3D versions so always follow older ddraw behavior. */
1117 for (i = 0; i < 3; ++i)
1118 for (j = 0; j < 3; ++j)
1119 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1121 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1122 checkGLcall("glUniformMatrix3fv");
1125 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1126 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1128 const struct wined3d_gl_info *gl_info = context->gl_info;
1129 struct wined3d_matrix mat;
1131 if (tex >= MAX_TEXTURES)
1132 return;
1133 if (prog->vs.texture_matrix_location[tex] == -1)
1134 return;
1136 get_texture_matrix(context, state, tex, &mat);
1137 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1138 checkGLcall("glUniformMatrix4fv");
1141 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1142 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1144 const struct wined3d_gl_info *gl_info = context->gl_info;
1146 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1148 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1149 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1151 else
1153 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1155 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1157 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1158 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1159 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1160 checkGLcall("setting FFP material uniforms");
1163 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1164 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1166 const struct wined3d_gl_info *gl_info = context->gl_info;
1167 struct wined3d_color color;
1169 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1170 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1171 checkGLcall("glUniform3fv");
1174 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1175 const struct wined3d_matrix *src2)
1177 struct wined3d_vec4 temp;
1179 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1180 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1181 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1182 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1184 *dest = temp;
1187 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1188 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1190 const struct wined3d_gl_info *gl_info = context->gl_info;
1191 const struct wined3d_light_info *light_info = state->lights[light];
1192 struct wined3d_vec4 vec4;
1193 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1195 if (!light_info)
1196 return;
1198 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1199 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1200 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1202 switch (light_info->OriginalParms.type)
1204 case WINED3D_LIGHT_POINT:
1205 multiply_vector_matrix(&vec4, &light_info->position, view);
1206 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1207 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1208 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1209 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1210 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1211 break;
1213 case WINED3D_LIGHT_SPOT:
1214 multiply_vector_matrix(&vec4, &light_info->position, view);
1215 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1217 multiply_vector_matrix(&vec4, &light_info->direction, view);
1218 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1220 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1221 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1222 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1223 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1224 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1225 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1226 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1227 break;
1229 case WINED3D_LIGHT_DIRECTIONAL:
1230 multiply_vector_matrix(&vec4, &light_info->direction, view);
1231 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1232 break;
1234 case WINED3D_LIGHT_PARALLELPOINT:
1235 multiply_vector_matrix(&vec4, &light_info->position, view);
1236 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1237 break;
1239 default:
1240 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1242 checkGLcall("setting FFP lights uniforms");
1245 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1246 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1248 const struct wined3d_gl_info *gl_info = context->gl_info;
1249 float min, max;
1250 float size, att[3];
1252 get_pointsize_minmax(context, state, &min, &max);
1254 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1255 checkGLcall("glUniform1f");
1256 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1257 checkGLcall("glUniform1f");
1259 get_pointsize(context, state, &size, att);
1261 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1262 checkGLcall("glUniform1f");
1263 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1264 checkGLcall("glUniform1f");
1265 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1266 checkGLcall("glUniform1f");
1267 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1268 checkGLcall("glUniform1f");
1271 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1272 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1274 const struct wined3d_gl_info *gl_info = context->gl_info;
1275 struct wined3d_color color;
1276 float start, end, scale;
1277 union
1279 DWORD d;
1280 float f;
1281 } tmpvalue;
1283 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1284 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1285 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1286 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1287 get_fog_start_end(context, state, &start, &end);
1288 scale = 1.0f / (end - start);
1289 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1290 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1291 checkGLcall("fog emulation uniforms");
1294 /* Context activation is done by the caller (state handler). */
1295 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1296 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1298 struct wined3d_color float_key[2];
1299 const struct wined3d_texture *texture = state->textures[0];
1301 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1302 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1305 /* Context activation is done by the caller (state handler). */
1306 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1307 const struct wined3d_state *state)
1309 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1310 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1311 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1312 const struct wined3d_gl_info *gl_info = context->gl_info;
1313 struct shader_glsl_priv *priv = shader_priv;
1314 float position_fixup[4];
1315 DWORD update_mask;
1317 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1318 UINT constant_version;
1319 int i;
1321 if (!prog) {
1322 /* No GLSL program set - nothing to do. */
1323 return;
1325 constant_version = prog->constant_version;
1326 update_mask = context->constant_update_mask & prog->constant_update_mask;
1328 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1329 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1330 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1332 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1333 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1334 vshader->reg_maps.integer_constants);
1336 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1337 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1338 vshader->reg_maps.boolean_constants);
1340 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1341 shader_glsl_pointsize_uniform(context, state, prog);
1343 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1345 shader_get_position_fixup(context, state, position_fixup);
1346 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1347 checkGLcall("glUniform4fv");
1350 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1352 struct wined3d_matrix mat;
1354 get_modelview_matrix(context, state, 0, &mat);
1355 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1356 checkGLcall("glUniformMatrix4fv");
1358 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1361 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1363 struct wined3d_matrix mat;
1365 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1367 if (prog->vs.modelview_matrix_location[i] == -1)
1368 break;
1370 get_modelview_matrix(context, state, i, &mat);
1371 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1372 checkGLcall("glUniformMatrix4fv");
1376 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1378 struct wined3d_matrix projection;
1380 get_projection_matrix(context, state, &projection);
1381 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1382 checkGLcall("glUniformMatrix4fv");
1385 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1387 for (i = 0; i < MAX_TEXTURES; ++i)
1388 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1391 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1392 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1394 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1396 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1397 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1398 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1401 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1402 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1403 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1405 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1406 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1407 pshader->reg_maps.integer_constants);
1409 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1410 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1411 pshader->reg_maps.boolean_constants);
1413 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1415 for (i = 0; i < MAX_TEXTURES; ++i)
1417 if (prog->ps.bumpenv_mat_location[i] == -1)
1418 continue;
1420 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1421 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1423 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1425 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1426 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1427 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1428 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1432 checkGLcall("bump env uniforms");
1435 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1437 float correction_params[] =
1439 /* position is window relative, not viewport relative */
1440 context->render_offscreen ? 0.0f : (float)context->current_rt->resource.height,
1441 context->render_offscreen ? 1.0f : -1.0f,
1442 0.0f,
1443 0.0f,
1446 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
1449 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1450 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1451 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1452 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1454 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1456 struct wined3d_color color;
1458 if (prog->ps.tex_factor_location != -1)
1460 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1461 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1464 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1465 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1466 else
1467 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1469 for (i = 0; i < MAX_TEXTURES; ++i)
1471 if (prog->ps.tss_constant_location[i] == -1)
1472 continue;
1474 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1475 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1478 checkGLcall("fixed function uniforms");
1481 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1482 shader_glsl_load_fog_uniform(context, state, prog);
1484 if (priv->next_constant_version == UINT_MAX)
1486 TRACE("Max constant version reached, resetting to 0.\n");
1487 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1488 priv->next_constant_version = 1;
1490 else
1492 prog->constant_version = priv->next_constant_version++;
1496 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1498 struct constant_entry *entries = heap->entries;
1499 unsigned int *positions = heap->positions;
1500 unsigned int heap_idx, parent_idx;
1502 if (!heap->contained[idx])
1504 heap_idx = heap->size++;
1505 heap->contained[idx] = TRUE;
1507 else
1509 heap_idx = positions[idx];
1512 while (heap_idx > 1)
1514 parent_idx = heap_idx >> 1;
1516 if (new_version <= entries[parent_idx].version) break;
1518 entries[heap_idx] = entries[parent_idx];
1519 positions[entries[parent_idx].idx] = heap_idx;
1520 heap_idx = parent_idx;
1523 entries[heap_idx].version = new_version;
1524 entries[heap_idx].idx = idx;
1525 positions[idx] = heap_idx;
1528 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1530 struct shader_glsl_priv *priv = device->shader_priv;
1531 struct constant_heap *heap = &priv->vconst_heap;
1532 UINT i;
1534 for (i = start; i < count + start; ++i)
1536 update_heap_entry(heap, i, priv->next_constant_version);
1539 for (i = 0; i < device->context_count; ++i)
1541 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1545 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1547 struct shader_glsl_priv *priv = device->shader_priv;
1548 struct constant_heap *heap = &priv->pconst_heap;
1549 UINT i;
1551 for (i = start; i < count + start; ++i)
1553 update_heap_entry(heap, i, priv->next_constant_version);
1556 for (i = 0; i < device->context_count; ++i)
1558 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1562 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1564 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1565 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1566 if(shader_major > 3) return ret;
1568 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1569 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1570 return ret;
1573 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1575 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1578 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1579 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1581 va_list args;
1582 int ret;
1584 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1585 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1586 for (;;)
1588 va_start(args, format);
1589 ret = shader_vaddline(buffer, format, args);
1590 va_end(args);
1591 if (!ret)
1592 return;
1593 if (!string_buffer_resize(buffer, ret))
1594 return;
1598 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1599 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1601 va_list args;
1602 int ret;
1604 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1605 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1606 for (;;)
1608 va_start(args, format);
1609 ret = shader_vaddline(buffer, format, args);
1610 va_end(args);
1611 if (!ret)
1612 return;
1613 if (!string_buffer_resize(buffer, ret))
1614 return;
1618 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1620 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1621 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1622 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1623 unsigned int i;
1625 if (reg_maps->shader_version.major < 3)
1626 return input_reg_used[idx];
1628 for (i = 0; i < input_signature->element_count; ++i)
1630 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1632 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1633 continue;
1635 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1636 && input->semantic_idx == idx)
1638 if (input_reg_used[input->register_idx])
1639 return TRUE;
1640 else
1641 return FALSE;
1644 return FALSE;
1647 /** Generate the variable & register declarations for the GLSL output target */
1648 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1649 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1650 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1652 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1653 const struct wined3d_state *state = &shader->device->state;
1654 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1655 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1656 const struct wined3d_gl_info *gl_info = context->gl_info;
1657 const struct wined3d_fb_state *fb = &shader->device->fb;
1658 unsigned int i, extra_constants_needed = 0;
1659 const struct wined3d_shader_lconst *lconst;
1660 const char *prefix;
1661 DWORD map;
1663 prefix = shader_glsl_get_prefix(version->type);
1665 /* Prototype the subroutines */
1666 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1668 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1671 /* Declare the constants (aka uniforms) */
1672 if (shader->limits->constant_float > 0)
1674 unsigned max_constantsF;
1676 /* Unless the shader uses indirect addressing, always declare the
1677 * maximum array size and ignore that we need some uniforms privately.
1678 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1679 * and immediate values, still declare VC[256]. If the shader needs
1680 * more uniforms than we have it won't work in any case. If it uses
1681 * less, the compiler will figure out which uniforms are really used
1682 * and strip them out. This allows a shader to use c255 on a dx9 card,
1683 * as long as it doesn't also use all the other constants.
1685 * If the shader uses indirect addressing the compiler must assume
1686 * that all declared uniforms are used. In this case, declare only the
1687 * amount that we're assured to have.
1689 * Thus we run into problems in these two cases:
1690 * 1) The shader really uses more uniforms than supported.
1691 * 2) The shader uses indirect addressing, less constants than
1692 * supported, but uses a constant index > #supported consts. */
1693 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1695 /* No indirect addressing here. */
1696 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1698 else
1700 if (reg_maps->usesrelconstF)
1702 /* Subtract the other potential uniforms from the max
1703 * available (bools, ints, and 1 row of projection matrix).
1704 * Subtract another uniform for immediate values, which have
1705 * to be loaded via uniform by the driver as well. The shader
1706 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1707 * shader code, so one vec4 should be enough. (Unfortunately
1708 * the Nvidia driver doesn't store 128 and -128 in one float).
1710 * Writing gl_ClipVertex requires one uniform for each
1711 * clipplane as well. */
1712 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1713 if (vs_args->clip_enabled)
1714 max_constantsF -= gl_info->limits.clipplanes;
1715 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1716 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1717 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1718 * for now take this into account when calculating the number of available constants
1720 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1721 /* Set by driver quirks in directx.c */
1722 max_constantsF -= gl_info->reserved_glsl_constants;
1724 if (max_constantsF < shader->limits->constant_float)
1726 static unsigned int once;
1728 if (!once++)
1729 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1730 " it may not render correctly.\n");
1731 else
1732 WARN("The hardware does not support enough uniform components to run this shader.\n");
1735 else
1737 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1740 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1741 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1744 /* Always declare the full set of constants, the compiler can remove the
1745 * unused ones because d3d doesn't (yet) support indirect int and bool
1746 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1747 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1748 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1750 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1751 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1753 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1755 if (reg_maps->cb_sizes[i])
1756 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1757 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1760 /* Declare texture samplers */
1761 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1763 struct wined3d_shader_sampler_map_entry *entry;
1764 const char *sampler_type_prefix, *sampler_type;
1765 BOOL shadow_sampler, tex_rect;
1767 entry = &reg_maps->sampler_map.entries[i];
1769 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1771 ERR("Invalid resource index %u.\n", entry->resource_idx);
1772 continue;
1775 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1777 case WINED3D_DATA_FLOAT:
1778 case WINED3D_DATA_UNORM:
1779 case WINED3D_DATA_SNORM:
1780 sampler_type_prefix = "";
1781 break;
1783 case WINED3D_DATA_INT:
1784 sampler_type_prefix = "i";
1785 break;
1787 case WINED3D_DATA_UINT:
1788 sampler_type_prefix = "u";
1789 break;
1791 default:
1792 sampler_type_prefix = "";
1793 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1794 break;
1797 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << entry->sampler_idx));
1798 switch (reg_maps->resource_info[entry->resource_idx].type)
1800 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1801 if (shadow_sampler)
1802 sampler_type = "sampler1DShadow";
1803 else
1804 sampler_type = "sampler1D";
1805 break;
1807 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1808 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1809 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1810 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1811 if (shadow_sampler)
1813 if (tex_rect)
1814 sampler_type = "sampler2DRectShadow";
1815 else
1816 sampler_type = "sampler2DShadow";
1818 else
1820 if (tex_rect)
1821 sampler_type = "sampler2DRect";
1822 else
1823 sampler_type = "sampler2D";
1825 break;
1827 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1828 if (shadow_sampler)
1829 FIXME("Unsupported 3D shadow sampler.\n");
1830 sampler_type = "sampler3D";
1831 break;
1833 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1834 if (shadow_sampler)
1835 FIXME("Unsupported Cube shadow sampler.\n");
1836 sampler_type = "samplerCube";
1837 break;
1839 default:
1840 sampler_type = "unsupported_sampler";
1841 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1842 break;
1844 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
1845 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
1848 /* Declare uniforms for NP2 texcoord fixup:
1849 * This is NOT done inside the loop that declares the texture samplers
1850 * since the NP2 fixup code is currently only used for the GeforceFX
1851 * series and when forcing the ARB_npot extension off. Modern cards just
1852 * skip the code anyway, so put it inside a separate loop. */
1853 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1855 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1856 UINT cur = 0;
1858 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1859 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1860 * samplerNP2Fixup stores texture dimensions and is updated through
1861 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1863 for (i = 0; i < shader->limits->sampler; ++i)
1865 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1866 continue;
1868 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1870 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1871 continue;
1874 fixup->idx[i] = cur++;
1877 fixup->num_consts = (cur + 1) >> 1;
1878 fixup->active = ps_args->np2_fixup;
1879 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1882 /* Declare address variables */
1883 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1885 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1888 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1890 for (i = 0; i < shader->input_signature.element_count; ++i)
1892 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1893 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1894 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1895 prefix, e->register_idx);
1896 else
1897 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1900 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1902 shader_addline(buffer, "uniform struct\n{\n");
1903 shader_addline(buffer, " float size;\n");
1904 shader_addline(buffer, " float size_min;\n");
1905 shader_addline(buffer, " float size_max;\n");
1906 shader_addline(buffer, "} ffp_point;\n");
1909 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1911 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1912 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
1913 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1914 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1917 shader_addline(buffer, "uniform vec4 posFixup;\n");
1918 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1920 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1922 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1924 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1926 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1928 shader_addline(buffer, "uniform struct\n{\n");
1929 shader_addline(buffer, " vec4 color;\n");
1930 shader_addline(buffer, " float density;\n");
1931 shader_addline(buffer, " float end;\n");
1932 shader_addline(buffer, " float scale;\n");
1933 shader_addline(buffer, "} ffp_fog;\n");
1935 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1937 if (glsl_is_color_reg_read(shader, 0))
1938 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
1939 if (glsl_is_color_reg_read(shader, 1))
1940 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
1941 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1942 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
1944 else
1946 if (glsl_is_color_reg_read(shader, 0))
1947 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1948 if (glsl_is_color_reg_read(shader, 1))
1949 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
1950 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1951 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1952 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1956 if (version->major >= 3)
1958 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1960 if (use_vs(state))
1961 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
1962 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1965 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1967 if (!(map & 1))
1968 continue;
1970 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1972 if (reg_maps->luminanceparams & (1u << i))
1974 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1975 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1976 extra_constants_needed++;
1979 extra_constants_needed++;
1982 if (ps_args->srgb_correction)
1984 shader_addline(buffer, "const vec4 srgb_const0 = ");
1985 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1986 shader_addline(buffer, ";\n");
1987 shader_addline(buffer, "const vec4 srgb_const1 = ");
1988 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1989 shader_addline(buffer, ";\n");
1991 if (reg_maps->vpos || reg_maps->usesdsy)
1993 if (shader->limits->constant_float + extra_constants_needed
1994 + 1 < gl_info->limits.glsl_ps_float_constants)
1996 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1997 extra_constants_needed++;
1999 else
2001 float ycorrection[] =
2003 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
2004 context->render_offscreen ? 1.0f : -1.0f,
2005 0.0f,
2006 0.0f,
2009 /* This happens because we do not have proper tracking of the
2010 * constant registers that are actually used, only the max
2011 * limit of the shader version. */
2012 FIXME("Cannot find a free uniform for vpos correction params\n");
2013 shader_addline(buffer, "const vec4 ycorrection = ");
2014 shader_glsl_append_imm_vec4(buffer, ycorrection);
2015 shader_addline(buffer, ";\n");
2017 shader_addline(buffer, "vec4 vpos;\n");
2021 /* Declare output register temporaries */
2022 if (shader->limits->packed_output)
2023 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2025 /* Declare temporary variables */
2026 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2028 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
2031 /* Declare loop registers aLx */
2032 if (version->major < 4)
2034 for (i = 0; i < reg_maps->loop_depth; ++i)
2036 shader_addline(buffer, "int aL%u;\n", i);
2037 shader_addline(buffer, "int tmpInt%u;\n", i);
2041 /* Temporary variables for matrix operations */
2042 shader_addline(buffer, "vec4 tmp0;\n");
2043 shader_addline(buffer, "vec4 tmp1;\n");
2045 if (!shader->load_local_constsF)
2047 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2049 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2050 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2051 shader_addline(buffer, ";\n");
2055 /* Start the main program. */
2056 shader_addline(buffer, "void main()\n{\n");
2058 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2059 * add approximately 0.5. This causes off-by-one problems as spotted by
2060 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2061 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2062 * causes precision troubles when we just subtract 0.5.
2064 * To deal with that, just floor() the position. This will eliminate the
2065 * fraction on all cards.
2067 * TODO: Test how this behaves with multisampling.
2069 * An advantage of floor is that it works even if the driver doesn't add
2070 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2071 * to return in gl_FragCoord, even though coordinates specify the pixel
2072 * centers instead of the pixel corners. This code will behave correctly
2073 * on drivers that returns integer values. */
2074 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
2076 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
2077 shader_addline(buffer,
2078 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2079 else
2080 shader_addline(buffer,
2081 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2085 /*****************************************************************************
2086 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2088 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2089 ****************************************************************************/
2091 /* Prototypes */
2092 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2093 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2095 /** Used for opcode modifiers - They multiply the result by the specified amount */
2096 static const char * const shift_glsl_tab[] = {
2097 "", /* 0 (none) */
2098 "2.0 * ", /* 1 (x2) */
2099 "4.0 * ", /* 2 (x4) */
2100 "8.0 * ", /* 3 (x8) */
2101 "16.0 * ", /* 4 (x16) */
2102 "32.0 * ", /* 5 (x32) */
2103 "", /* 6 (x64) */
2104 "", /* 7 (x128) */
2105 "", /* 8 (d256) */
2106 "", /* 9 (d128) */
2107 "", /* 10 (d64) */
2108 "", /* 11 (d32) */
2109 "0.0625 * ", /* 12 (d16) */
2110 "0.125 * ", /* 13 (d8) */
2111 "0.25 * ", /* 14 (d4) */
2112 "0.5 * " /* 15 (d2) */
2115 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2116 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2117 const char *in_reg, const char *in_regswizzle, char *out_str)
2119 switch (src_modifier)
2121 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2122 case WINED3DSPSM_DW:
2123 case WINED3DSPSM_NONE:
2124 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2125 break;
2126 case WINED3DSPSM_NEG:
2127 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2128 break;
2129 case WINED3DSPSM_NOT:
2130 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2131 break;
2132 case WINED3DSPSM_BIAS:
2133 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2134 break;
2135 case WINED3DSPSM_BIASNEG:
2136 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2137 break;
2138 case WINED3DSPSM_SIGN:
2139 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2140 break;
2141 case WINED3DSPSM_SIGNNEG:
2142 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2143 break;
2144 case WINED3DSPSM_COMP:
2145 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2146 break;
2147 case WINED3DSPSM_X2:
2148 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2149 break;
2150 case WINED3DSPSM_X2NEG:
2151 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2152 break;
2153 case WINED3DSPSM_ABS:
2154 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2155 break;
2156 case WINED3DSPSM_ABSNEG:
2157 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2158 break;
2159 default:
2160 FIXME("Unhandled modifier %u\n", src_modifier);
2161 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2165 /** Writes the GLSL variable name that corresponds to the register that the
2166 * DX opcode parameter is trying to access */
2167 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2168 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2170 /* oPos, oFog and oPts in D3D */
2171 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2173 const struct wined3d_shader *shader = ins->ctx->shader;
2174 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2175 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2176 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2177 const char *prefix = shader_glsl_get_prefix(version->type);
2178 struct glsl_src_param rel_param0, rel_param1;
2179 char imm_str[4][17];
2181 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2182 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2183 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2184 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2185 *is_color = FALSE;
2187 switch (reg->type)
2189 case WINED3DSPR_TEMP:
2190 sprintf(register_name, "R%u", reg->idx[0].offset);
2191 break;
2193 case WINED3DSPR_INPUT:
2194 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2196 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2198 if (reg->idx[0].rel_addr)
2199 FIXME("VS3+ input registers relative addressing.\n");
2200 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2201 *is_color = TRUE;
2202 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2203 break;
2206 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2208 if (reg->idx[0].rel_addr)
2210 if (reg->idx[1].rel_addr)
2211 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2212 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2213 else
2214 sprintf(register_name, "gs_in[%s + %u][%u]",
2215 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2217 else if (reg->idx[1].rel_addr)
2218 sprintf(register_name, "gs_in[%u][%s + %u]",
2219 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2220 else
2221 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2222 break;
2225 /* pixel shaders >= 3.0 */
2226 if (version->major >= 3)
2228 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2229 unsigned int in_count = vec4_varyings(version->major, gl_info);
2231 if (reg->idx[0].rel_addr)
2233 /* Removing a + 0 would be an obvious optimization, but
2234 * OS X doesn't see the NOP operation there. */
2235 if (idx)
2237 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2238 && shader->u.ps.declared_in_count > in_count)
2240 sprintf(register_name,
2241 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2242 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2243 prefix, rel_param0.param_str, idx);
2245 else
2247 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2250 else
2252 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2253 && shader->u.ps.declared_in_count > in_count)
2255 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2256 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2257 prefix, rel_param0.param_str);
2259 else
2261 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2265 else
2267 if (idx == in_count) sprintf(register_name, "gl_Color");
2268 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2269 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2272 else
2274 if (!reg->idx[0].offset)
2275 strcpy(register_name, "ffp_varying_diffuse");
2276 else
2277 strcpy(register_name, "ffp_varying_specular");
2278 break;
2280 break;
2282 case WINED3DSPR_CONST:
2284 /* Relative addressing */
2285 if (reg->idx[0].rel_addr)
2287 if (wined3d_settings.check_float_constants)
2288 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2289 rel_param0.param_str, reg->idx[0].offset,
2290 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2291 prefix, rel_param0.param_str, reg->idx[0].offset);
2292 else if (reg->idx[0].offset)
2293 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2294 else
2295 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2297 else
2299 if (shader_constant_is_local(shader, reg->idx[0].offset))
2300 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2301 else
2302 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2305 break;
2307 case WINED3DSPR_CONSTINT:
2308 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2309 break;
2311 case WINED3DSPR_CONSTBOOL:
2312 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2313 break;
2315 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2316 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2317 sprintf(register_name, "T%u", reg->idx[0].offset);
2318 else
2319 sprintf(register_name, "A%u", reg->idx[0].offset);
2320 break;
2322 case WINED3DSPR_LOOP:
2323 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2324 break;
2326 case WINED3DSPR_SAMPLER:
2327 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2328 break;
2330 case WINED3DSPR_COLOROUT:
2331 if (reg->idx[0].offset >= gl_info->limits.buffers)
2332 WARN("Write to render target %u, only %d supported.\n",
2333 reg->idx[0].offset, gl_info->limits.buffers);
2335 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
2336 break;
2338 case WINED3DSPR_RASTOUT:
2339 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2340 break;
2342 case WINED3DSPR_DEPTHOUT:
2343 sprintf(register_name, "gl_FragDepth");
2344 break;
2346 case WINED3DSPR_ATTROUT:
2347 if (!reg->idx[0].offset)
2348 sprintf(register_name, "%s_out[8]", prefix);
2349 else
2350 sprintf(register_name, "%s_out[9]", prefix);
2351 break;
2353 case WINED3DSPR_TEXCRDOUT:
2354 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2355 if (reg->idx[0].rel_addr)
2356 FIXME("VS3 output registers relative addressing.\n");
2357 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2358 break;
2360 case WINED3DSPR_MISCTYPE:
2361 if (!reg->idx[0].offset)
2363 /* vPos */
2364 sprintf(register_name, "vpos");
2366 else if (reg->idx[0].offset == 1)
2368 /* Note that gl_FrontFacing is a bool, while vFace is
2369 * a float for which the sign determines front/back */
2370 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2372 else
2374 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2375 sprintf(register_name, "unrecognized_register");
2377 break;
2379 case WINED3DSPR_IMMCONST:
2380 switch (reg->immconst_type)
2382 case WINED3D_IMMCONST_SCALAR:
2383 switch (reg->data_type)
2385 case WINED3D_DATA_FLOAT:
2386 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2387 break;
2388 case WINED3D_DATA_INT:
2389 sprintf(register_name, "%#x", reg->immconst_data[0]);
2390 break;
2391 case WINED3D_DATA_RESOURCE:
2392 case WINED3D_DATA_SAMPLER:
2393 case WINED3D_DATA_UINT:
2394 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2395 break;
2396 default:
2397 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2398 break;
2400 break;
2402 case WINED3D_IMMCONST_VEC4:
2403 switch (reg->data_type)
2405 case WINED3D_DATA_FLOAT:
2406 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2407 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2408 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2409 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2410 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2411 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2412 break;
2413 case WINED3D_DATA_INT:
2414 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2415 reg->immconst_data[0], reg->immconst_data[1],
2416 reg->immconst_data[2], reg->immconst_data[3]);
2417 break;
2418 case WINED3D_DATA_RESOURCE:
2419 case WINED3D_DATA_SAMPLER:
2420 case WINED3D_DATA_UINT:
2421 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2422 reg->immconst_data[0], reg->immconst_data[1],
2423 reg->immconst_data[2], reg->immconst_data[3]);
2424 break;
2425 default:
2426 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2427 break;
2429 break;
2431 default:
2432 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2433 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2435 break;
2437 case WINED3DSPR_CONSTBUFFER:
2438 if (reg->idx[1].rel_addr)
2439 sprintf(register_name, "%s_cb%u[%s + %u]",
2440 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2441 else
2442 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2443 break;
2445 case WINED3DSPR_IMMCONSTBUFFER:
2446 if (reg->idx[0].rel_addr)
2447 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2448 else
2449 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2450 break;
2452 case WINED3DSPR_PRIMID:
2453 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2454 break;
2456 default:
2457 FIXME("Unhandled register type %#x.\n", reg->type);
2458 sprintf(register_name, "unrecognized_register");
2459 break;
2463 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2465 *str++ = '.';
2466 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2467 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2468 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2469 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2470 *str = '\0';
2473 /* Get the GLSL write mask for the destination register */
2474 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2476 DWORD mask = param->write_mask;
2478 if (shader_is_scalar(&param->reg))
2480 mask = WINED3DSP_WRITEMASK_0;
2481 *write_mask = '\0';
2483 else
2485 shader_glsl_write_mask_to_str(mask, write_mask);
2488 return mask;
2491 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2492 unsigned int size = 0;
2494 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2495 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2496 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2497 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2499 return size;
2502 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2504 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2505 * but addressed as "rgba". To fix this we need to swap the register's x
2506 * and z components. */
2507 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2509 *str++ = '.';
2510 /* swizzle bits fields: wwzzyyxx */
2511 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2512 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2513 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2514 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2515 *str = '\0';
2518 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2519 BOOL fixup, DWORD mask, char *swizzle_str)
2521 if (shader_is_scalar(&param->reg))
2522 *swizzle_str = '\0';
2523 else
2524 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2527 /* From a given parameter token, generate the corresponding GLSL string.
2528 * Also, return the actual register name and swizzle in case the
2529 * caller needs this information as well. */
2530 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2531 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2533 BOOL is_color = FALSE;
2534 char swizzle_str[6];
2536 glsl_src->reg_name[0] = '\0';
2537 glsl_src->param_str[0] = '\0';
2538 swizzle_str[0] = '\0';
2540 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2541 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2543 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2545 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2547 else
2549 char reg_name[200];
2551 switch (wined3d_src->reg.data_type)
2553 case WINED3D_DATA_FLOAT:
2554 sprintf(reg_name, "%s", glsl_src->reg_name);
2555 break;
2556 case WINED3D_DATA_INT:
2557 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2558 break;
2559 case WINED3D_DATA_RESOURCE:
2560 case WINED3D_DATA_SAMPLER:
2561 case WINED3D_DATA_UINT:
2562 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2563 break;
2564 default:
2565 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2566 sprintf(reg_name, "%s", glsl_src->reg_name);
2567 break;
2570 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2574 /* From a given parameter token, generate the corresponding GLSL string.
2575 * Also, return the actual register name and swizzle in case the
2576 * caller needs this information as well. */
2577 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2578 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2580 BOOL is_color = FALSE;
2582 glsl_dst->mask_str[0] = '\0';
2583 glsl_dst->reg_name[0] = '\0';
2585 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2586 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2589 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2590 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2591 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2592 enum wined3d_data_type data_type)
2594 struct glsl_dst_param glsl_dst;
2595 DWORD mask;
2597 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2599 switch (data_type)
2601 case WINED3D_DATA_FLOAT:
2602 shader_addline(buffer, "%s%s = %s(",
2603 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2604 break;
2605 case WINED3D_DATA_INT:
2606 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2607 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2608 break;
2609 case WINED3D_DATA_RESOURCE:
2610 case WINED3D_DATA_SAMPLER:
2611 case WINED3D_DATA_UINT:
2612 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2613 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2614 break;
2615 default:
2616 FIXME("Unhandled data type %#x.\n", data_type);
2617 shader_addline(buffer, "%s%s = %s(",
2618 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2619 break;
2623 return mask;
2626 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2627 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2629 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2632 /** Process GLSL instruction modifiers */
2633 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2635 struct glsl_dst_param dst_param;
2636 DWORD modifiers;
2638 if (!ins->dst_count) return;
2640 modifiers = ins->dst[0].modifiers;
2641 if (!modifiers) return;
2643 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2645 if (modifiers & WINED3DSPDM_SATURATE)
2647 /* _SAT means to clamp the value of the register to between 0 and 1 */
2648 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2649 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2652 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2654 FIXME("_centroid modifier not handled\n");
2657 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2659 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2663 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2665 switch (op)
2667 case WINED3D_SHADER_REL_OP_GT: return ">";
2668 case WINED3D_SHADER_REL_OP_EQ: return "==";
2669 case WINED3D_SHADER_REL_OP_GE: return ">=";
2670 case WINED3D_SHADER_REL_OP_LT: return "<";
2671 case WINED3D_SHADER_REL_OP_NE: return "!=";
2672 case WINED3D_SHADER_REL_OP_LE: return "<=";
2673 default:
2674 FIXME("Unrecognized operator %#x.\n", op);
2675 return "(\?\?)";
2679 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2680 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2682 static const struct
2684 unsigned int coord_size;
2685 unsigned int offset_size;
2686 const char *type_part;
2688 resource_types[] =
2690 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2691 {1, 0, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2692 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2693 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2694 {2, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2695 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2696 {3, 0, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2697 {2, 1, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2698 {3, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2699 {3, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2701 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2702 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2703 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2704 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2705 && (priv->cur_ps_args->shadow & (1u << resource_idx));
2706 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2707 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2708 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2709 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2710 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2711 const char *base = "texture", *type_part = "", *suffix = "";
2712 unsigned int coord_size;
2714 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2716 if (resource_type >= ARRAY_SIZE(resource_types))
2718 ERR("Unexpected resource type %#x.\n", resource_type);
2719 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2722 /* Note that there's no such thing as a projected cube texture. */
2723 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2724 projected = FALSE;
2726 if (needs_legacy_glsl_syntax(gl_info))
2728 if (shadow)
2729 base = "shadow";
2731 type_part = resource_types[resource_type].type_part;
2732 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2733 type_part = "2DRect";
2734 if (!type_part[0])
2735 FIXME("Unhandled resource type %#x.\n", resource_type);
2737 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2739 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2740 suffix = "ARB";
2741 else
2742 FIXME("Unsupported grad function.\n");
2746 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2748 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2749 if (flags & ~texel_fetch_flags)
2750 ERR("Unexpected flags for texelFetch %#x.\n", flags & ~texel_fetch_flags);
2752 base = "texelFetch";
2753 type_part = "";
2756 sample_function->offset_size = offset ? resource_types[resource_type].offset_size : 0;
2757 if (offset && !sample_function->offset_size)
2759 FIXME("Offset not supported for resource type %#x.\n", resource_type);
2760 offset = FALSE;
2763 sample_function->name = string_buffer_get(priv->string_buffers);
2764 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2765 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2767 coord_size = resource_types[resource_type].coord_size;
2768 if (shadow)
2769 ++coord_size;
2770 sample_function->coord_mask = (1u << coord_size) - 1;
2771 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2774 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2775 struct glsl_sample_function *sample_function)
2777 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2779 string_buffer_release(priv->string_buffers, sample_function->name);
2782 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2783 BOOL sign_fixup, enum fixup_channel_source channel_source)
2785 switch(channel_source)
2787 case CHANNEL_SOURCE_ZERO:
2788 strcat(arguments, "0.0");
2789 break;
2791 case CHANNEL_SOURCE_ONE:
2792 strcat(arguments, "1.0");
2793 break;
2795 case CHANNEL_SOURCE_X:
2796 strcat(arguments, reg_name);
2797 strcat(arguments, ".x");
2798 break;
2800 case CHANNEL_SOURCE_Y:
2801 strcat(arguments, reg_name);
2802 strcat(arguments, ".y");
2803 break;
2805 case CHANNEL_SOURCE_Z:
2806 strcat(arguments, reg_name);
2807 strcat(arguments, ".z");
2808 break;
2810 case CHANNEL_SOURCE_W:
2811 strcat(arguments, reg_name);
2812 strcat(arguments, ".w");
2813 break;
2815 default:
2816 FIXME("Unhandled channel source %#x\n", channel_source);
2817 strcat(arguments, "undefined");
2818 break;
2821 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2824 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2825 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2827 unsigned int mask_size, remaining;
2828 DWORD fixup_mask = 0;
2829 char arguments[256];
2830 char mask_str[6];
2832 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2833 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2834 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2835 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2836 if (!(mask &= fixup_mask))
2837 return;
2839 if (is_complex_fixup(fixup))
2841 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2842 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2843 return;
2846 shader_glsl_write_mask_to_str(mask, mask_str);
2847 mask_size = shader_glsl_get_write_mask_size(mask);
2849 arguments[0] = '\0';
2850 remaining = mask_size;
2851 if (mask & WINED3DSP_WRITEMASK_0)
2853 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2854 if (--remaining) strcat(arguments, ", ");
2856 if (mask & WINED3DSP_WRITEMASK_1)
2858 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2859 if (--remaining) strcat(arguments, ", ");
2861 if (mask & WINED3DSP_WRITEMASK_2)
2863 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2864 if (--remaining) strcat(arguments, ", ");
2866 if (mask & WINED3DSP_WRITEMASK_3)
2868 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2869 if (--remaining) strcat(arguments, ", ");
2872 if (mask_size > 1)
2873 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2874 else
2875 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2878 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2880 char reg_name[256];
2881 BOOL is_color;
2883 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2884 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2887 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2888 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
2889 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
2890 const char *coord_reg_fmt, ...)
2892 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2893 char dst_swizzle[6];
2894 struct color_fixup_desc fixup;
2895 BOOL np2_fixup = FALSE;
2896 va_list args;
2897 int ret;
2899 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2901 /* FIXME: We currently don't support fixups for vertex shaders or anything
2902 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2903 * the resource index. */
2904 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2906 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2907 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
2909 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
2910 np2_fixup = TRUE;
2912 else
2914 fixup = COLOR_FIXUP_IDENTITY;
2917 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2919 if (sample_function->output_single_component)
2920 shader_addline(ins->ctx->buffer, "vec4(");
2922 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2923 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
2925 for (;;)
2927 va_start(args, coord_reg_fmt);
2928 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2929 va_end(args);
2930 if (!ret)
2931 break;
2932 if (!string_buffer_resize(ins->ctx->buffer, ret))
2933 break;
2936 if (np2_fixup)
2938 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2939 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
2941 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2943 case 1:
2944 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2945 idx >> 1, (idx % 2) ? "z" : "x");
2946 break;
2947 case 2:
2948 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2949 idx >> 1, (idx % 2) ? "zw" : "xy");
2950 break;
2951 case 3:
2952 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2953 idx >> 1, (idx % 2) ? "zw" : "xy");
2954 break;
2955 case 4:
2956 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2957 idx >> 1, (idx % 2) ? "zw" : "xy");
2958 break;
2961 if (dx && dy)
2962 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
2963 else if (bias)
2964 shader_addline(ins->ctx->buffer, ", %s", bias);
2965 if (sample_function->offset_size)
2967 int offset_immdata[4] = {offset->u, offset->v, offset->w};
2968 shader_addline(ins->ctx->buffer, ", ");
2969 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
2971 shader_addline(ins->ctx->buffer, ")");
2973 if (sample_function->output_single_component)
2974 shader_addline(ins->ctx->buffer, ")");
2976 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2978 if (!is_identity_fixup(fixup))
2979 shader_glsl_color_correction(ins, fixup);
2982 /*****************************************************************************
2983 * Begin processing individual instruction opcodes
2984 ****************************************************************************/
2986 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2988 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2989 struct glsl_src_param src0_param;
2990 struct glsl_src_param src1_param;
2991 DWORD write_mask;
2992 const char *op;
2994 /* Determine the GLSL operator to use based on the opcode */
2995 switch (ins->handler_idx)
2997 case WINED3DSIH_ADD: op = "+"; break;
2998 case WINED3DSIH_AND: op = "&"; break;
2999 case WINED3DSIH_DIV: op = "/"; break;
3000 case WINED3DSIH_IADD: op = "+"; break;
3001 case WINED3DSIH_ISHL: op = "<<"; break;
3002 case WINED3DSIH_MUL: op = "*"; break;
3003 case WINED3DSIH_OR: op = "|"; break;
3004 case WINED3DSIH_SUB: op = "-"; break;
3005 case WINED3DSIH_USHR: op = ">>"; break;
3006 case WINED3DSIH_XOR: op = "^"; break;
3007 default:
3008 op = "<unhandled operator>";
3009 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3010 break;
3013 write_mask = shader_glsl_append_dst(buffer, ins);
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);
3016 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3019 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3021 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3022 struct glsl_src_param src0_param;
3023 struct glsl_src_param src1_param;
3024 unsigned int mask_size;
3025 DWORD write_mask;
3026 const char *op;
3028 write_mask = shader_glsl_append_dst(buffer, ins);
3029 mask_size = shader_glsl_get_write_mask_size(write_mask);
3030 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3031 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3033 if (mask_size > 1)
3035 switch (ins->handler_idx)
3037 case WINED3DSIH_EQ: op = "equal"; break;
3038 case WINED3DSIH_IEQ: op = "equal"; break;
3039 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3040 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3041 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3042 case WINED3DSIH_LT: op = "lessThan"; break;
3043 case WINED3DSIH_ILT: op = "lessThan"; break;
3044 case WINED3DSIH_NE: op = "notEqual"; break;
3045 case WINED3DSIH_INE: op = "notEqual"; break;
3046 default:
3047 op = "<unhandled operator>";
3048 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3049 break;
3052 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3053 mask_size, op, src0_param.param_str, src1_param.param_str);
3055 else
3057 switch (ins->handler_idx)
3059 case WINED3DSIH_EQ: op = "=="; break;
3060 case WINED3DSIH_IEQ: op = "=="; break;
3061 case WINED3DSIH_GE: op = ">="; break;
3062 case WINED3DSIH_IGE: op = ">="; break;
3063 case WINED3DSIH_UGE: op = ">="; break;
3064 case WINED3DSIH_LT: op = "<"; break;
3065 case WINED3DSIH_ILT: op = "<"; break;
3066 case WINED3DSIH_NE: op = "!="; break;
3067 case WINED3DSIH_INE: op = "!="; break;
3068 default:
3069 op = "<unhandled operator>";
3070 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3071 break;
3074 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3075 src0_param.param_str, op, src1_param.param_str);
3079 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3081 struct glsl_src_param src_param;
3082 DWORD write_mask;
3083 const char *op;
3085 switch (ins->handler_idx)
3087 case WINED3DSIH_INEG: op = "-"; break;
3088 case WINED3DSIH_NOT: op = "~"; break;
3089 default:
3090 op = "<unhandled operator>";
3091 ERR("Unhandled opcode %s.\n",
3092 debug_d3dshaderinstructionhandler(ins->handler_idx));
3093 break;
3096 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3097 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3098 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3101 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3103 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3104 struct glsl_src_param src0_param;
3105 struct glsl_src_param src1_param;
3106 DWORD write_mask;
3108 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3109 * not, we can emulate it. */
3110 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3111 FIXME("64-bit integer multiplies not implemented.\n");
3113 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3115 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3116 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3117 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3119 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3120 src0_param.param_str, src1_param.param_str);
3124 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3126 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3127 struct glsl_src_param src0_param, src1_param;
3128 DWORD write_mask;
3130 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3132 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3134 char dst_mask[6];
3136 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
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, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3140 dst_mask, src0_param.param_str, src1_param.param_str);
3142 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3143 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3144 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3145 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3147 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3148 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3150 else
3152 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3153 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3154 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3155 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3158 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3160 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3161 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3162 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3163 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3167 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3168 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3170 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3171 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3172 struct glsl_src_param src0_param;
3173 DWORD write_mask;
3175 write_mask = shader_glsl_append_dst(buffer, ins);
3176 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3178 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3179 * shader versions WINED3DSIO_MOVA is used for this. */
3180 if (ins->ctx->reg_maps->shader_version.major == 1
3181 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3182 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3184 /* This is a simple floor() */
3185 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3186 if (mask_size > 1) {
3187 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3188 } else {
3189 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3192 else if(ins->handler_idx == WINED3DSIH_MOVA)
3194 /* We need to *round* to the nearest int here. */
3195 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3197 if (gl_info->supported[EXT_GPU_SHADER4])
3199 if (mask_size > 1)
3200 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3201 else
3202 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3204 else
3206 if (mask_size > 1)
3207 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3208 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3209 else
3210 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3211 src0_param.param_str, src0_param.param_str);
3214 else
3216 shader_addline(buffer, "%s);\n", src0_param.param_str);
3220 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3221 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3223 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3224 struct glsl_src_param src0_param;
3225 struct glsl_src_param src1_param;
3226 DWORD dst_write_mask, src_write_mask;
3227 unsigned int dst_size;
3229 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3230 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3232 /* dp4 works on vec4, dp3 on vec3, etc. */
3233 if (ins->handler_idx == WINED3DSIH_DP4)
3234 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3235 else if (ins->handler_idx == WINED3DSIH_DP3)
3236 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3237 else
3238 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3240 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3241 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3243 if (dst_size > 1) {
3244 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3245 } else {
3246 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3250 /* Note that this instruction has some restrictions. The destination write mask
3251 * can't contain the w component, and the source swizzles have to be .xyzw */
3252 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3254 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3255 struct glsl_src_param src0_param;
3256 struct glsl_src_param src1_param;
3257 char dst_mask[6];
3259 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3260 shader_glsl_append_dst(ins->ctx->buffer, ins);
3261 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3262 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3263 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3266 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3268 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3271 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3272 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3273 * GLSL uses the value as-is. */
3274 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3276 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3277 struct glsl_src_param src0_param;
3278 struct glsl_src_param src1_param;
3279 DWORD dst_write_mask;
3280 unsigned int dst_size;
3282 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3283 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3285 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3286 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3288 if (dst_size > 1)
3290 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3291 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3293 else
3295 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3296 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3300 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3301 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3303 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3304 struct glsl_src_param src_param;
3305 const char *instruction;
3306 DWORD write_mask;
3307 unsigned i;
3309 /* Determine the GLSL function to use based on the opcode */
3310 /* TODO: Possibly make this a table for faster lookups */
3311 switch (ins->handler_idx)
3313 case WINED3DSIH_ABS: instruction = "abs"; break;
3314 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3315 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3316 case WINED3DSIH_FRC: instruction = "fract"; break;
3317 case WINED3DSIH_IMAX: instruction = "max"; break;
3318 case WINED3DSIH_IMIN: instruction = "min"; break;
3319 case WINED3DSIH_MAX: instruction = "max"; break;
3320 case WINED3DSIH_MIN: instruction = "min"; break;
3321 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3322 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3323 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3324 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3325 default: instruction = "";
3326 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3327 break;
3330 write_mask = shader_glsl_append_dst(buffer, ins);
3332 shader_addline(buffer, "%s(", instruction);
3334 if (ins->src_count)
3336 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3337 shader_addline(buffer, "%s", src_param.param_str);
3338 for (i = 1; i < ins->src_count; ++i)
3340 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3341 shader_addline(buffer, ", %s", src_param.param_str);
3345 shader_addline(buffer, "));\n");
3348 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3350 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3352 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3353 struct glsl_src_param src_param;
3354 unsigned int mask_size;
3355 DWORD write_mask;
3356 char dst_mask[6];
3358 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3359 mask_size = shader_glsl_get_write_mask_size(write_mask);
3360 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3362 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3363 src_param.param_str, src_param.param_str);
3364 shader_glsl_append_dst(buffer, ins);
3366 if (mask_size > 1)
3368 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3369 mask_size, src_param.param_str);
3371 else
3373 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3374 src_param.param_str);
3378 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3380 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3381 ins->ctx->reg_maps->shader_version.minor);
3382 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3383 struct glsl_src_param src0_param;
3384 const char *prefix, *suffix;
3385 unsigned int dst_size;
3386 DWORD dst_write_mask;
3388 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3389 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3391 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3392 dst_write_mask = WINED3DSP_WRITEMASK_3;
3394 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3396 switch (ins->handler_idx)
3398 case WINED3DSIH_EXP:
3399 case WINED3DSIH_EXPP:
3400 prefix = "exp2(";
3401 suffix = ")";
3402 break;
3404 case WINED3DSIH_LOG:
3405 case WINED3DSIH_LOGP:
3406 prefix = "log2(abs(";
3407 suffix = "))";
3408 break;
3410 case WINED3DSIH_RCP:
3411 prefix = "1.0 / ";
3412 suffix = "";
3413 break;
3415 case WINED3DSIH_RSQ:
3416 prefix = "inversesqrt(abs(";
3417 suffix = "))";
3418 break;
3420 default:
3421 prefix = "";
3422 suffix = "";
3423 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3424 break;
3427 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3428 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3429 else
3430 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3433 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3434 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3435 * dst.x = 2^(floor(src))
3436 * dst.y = src - floor(src)
3437 * dst.z = 2^src (partial precision is allowed, but optional)
3438 * dst.w = 1.0;
3439 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3440 * dst = 2^src; (partial precision is allowed, but optional)
3442 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3444 if (ins->ctx->reg_maps->shader_version.major < 2)
3446 struct glsl_src_param src_param;
3447 char dst_mask[6];
3449 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3451 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3452 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3453 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3454 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3456 shader_glsl_append_dst(ins->ctx->buffer, ins);
3457 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3458 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3459 return;
3462 shader_glsl_scalar_op(ins);
3465 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3466 const char *vector_constructor, const char *scalar_constructor)
3468 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3469 struct glsl_src_param src_param;
3470 unsigned int mask_size;
3471 DWORD write_mask;
3473 write_mask = shader_glsl_append_dst(buffer, ins);
3474 mask_size = shader_glsl_get_write_mask_size(write_mask);
3475 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3477 if (mask_size > 1)
3478 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3479 else
3480 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3483 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3485 shader_glsl_cast(ins, "ivec", "int");
3488 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3490 shader_glsl_cast(ins, "uvec", "uint");
3493 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3495 shader_glsl_cast(ins, "vec", "float");
3498 /** Process signed comparison opcodes in GLSL. */
3499 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3501 struct glsl_src_param src0_param;
3502 struct glsl_src_param src1_param;
3503 DWORD write_mask;
3504 unsigned int mask_size;
3506 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3507 mask_size = shader_glsl_get_write_mask_size(write_mask);
3508 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3509 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3511 if (mask_size > 1) {
3512 const char *compare;
3514 switch(ins->handler_idx)
3516 case WINED3DSIH_SLT: compare = "lessThan"; break;
3517 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3518 default: compare = "";
3519 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3522 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3523 src0_param.param_str, src1_param.param_str);
3524 } else {
3525 switch(ins->handler_idx)
3527 case WINED3DSIH_SLT:
3528 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3529 * to return 0.0 but step returns 1.0 because step is not < x
3530 * An alternative is a bvec compare padded with an unused second component.
3531 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3532 * issue. Playing with not() is not possible either because not() does not accept
3533 * a scalar.
3535 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3536 src0_param.param_str, src1_param.param_str);
3537 break;
3538 case WINED3DSIH_SGE:
3539 /* Here we can use the step() function and safe a conditional */
3540 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3541 break;
3542 default:
3543 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3549 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3551 const char *condition_prefix, *condition_suffix;
3552 struct wined3d_shader_dst_param dst;
3553 struct glsl_src_param src0_param;
3554 struct glsl_src_param src1_param;
3555 struct glsl_src_param src2_param;
3556 BOOL temp_destination = FALSE;
3557 DWORD cmp_channel = 0;
3558 unsigned int i, j;
3559 char mask_char[6];
3560 DWORD write_mask;
3562 switch (ins->handler_idx)
3564 case WINED3DSIH_CMP:
3565 condition_prefix = "";
3566 condition_suffix = " >= 0.0";
3567 break;
3569 case WINED3DSIH_CND:
3570 condition_prefix = "";
3571 condition_suffix = " > 0.5";
3572 break;
3574 case WINED3DSIH_MOVC:
3575 condition_prefix = "bool(";
3576 condition_suffix = ")";
3577 break;
3579 default:
3580 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3581 condition_prefix = "<unhandled prefix>";
3582 condition_suffix = "<unhandled suffix>";
3583 break;
3586 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3588 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3589 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3590 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3591 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3593 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3594 condition_prefix, src0_param.param_str, condition_suffix,
3595 src1_param.param_str, src2_param.param_str);
3596 return;
3599 dst = ins->dst[0];
3601 /* Splitting the instruction up in multiple lines imposes a problem:
3602 * The first lines may overwrite source parameters of the following lines.
3603 * Deal with that by using a temporary destination register if needed. */
3604 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3605 && ins->src[0].reg.type == dst.reg.type)
3606 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3607 && ins->src[1].reg.type == dst.reg.type)
3608 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3609 && ins->src[2].reg.type == dst.reg.type))
3610 temp_destination = TRUE;
3612 /* Cycle through all source0 channels. */
3613 for (i = 0; i < 4; ++i)
3615 write_mask = 0;
3616 /* Find the destination channels which use the current source0 channel. */
3617 for (j = 0; j < 4; ++j)
3619 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3621 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3622 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3625 dst.write_mask = ins->dst[0].write_mask & write_mask;
3627 if (temp_destination)
3629 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3630 continue;
3631 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3633 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3634 continue;
3636 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3637 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3638 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3640 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3641 condition_prefix, src0_param.param_str, condition_suffix,
3642 src1_param.param_str, src2_param.param_str);
3645 if (temp_destination)
3647 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3648 shader_glsl_append_dst(ins->ctx->buffer, ins);
3649 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3653 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3654 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3655 * the compare is done per component of src0. */
3656 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3658 struct glsl_src_param src0_param;
3659 struct glsl_src_param src1_param;
3660 struct glsl_src_param src2_param;
3661 DWORD write_mask;
3662 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3663 ins->ctx->reg_maps->shader_version.minor);
3665 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3667 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3668 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3669 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3670 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3672 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3673 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3674 else
3675 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3676 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3677 return;
3680 shader_glsl_conditional_move(ins);
3683 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3684 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3686 struct glsl_src_param src0_param;
3687 struct glsl_src_param src1_param;
3688 struct glsl_src_param src2_param;
3689 DWORD write_mask;
3691 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3692 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3693 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3694 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3695 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3696 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3699 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3700 Vertex shaders to GLSL codes */
3701 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3703 int i;
3704 int nComponents = 0;
3705 struct wined3d_shader_dst_param tmp_dst = {{0}};
3706 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3707 struct wined3d_shader_instruction tmp_ins;
3709 memset(&tmp_ins, 0, sizeof(tmp_ins));
3711 /* Set constants for the temporary argument */
3712 tmp_ins.ctx = ins->ctx;
3713 tmp_ins.dst_count = 1;
3714 tmp_ins.dst = &tmp_dst;
3715 tmp_ins.src_count = 2;
3716 tmp_ins.src = tmp_src;
3718 switch(ins->handler_idx)
3720 case WINED3DSIH_M4x4:
3721 nComponents = 4;
3722 tmp_ins.handler_idx = WINED3DSIH_DP4;
3723 break;
3724 case WINED3DSIH_M4x3:
3725 nComponents = 3;
3726 tmp_ins.handler_idx = WINED3DSIH_DP4;
3727 break;
3728 case WINED3DSIH_M3x4:
3729 nComponents = 4;
3730 tmp_ins.handler_idx = WINED3DSIH_DP3;
3731 break;
3732 case WINED3DSIH_M3x3:
3733 nComponents = 3;
3734 tmp_ins.handler_idx = WINED3DSIH_DP3;
3735 break;
3736 case WINED3DSIH_M3x2:
3737 nComponents = 2;
3738 tmp_ins.handler_idx = WINED3DSIH_DP3;
3739 break;
3740 default:
3741 break;
3744 tmp_dst = ins->dst[0];
3745 tmp_src[0] = ins->src[0];
3746 tmp_src[1] = ins->src[1];
3747 for (i = 0; i < nComponents; ++i)
3749 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3750 shader_glsl_dot(&tmp_ins);
3751 ++tmp_src[1].reg.idx[0].offset;
3756 The LRP instruction performs a component-wise linear interpolation
3757 between the second and third operands using the first operand as the
3758 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3759 This is equivalent to mix(src2, src1, src0);
3761 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3763 struct glsl_src_param src0_param;
3764 struct glsl_src_param src1_param;
3765 struct glsl_src_param src2_param;
3766 DWORD write_mask;
3768 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3770 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3771 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3772 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3774 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3775 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3778 /** Process the WINED3DSIO_LIT instruction in GLSL:
3779 * dst.x = dst.w = 1.0
3780 * dst.y = (src0.x > 0) ? src0.x
3781 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3782 * where src.w is clamped at +- 128
3784 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3786 struct glsl_src_param src0_param;
3787 struct glsl_src_param src1_param;
3788 struct glsl_src_param src3_param;
3789 char dst_mask[6];
3791 shader_glsl_append_dst(ins->ctx->buffer, ins);
3792 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3794 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3795 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3796 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3798 /* The sdk specifies the instruction like this
3799 * dst.x = 1.0;
3800 * if(src.x > 0.0) dst.y = src.x
3801 * else dst.y = 0.0.
3802 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3803 * else dst.z = 0.0;
3804 * dst.w = 1.0;
3805 * (where power = src.w clamped between -128 and 128)
3807 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3808 * dst.x = 1.0 ... No further explanation needed
3809 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3810 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3811 * dst.w = 1.0. ... Nothing fancy.
3813 * So we still have one conditional in there. So do this:
3814 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3816 * 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),
3817 * 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.
3818 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3820 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3821 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3822 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3824 shader_addline(ins->ctx->buffer,
3825 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3826 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3827 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3828 src0_param.param_str, src3_param.param_str, dst_mask);
3831 /** Process the WINED3DSIO_DST instruction in GLSL:
3832 * dst.x = 1.0
3833 * dst.y = src0.x * src0.y
3834 * dst.z = src0.z
3835 * dst.w = src1.w
3837 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3839 struct glsl_src_param src0y_param;
3840 struct glsl_src_param src0z_param;
3841 struct glsl_src_param src1y_param;
3842 struct glsl_src_param src1w_param;
3843 char dst_mask[6];
3845 shader_glsl_append_dst(ins->ctx->buffer, ins);
3846 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3848 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3849 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3850 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3851 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3853 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3854 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3857 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3858 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3859 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3861 * dst.x = cos(src0.?)
3862 * dst.y = sin(src0.?)
3863 * dst.z = dst.z
3864 * dst.w = dst.w
3866 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3868 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3869 struct glsl_src_param src0_param;
3870 DWORD write_mask;
3872 if (ins->ctx->reg_maps->shader_version.major < 4)
3874 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3876 write_mask = shader_glsl_append_dst(buffer, ins);
3877 switch (write_mask)
3879 case WINED3DSP_WRITEMASK_0:
3880 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3881 break;
3883 case WINED3DSP_WRITEMASK_1:
3884 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3885 break;
3887 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3888 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3889 src0_param.param_str, src0_param.param_str);
3890 break;
3892 default:
3893 ERR("Write mask should be .x, .y or .xy\n");
3894 break;
3897 return;
3900 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3903 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3905 char dst_mask[6];
3907 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3908 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3909 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
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);
3915 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3916 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3918 else
3920 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3921 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3922 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3925 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3927 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3928 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3929 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3933 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3934 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3935 * generate invalid code
3937 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3939 struct glsl_src_param src0_param;
3940 DWORD write_mask;
3942 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3943 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3945 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3948 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3949 * Start a for() loop where src1.y is the initial value of aL,
3950 * increment aL by src1.z for a total of src1.x iterations.
3951 * Need to use a temporary variable for this operation.
3953 /* FIXME: I don't think nested loops will work correctly this way. */
3954 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3956 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3957 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3958 const struct wined3d_shader *shader = ins->ctx->shader;
3959 const struct wined3d_shader_lconst *constant;
3960 struct glsl_src_param src1_param;
3961 const DWORD *control_values = NULL;
3963 if (ins->ctx->reg_maps->shader_version.major < 4)
3965 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3967 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3968 * class hardware doesn't support real varying indexing, but Microsoft
3969 * designed this feature for Shader model 2.x+. If the loop control is
3970 * known at compile time, the GLSL compiler can unroll the loop, and
3971 * replace indirect addressing with direct addressing. */
3972 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3974 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3976 if (constant->idx == ins->src[1].reg.idx[0].offset)
3978 control_values = constant->value;
3979 break;
3984 if (control_values)
3986 struct wined3d_shader_loop_control loop_control;
3987 loop_control.count = control_values[0];
3988 loop_control.start = control_values[1];
3989 loop_control.step = (int)control_values[2];
3991 if (loop_control.step > 0)
3993 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3994 loop_state->current_depth, loop_control.start,
3995 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3996 loop_state->current_depth, loop_control.step);
3998 else if (loop_control.step < 0)
4000 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4001 loop_state->current_depth, loop_control.start,
4002 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4003 loop_state->current_depth, loop_control.step);
4005 else
4007 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4008 loop_state->current_depth, loop_control.start, loop_state->current_depth,
4009 loop_state->current_depth, loop_control.count,
4010 loop_state->current_depth);
4013 else
4015 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4016 loop_state->current_depth, loop_state->current_reg,
4017 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4018 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4021 ++loop_state->current_reg;
4023 else
4025 shader_addline(buffer, "for (;;)\n{\n");
4028 ++loop_state->current_depth;
4031 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4033 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4035 shader_addline(ins->ctx->buffer, "}\n");
4037 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4039 --loop_state->current_depth;
4040 --loop_state->current_reg;
4043 if (ins->handler_idx == WINED3DSIH_ENDREP)
4045 --loop_state->current_depth;
4049 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4051 const struct wined3d_shader *shader = ins->ctx->shader;
4052 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4053 const struct wined3d_shader_lconst *constant;
4054 struct glsl_src_param src0_param;
4055 const DWORD *control_values = NULL;
4057 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4058 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4060 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4062 if (constant->idx == ins->src[0].reg.idx[0].offset)
4064 control_values = constant->value;
4065 break;
4070 if (control_values)
4072 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4073 loop_state->current_depth, loop_state->current_depth,
4074 control_values[0], loop_state->current_depth);
4076 else
4078 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4079 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4080 loop_state->current_depth, loop_state->current_depth,
4081 src0_param.param_str, loop_state->current_depth);
4084 ++loop_state->current_depth;
4087 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4089 struct glsl_src_param src0_param;
4091 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4092 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
4095 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4097 struct glsl_src_param src0_param;
4098 struct glsl_src_param src1_param;
4100 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4101 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4103 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4104 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4107 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4109 shader_addline(ins->ctx->buffer, "} else {\n");
4112 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4114 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4117 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4119 shader_addline(ins->ctx->buffer, "break;\n");
4122 /* FIXME: According to MSDN the compare is done per component. */
4123 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4125 struct glsl_src_param src0_param;
4126 struct glsl_src_param src1_param;
4128 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4129 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4131 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4132 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4135 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4137 struct glsl_src_param src_param;
4139 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4140 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4143 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4145 shader_addline(ins->ctx->buffer, "}\n");
4146 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4149 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4151 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4154 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4156 struct glsl_src_param src1_param;
4158 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4159 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4160 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4163 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4165 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4166 * function only suppresses the unhandled instruction warning
4170 /*********************************************
4171 * Pixel Shader Specific Code begins here
4172 ********************************************/
4173 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4175 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4176 ins->ctx->reg_maps->shader_version.minor);
4177 struct glsl_sample_function sample_function;
4178 DWORD sample_flags = 0;
4179 DWORD resource_idx;
4180 DWORD mask = 0, swizzle;
4181 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4183 /* 1.0-1.4: Use destination register as sampler source.
4184 * 2.0+: Use provided sampler source. */
4185 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4186 resource_idx = ins->dst[0].reg.idx[0].offset;
4187 else
4188 resource_idx = ins->src[1].reg.idx[0].offset;
4190 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4192 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4193 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4194 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4196 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4197 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4199 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4200 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4202 case WINED3D_TTFF_COUNT1:
4203 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4204 break;
4205 case WINED3D_TTFF_COUNT2:
4206 mask = WINED3DSP_WRITEMASK_1;
4207 break;
4208 case WINED3D_TTFF_COUNT3:
4209 mask = WINED3DSP_WRITEMASK_2;
4210 break;
4211 case WINED3D_TTFF_COUNT4:
4212 case WINED3D_TTFF_DISABLE:
4213 mask = WINED3DSP_WRITEMASK_3;
4214 break;
4218 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4220 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4222 if (src_mod == WINED3DSPSM_DZ) {
4223 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4224 mask = WINED3DSP_WRITEMASK_2;
4225 } else if (src_mod == WINED3DSPSM_DW) {
4226 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4227 mask = WINED3DSP_WRITEMASK_3;
4230 else
4232 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4233 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4235 /* ps 2.0 texldp instruction always divides by the fourth component. */
4236 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4237 mask = WINED3DSP_WRITEMASK_3;
4241 if (priv->cur_ps_args->np2_fixup & (1u << resource_idx))
4242 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4244 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
4245 mask |= sample_function.coord_mask;
4246 sample_function.coord_mask = mask;
4248 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4249 else swizzle = ins->src[1].swizzle;
4251 /* 1.0-1.3: Use destination register as coordinate source.
4252 1.4+: Use provided coordinate source register. */
4253 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4255 char coord_mask[6];
4256 shader_glsl_write_mask_to_str(mask, coord_mask);
4257 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4258 "T%u%s", resource_idx, coord_mask);
4260 else
4262 struct glsl_src_param coord_param;
4263 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4264 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4266 struct glsl_src_param bias;
4267 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4268 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4269 NULL, "%s", coord_param.param_str);
4270 } else {
4271 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4272 "%s", coord_param.param_str);
4275 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4278 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4280 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4281 struct glsl_src_param coord_param, dx_param, dy_param;
4282 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
4283 struct glsl_sample_function sample_function;
4284 DWORD sampler_idx;
4285 DWORD swizzle = ins->src[1].swizzle;
4286 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4288 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4290 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4291 shader_glsl_tex(ins);
4292 return;
4295 sampler_idx = ins->src[1].reg.idx[0].offset;
4296 if (priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4297 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4299 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4300 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4301 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4302 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4304 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4305 NULL, NULL, "%s", coord_param.param_str);
4306 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4309 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4311 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4312 struct glsl_src_param coord_param, lod_param;
4313 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4314 struct glsl_sample_function sample_function;
4315 DWORD sampler_idx;
4316 DWORD swizzle = ins->src[1].swizzle;
4317 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4319 sampler_idx = ins->src[1].reg.idx[0].offset;
4320 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4321 && priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4322 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4324 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4325 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4327 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4329 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4330 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4332 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4333 * However, the NVIDIA drivers allow them in fragment shaders as well,
4334 * even without the appropriate extension. */
4335 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4337 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4338 "%s", coord_param.param_str);
4339 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4342 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4343 unsigned int resource_idx, unsigned int sampler_idx)
4345 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4346 unsigned int i;
4348 for (i = 0; i < sampler_map->count; ++i)
4350 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4351 return entries[i].bind_idx;
4354 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4356 return ~0u;
4359 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4361 static const unsigned int texture_size_component_count[] =
4363 0, /* WINED3D_SHADER_RESOURCE_NONE */
4364 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4365 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4366 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4367 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4368 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4369 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4370 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4371 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4372 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4375 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4376 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4377 enum wined3d_shader_resource_type resource_type;
4378 unsigned int resource_idx, sampler_bind_idx, i;
4379 enum wined3d_data_type dst_data_type;
4380 struct glsl_src_param lod_param;
4381 char dst_swizzle[6];
4382 DWORD write_mask;
4384 dst_data_type = ins->dst[0].reg.data_type;
4385 if (ins->flags == WINED3DSI_RESINFO_UINT)
4386 dst_data_type = WINED3D_DATA_UINT;
4387 else if (ins->flags)
4388 FIXME("Unhandled flags %#x.\n", ins->flags);
4390 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4391 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4393 resource_idx = ins->src[1].reg.idx[0].offset;
4394 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4395 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4396 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4397 resource_idx, WINED3D_SAMPLER_DEFAULT);
4399 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4401 ERR("Unexpected resource type %#x.\n", resource_type);
4402 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4405 if (dst_data_type == WINED3D_DATA_UINT)
4406 shader_addline(ins->ctx->buffer, "uvec4(");
4407 else
4408 shader_addline(ins->ctx->buffer, "vec4(");
4410 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4411 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4413 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4414 shader_addline(ins->ctx->buffer, "0, ");
4416 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4418 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4419 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4421 else
4423 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4424 shader_addline(ins->ctx->buffer, "1");
4427 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4430 /* FIXME: The current implementation does not handle multisample textures correctly. */
4431 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4433 struct glsl_src_param coord_param, lod_param;
4434 struct glsl_sample_function sample_function;
4435 unsigned int sampler_bind_idx;
4436 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4438 if (wined3d_shader_instruction_has_texel_offset(ins))
4439 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4441 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, flags, &sample_function);
4442 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4443 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4444 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4445 ins->src[1].reg.idx[0].offset, WINED3D_SAMPLER_DEFAULT);
4446 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4447 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4448 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4451 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4453 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4454 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4455 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4456 struct glsl_sample_function sample_function;
4457 DWORD flags = 0;
4459 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4460 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4461 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4462 flags |= WINED3D_GLSL_SAMPLE_LOD;
4463 if (wined3d_shader_instruction_has_texel_offset(ins))
4464 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4466 resource_idx = ins->src[1].reg.idx[0].offset;
4467 sampler_idx = ins->src[2].reg.idx[0].offset;
4469 shader_glsl_get_sample_function(ins->ctx, resource_idx, flags, &sample_function);
4470 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4472 switch (ins->handler_idx)
4474 case WINED3DSIH_SAMPLE:
4475 break;
4476 case WINED3DSIH_SAMPLE_B:
4477 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4478 lod_param_str = lod_param.param_str;
4479 break;
4480 case WINED3DSIH_SAMPLE_GRAD:
4481 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dx_param);
4482 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.coord_mask, &dy_param);
4483 dx_param_str = dx_param.param_str;
4484 dy_param_str = dy_param.param_str;
4485 break;
4486 case WINED3DSIH_SAMPLE_LOD:
4487 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4488 lod_param_str = lod_param.param_str;
4489 break;
4490 default:
4491 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4492 break;
4495 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4496 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4497 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4498 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4501 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4503 /* FIXME: Make this work for more than just 2D textures */
4504 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4505 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4507 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4509 char dst_mask[6];
4511 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4512 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4513 ins->dst[0].reg.idx[0].offset, dst_mask);
4515 else
4517 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4518 DWORD reg = ins->src[0].reg.idx[0].offset;
4519 char dst_swizzle[6];
4521 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4523 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4525 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4526 struct glsl_src_param div_param;
4527 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4529 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4531 if (mask_size > 1)
4532 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4533 else
4534 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4536 else
4538 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4543 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4544 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4545 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4546 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4548 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4549 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4550 struct glsl_sample_function sample_function;
4551 struct glsl_src_param src0_param;
4552 UINT mask_size;
4554 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4556 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4557 * scalar, and projected sampling would require 4.
4559 * It is a dependent read - not valid with conditional NP2 textures
4561 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4562 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4564 switch(mask_size)
4566 case 1:
4567 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4568 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4569 break;
4571 case 2:
4572 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4573 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4574 break;
4576 case 3:
4577 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4578 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4579 break;
4581 default:
4582 FIXME("Unexpected mask size %u\n", mask_size);
4583 break;
4585 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4588 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4589 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4590 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4592 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4593 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4594 struct glsl_src_param src0_param;
4595 DWORD dst_mask;
4596 unsigned int mask_size;
4598 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4599 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4600 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4602 if (mask_size > 1) {
4603 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4604 } else {
4605 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4609 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4610 * Calculate the depth as dst.x / dst.y */
4611 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4613 struct glsl_dst_param dst_param;
4615 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4617 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4618 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4619 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4620 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4621 * >= 1.0 or < 0.0
4623 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4624 dst_param.reg_name, dst_param.reg_name);
4627 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4628 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4629 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4630 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4632 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4634 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4635 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4636 struct glsl_src_param src0_param;
4638 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4640 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4641 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4644 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4645 * Calculate the 1st of a 2-row matrix multiplication. */
4646 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4648 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4649 DWORD reg = ins->dst[0].reg.idx[0].offset;
4650 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4651 struct glsl_src_param src0_param;
4653 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4654 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4657 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4658 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4659 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4661 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4662 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4663 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4664 DWORD reg = ins->dst[0].reg.idx[0].offset;
4665 struct glsl_src_param src0_param;
4667 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4668 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4669 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4672 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4674 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4675 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4676 struct glsl_sample_function sample_function;
4677 DWORD reg = ins->dst[0].reg.idx[0].offset;
4678 struct glsl_src_param src0_param;
4680 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4681 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4683 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4685 /* Sample the texture using the calculated coordinates */
4686 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
4687 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4690 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4691 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4692 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4694 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4695 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4696 struct glsl_sample_function sample_function;
4697 DWORD reg = ins->dst[0].reg.idx[0].offset;
4698 struct glsl_src_param src0_param;
4700 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4701 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4703 /* Dependent read, not valid with conditional NP2 */
4704 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4706 /* Sample the texture using the calculated coordinates */
4707 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
4708 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4710 tex_mx->current_row = 0;
4713 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4714 * Perform the 3rd row of a 3x3 matrix multiply */
4715 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4717 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4718 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4719 DWORD reg = ins->dst[0].reg.idx[0].offset;
4720 struct glsl_src_param src0_param;
4721 char dst_mask[6];
4723 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4725 shader_glsl_append_dst(ins->ctx->buffer, ins);
4726 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4727 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4729 tex_mx->current_row = 0;
4732 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4733 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4734 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4736 struct glsl_src_param src0_param;
4737 struct glsl_src_param src1_param;
4738 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4739 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4740 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4741 struct glsl_sample_function sample_function;
4742 DWORD reg = ins->dst[0].reg.idx[0].offset;
4743 char coord_mask[6];
4745 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4746 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4748 /* Perform the last matrix multiply operation */
4749 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4750 /* Reflection calculation */
4751 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4753 /* Dependent read, not valid with conditional NP2 */
4754 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4755 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4757 /* Sample the texture */
4758 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4759 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4760 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4762 tex_mx->current_row = 0;
4765 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4766 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4767 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4769 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4770 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4771 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4772 struct glsl_sample_function sample_function;
4773 DWORD reg = ins->dst[0].reg.idx[0].offset;
4774 struct glsl_src_param src0_param;
4775 char coord_mask[6];
4777 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4779 /* Perform the last matrix multiply operation */
4780 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4782 /* Construct the eye-ray vector from w coordinates */
4783 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4784 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4785 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4787 /* Dependent read, not valid with conditional NP2 */
4788 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4789 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4791 /* Sample the texture using the calculated coordinates */
4792 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4793 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4794 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4796 tex_mx->current_row = 0;
4799 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4800 * Apply a fake bump map transform.
4801 * texbem is pshader <= 1.3 only, this saves a few version checks
4803 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4805 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4806 struct glsl_sample_function sample_function;
4807 struct glsl_src_param coord_param;
4808 DWORD sampler_idx;
4809 DWORD mask;
4810 DWORD flags;
4811 char coord_mask[6];
4813 sampler_idx = ins->dst[0].reg.idx[0].offset;
4814 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4815 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4817 /* Dependent read, not valid with conditional NP2 */
4818 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4819 mask = sample_function.coord_mask;
4821 shader_glsl_write_mask_to_str(mask, coord_mask);
4823 /* With projected textures, texbem only divides the static texture coord,
4824 * not the displacement, so we can't let GL handle this. */
4825 if (flags & WINED3D_PSARGS_PROJECTED)
4827 DWORD div_mask=0;
4828 char coord_div_mask[3];
4829 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4831 case WINED3D_TTFF_COUNT1:
4832 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4833 break;
4834 case WINED3D_TTFF_COUNT2:
4835 div_mask = WINED3DSP_WRITEMASK_1;
4836 break;
4837 case WINED3D_TTFF_COUNT3:
4838 div_mask = WINED3DSP_WRITEMASK_2;
4839 break;
4840 case WINED3D_TTFF_COUNT4:
4841 case WINED3D_TTFF_DISABLE:
4842 div_mask = WINED3DSP_WRITEMASK_3;
4843 break;
4845 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4846 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4849 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4851 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4852 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4853 coord_param.param_str, coord_mask);
4855 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4857 struct glsl_src_param luminance_param;
4858 struct glsl_dst_param dst_param;
4860 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4861 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4863 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4864 dst_param.reg_name, dst_param.mask_str,
4865 luminance_param.param_str, sampler_idx, sampler_idx);
4867 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4870 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4872 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4873 struct glsl_src_param src0_param, src1_param;
4875 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4876 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4878 shader_glsl_append_dst(ins->ctx->buffer, ins);
4879 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4880 src0_param.param_str, sampler_idx, src1_param.param_str);
4883 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4884 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4885 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4887 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4888 struct glsl_sample_function sample_function;
4889 struct glsl_src_param src0_param;
4891 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4893 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4894 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4895 "%s.wx", src0_param.reg_name);
4896 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4899 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4900 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4901 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4903 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4904 struct glsl_sample_function sample_function;
4905 struct glsl_src_param src0_param;
4907 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4909 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4910 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4911 "%s.yz", src0_param.reg_name);
4912 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4915 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4916 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4917 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4919 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4920 struct glsl_sample_function sample_function;
4921 struct glsl_src_param src0_param;
4923 /* Dependent read, not valid with conditional NP2 */
4924 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4925 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4927 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4928 "%s", src0_param.param_str);
4929 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4932 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4933 * If any of the first 3 components are < 0, discard this pixel */
4934 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4936 if (ins->ctx->reg_maps->shader_version.major >= 4)
4938 struct glsl_src_param src_param;
4940 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4941 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4943 else
4945 struct glsl_dst_param dst_param;
4947 /* The argument is a destination parameter, and no writemasks are allowed */
4948 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4950 /* 2.0 shaders compare all 4 components in texkill. */
4951 if (ins->ctx->reg_maps->shader_version.major >= 2)
4952 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4953 /* 1.x shaders only compare the first 3 components, probably due to
4954 * the nature of the texkill instruction as a tex* instruction, and
4955 * phase, which kills all .w components. Even if all 4 components are
4956 * defined, only the first 3 are used. */
4957 else
4958 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4962 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4963 * dst = dot2(src0, src1) + src2 */
4964 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4966 struct glsl_src_param src0_param;
4967 struct glsl_src_param src1_param;
4968 struct glsl_src_param src2_param;
4969 DWORD write_mask;
4970 unsigned int mask_size;
4972 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4973 mask_size = shader_glsl_get_write_mask_size(write_mask);
4975 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4976 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4977 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4979 if (mask_size > 1) {
4980 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4981 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4982 } else {
4983 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4984 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4988 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
4989 const struct wined3d_shader_signature *input_signature,
4990 const struct wined3d_shader_reg_maps *reg_maps,
4991 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
4993 unsigned int i;
4995 for (i = 0; i < input_signature->element_count; ++i)
4997 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4998 const char *semantic_name;
4999 UINT semantic_idx;
5000 char reg_mask[6];
5002 /* Unused */
5003 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5004 continue;
5006 semantic_name = input->semantic_name;
5007 semantic_idx = input->semantic_idx;
5008 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5010 if (args->vp_mode == vertexshader)
5012 if (input->sysval_semantic == WINED3D_SV_POSITION)
5013 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5014 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5015 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5016 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5017 else
5018 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5019 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5020 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5022 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5024 if (args->pointsprite)
5025 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5026 shader->u.ps.input_reg_map[input->register_idx]);
5027 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5028 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5029 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5030 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5031 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5032 else
5033 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5034 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5036 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5038 if (!semantic_idx)
5039 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5040 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5041 else if (semantic_idx == 1)
5042 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5043 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5044 else
5045 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5046 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5048 else
5050 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5051 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5056 /*********************************************
5057 * Vertex Shader Specific Code begins here
5058 ********************************************/
5060 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5062 struct glsl_program_key key;
5064 key.vs_id = entry->vs.id;
5065 key.gs_id = entry->gs.id;
5066 key.ps_id = entry->ps.id;
5068 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5070 ERR("Failed to insert program entry.\n");
5074 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5075 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5077 struct wine_rb_entry *entry;
5078 struct glsl_program_key key;
5080 key.vs_id = vs_id;
5081 key.gs_id = gs_id;
5082 key.ps_id = ps_id;
5084 entry = wine_rb_get(&priv->program_lookup, &key);
5085 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5088 /* Context activation is done by the caller. */
5089 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5090 struct glsl_shader_prog_link *entry)
5092 struct glsl_program_key key;
5094 key.vs_id = entry->vs.id;
5095 key.gs_id = entry->gs.id;
5096 key.ps_id = entry->ps.id;
5097 wine_rb_remove(&priv->program_lookup, &key);
5099 GL_EXTCALL(glDeleteProgram(entry->id));
5100 if (entry->vs.id)
5101 list_remove(&entry->vs.shader_entry);
5102 if (entry->gs.id)
5103 list_remove(&entry->gs.shader_entry);
5104 if (entry->ps.id)
5105 list_remove(&entry->ps.shader_entry);
5106 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
5107 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
5108 HeapFree(GetProcessHeap(), 0, entry);
5111 static void handle_ps3_input(struct shader_glsl_priv *priv,
5112 const struct wined3d_gl_info *gl_info, const DWORD *map,
5113 const struct wined3d_shader_signature *input_signature,
5114 const struct wined3d_shader_reg_maps *reg_maps_in,
5115 const struct wined3d_shader_signature *output_signature,
5116 const struct wined3d_shader_reg_maps *reg_maps_out)
5118 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5119 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5120 unsigned int i, j;
5121 DWORD *set;
5122 DWORD in_idx;
5123 unsigned int in_count = vec4_varyings(3, gl_info);
5124 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5125 char reg_mask[6];
5126 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5128 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * max_varyings);
5130 for (i = 0; i < input_signature->element_count; ++i)
5132 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5134 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5135 continue;
5137 in_idx = map[input->register_idx];
5138 /* Declared, but not read register */
5139 if (in_idx == ~0u)
5140 continue;
5141 if (in_idx >= max_varyings)
5143 FIXME("More input varyings declared than supported, expect issues.\n");
5144 continue;
5147 if (in_idx == in_count)
5148 string_buffer_sprintf(destination, "gl_FrontColor");
5149 else if (in_idx == in_count + 1)
5150 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5151 else
5152 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
5154 if (!set[in_idx])
5155 set[in_idx] = ~0u;
5157 for (j = 0; j < output_signature->element_count; ++j)
5159 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5160 DWORD mask;
5162 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5163 || input->semantic_idx != output->semantic_idx
5164 || strcmp(input->semantic_name, output->semantic_name)
5165 || !(mask = input->mask & output->mask))
5166 continue;
5168 if (set[in_idx] == ~0u)
5169 set[in_idx] = 0;
5170 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5171 shader_glsl_write_mask_to_str(mask, reg_mask);
5173 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
5174 destination->buffer, reg_mask, output->register_idx, reg_mask);
5178 for (i = 0; i < max_varyings; ++i)
5180 unsigned int size;
5182 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5183 continue;
5185 if (set[i] == ~0U) set[i] = 0;
5187 size = 0;
5188 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
5189 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
5190 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
5191 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
5192 reg_mask[size] = '\0';
5194 if (i == in_count)
5195 string_buffer_sprintf(destination, "gl_FrontColor");
5196 else if (i == in_count + 1)
5197 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5198 else
5199 string_buffer_sprintf(destination, "ps_link[%u]", i);
5201 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5202 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5205 HeapFree(GetProcessHeap(), 0, set);
5206 string_buffer_release(&priv->string_buffers, destination);
5209 /* Context activation is done by the caller. */
5210 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
5211 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5212 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5214 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5215 GLuint ret;
5216 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5217 unsigned int i;
5218 const char *semantic_name;
5219 UINT semantic_idx;
5220 char reg_mask[6];
5221 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5223 string_buffer_clear(buffer);
5225 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5227 if (per_vertex_point_size)
5229 shader_addline(buffer, "uniform struct\n{\n");
5230 shader_addline(buffer, " float size_min;\n");
5231 shader_addline(buffer, " float size_max;\n");
5232 shader_addline(buffer, "} ffp_point;\n");
5235 if (ps_major < 3)
5237 DWORD colors_written_mask[2] = {0};
5238 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5240 if (!legacy_context)
5242 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5243 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5244 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5245 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5248 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5250 for (i = 0; i < vs->output_signature.element_count; ++i)
5252 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5253 DWORD write_mask;
5255 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5256 continue;
5258 semantic_name = output->semantic_name;
5259 semantic_idx = output->semantic_idx;
5260 write_mask = output->mask;
5261 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5263 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5265 if (legacy_context)
5266 shader_addline(buffer, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5267 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5268 else
5269 shader_addline(buffer, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5270 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5272 colors_written_mask[semantic_idx] = write_mask;
5274 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5276 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5277 reg_mask, output->register_idx, reg_mask);
5279 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5281 if (semantic_idx < MAX_TEXTURES)
5283 shader_addline(buffer, "%s[%u]%s = vs_out[%u]%s;\n",
5284 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5285 semantic_idx, reg_mask, output->register_idx, reg_mask);
5286 texcoords_written_mask[semantic_idx] = write_mask;
5289 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5291 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5292 output->register_idx, reg_mask[1]);
5294 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5296 shader_addline(buffer, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5297 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5298 output->register_idx, reg_mask[1]);
5302 for (i = 0; i < 2; ++i)
5304 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5306 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5307 if (!i)
5308 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5309 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5310 reg_mask, reg_mask);
5311 else
5312 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5313 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5314 reg_mask, reg_mask);
5317 for (i = 0; i < MAX_TEXTURES; ++i)
5319 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5320 continue;
5322 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5324 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5325 && !texcoords_written_mask[i])
5326 continue;
5328 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5329 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5330 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5334 else
5336 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5338 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5339 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5341 /* First, sort out position and point size. Those are not passed to the pixel shader */
5342 for (i = 0; i < vs->output_signature.element_count; ++i)
5344 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5346 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5347 continue;
5349 semantic_name = output->semantic_name;
5350 semantic_idx = output->semantic_idx;
5351 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5353 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5355 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5356 reg_mask, output->register_idx, reg_mask);
5358 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5360 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5361 output->register_idx, reg_mask[1]);
5365 /* Then, fix the pixel shader input */
5366 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5367 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
5370 shader_addline(buffer, "}\n");
5372 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5373 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5374 shader_glsl_compile(gl_info, ret, buffer->buffer);
5376 return ret;
5379 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
5381 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
5382 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5383 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
5384 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
5385 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
5386 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
5389 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
5391 switch (mode)
5393 case WINED3D_FFP_PS_FOG_OFF:
5394 return;
5396 case WINED3D_FFP_PS_FOG_LINEAR:
5397 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5398 break;
5400 case WINED3D_FFP_PS_FOG_EXP:
5401 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5402 break;
5404 case WINED3D_FFP_PS_FOG_EXP2:
5405 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5406 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5407 break;
5409 default:
5410 ERR("Invalid fog mode %#x.\n", mode);
5411 return;
5414 shader_addline(buffer, "gl_FragData[0].xyz = mix(ffp_fog.color.xyz, gl_FragData[0].xyz,"
5415 " clamp(fog, 0.0, 1.0));\n");
5418 /* Context activation is done by the caller. */
5419 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5420 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5421 const struct wined3d_shader *shader,
5422 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5424 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5425 const struct wined3d_gl_info *gl_info = context->gl_info;
5426 const DWORD *function = shader->function;
5427 struct shader_glsl_ctx_priv priv_ctx;
5428 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5430 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5431 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5433 memset(&priv_ctx, 0, sizeof(priv_ctx));
5434 priv_ctx.cur_ps_args = args;
5435 priv_ctx.cur_np2fixup_info = np2fixup_info;
5436 priv_ctx.string_buffers = string_buffers;
5438 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5440 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5441 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5442 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5443 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5444 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5445 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5446 /* The spec says that it doesn't have to be explicitly enabled, but the
5447 * nvidia drivers write a warning if we don't do so. */
5448 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5449 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5450 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5451 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5452 if (gl_info->supported[EXT_GPU_SHADER4])
5453 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5455 /* Base Declarations */
5456 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5458 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5460 unsigned int i;
5461 WORD map = reg_maps->texcoord;
5463 if (legacy_context)
5465 if (glsl_is_color_reg_read(shader, 0))
5466 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5467 if (glsl_is_color_reg_read(shader, 1))
5468 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5471 for (i = 0; map; map >>= 1, ++i)
5473 if (map & 1)
5475 if (args->pointsprite)
5476 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5477 else if (args->texcoords_initialized & (1u << i))
5478 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5479 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5480 else
5481 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5482 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5486 if (legacy_context)
5487 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5490 /* Pack 3.0 inputs */
5491 if (reg_maps->shader_version.major >= 3)
5492 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5494 /* Base Shader Body */
5495 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5497 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5498 if (reg_maps->shader_version.major < 2)
5500 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5501 shader_addline(buffer, "gl_FragData[0] = R0;\n");
5504 if (args->srgb_correction)
5505 shader_glsl_generate_srgb_write_correction(buffer);
5507 /* SM < 3 does not replace the fog stage. */
5508 if (reg_maps->shader_version.major < 3)
5509 shader_glsl_generate_fog_code(buffer, args->fog);
5511 shader_addline(buffer, "}\n");
5513 TRACE("Compiling shader object %u.\n", shader_id);
5514 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5516 return shader_id;
5519 /* Context activation is done by the caller. */
5520 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5521 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5522 const struct wined3d_shader *shader,
5523 const struct vs_compile_args *args)
5525 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5526 const struct wined3d_gl_info *gl_info = context->gl_info;
5527 const DWORD *function = shader->function;
5528 struct shader_glsl_ctx_priv priv_ctx;
5529 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5531 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5532 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5534 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5536 if (gl_info->supported[ARB_DRAW_INSTANCED])
5537 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5538 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5539 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5540 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5541 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5542 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5543 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5544 if (gl_info->supported[EXT_GPU_SHADER4])
5545 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5547 memset(&priv_ctx, 0, sizeof(priv_ctx));
5548 priv_ctx.cur_vs_args = args;
5549 priv_ctx.string_buffers = string_buffers;
5551 /* Base Declarations */
5552 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5554 /* Base Shader Body */
5555 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5557 /* Unpack outputs */
5558 shader_addline(buffer, "order_ps_input(vs_out);\n");
5560 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5561 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5562 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5563 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5565 if (reg_maps->shader_version.major < 3)
5567 if (args->fog_src == VS_FOG_Z)
5568 shader_addline(buffer, "%s = gl_Position.z;\n",
5569 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5570 else if (!reg_maps->fog)
5571 shader_addline(buffer, "%s = 0.0;\n",
5572 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5575 /* We always store the clipplanes without y inversion */
5576 if (args->clip_enabled)
5577 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5579 if (args->point_size && !args->per_vertex_point_size)
5580 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5582 /* Write the final position.
5584 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5585 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5586 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5587 * contains 1.0 to allow a mad.
5589 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5590 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5592 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5594 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5595 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5596 * which is the same as z = z * 2 - w.
5598 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5600 shader_addline(buffer, "}\n");
5602 TRACE("Compiling shader object %u.\n", shader_id);
5603 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5605 return shader_id;
5608 /* Context activation is done by the caller. */
5609 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5610 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5611 const struct wined3d_shader *shader)
5613 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5614 const struct wined3d_gl_info *gl_info = context->gl_info;
5615 const DWORD *function = shader->function;
5616 struct shader_glsl_ctx_priv priv_ctx;
5617 GLuint shader_id;
5619 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5621 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5623 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5624 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5625 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5626 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5627 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5628 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5629 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5630 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5631 if (gl_info->supported[EXT_GPU_SHADER4])
5632 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5634 memset(&priv_ctx, 0, sizeof(priv_ctx));
5635 priv_ctx.string_buffers = string_buffers;
5636 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5637 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5638 shader_addline(buffer, "}\n");
5640 TRACE("Compiling shader object %u.\n", shader_id);
5641 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5643 return shader_id;
5646 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5647 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5648 struct wined3d_shader *shader,
5649 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5651 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5652 struct glsl_shader_private *shader_data;
5653 struct ps_np2fixup_info *np2fixup;
5654 UINT i;
5655 DWORD new_size;
5656 GLuint ret;
5658 if (!shader->backend_data)
5660 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5661 if (!shader->backend_data)
5663 ERR("Failed to allocate backend data.\n");
5664 return 0;
5667 shader_data = shader->backend_data;
5668 gl_shaders = shader_data->gl_shaders.ps;
5670 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5671 * so a linear search is more performant than a hashmap or a binary search
5672 * (cache coherency etc)
5674 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5676 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5678 if (args->np2_fixup)
5679 *np2fixup_info = &gl_shaders[i].np2fixup;
5680 return gl_shaders[i].id;
5684 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5685 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5686 if (shader_data->num_gl_shaders)
5688 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5689 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5690 new_size * sizeof(*gl_shaders));
5692 else
5694 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5695 new_size = 1;
5698 if(!new_array) {
5699 ERR("Out of memory\n");
5700 return 0;
5702 shader_data->gl_shaders.ps = new_array;
5703 shader_data->shader_array_size = new_size;
5704 gl_shaders = new_array;
5707 gl_shaders[shader_data->num_gl_shaders].args = *args;
5709 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5710 memset(np2fixup, 0, sizeof(*np2fixup));
5711 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5713 pixelshader_update_resource_types(shader, args->tex_types);
5715 string_buffer_clear(buffer);
5716 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5717 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5719 return ret;
5722 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5723 const DWORD use_map)
5725 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5726 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5727 if (stored->point_size != new->point_size)
5728 return FALSE;
5729 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5730 return FALSE;
5731 if (stored->flatshading != new->flatshading)
5732 return FALSE;
5733 return stored->fog_src == new->fog_src;
5736 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5737 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5738 struct wined3d_shader *shader,
5739 const struct vs_compile_args *args)
5741 UINT i;
5742 DWORD new_size;
5743 DWORD use_map = context->stream_info.use_map;
5744 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5745 struct glsl_shader_private *shader_data;
5746 GLuint ret;
5748 if (!shader->backend_data)
5750 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5751 if (!shader->backend_data)
5753 ERR("Failed to allocate backend data.\n");
5754 return 0;
5757 shader_data = shader->backend_data;
5758 gl_shaders = shader_data->gl_shaders.vs;
5760 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5761 * so a linear search is more performant than a hashmap or a binary search
5762 * (cache coherency etc)
5764 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5766 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5767 return gl_shaders[i].id;
5770 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5772 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5773 if (shader_data->num_gl_shaders)
5775 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5776 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5777 new_size * sizeof(*gl_shaders));
5779 else
5781 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5782 new_size = 1;
5785 if(!new_array) {
5786 ERR("Out of memory\n");
5787 return 0;
5789 shader_data->gl_shaders.vs = new_array;
5790 shader_data->shader_array_size = new_size;
5791 gl_shaders = new_array;
5794 gl_shaders[shader_data->num_gl_shaders].args = *args;
5796 string_buffer_clear(buffer);
5797 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5798 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5800 return ret;
5803 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5804 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5805 struct wined3d_shader *shader)
5807 struct glsl_gs_compiled_shader *gl_shaders;
5808 struct glsl_shader_private *shader_data;
5809 GLuint ret;
5811 if (!shader->backend_data)
5813 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5815 ERR("Failed to allocate backend data.\n");
5816 return 0;
5819 shader_data = shader->backend_data;
5820 gl_shaders = shader_data->gl_shaders.gs;
5822 if (shader_data->num_gl_shaders)
5823 return gl_shaders[0].id;
5825 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5827 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5829 ERR("Failed to allocate GL shader array.\n");
5830 return 0;
5832 shader_data->shader_array_size = 1;
5833 gl_shaders = shader_data->gl_shaders.gs;
5835 string_buffer_clear(buffer);
5836 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5837 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5839 return ret;
5842 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5844 switch (mcs)
5846 case WINED3D_MCS_MATERIAL:
5847 return material;
5848 case WINED3D_MCS_COLOR1:
5849 return "ffp_attrib_diffuse";
5850 case WINED3D_MCS_COLOR2:
5851 return "ffp_attrib_specular";
5852 default:
5853 ERR("Invalid material color source %#x.\n", mcs);
5854 return "<invalid>";
5858 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5859 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5861 const char *diffuse, *specular, *emissive, *ambient;
5862 enum wined3d_light_type light_type;
5863 unsigned int i;
5865 if (!settings->lighting)
5867 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5868 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
5869 return;
5872 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5873 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5874 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5875 shader_addline(buffer, "vec3 dir, dst;\n");
5876 shader_addline(buffer, "float att, t;\n");
5878 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5879 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5880 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5881 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5883 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5885 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5886 switch (light_type)
5888 case WINED3D_LIGHT_POINT:
5889 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5890 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5891 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5892 shader_addline(buffer, "dst.x = 1.0;\n");
5893 if (legacy_lighting)
5895 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5896 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5898 else
5900 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5902 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5903 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5904 if (!legacy_lighting)
5905 shader_addline(buffer, "att = 1.0 / att;\n");
5906 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5907 if (!settings->normal)
5909 if (!legacy_lighting)
5910 shader_addline(buffer, "}\n");
5911 break;
5913 shader_addline(buffer, "dir = normalize(dir);\n");
5914 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5915 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5916 if (settings->localviewer)
5917 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5918 else
5919 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5920 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5921 " * ffp_light[%u].specular) * att;\n", i);
5922 if (!legacy_lighting)
5923 shader_addline(buffer, "}\n");
5924 break;
5926 case WINED3D_LIGHT_SPOT:
5927 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5928 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5929 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5930 shader_addline(buffer, "dst.x = 1.0;\n");
5931 if (legacy_lighting)
5933 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5934 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5936 else
5938 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5940 shader_addline(buffer, "dir = normalize(dir);\n");
5941 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5942 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5943 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5944 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5945 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5946 i, i, i, i);
5947 if (legacy_lighting)
5948 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5949 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5950 i, i, i);
5951 else
5952 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5953 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5954 i, i, i);
5955 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5956 if (!settings->normal)
5958 if (!legacy_lighting)
5959 shader_addline(buffer, "}\n");
5960 break;
5962 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5963 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5964 if (settings->localviewer)
5965 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5966 else
5967 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5968 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5969 " * ffp_light[%u].specular) * att;\n", i);
5970 if (!legacy_lighting)
5971 shader_addline(buffer, "}\n");
5972 break;
5974 case WINED3D_LIGHT_DIRECTIONAL:
5975 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5976 if (!settings->normal)
5977 break;
5978 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
5979 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5980 " * ffp_light[%u].diffuse.xyz;\n", i);
5981 /* TODO: In the non-local viewer case the halfvector is constant
5982 * and could be precomputed and stored in a uniform. */
5983 if (settings->localviewer)
5984 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5985 else
5986 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5987 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5988 " * ffp_light[%u].specular;\n", i);
5989 break;
5991 case WINED3D_LIGHT_PARALLELPOINT:
5992 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5993 if (!settings->normal)
5994 break;
5995 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
5996 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5997 " * ffp_light[%u].diffuse.xyz;\n", i);
5998 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5999 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6000 " * ffp_light[%u].specular;\n", i);
6001 break;
6003 default:
6004 if (light_type)
6005 FIXME("Unhandled light type %#x.\n", light_type);
6006 continue;
6010 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6011 ambient, diffuse, emissive);
6012 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6013 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6016 /* Context activation is done by the caller. */
6017 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6018 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6020 static const struct attrib_info
6022 const char type[6];
6023 const char name[24];
6025 attrib_info[] =
6027 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6028 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6029 /* TODO: Indexed vertex blending */
6030 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6031 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6032 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6033 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6034 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6036 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6037 BOOL legacy_lighting = priv->legacy_lighting;
6038 GLuint shader_obj;
6039 unsigned int i;
6040 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6041 BOOL output_legacy_fogcoord = legacy_context;
6043 string_buffer_clear(buffer);
6045 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6047 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6049 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6051 shader_addline(buffer, "attribute %s vs_in%u;\n", type, i);
6053 shader_addline(buffer, "\n");
6055 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6056 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6057 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6058 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6060 shader_addline(buffer, "uniform struct\n{\n");
6061 shader_addline(buffer, " vec4 emissive;\n");
6062 shader_addline(buffer, " vec4 ambient;\n");
6063 shader_addline(buffer, " vec4 diffuse;\n");
6064 shader_addline(buffer, " vec4 specular;\n");
6065 shader_addline(buffer, " float shininess;\n");
6066 shader_addline(buffer, "} ffp_material;\n");
6068 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6069 shader_addline(buffer, "uniform struct\n{\n");
6070 shader_addline(buffer, " vec4 diffuse;\n");
6071 shader_addline(buffer, " vec4 specular;\n");
6072 shader_addline(buffer, " vec4 ambient;\n");
6073 shader_addline(buffer, " vec4 position;\n");
6074 shader_addline(buffer, " vec3 direction;\n");
6075 shader_addline(buffer, " float range;\n");
6076 shader_addline(buffer, " float falloff;\n");
6077 shader_addline(buffer, " float c_att;\n");
6078 shader_addline(buffer, " float l_att;\n");
6079 shader_addline(buffer, " float q_att;\n");
6080 shader_addline(buffer, " float cos_htheta;\n");
6081 shader_addline(buffer, " float cos_hphi;\n");
6082 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6084 if (settings->point_size)
6086 shader_addline(buffer, "uniform struct\n{\n");
6087 shader_addline(buffer, " float size;\n");
6088 shader_addline(buffer, " float size_min;\n");
6089 shader_addline(buffer, " float size_max;\n");
6090 shader_addline(buffer, " float c_att;\n");
6091 shader_addline(buffer, " float l_att;\n");
6092 shader_addline(buffer, " float q_att;\n");
6093 shader_addline(buffer, "} ffp_point;\n");
6096 if (legacy_context)
6098 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6099 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6100 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6101 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6103 else
6105 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6106 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6107 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6108 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6111 shader_addline(buffer, "\nvoid main()\n{\n");
6112 shader_addline(buffer, "float m;\n");
6113 shader_addline(buffer, "vec3 r;\n");
6115 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6117 if (attrib_info[i].name[0])
6118 shader_addline(buffer, "%s %s = vs_in%u;\n",
6119 attrib_info[i].type, attrib_info[i].name, i);
6121 for (i = 0; i < MAX_TEXTURES; ++i)
6123 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6124 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU)
6126 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6130 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6132 if (settings->transformed)
6134 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6135 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6136 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6138 else
6140 for (i = 0; i < settings->vertexblends; ++i)
6141 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6143 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6144 for (i = 0; i < settings->vertexblends + 1; ++i)
6145 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6147 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6148 if (settings->clipping)
6149 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6150 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6153 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6154 if (settings->normal)
6156 if (!settings->vertexblends)
6158 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6160 else
6162 for (i = 0; i < settings->vertexblends + 1; ++i)
6163 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6166 if (settings->normalize)
6167 shader_addline(buffer, "normal = normalize(normal);\n");
6170 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6171 if (legacy_context)
6173 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6174 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6177 for (i = 0; i < MAX_TEXTURES; ++i)
6179 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6181 switch (settings->texgen[i] & 0xffff0000)
6183 case WINED3DTSS_TCI_PASSTHRU:
6184 if (settings->texcoords & (1u << i))
6185 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6186 i, i, i);
6187 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6188 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6189 else
6190 output_legacy_texcoord = FALSE;
6191 break;
6193 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6194 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6195 break;
6197 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6198 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6199 break;
6201 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6202 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6203 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6204 break;
6206 case WINED3DTSS_TCI_SPHEREMAP:
6207 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6208 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6209 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6210 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6211 break;
6213 default:
6214 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6215 break;
6217 if (output_legacy_texcoord)
6218 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6221 switch (settings->fog_mode)
6223 case WINED3D_FFP_VS_FOG_OFF:
6224 output_legacy_fogcoord = FALSE;
6225 break;
6227 case WINED3D_FFP_VS_FOG_FOGCOORD:
6228 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6229 break;
6231 case WINED3D_FFP_VS_FOG_RANGE:
6232 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6233 break;
6235 case WINED3D_FFP_VS_FOG_DEPTH:
6236 if (settings->ortho_fog)
6237 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6238 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6239 else if (settings->transformed)
6240 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6241 else
6242 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6243 break;
6245 default:
6246 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6247 break;
6249 if (output_legacy_fogcoord)
6250 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6252 if (settings->point_size)
6254 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6255 " + ffp_point.l_att * length(ec_pos.xyz)"
6256 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6257 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6258 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6261 shader_addline(buffer, "}\n");
6263 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6264 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6266 return shader_obj;
6269 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6270 DWORD argnum, unsigned int stage, DWORD arg)
6272 const char *ret;
6274 if (arg == ARG_UNUSED)
6275 return "<unused arg>";
6277 switch (arg & WINED3DTA_SELECTMASK)
6279 case WINED3DTA_DIFFUSE:
6280 ret = "ffp_varying_diffuse";
6281 break;
6283 case WINED3DTA_CURRENT:
6284 ret = "ret";
6285 break;
6287 case WINED3DTA_TEXTURE:
6288 switch (stage)
6290 case 0: ret = "tex0"; break;
6291 case 1: ret = "tex1"; break;
6292 case 2: ret = "tex2"; break;
6293 case 3: ret = "tex3"; break;
6294 case 4: ret = "tex4"; break;
6295 case 5: ret = "tex5"; break;
6296 case 6: ret = "tex6"; break;
6297 case 7: ret = "tex7"; break;
6298 default:
6299 ret = "<invalid texture>";
6300 break;
6302 break;
6304 case WINED3DTA_TFACTOR:
6305 ret = "tex_factor";
6306 break;
6308 case WINED3DTA_SPECULAR:
6309 ret = "ffp_varying_specular";
6310 break;
6312 case WINED3DTA_TEMP:
6313 ret = "temp_reg";
6314 break;
6316 case WINED3DTA_CONSTANT:
6317 switch (stage)
6319 case 0: ret = "tss_const0"; break;
6320 case 1: ret = "tss_const1"; break;
6321 case 2: ret = "tss_const2"; break;
6322 case 3: ret = "tss_const3"; break;
6323 case 4: ret = "tss_const4"; break;
6324 case 5: ret = "tss_const5"; break;
6325 case 6: ret = "tss_const6"; break;
6326 case 7: ret = "tss_const7"; break;
6327 default:
6328 ret = "<invalid constant>";
6329 break;
6331 break;
6333 default:
6334 return "<unhandled arg>";
6337 if (arg & WINED3DTA_COMPLEMENT)
6339 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6340 if (argnum == 0)
6341 ret = "arg0";
6342 else if (argnum == 1)
6343 ret = "arg1";
6344 else if (argnum == 2)
6345 ret = "arg2";
6348 if (arg & WINED3DTA_ALPHAREPLICATE)
6350 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6351 if (argnum == 0)
6352 ret = "arg0";
6353 else if (argnum == 1)
6354 ret = "arg1";
6355 else if (argnum == 2)
6356 ret = "arg2";
6359 return ret;
6362 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6363 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6365 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6367 if (color && alpha)
6368 dstmask = "";
6369 else if (color)
6370 dstmask = ".xyz";
6371 else
6372 dstmask = ".w";
6374 if (dst == tempreg)
6375 dstreg = "temp_reg";
6376 else
6377 dstreg = "ret";
6379 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6380 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6381 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6383 switch (op)
6385 case WINED3D_TOP_DISABLE:
6386 break;
6388 case WINED3D_TOP_SELECT_ARG1:
6389 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6390 break;
6392 case WINED3D_TOP_SELECT_ARG2:
6393 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6394 break;
6396 case WINED3D_TOP_MODULATE:
6397 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6398 break;
6400 case WINED3D_TOP_MODULATE_4X:
6401 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6402 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6403 break;
6405 case WINED3D_TOP_MODULATE_2X:
6406 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6407 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6408 break;
6410 case WINED3D_TOP_ADD:
6411 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6412 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6413 break;
6415 case WINED3D_TOP_ADD_SIGNED:
6416 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6417 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6418 break;
6420 case WINED3D_TOP_ADD_SIGNED_2X:
6421 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6422 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6423 break;
6425 case WINED3D_TOP_SUBTRACT:
6426 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6427 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6428 break;
6430 case WINED3D_TOP_ADD_SMOOTH:
6431 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6432 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6433 break;
6435 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6436 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6437 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6438 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6439 break;
6441 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6442 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6443 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6444 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6445 break;
6447 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6448 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6449 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6450 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6451 break;
6453 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6454 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6455 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6456 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6457 break;
6459 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6460 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
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_MODULATE_ALPHA_ADD_COLOR:
6466 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6467 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6468 break;
6470 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6471 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6472 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6473 break;
6475 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6476 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6477 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6478 break;
6479 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6480 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6481 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6482 break;
6484 case WINED3D_TOP_BUMPENVMAP:
6485 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6486 /* These are handled in the first pass, nothing to do. */
6487 break;
6489 case WINED3D_TOP_DOTPRODUCT3:
6490 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6491 dstreg, dstmask, arg1, arg2, dstmask);
6492 break;
6494 case WINED3D_TOP_MULTIPLY_ADD:
6495 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6496 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6497 break;
6499 case WINED3D_TOP_LERP:
6500 /* MSDN isn't quite right here. */
6501 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6502 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6503 break;
6505 default:
6506 FIXME("Unhandled operation %#x.\n", op);
6507 break;
6511 /* Context activation is done by the caller. */
6512 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6513 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6515 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6516 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6517 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6518 UINT lowest_disabled_stage;
6519 GLuint shader_id;
6520 DWORD arg0, arg1, arg2;
6521 unsigned int stage;
6522 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6523 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6525 string_buffer_clear(buffer);
6527 /* Find out which textures are read */
6528 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6530 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6531 break;
6533 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6534 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6535 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6537 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6538 || (stage == 0 && settings->color_key_enabled))
6539 tex_map |= 1u << stage;
6540 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6541 tfactor_used = TRUE;
6542 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6543 tempreg_used = TRUE;
6544 if (settings->op[stage].dst == tempreg)
6545 tempreg_used = TRUE;
6546 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6547 tss_const_map |= 1u << stage;
6549 switch (settings->op[stage].cop)
6551 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6552 lum_map |= 1u << stage;
6553 /* fall through */
6554 case WINED3D_TOP_BUMPENVMAP:
6555 bump_map |= 1u << stage;
6556 /* fall through */
6557 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6558 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6559 tex_map |= 1u << stage;
6560 break;
6562 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6563 tfactor_used = TRUE;
6564 break;
6566 default:
6567 break;
6570 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6571 continue;
6573 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6574 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6575 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6577 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6578 tex_map |= 1u << stage;
6579 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6580 tfactor_used = TRUE;
6581 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6582 tempreg_used = TRUE;
6583 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6584 tss_const_map |= 1u << stage;
6586 lowest_disabled_stage = stage;
6588 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6590 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6591 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6593 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6594 shader_addline(buffer, "vec4 ret;\n");
6595 if (tempreg_used || settings->sRGB_write)
6596 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6597 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6599 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6601 if (tss_const_map & (1u << stage))
6602 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6604 if (!(tex_map & (1u << stage)))
6605 continue;
6607 switch (settings->op[stage].tex_type)
6609 case WINED3D_GL_RES_TYPE_TEX_1D:
6610 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6611 break;
6612 case WINED3D_GL_RES_TYPE_TEX_2D:
6613 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6614 break;
6615 case WINED3D_GL_RES_TYPE_TEX_3D:
6616 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6617 break;
6618 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6619 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6620 break;
6621 case WINED3D_GL_RES_TYPE_TEX_RECT:
6622 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6623 break;
6624 default:
6625 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6626 break;
6629 shader_addline(buffer, "vec4 tex%u;\n", stage);
6631 if (!(bump_map & (1u << stage)))
6632 continue;
6633 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6635 if (!(lum_map & (1u << stage)))
6636 continue;
6637 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6638 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6640 if (tfactor_used)
6641 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6642 if (settings->color_key_enabled)
6643 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6644 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6646 if (settings->sRGB_write)
6648 shader_addline(buffer, "const vec4 srgb_const0 = ");
6649 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6650 shader_addline(buffer, ";\n");
6651 shader_addline(buffer, "const vec4 srgb_const1 = ");
6652 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6653 shader_addline(buffer, ";\n");
6656 shader_addline(buffer, "uniform struct\n{\n");
6657 shader_addline(buffer, " vec4 color;\n");
6658 shader_addline(buffer, " float density;\n");
6659 shader_addline(buffer, " float end;\n");
6660 shader_addline(buffer, " float scale;\n");
6661 shader_addline(buffer, "} ffp_fog;\n");
6663 if (legacy_context)
6665 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6666 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6667 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6668 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6669 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6671 else
6673 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6674 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6675 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6676 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6677 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6680 shader_addline(buffer, "void main()\n{\n");
6682 if (legacy_context)
6684 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6685 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6688 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6690 if (tex_map & (1u << stage))
6692 if (settings->pointsprite)
6693 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6694 else if (settings->texcoords_initialized & (1u << stage))
6695 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6696 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6697 else
6698 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6702 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6703 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6705 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6706 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6708 /* Generate texture sampling instructions */
6709 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6711 const char *texture_function, *coord_mask;
6712 BOOL proj;
6714 if (!(tex_map & (1u << stage)))
6715 continue;
6717 if (settings->op[stage].projected == proj_none)
6719 proj = FALSE;
6721 else if (settings->op[stage].projected == proj_count4
6722 || settings->op[stage].projected == proj_count3)
6724 proj = TRUE;
6726 else
6728 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6729 proj = TRUE;
6732 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
6733 proj = FALSE;
6735 switch (settings->op[stage].tex_type)
6737 case WINED3D_GL_RES_TYPE_TEX_1D:
6738 if (proj)
6740 texture_function = "texture1DProj";
6741 coord_mask = "xw";
6743 else
6745 texture_function = "texture1D";
6746 coord_mask = "x";
6748 break;
6749 case WINED3D_GL_RES_TYPE_TEX_2D:
6750 if (proj)
6752 texture_function = "texture2DProj";
6753 coord_mask = "xyw";
6755 else
6757 texture_function = "texture2D";
6758 coord_mask = "xy";
6760 break;
6761 case WINED3D_GL_RES_TYPE_TEX_3D:
6762 if (proj)
6764 texture_function = "texture3DProj";
6765 coord_mask = "xyzw";
6767 else
6769 texture_function = "texture3D";
6770 coord_mask = "xyz";
6772 break;
6773 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6774 texture_function = "textureCube";
6775 coord_mask = "xyz";
6776 break;
6777 case WINED3D_GL_RES_TYPE_TEX_RECT:
6778 if (proj)
6780 texture_function = "texture2DRectProj";
6781 coord_mask = "xyw";
6783 else
6785 texture_function = "texture2DRect";
6786 coord_mask = "xy";
6788 break;
6789 default:
6790 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6791 texture_function = "";
6792 coord_mask = "xyzw";
6793 break;
6795 if (!needs_legacy_glsl_syntax(gl_info))
6796 texture_function = proj ? "textureProj" : "texture";
6798 if (stage > 0
6799 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6800 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6802 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6804 /* With projective textures, texbem only divides the static
6805 * texture coord, not the displacement, so multiply the
6806 * displacement with the dividing parameter before passing it to
6807 * TXP. */
6808 if (settings->op[stage].projected != proj_none)
6810 if (settings->op[stage].projected == proj_count4)
6812 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6813 stage, stage);
6814 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
6816 else
6818 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6819 stage, stage);
6820 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
6823 else
6825 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
6828 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6829 stage, texture_function, stage, coord_mask);
6831 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6832 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6833 stage, stage - 1, stage - 1, stage - 1);
6835 else if (settings->op[stage].projected == proj_count3)
6837 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6838 stage, texture_function, stage, stage);
6840 else
6842 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6843 stage, texture_function, stage, stage, coord_mask);
6846 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6847 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6848 settings->op[stage].color_fixup);
6851 if (settings->color_key_enabled)
6853 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6854 shader_addline(buffer, " discard;\n");
6857 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
6859 /* Generate the main shader */
6860 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6862 BOOL op_equal;
6864 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6865 break;
6867 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6868 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6869 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6870 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6871 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6872 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6873 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6874 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6875 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6876 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6877 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6878 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6879 else
6880 op_equal = settings->op[stage].aop == settings->op[stage].cop
6881 && settings->op[stage].carg0 == settings->op[stage].aarg0
6882 && settings->op[stage].carg1 == settings->op[stage].aarg1
6883 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6885 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6887 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6888 settings->op[stage].cop, settings->op[stage].carg0,
6889 settings->op[stage].carg1, settings->op[stage].carg2);
6891 else if (op_equal)
6893 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6894 settings->op[stage].cop, settings->op[stage].carg0,
6895 settings->op[stage].carg1, settings->op[stage].carg2);
6897 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
6898 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6900 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6901 settings->op[stage].cop, settings->op[stage].carg0,
6902 settings->op[stage].carg1, settings->op[stage].carg2);
6903 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6904 settings->op[stage].aop, settings->op[stage].aarg0,
6905 settings->op[stage].aarg1, settings->op[stage].aarg2);
6909 shader_addline(buffer, "gl_FragData[0] = ffp_varying_specular * specular_enable + ret;\n");
6911 if (settings->sRGB_write)
6912 shader_glsl_generate_srgb_write_correction(buffer);
6914 shader_glsl_generate_fog_code(buffer, settings->fog);
6916 shader_addline(buffer, "}\n");
6918 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6919 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6921 string_buffer_release(&priv->string_buffers, tex_reg_name);
6922 return shader_id;
6925 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6926 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6928 struct glsl_ffp_vertex_shader *shader;
6929 const struct wine_rb_entry *entry;
6931 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6932 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6934 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6935 return NULL;
6937 shader->desc.settings = *settings;
6938 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
6939 list_init(&shader->linked_programs);
6940 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6941 ERR("Failed to insert ffp vertex shader.\n");
6943 return shader;
6946 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6947 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6949 struct glsl_ffp_fragment_shader *glsl_desc;
6950 const struct ffp_frag_desc *desc;
6952 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6953 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6955 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6956 return NULL;
6958 glsl_desc->entry.settings = *args;
6959 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
6960 list_init(&glsl_desc->linked_programs);
6961 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6963 return glsl_desc;
6967 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6968 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6970 unsigned int i;
6971 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6973 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6974 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6975 for (i = 0; i < vs_c_count; ++i)
6977 string_buffer_sprintf(name, "vs_c[%u]", i);
6978 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6980 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6981 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
6983 for (i = 0; i < MAX_CONST_I; ++i)
6985 string_buffer_sprintf(name, "vs_i[%u]", i);
6986 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6989 for (i = 0; i < MAX_CONST_B; ++i)
6991 string_buffer_sprintf(name, "vs_b[%u]", i);
6992 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6995 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
6997 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
6999 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7000 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7002 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7003 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7004 for (i = 0; i < MAX_TEXTURES; ++i)
7006 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7007 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7009 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7010 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7011 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7012 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7013 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7014 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7015 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7017 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7018 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7019 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7020 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7021 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7022 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7023 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7024 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7025 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7026 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7027 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7028 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7029 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7030 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7031 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7032 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7033 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7034 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7035 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7036 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7037 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7038 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7039 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7040 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7042 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7043 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7044 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7045 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7046 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7047 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7049 string_buffer_release(&priv->string_buffers, name);
7052 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7053 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7055 unsigned int i;
7056 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7058 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
7059 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
7060 for (i = 0; i < ps_c_count; ++i)
7062 string_buffer_sprintf(name, "ps_c[%u]", i);
7063 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7065 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
7066 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
7068 for (i = 0; i < MAX_CONST_I; ++i)
7070 string_buffer_sprintf(name, "ps_i[%u]", i);
7071 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7074 for (i = 0; i < MAX_CONST_B; ++i)
7076 string_buffer_sprintf(name, "ps_b[%u]", i);
7077 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7080 for (i = 0; i < MAX_TEXTURES; ++i)
7082 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7083 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7084 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7085 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7086 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7087 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7088 string_buffer_sprintf(name, "tss_const%u", i);
7089 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7092 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7093 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7095 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7096 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7097 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7098 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7100 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7101 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7102 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7104 string_buffer_release(&priv->string_buffers, name);
7107 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7108 struct shader_glsl_priv *priv, GLuint program_id,
7109 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
7111 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7112 GLuint block_idx;
7113 unsigned int i;
7114 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7116 for (i = 0; i < count; ++i)
7118 if (!reg_maps->cb_sizes[i])
7119 continue;
7121 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7122 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7123 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7125 checkGLcall("glUniformBlockBinding");
7126 string_buffer_release(&priv->string_buffers, name);
7129 /* Context activation is done by the caller. */
7130 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7131 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7133 const struct wined3d_gl_info *gl_info = context->gl_info;
7134 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7135 const struct ps_np2fixup_info *np2fixup_info = NULL;
7136 struct glsl_shader_prog_link *entry = NULL;
7137 struct wined3d_shader *vshader = NULL;
7138 struct wined3d_shader *gshader = NULL;
7139 struct wined3d_shader *pshader = NULL;
7140 GLuint program_id;
7141 GLuint reorder_shader_id = 0;
7142 unsigned int i;
7143 GLuint vs_id = 0;
7144 GLuint gs_id = 0;
7145 GLuint ps_id = 0;
7146 struct list *ps_list, *vs_list;
7147 WORD attribs_map;
7148 struct wined3d_string_buffer *tmp_name;
7150 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7152 vs_id = ctx_data->glsl_program->vs.id;
7153 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7155 if (use_vs(state))
7157 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7158 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7160 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7161 && ctx_data->glsl_program->gs.id)
7162 gs_id = ctx_data->glsl_program->gs.id;
7163 else if (gshader)
7164 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7167 else if (use_vs(state))
7169 struct vs_compile_args vs_compile_args;
7171 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7173 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7174 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
7175 vs_list = &vshader->linked_programs;
7177 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
7178 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7180 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7182 struct glsl_ffp_vertex_shader *ffp_shader;
7183 struct wined3d_ffp_vs_settings settings;
7185 wined3d_ffp_get_vs_settings(context, state, &settings);
7186 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7187 vs_id = ffp_shader->id;
7188 vs_list = &ffp_shader->linked_programs;
7191 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7193 ps_id = ctx_data->glsl_program->ps.id;
7194 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7196 if (use_ps(state))
7197 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7199 else if (use_ps(state))
7201 struct ps_compile_args ps_compile_args;
7202 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7203 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7204 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7205 pshader, &ps_compile_args, &np2fixup_info);
7206 ps_list = &pshader->linked_programs;
7208 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7210 struct glsl_ffp_fragment_shader *ffp_shader;
7211 struct ffp_frag_settings settings;
7213 gen_ffp_frag_op(context, state, &settings, FALSE);
7214 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7215 ps_id = ffp_shader->id;
7216 ps_list = &ffp_shader->linked_programs;
7219 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7221 ctx_data->glsl_program = entry;
7222 return;
7225 /* If we get to this point, then no matching program exists, so we create one */
7226 program_id = GL_EXTCALL(glCreateProgram());
7227 TRACE("Created new GLSL shader program %u.\n", program_id);
7229 /* Create the entry */
7230 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7231 entry->id = program_id;
7232 entry->vs.id = vs_id;
7233 entry->gs.id = gs_id;
7234 entry->ps.id = ps_id;
7235 entry->constant_version = 0;
7236 entry->ps.np2_fixup_info = np2fixup_info;
7237 /* Add the hash table entry */
7238 add_glsl_program_entry(priv, entry);
7240 /* Set the current program */
7241 ctx_data->glsl_program = entry;
7243 /* Attach GLSL vshader */
7244 if (vs_id)
7246 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7247 GL_EXTCALL(glAttachShader(program_id, vs_id));
7248 checkGLcall("glAttachShader");
7250 list_add_head(vs_list, &entry->vs.shader_entry);
7253 if (vshader)
7255 attribs_map = vshader->reg_maps.input_registers;
7256 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
7257 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7258 d3d_info->emulated_flatshading
7259 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7260 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7261 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7262 checkGLcall("glAttachShader");
7263 /* Flag the reorder function for deletion, then it will be freed automatically when the program
7264 * is destroyed
7266 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7268 else
7270 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7273 /* Bind vertex attributes to a corresponding index number to match
7274 * the same index numbers as ARB_vertex_programs (makes loading
7275 * vertex attributes simpler). With this method, we can use the
7276 * exact same code to load the attributes later for both ARB and
7277 * GLSL shaders.
7279 * We have to do this here because we need to know the Program ID
7280 * in order to make the bindings work, and it has to be done prior
7281 * to linking the GLSL program. */
7282 tmp_name = string_buffer_get(&priv->string_buffers);
7283 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7285 if (!(attribs_map & 1))
7286 continue;
7288 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7289 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7291 checkGLcall("glBindAttribLocation");
7292 string_buffer_release(&priv->string_buffers, tmp_name);
7294 if (gshader)
7296 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7297 GL_EXTCALL(glAttachShader(program_id, gs_id));
7298 checkGLcall("glAttachShader");
7300 TRACE("input type %s, output type %s, vertices out %u.\n",
7301 debug_d3dprimitivetype(gshader->u.gs.input_type),
7302 debug_d3dprimitivetype(gshader->u.gs.output_type),
7303 gshader->u.gs.vertices_out);
7304 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7305 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7306 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7307 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7308 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7309 gshader->u.gs.vertices_out));
7310 checkGLcall("glProgramParameteriARB");
7312 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7315 /* Attach GLSL pshader */
7316 if (ps_id)
7318 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7319 GL_EXTCALL(glAttachShader(program_id, ps_id));
7320 checkGLcall("glAttachShader");
7322 list_add_head(ps_list, &entry->ps.shader_entry);
7325 /* Link the program */
7326 TRACE("Linking GLSL shader program %u.\n", program_id);
7327 GL_EXTCALL(glLinkProgram(program_id));
7328 shader_glsl_validate_link(gl_info, program_id);
7330 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7331 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
7332 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7333 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
7334 checkGLcall("Find glsl program uniform locations");
7336 if (pshader && pshader->reg_maps.shader_version.major >= 3
7337 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7339 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7340 entry->vs.vertex_color_clamp = GL_FALSE;
7342 else
7344 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7347 /* Set the shader to allow uniform loading on it */
7348 GL_EXTCALL(glUseProgram(program_id));
7349 checkGLcall("glUseProgram");
7351 /* Texture unit mapping is set up to be the same each time the shader
7352 * program is used so we can hardcode the sampler uniform values. */
7353 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7355 entry->constant_update_mask = 0;
7356 if (vshader)
7358 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7359 if (vshader->reg_maps.integer_constants)
7360 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7361 if (vshader->reg_maps.boolean_constants)
7362 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7363 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7365 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7366 0, gl_info->limits.vertex_uniform_blocks);
7368 else
7370 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7371 | WINED3D_SHADER_CONST_FFP_PROJ;
7373 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7375 if (entry->vs.modelview_matrix_location[i] != -1)
7377 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7378 break;
7382 for (i = 0; i < MAX_TEXTURES; ++i)
7384 if (entry->vs.texture_matrix_location[i] != -1)
7386 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7387 break;
7390 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7391 || entry->vs.material_specular_location != -1
7392 || entry->vs.material_emissive_location != -1
7393 || entry->vs.material_shininess_location != -1)
7394 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7395 if (entry->vs.light_ambient_location != -1)
7396 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7398 if (entry->vs.pointsize_min_location != -1)
7399 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7401 if (gshader)
7402 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7403 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7405 if (ps_id)
7407 if (pshader)
7409 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7410 if (pshader->reg_maps.integer_constants)
7411 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7412 if (pshader->reg_maps.boolean_constants)
7413 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7414 if (entry->ps.ycorrection_location != -1)
7415 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7417 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7418 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7419 gl_info->limits.fragment_uniform_blocks);
7421 else
7423 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7426 for (i = 0; i < MAX_TEXTURES; ++i)
7428 if (entry->ps.bumpenv_mat_location[i] != -1)
7430 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7431 break;
7435 if (entry->ps.fog_color_location != -1)
7436 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7437 if (entry->ps.np2_fixup_location != -1)
7438 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7439 if (entry->ps.color_key_location != -1)
7440 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7444 /* Context activation is done by the caller. */
7445 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7446 BOOL masked)
7448 GLuint program_id;
7449 GLuint vshader_id, pshader_id;
7450 const char *blt_pshader;
7452 static const char blt_vshader[] =
7453 "#version 120\n"
7454 "void main(void)\n"
7455 "{\n"
7456 " gl_Position = gl_Vertex;\n"
7457 " gl_FrontColor = vec4(1.0);\n"
7458 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7459 "}\n";
7461 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7463 /* WINED3D_GL_RES_TYPE_TEX_1D */
7464 NULL,
7465 /* WINED3D_GL_RES_TYPE_TEX_2D */
7466 "#version 120\n"
7467 "uniform sampler2D sampler;\n"
7468 "void main(void)\n"
7469 "{\n"
7470 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7471 "}\n",
7472 /* WINED3D_GL_RES_TYPE_TEX_3D */
7473 NULL,
7474 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7475 "#version 120\n"
7476 "uniform samplerCube sampler;\n"
7477 "void main(void)\n"
7478 "{\n"
7479 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7480 "}\n",
7481 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7482 "#version 120\n"
7483 "#extension GL_ARB_texture_rectangle : enable\n"
7484 "uniform sampler2DRect sampler;\n"
7485 "void main(void)\n"
7486 "{\n"
7487 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7488 "}\n",
7489 /* WINED3D_GL_RES_TYPE_BUFFER */
7490 NULL,
7491 /* WINED3D_GL_RES_TYPE_RB */
7492 NULL,
7495 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7497 /* WINED3D_GL_RES_TYPE_TEX_1D */
7498 NULL,
7499 /* WINED3D_GL_RES_TYPE_TEX_2D */
7500 "#version 120\n"
7501 "uniform sampler2D sampler;\n"
7502 "uniform vec4 mask;\n"
7503 "void main(void)\n"
7504 "{\n"
7505 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7506 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7507 "}\n",
7508 /* WINED3D_GL_RES_TYPE_TEX_3D */
7509 NULL,
7510 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7511 "#version 120\n"
7512 "uniform samplerCube sampler;\n"
7513 "uniform vec4 mask;\n"
7514 "void main(void)\n"
7515 "{\n"
7516 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7517 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7518 "}\n",
7519 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7520 "#version 120\n"
7521 "#extension GL_ARB_texture_rectangle : enable\n"
7522 "uniform sampler2DRect sampler;\n"
7523 "uniform vec4 mask;\n"
7524 "void main(void)\n"
7525 "{\n"
7526 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7527 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7528 "}\n",
7529 /* WINED3D_GL_RES_TYPE_BUFFER */
7530 NULL,
7531 /* WINED3D_GL_RES_TYPE_RB */
7532 NULL,
7535 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7536 if (!blt_pshader)
7538 FIXME("tex_type %#x not supported\n", tex_type);
7539 return 0;
7542 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7543 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7545 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7546 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7548 program_id = GL_EXTCALL(glCreateProgram());
7549 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7550 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7551 GL_EXTCALL(glLinkProgram(program_id));
7553 shader_glsl_validate_link(gl_info, program_id);
7555 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7556 * is destroyed
7558 GL_EXTCALL(glDeleteShader(vshader_id));
7559 GL_EXTCALL(glDeleteShader(pshader_id));
7560 return program_id;
7563 /* Context activation is done by the caller. */
7564 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7565 const struct wined3d_state *state)
7567 struct glsl_context_data *ctx_data = context->shader_backend_data;
7568 const struct wined3d_gl_info *gl_info = context->gl_info;
7569 struct shader_glsl_priv *priv = shader_priv;
7570 GLuint program_id = 0, prev_id = 0;
7571 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7573 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7574 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7576 if (ctx_data->glsl_program)
7578 prev_id = ctx_data->glsl_program->id;
7579 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7581 else
7583 prev_id = 0;
7584 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7587 set_glsl_shader_program(context, state, priv, ctx_data);
7589 if (ctx_data->glsl_program)
7591 program_id = ctx_data->glsl_program->id;
7592 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7594 else
7596 program_id = 0;
7597 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7600 if (old_vertex_color_clamp != current_vertex_color_clamp)
7602 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7604 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7605 checkGLcall("glClampColorARB");
7607 else
7609 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7613 TRACE("Using GLSL program %u.\n", program_id);
7615 if (prev_id != program_id)
7617 GL_EXTCALL(glUseProgram(program_id));
7618 checkGLcall("glUseProgram");
7620 if (program_id)
7621 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7625 /* "context" is not necessarily the currently active context. */
7626 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7628 struct glsl_context_data *ctx_data = context->shader_backend_data;
7630 ctx_data->glsl_program = NULL;
7631 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7632 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7633 | (1u << WINED3D_SHADER_TYPE_GEOMETRY);
7636 /* Context activation is done by the caller. */
7637 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7639 const struct wined3d_gl_info *gl_info = context->gl_info;
7640 struct shader_glsl_priv *priv = shader_priv;
7642 shader_glsl_invalidate_current_program(context);
7643 GL_EXTCALL(glUseProgram(0));
7644 checkGLcall("glUseProgram");
7646 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7647 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7649 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7651 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7652 checkGLcall("glClampColorARB");
7656 /* Context activation is done by the caller. */
7657 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7658 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7660 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7661 struct shader_glsl_priv *priv = shader_priv;
7662 GLuint *blt_program;
7663 GLint loc;
7665 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7666 if (!*blt_program)
7668 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7669 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7670 GL_EXTCALL(glUseProgram(*blt_program));
7671 GL_EXTCALL(glUniform1i(loc, 0));
7673 else
7675 GL_EXTCALL(glUseProgram(*blt_program));
7678 if (masked)
7680 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7681 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7685 /* Context activation is done by the caller. */
7686 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7688 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7689 GLuint program_id;
7691 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7692 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7694 GL_EXTCALL(glUseProgram(program_id));
7695 checkGLcall("glUseProgram");
7698 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7699 const struct glsl_shader_prog_link *program)
7701 const struct glsl_context_data *ctx_data;
7702 struct wined3d_context *context;
7703 unsigned int i;
7705 for (i = 0; i < device->context_count; ++i)
7707 context = device->contexts[i];
7708 ctx_data = context->shader_backend_data;
7710 if (ctx_data->glsl_program == program)
7711 shader_glsl_invalidate_current_program(context);
7715 static void shader_glsl_destroy(struct wined3d_shader *shader)
7717 struct glsl_shader_private *shader_data = shader->backend_data;
7718 struct wined3d_device *device = shader->device;
7719 struct shader_glsl_priv *priv = device->shader_priv;
7720 const struct wined3d_gl_info *gl_info;
7721 const struct list *linked_programs;
7722 struct wined3d_context *context;
7724 if (!shader_data || !shader_data->num_gl_shaders)
7726 HeapFree(GetProcessHeap(), 0, shader_data);
7727 shader->backend_data = NULL;
7728 return;
7731 context = context_acquire(device, NULL);
7732 gl_info = context->gl_info;
7734 TRACE("Deleting linked programs.\n");
7735 linked_programs = &shader->linked_programs;
7736 if (linked_programs->next)
7738 struct glsl_shader_prog_link *entry, *entry2;
7739 UINT i;
7741 switch (shader->reg_maps.shader_version.type)
7743 case WINED3D_SHADER_TYPE_PIXEL:
7745 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7747 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7749 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7750 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7751 checkGLcall("glDeleteShader");
7753 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7755 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7756 struct glsl_shader_prog_link, ps.shader_entry)
7758 shader_glsl_invalidate_contexts_program(device, entry);
7759 delete_glsl_program_entry(priv, gl_info, entry);
7762 break;
7765 case WINED3D_SHADER_TYPE_VERTEX:
7767 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7769 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7771 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7772 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7773 checkGLcall("glDeleteShader");
7775 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7777 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7778 struct glsl_shader_prog_link, vs.shader_entry)
7780 shader_glsl_invalidate_contexts_program(device, entry);
7781 delete_glsl_program_entry(priv, gl_info, entry);
7784 break;
7787 case WINED3D_SHADER_TYPE_GEOMETRY:
7789 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7791 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7793 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7794 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7795 checkGLcall("glDeleteShader");
7797 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7799 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7800 struct glsl_shader_prog_link, gs.shader_entry)
7802 shader_glsl_invalidate_contexts_program(device, entry);
7803 delete_glsl_program_entry(priv, gl_info, entry);
7806 break;
7809 default:
7810 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7811 break;
7815 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7816 shader->backend_data = NULL;
7818 context_release(context);
7821 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7823 const struct glsl_program_key *k = key;
7824 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7825 const struct glsl_shader_prog_link, program_lookup_entry);
7827 if (k->vs_id > prog->vs.id) return 1;
7828 else if (k->vs_id < prog->vs.id) return -1;
7830 if (k->gs_id > prog->gs.id) return 1;
7831 else if (k->gs_id < prog->gs.id) return -1;
7833 if (k->ps_id > prog->ps.id) return 1;
7834 else if (k->ps_id < prog->ps.id) return -1;
7836 return 0;
7839 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7841 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7842 + constant_count * sizeof(*heap->contained)
7843 + constant_count * sizeof(*heap->positions);
7844 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7846 if (!mem)
7848 ERR("Failed to allocate memory\n");
7849 return FALSE;
7852 heap->entries = mem;
7853 heap->entries[1].version = 0;
7854 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7855 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7856 heap->positions = (unsigned int *)(heap->contained + constant_count);
7857 heap->size = 1;
7859 return TRUE;
7862 static void constant_heap_free(struct constant_heap *heap)
7864 HeapFree(GetProcessHeap(), 0, heap->entries);
7867 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7869 wined3d_rb_alloc,
7870 wined3d_rb_realloc,
7871 wined3d_rb_free,
7872 glsl_program_key_compare,
7875 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7876 const struct fragment_pipeline *fragment_pipe)
7878 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7879 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7880 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7881 gl_info->limits.glsl_ps_float_constants)) + 1;
7882 struct fragment_caps fragment_caps;
7883 void *vertex_priv, *fragment_priv;
7885 string_buffer_list_init(&priv->string_buffers);
7887 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7889 ERR("Failed to initialize vertex pipe.\n");
7890 HeapFree(GetProcessHeap(), 0, priv);
7891 return E_FAIL;
7894 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7896 ERR("Failed to initialize fragment pipe.\n");
7897 vertex_pipe->vp_free(device);
7898 HeapFree(GetProcessHeap(), 0, priv);
7899 return E_FAIL;
7902 if (!string_buffer_init(&priv->shader_buffer))
7904 ERR("Failed to initialize shader buffer.\n");
7905 goto fail;
7908 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7909 if (!priv->stack)
7911 ERR("Failed to allocate memory.\n");
7912 goto fail;
7915 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7917 ERR("Failed to initialize vertex shader constant heap\n");
7918 goto fail;
7921 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7923 ERR("Failed to initialize pixel shader constant heap\n");
7924 goto fail;
7927 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7929 ERR("Failed to initialize rbtree.\n");
7930 goto fail;
7933 priv->next_constant_version = 1;
7934 priv->vertex_pipe = vertex_pipe;
7935 priv->fragment_pipe = fragment_pipe;
7936 fragment_pipe->get_caps(gl_info, &fragment_caps);
7937 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7938 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7940 device->vertex_priv = vertex_priv;
7941 device->fragment_priv = fragment_priv;
7942 device->shader_priv = priv;
7944 return WINED3D_OK;
7946 fail:
7947 constant_heap_free(&priv->pconst_heap);
7948 constant_heap_free(&priv->vconst_heap);
7949 HeapFree(GetProcessHeap(), 0, priv->stack);
7950 string_buffer_free(&priv->shader_buffer);
7951 fragment_pipe->free_private(device);
7952 vertex_pipe->vp_free(device);
7953 HeapFree(GetProcessHeap(), 0, priv);
7954 return E_OUTOFMEMORY;
7957 /* Context activation is done by the caller. */
7958 static void shader_glsl_free(struct wined3d_device *device)
7960 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7961 struct shader_glsl_priv *priv = device->shader_priv;
7962 int i;
7964 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7966 if (priv->depth_blt_program_full[i])
7968 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7970 if (priv->depth_blt_program_masked[i])
7972 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7976 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7977 constant_heap_free(&priv->pconst_heap);
7978 constant_heap_free(&priv->vconst_heap);
7979 HeapFree(GetProcessHeap(), 0, priv->stack);
7980 string_buffer_list_cleanup(&priv->string_buffers);
7981 string_buffer_free(&priv->shader_buffer);
7982 priv->fragment_pipe->free_private(device);
7983 priv->vertex_pipe->vp_free(device);
7985 HeapFree(GetProcessHeap(), 0, device->shader_priv);
7986 device->shader_priv = NULL;
7989 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
7991 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
7992 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
7995 static void shader_glsl_free_context_data(struct wined3d_context *context)
7997 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8000 static void shader_glsl_init_context_state(struct wined3d_context *context)
8002 const struct wined3d_gl_info *gl_info = context->gl_info;
8004 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8005 checkGLcall("GL_PROGRAM_POINT_SIZE");
8008 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8010 UINT shader_model;
8012 if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8013 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS])
8014 shader_model = 4;
8015 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
8016 * texldd and texldl instructions. */
8017 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
8018 shader_model = 3;
8019 else
8020 shader_model = 2;
8021 TRACE("Shader model %u.\n", shader_model);
8023 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8024 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8025 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8027 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
8028 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
8029 caps->varying_count = gl_info->limits.glsl_varyings;
8031 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8032 * Direct3D minimum requirement.
8034 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8035 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8037 * The problem is that the refrast clamps temporary results in the shader to
8038 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8039 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8040 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8041 * offer a way to query this.
8043 if (shader_model >= 4)
8044 caps->ps_1x_max_value = FLT_MAX;
8045 else
8046 caps->ps_1x_max_value = 1024.0f;
8048 /* Ideally we'd only set caps like sRGB writes here if supported by both
8049 * the shader backend and the fragment pipe, but we can get called before
8050 * shader_glsl_alloc(). */
8051 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8052 | WINED3D_SHADER_CAP_SRGB_WRITE;
8055 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8057 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8059 TRACE("Checking support for fixup:\n");
8060 dump_color_fixup_desc(fixup);
8063 /* We support everything except YUV conversions. */
8064 if (!is_complex_fixup(fixup))
8066 TRACE("[OK]\n");
8067 return TRUE;
8070 TRACE("[FAILED]\n");
8071 return FALSE;
8074 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8076 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8077 /* WINED3DSIH_ADD */ shader_glsl_binop,
8078 /* WINED3DSIH_AND */ shader_glsl_binop,
8079 /* WINED3DSIH_BEM */ shader_glsl_bem,
8080 /* WINED3DSIH_BREAK */ shader_glsl_break,
8081 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8082 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8083 /* WINED3DSIH_CALL */ shader_glsl_call,
8084 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8085 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8086 /* WINED3DSIH_CND */ shader_glsl_cnd,
8087 /* WINED3DSIH_CRS */ shader_glsl_cross,
8088 /* WINED3DSIH_CUT */ shader_glsl_cut,
8089 /* WINED3DSIH_DCL */ shader_glsl_nop,
8090 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8091 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8092 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ NULL,
8093 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8094 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8095 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8096 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8097 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8098 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8099 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8100 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8101 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8102 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8103 /* WINED3DSIH_DCL_SAMPLER */ NULL,
8104 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8105 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8106 /* WINED3DSIH_DEF */ shader_glsl_nop,
8107 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8108 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8109 /* WINED3DSIH_DIV */ shader_glsl_binop,
8110 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8111 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8112 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8113 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8114 /* WINED3DSIH_DST */ shader_glsl_dst,
8115 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8116 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8117 /* WINED3DSIH_ELSE */ shader_glsl_else,
8118 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8119 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8120 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8121 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8122 /* WINED3DSIH_EQ */ shader_glsl_relop,
8123 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8124 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8125 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8126 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8127 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8128 /* WINED3DSIH_GE */ shader_glsl_relop,
8129 /* WINED3DSIH_IADD */ shader_glsl_binop,
8130 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8131 /* WINED3DSIH_IF */ shader_glsl_if,
8132 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8133 /* WINED3DSIH_IGE */ shader_glsl_relop,
8134 /* WINED3DSIH_ILT */ shader_glsl_relop,
8135 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8136 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8137 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8138 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8139 /* WINED3DSIH_INE */ shader_glsl_relop,
8140 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8141 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8142 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8143 /* WINED3DSIH_LABEL */ shader_glsl_label,
8144 /* WINED3DSIH_LD */ shader_glsl_ld,
8145 /* WINED3DSIH_LIT */ shader_glsl_lit,
8146 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8147 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8148 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8149 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8150 /* WINED3DSIH_LT */ shader_glsl_relop,
8151 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8152 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8153 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8154 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8155 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8156 /* WINED3DSIH_MAD */ shader_glsl_mad,
8157 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8158 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8159 /* WINED3DSIH_MOV */ shader_glsl_mov,
8160 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8161 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8162 /* WINED3DSIH_MUL */ shader_glsl_binop,
8163 /* WINED3DSIH_NE */ shader_glsl_relop,
8164 /* WINED3DSIH_NOP */ shader_glsl_nop,
8165 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8166 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8167 /* WINED3DSIH_OR */ shader_glsl_binop,
8168 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8169 /* WINED3DSIH_POW */ shader_glsl_pow,
8170 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8171 /* WINED3DSIH_REP */ shader_glsl_rep,
8172 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8173 /* WINED3DSIH_RET */ shader_glsl_ret,
8174 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8175 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8176 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8177 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8178 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8179 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8180 /* WINED3DSIH_SAMPLE_C */ NULL,
8181 /* WINED3DSIH_SAMPLE_C_LZ */ NULL,
8182 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8183 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8184 /* WINED3DSIH_SETP */ NULL,
8185 /* WINED3DSIH_SGE */ shader_glsl_compare,
8186 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8187 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8188 /* WINED3DSIH_SLT */ shader_glsl_compare,
8189 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8190 /* WINED3DSIH_SUB */ shader_glsl_binop,
8191 /* WINED3DSIH_TEX */ shader_glsl_tex,
8192 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8193 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8194 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8195 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8196 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8197 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8198 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8199 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8200 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8201 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8202 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8203 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8204 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8205 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8206 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8207 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8208 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8209 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8210 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8211 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8212 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8213 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8214 /* WINED3DSIH_UGE */ shader_glsl_relop,
8215 /* WINED3DSIH_USHR */ shader_glsl_binop,
8216 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8217 /* WINED3DSIH_XOR */ shader_glsl_binop,
8220 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8221 SHADER_HANDLER hw_fct;
8223 /* Select handler */
8224 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8226 /* Unhandled opcode */
8227 if (!hw_fct)
8229 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8230 return;
8232 hw_fct(ins);
8234 shader_glsl_add_instruction_modifiers(ins);
8237 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8239 struct shader_glsl_priv *priv = shader_priv;
8241 return priv->ffp_proj_control;
8244 const struct wined3d_shader_backend_ops glsl_shader_backend =
8246 shader_glsl_handle_instruction,
8247 shader_glsl_select,
8248 shader_glsl_disable,
8249 shader_glsl_select_depth_blt,
8250 shader_glsl_deselect_depth_blt,
8251 shader_glsl_update_float_vertex_constants,
8252 shader_glsl_update_float_pixel_constants,
8253 shader_glsl_load_constants,
8254 shader_glsl_destroy,
8255 shader_glsl_alloc,
8256 shader_glsl_free,
8257 shader_glsl_allocate_context_data,
8258 shader_glsl_free_context_data,
8259 shader_glsl_init_context_state,
8260 shader_glsl_get_caps,
8261 shader_glsl_color_fixup_supported,
8262 shader_glsl_has_ffp_proj_control,
8265 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8267 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8269 caps->xyzrhw = TRUE;
8270 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8271 caps->ffp_generic_attributes = TRUE;
8272 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8273 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8274 caps->max_vertex_blend_matrix_index = 0;
8275 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8276 | WINED3DVTXPCAPS_MATERIALSOURCE7
8277 | WINED3DVTXPCAPS_VERTEXFOG
8278 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8279 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8280 | WINED3DVTXPCAPS_LOCALVIEWER
8281 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8282 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8283 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8284 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8287 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8289 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8290 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8291 return 0;
8294 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8296 struct shader_glsl_priv *priv;
8298 if (shader_backend == &glsl_shader_backend)
8300 priv = shader_priv;
8302 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8304 ERR("Failed to initialize rbtree.\n");
8305 return NULL;
8308 return priv;
8311 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8313 return NULL;
8316 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8318 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8319 struct glsl_ffp_vertex_shader, desc.entry);
8320 struct glsl_shader_prog_link *program, *program2;
8321 struct glsl_ffp_destroy_ctx *ctx = context;
8323 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8324 struct glsl_shader_prog_link, vs.shader_entry)
8326 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8328 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8329 HeapFree(GetProcessHeap(), 0, shader);
8332 /* Context activation is done by the caller. */
8333 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8335 struct shader_glsl_priv *priv = device->vertex_priv;
8336 struct glsl_ffp_destroy_ctx ctx;
8338 ctx.priv = priv;
8339 ctx.gl_info = &device->adapter->gl_info;
8340 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8343 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8344 const struct wined3d_state *state, DWORD state_id) {}
8346 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8347 const struct wined3d_state *state, DWORD state_id)
8349 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8352 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8353 const struct wined3d_state *state, DWORD state_id)
8355 const struct wined3d_gl_info *gl_info = context->gl_info;
8356 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8357 BOOL transformed = context->stream_info.position_transformed;
8358 BOOL wasrhw = context->last_was_rhw;
8359 unsigned int i;
8361 context->last_was_rhw = transformed;
8363 /* If the vertex declaration contains a transformed position attribute,
8364 * the draw uses the fixed function vertex pipeline regardless of any
8365 * vertex shader set by the application. */
8366 if (transformed != wasrhw)
8367 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8369 if (!use_vs(state))
8371 if (context->last_was_vshader)
8373 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8374 clipplane(context, state, STATE_CLIPPLANE(i));
8377 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8379 /* Because of settings->texcoords, we have to regenerate the vertex
8380 * shader on a vdecl change if there aren't enough varyings to just
8381 * always output all the texture coordinates. */
8382 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
8383 || normal != context->last_was_normal)
8384 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8386 if (use_ps(state)
8387 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8388 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8389 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8391 else
8393 if (!context->last_was_vshader)
8395 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8396 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8397 clipplane(context, state, STATE_CLIPPLANE(i));
8401 context->last_was_vshader = use_vs(state);
8402 context->last_was_normal = normal;
8405 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8406 const struct wined3d_state *state, DWORD state_id)
8408 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8409 /* Different vertex shaders potentially require a different vertex attributes setup. */
8410 if (!isStateDirty(context, STATE_VDECL))
8411 context_apply_state(context, state, STATE_VDECL);
8414 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8415 const struct wined3d_state *state, DWORD state_id)
8417 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8420 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8421 const struct wined3d_state *state, DWORD state_id)
8423 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8426 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8428 const struct wined3d_gl_info *gl_info = context->gl_info;
8429 unsigned int k;
8431 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8432 | WINED3D_SHADER_CONST_FFP_LIGHTS
8433 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8435 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8437 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8438 clipplane(context, state, STATE_CLIPPLANE(k));
8442 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8443 const struct wined3d_state *state, DWORD state_id)
8445 /* Table fog behavior depends on the projection matrix. */
8446 if (state->render_states[WINED3D_RS_FOGENABLE]
8447 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8448 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8449 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8452 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8453 const struct wined3d_state *state, DWORD state_id)
8455 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8456 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8457 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8458 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8459 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8460 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
8463 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8464 const struct wined3d_state *state, DWORD state_id)
8466 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8469 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8470 const struct wined3d_state *state, DWORD state_id)
8472 DWORD sampler = state_id - STATE_SAMPLER(0);
8473 const struct wined3d_texture *texture = state->textures[sampler];
8474 BOOL np2;
8476 if (!texture)
8477 return;
8479 if (sampler >= MAX_TEXTURES)
8480 return;
8482 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8483 || context->lastWasPow2Texture & (1u << sampler))
8485 if (np2)
8486 context->lastWasPow2Texture |= 1u << sampler;
8487 else
8488 context->lastWasPow2Texture &= ~(1u << sampler);
8490 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8494 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8495 const struct wined3d_state *state, DWORD state_id)
8497 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8500 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8501 const struct wined3d_state *state, DWORD state_id)
8503 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8506 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8507 const struct wined3d_state *state, DWORD state_id)
8509 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8512 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8513 const struct wined3d_state *state, DWORD state_id)
8515 if (!use_vs(state))
8516 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8519 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
8520 const struct wined3d_state *state, DWORD state_id)
8522 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
8525 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8527 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8528 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8529 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8530 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8531 /* Clip planes */
8532 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8533 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8534 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8535 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8536 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8537 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8538 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8539 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8540 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8541 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8542 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8543 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8544 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8545 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8546 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8547 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8548 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8549 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8550 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8551 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8552 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8553 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8554 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8555 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8556 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8557 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8558 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8559 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8560 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8561 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8562 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8563 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8564 /* Lights */
8565 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8566 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8567 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8568 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8569 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8570 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8571 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8572 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8573 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8574 /* Viewport */
8575 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8576 /* Transform states */
8577 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8578 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8579 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8580 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8581 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8582 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8583 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8584 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8585 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8586 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8587 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8588 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8589 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8590 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8591 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8592 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8593 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8594 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8595 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8596 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8597 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8598 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8599 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8600 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8601 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8602 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8603 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8604 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8605 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8606 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8607 /* Fog */
8608 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8609 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8610 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8611 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8612 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8613 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8614 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8615 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8616 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8617 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8618 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8619 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8620 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8621 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8622 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8623 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8624 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8625 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8626 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8627 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
8628 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8629 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8630 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8631 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8632 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8633 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8634 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8635 /* NP2 texture matrix fixups. They are not needed if
8636 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8637 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8638 * matrix. */
8639 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8640 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8641 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8642 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8643 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8644 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8645 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8646 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8647 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8648 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8649 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8650 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8651 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8652 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8653 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8654 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8655 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8656 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8657 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8658 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8659 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8660 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8661 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8662 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8663 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8664 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
8665 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
8666 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8669 /* TODO:
8670 * - Implement vertex tweening. */
8671 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8673 glsl_vertex_pipe_vp_enable,
8674 glsl_vertex_pipe_vp_get_caps,
8675 glsl_vertex_pipe_vp_get_emul_mask,
8676 glsl_vertex_pipe_vp_alloc,
8677 glsl_vertex_pipe_vp_free,
8678 glsl_vertex_pipe_vp_states,
8681 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8683 /* Nothing to do. */
8686 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8688 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8689 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8690 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8691 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8692 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8693 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8694 | WINED3DTEXOPCAPS_SELECTARG1
8695 | WINED3DTEXOPCAPS_SELECTARG2
8696 | WINED3DTEXOPCAPS_MODULATE4X
8697 | WINED3DTEXOPCAPS_MODULATE2X
8698 | WINED3DTEXOPCAPS_MODULATE
8699 | WINED3DTEXOPCAPS_ADDSIGNED2X
8700 | WINED3DTEXOPCAPS_ADDSIGNED
8701 | WINED3DTEXOPCAPS_ADD
8702 | WINED3DTEXOPCAPS_SUBTRACT
8703 | WINED3DTEXOPCAPS_ADDSMOOTH
8704 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8705 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8706 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8707 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8708 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8709 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8710 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8711 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8712 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8713 | WINED3DTEXOPCAPS_DOTPRODUCT3
8714 | WINED3DTEXOPCAPS_MULTIPLYADD
8715 | WINED3DTEXOPCAPS_LERP
8716 | WINED3DTEXOPCAPS_BUMPENVMAP
8717 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8718 caps->MaxTextureBlendStages = 8;
8719 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8722 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
8724 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8725 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8726 return 0;
8729 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8731 struct shader_glsl_priv *priv;
8733 if (shader_backend == &glsl_shader_backend)
8735 priv = shader_priv;
8737 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8739 ERR("Failed to initialize rbtree.\n");
8740 return NULL;
8743 return priv;
8746 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8748 return NULL;
8751 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8753 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8754 struct glsl_ffp_fragment_shader, entry.entry);
8755 struct glsl_shader_prog_link *program, *program2;
8756 struct glsl_ffp_destroy_ctx *ctx = context;
8758 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8759 struct glsl_shader_prog_link, ps.shader_entry)
8761 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8763 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8764 HeapFree(GetProcessHeap(), 0, shader);
8767 /* Context activation is done by the caller. */
8768 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8770 struct shader_glsl_priv *priv = device->fragment_priv;
8771 struct glsl_ffp_destroy_ctx ctx;
8773 ctx.priv = priv;
8774 ctx.gl_info = &device->adapter->gl_info;
8775 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8778 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8779 const struct wined3d_state *state, DWORD state_id)
8781 context->last_was_pshader = use_ps(state);
8783 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8786 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8787 const struct wined3d_state *state, DWORD state_id)
8789 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8792 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8793 const struct wined3d_state *state, DWORD state_id)
8795 BOOL use_vshader = use_vs(state);
8796 enum fogsource new_source;
8797 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8798 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8800 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8802 if (!state->render_states[WINED3D_RS_FOGENABLE])
8803 return;
8805 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8807 if (use_vshader)
8808 new_source = FOGSOURCE_VS;
8809 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8810 new_source = FOGSOURCE_COORD;
8811 else
8812 new_source = FOGSOURCE_FFP;
8814 else
8816 new_source = FOGSOURCE_FFP;
8819 if (new_source != context->fog_source || fogstart == fogend)
8821 context->fog_source = new_source;
8822 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8826 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8827 const struct wined3d_state *state, DWORD state_id)
8829 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8830 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8831 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8833 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8834 glsl_fragment_pipe_fog(context, state, state_id);
8837 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
8838 const struct wined3d_state *state, DWORD state_id)
8840 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8841 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8842 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8845 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8846 const struct wined3d_state *state, DWORD state_id)
8848 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8851 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8852 const struct wined3d_state *state, DWORD state_id)
8854 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8857 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8858 const struct wined3d_state *state, DWORD state_id)
8860 const struct wined3d_gl_info *gl_info = context->gl_info;
8861 int glParm;
8862 float ref;
8864 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8866 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8868 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8869 checkGLcall("glEnable GL_ALPHA_TEST");
8871 else
8873 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8874 checkGLcall("glDisable GL_ALPHA_TEST");
8875 return;
8878 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8879 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8881 if (glParm)
8883 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8884 checkGLcall("glAlphaFunc");
8888 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8889 const struct wined3d_state *state, DWORD state_id)
8891 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8894 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
8895 const struct wined3d_state *state, DWORD state_id)
8897 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8900 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8902 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8903 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
8904 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8905 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8906 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8907 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8908 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8909 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8910 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8911 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8912 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8913 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8914 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8915 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8916 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8917 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8918 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8919 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8920 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8921 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8922 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8923 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8924 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8925 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8926 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8927 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8928 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8929 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8930 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8931 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8932 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8933 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8934 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8935 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8936 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8937 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8938 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8939 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8940 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8941 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8942 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8943 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8944 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8945 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8946 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8947 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8948 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8949 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8950 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8951 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8952 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8953 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8954 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8955 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8956 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8957 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8958 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8959 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8960 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8961 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8962 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8963 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8964 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8965 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8966 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8967 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8968 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8969 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8970 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8971 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8972 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8973 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8974 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8975 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8976 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8977 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8978 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8979 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8980 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
8981 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8982 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
8983 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
8984 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8985 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8986 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8987 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
8988 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
8989 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8990 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8991 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8992 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
8993 {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 },
8994 {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 },
8995 {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 },
8996 {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 },
8997 {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 },
8998 {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 },
8999 {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 },
9000 {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 },
9001 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9002 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9003 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9004 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9005 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9006 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9007 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9008 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9009 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9010 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9011 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9012 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9013 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9016 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9018 return TRUE;
9021 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9025 const struct fragment_pipeline glsl_fragment_pipe =
9027 glsl_fragment_pipe_enable,
9028 glsl_fragment_pipe_get_caps,
9029 glsl_fragment_pipe_get_emul_mask,
9030 glsl_fragment_pipe_alloc,
9031 glsl_fragment_pipe_free,
9032 glsl_fragment_pipe_alloc_context_data,
9033 glsl_fragment_pipe_free_context_data,
9034 shader_glsl_color_fixup_supported,
9035 glsl_fragment_pipe_state_template,