quartz: Clarify debug strings.
[wine.git] / dlls / wined3d / glsl_shader.c
blob9e9c83b64580dbd1e090f12d260aabccb25604b4
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_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
53 struct glsl_dst_param
55 char reg_name[150];
56 char mask_str[6];
59 struct glsl_src_param
61 char reg_name[150];
62 char param_str[200];
65 struct glsl_sample_function
67 struct wined3d_string_buffer *name;
68 unsigned int coord_mask;
69 unsigned int deriv_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[WINED3D_MAX_VS_CONSTS_F];
122 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
123 GLint uniform_b_locations[WINED3D_MAX_CONSTS_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;
157 GLint clip_planes_location;
160 struct glsl_gs_program
162 struct list shader_entry;
163 GLuint id;
165 GLint pos_fixup_location;
168 struct glsl_ps_program
170 struct list shader_entry;
171 GLuint id;
172 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
173 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
174 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
175 GLint bumpenv_mat_location[MAX_TEXTURES];
176 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
177 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
178 GLint tss_constant_location[MAX_TEXTURES];
179 GLint tex_factor_location;
180 GLint specular_enable_location;
181 GLint fog_color_location;
182 GLint fog_density_location;
183 GLint fog_end_location;
184 GLint fog_scale_location;
185 GLint alpha_test_ref_location;
186 GLint ycorrection_location;
187 GLint np2_fixup_location;
188 GLint color_key_location;
189 const struct ps_np2fixup_info *np2_fixup_info;
192 /* Struct to maintain data about a linked GLSL program */
193 struct glsl_shader_prog_link
195 struct wine_rb_entry program_lookup_entry;
196 struct glsl_vs_program vs;
197 struct glsl_gs_program gs;
198 struct glsl_ps_program ps;
199 GLuint id;
200 DWORD constant_update_mask;
201 UINT constant_version;
204 struct glsl_program_key
206 GLuint vs_id;
207 GLuint gs_id;
208 GLuint ps_id;
211 struct shader_glsl_ctx_priv {
212 const struct vs_compile_args *cur_vs_args;
213 const struct ps_compile_args *cur_ps_args;
214 struct ps_np2fixup_info *cur_np2fixup_info;
215 struct wined3d_string_buffer_list *string_buffers;
218 struct glsl_context_data
220 struct glsl_shader_prog_link *glsl_program;
223 struct glsl_ps_compiled_shader
225 struct ps_compile_args args;
226 struct ps_np2fixup_info np2fixup;
227 GLuint id;
230 struct glsl_vs_compiled_shader
232 struct vs_compile_args args;
233 GLuint id;
236 struct glsl_gs_compiled_shader
238 struct gs_compile_args args;
239 GLuint id;
242 struct glsl_shader_private
244 union
246 struct glsl_vs_compiled_shader *vs;
247 struct glsl_gs_compiled_shader *gs;
248 struct glsl_ps_compiled_shader *ps;
249 } gl_shaders;
250 UINT num_gl_shaders, shader_array_size;
253 struct glsl_ffp_vertex_shader
255 struct wined3d_ffp_vs_desc desc;
256 GLuint id;
257 struct list linked_programs;
260 struct glsl_ffp_fragment_shader
262 struct ffp_frag_desc entry;
263 GLuint id;
264 struct list linked_programs;
267 struct glsl_ffp_destroy_ctx
269 struct shader_glsl_priv *priv;
270 const struct wined3d_gl_info *gl_info;
273 static const char *debug_gl_shader_type(GLenum type)
275 switch (type)
277 #define WINED3D_TO_STR(u) case u: return #u
278 WINED3D_TO_STR(GL_VERTEX_SHADER);
279 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
280 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
281 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
282 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
283 WINED3D_TO_STR(GL_COMPUTE_SHADER);
284 #undef WINED3D_TO_STR
285 default:
286 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
290 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
292 switch (type)
294 case WINED3D_SHADER_TYPE_VERTEX:
295 return "vs";
297 case WINED3D_SHADER_TYPE_HULL:
298 return "hs";
300 case WINED3D_SHADER_TYPE_DOMAIN:
301 return "ds";
303 case WINED3D_SHADER_TYPE_GEOMETRY:
304 return "gs";
306 case WINED3D_SHADER_TYPE_PIXEL:
307 return "ps";
309 case WINED3D_SHADER_TYPE_COMPUTE:
310 return "cs";
312 default:
313 FIXME("Unhandled shader type %#x.\n", type);
314 return "unknown";
318 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
319 const struct wined3d_shader_version *version)
321 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
322 return 150;
323 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
324 return 130;
325 else
326 return 120;
329 static const char *shader_glsl_get_version_declaration(const struct wined3d_gl_info *gl_info,
330 const struct wined3d_shader_version *version)
332 unsigned int glsl_version;
334 switch (glsl_version = shader_glsl_get_version(gl_info, version))
336 case 150:
337 return "#version 150";
338 case 130:
339 return "#version 130";
340 case 120:
341 return "#version 120";
342 default:
343 FIXME("Unexpected GLSL version %u requested.\n", glsl_version);
344 return "";
348 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
350 char str[4][17];
352 wined3d_ftoa(values[0], str[0]);
353 wined3d_ftoa(values[1], str[1]);
354 wined3d_ftoa(values[2], str[2]);
355 wined3d_ftoa(values[3], str[3]);
356 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
359 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
360 const int *values, unsigned int size)
362 int i;
364 if (!size || size > 4)
366 ERR("Invalid vector size %u.\n", size);
367 return;
370 if (size > 1)
371 shader_addline(buffer, "ivec%u(", size);
373 for (i = 0; i < size; ++i)
374 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
376 if (size > 1)
377 shader_addline(buffer, ")");
380 static const char *get_info_log_line(const char **ptr)
382 const char *p, *q;
384 p = *ptr;
385 if (!(q = strstr(p, "\n")))
387 if (!*p) return NULL;
388 *ptr += strlen(p);
389 return p;
391 *ptr = q + 1;
393 return p;
396 /* Context activation is done by the caller. */
397 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
399 int length = 0;
400 char *log;
402 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
403 return;
405 if (program)
406 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
407 else
408 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
410 /* A size of 1 is just a null-terminated string, so the log should be bigger than
411 * that if there are errors. */
412 if (length > 1)
414 const char *ptr, *line;
416 log = HeapAlloc(GetProcessHeap(), 0, length);
417 /* The info log is supposed to be zero-terminated, but at least some
418 * versions of fglrx don't terminate the string properly. The reported
419 * length does include the terminator, so explicitly set it to zero
420 * here. */
421 log[length - 1] = 0;
422 if (program)
423 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
424 else
425 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
427 ptr = log;
428 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
430 WARN("Info log received from GLSL shader #%u:\n", id);
431 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
433 else
435 FIXME("Info log received from GLSL shader #%u:\n", id);
436 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
438 HeapFree(GetProcessHeap(), 0, log);
442 /* Context activation is done by the caller. */
443 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
445 const char *ptr, *line;
447 TRACE("Compiling shader object %u.\n", shader);
449 if (TRACE_ON(d3d_shader))
451 ptr = src;
452 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
455 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
456 checkGLcall("glShaderSource");
457 GL_EXTCALL(glCompileShader(shader));
458 checkGLcall("glCompileShader");
459 print_glsl_info_log(gl_info, shader, FALSE);
462 /* Context activation is done by the caller. */
463 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
465 GLint i, shader_count, source_size = -1;
466 GLuint *shaders;
467 char *source = NULL;
469 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
470 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
472 ERR("Failed to allocate shader array memory.\n");
473 return;
476 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
477 for (i = 0; i < shader_count; ++i)
479 const char *ptr, *line;
480 GLint tmp;
482 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
484 if (source_size < tmp)
486 HeapFree(GetProcessHeap(), 0, source);
488 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
489 if (!source)
491 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
492 HeapFree(GetProcessHeap(), 0, shaders);
493 return;
495 source_size = tmp;
498 FIXME("Shader %u:\n", shaders[i]);
499 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
500 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
501 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
502 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
503 FIXME("\n");
505 ptr = source;
506 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
507 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
508 FIXME("\n");
511 HeapFree(GetProcessHeap(), 0, source);
512 HeapFree(GetProcessHeap(), 0, shaders);
515 /* Context activation is done by the caller. */
516 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
518 GLint tmp;
520 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
521 return;
523 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
524 if (!tmp)
526 FIXME("Program %u link status invalid.\n", program);
527 shader_glsl_dump_program_source(gl_info, program);
530 print_glsl_info_log(gl_info, program, TRUE);
533 /* Context activation is done by the caller. */
534 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
535 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
537 unsigned int mapped_unit;
538 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
539 const char *prefix;
540 unsigned int i, j;
541 GLint name_loc;
543 static const struct
545 enum wined3d_shader_type type;
546 unsigned int base_idx;
547 unsigned int count;
549 sampler_info[] =
551 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
552 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
555 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
557 prefix = shader_glsl_get_prefix(sampler_info[i].type);
559 for (j = 0; j < sampler_info[i].count; ++j)
561 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
562 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
563 if (name_loc == -1)
564 continue;
566 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
567 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
569 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
570 continue;
573 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
574 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
577 checkGLcall("glUniform1i");
578 string_buffer_release(&priv->string_buffers, sampler_name);
581 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
582 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
584 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
586 if (icb)
588 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
589 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
590 GLint icb_location;
592 string_buffer_sprintf(icb_name, "%s_icb", prefix);
593 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
594 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
595 checkGLcall("Load immediate constant buffer");
597 string_buffer_release(&priv->string_buffers, icb_name);
601 /* Context activation is done by the caller. */
602 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
603 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
605 unsigned int start = ~0U, end = 0;
606 int stack_idx = 0;
607 unsigned int heap_idx = 1;
608 unsigned int idx;
610 if (heap->entries[heap_idx].version <= version) return;
612 idx = heap->entries[heap_idx].idx;
613 if (constant_locations[idx] != -1)
614 start = end = idx;
615 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
617 while (stack_idx >= 0)
619 /* Note that we fall through to the next case statement. */
620 switch(stack[stack_idx])
622 case HEAP_NODE_TRAVERSE_LEFT:
624 unsigned int left_idx = heap_idx << 1;
625 if (left_idx < heap->size && heap->entries[left_idx].version > version)
627 heap_idx = left_idx;
628 idx = heap->entries[heap_idx].idx;
629 if (constant_locations[idx] != -1)
631 if (start > idx)
632 start = idx;
633 if (end < idx)
634 end = idx;
637 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
638 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
639 break;
643 case HEAP_NODE_TRAVERSE_RIGHT:
645 unsigned int right_idx = (heap_idx << 1) + 1;
646 if (right_idx < heap->size && heap->entries[right_idx].version > version)
648 heap_idx = right_idx;
649 idx = heap->entries[heap_idx].idx;
650 if (constant_locations[idx] != -1)
652 if (start > idx)
653 start = idx;
654 if (end < idx)
655 end = idx;
658 stack[stack_idx++] = HEAP_NODE_POP;
659 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
660 break;
664 case HEAP_NODE_POP:
665 heap_idx >>= 1;
666 --stack_idx;
667 break;
670 if (start <= end)
671 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
672 checkGLcall("walk_constant_heap()");
675 /* Context activation is done by the caller. */
676 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
677 GLint location, const struct wined3d_vec4 *data)
679 GLfloat clamped_constant[4];
681 if (location == -1) return;
683 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
684 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
685 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
686 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
688 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
691 /* Context activation is done by the caller. */
692 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
693 const struct wined3d_vec4 *constants, const GLint *constant_locations,
694 const struct constant_heap *heap, unsigned char *stack, DWORD version)
696 int stack_idx = 0;
697 unsigned int heap_idx = 1;
698 unsigned int idx;
700 if (heap->entries[heap_idx].version <= version) return;
702 idx = heap->entries[heap_idx].idx;
703 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
704 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
706 while (stack_idx >= 0)
708 /* Note that we fall through to the next case statement. */
709 switch(stack[stack_idx])
711 case HEAP_NODE_TRAVERSE_LEFT:
713 unsigned int left_idx = heap_idx << 1;
714 if (left_idx < heap->size && heap->entries[left_idx].version > version)
716 heap_idx = left_idx;
717 idx = heap->entries[heap_idx].idx;
718 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
720 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
721 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
722 break;
726 case HEAP_NODE_TRAVERSE_RIGHT:
728 unsigned int right_idx = (heap_idx << 1) + 1;
729 if (right_idx < heap->size && heap->entries[right_idx].version > version)
731 heap_idx = right_idx;
732 idx = heap->entries[heap_idx].idx;
733 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
735 stack[stack_idx++] = HEAP_NODE_POP;
736 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
737 break;
741 case HEAP_NODE_POP:
742 heap_idx >>= 1;
743 --stack_idx;
744 break;
747 checkGLcall("walk_constant_heap_clamped()");
750 /* Context activation is done by the caller. */
751 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
752 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
753 unsigned char *stack, unsigned int version)
755 const struct wined3d_shader_lconst *lconst;
757 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
758 if (shader->reg_maps.shader_version.major == 1
759 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
760 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
761 else
762 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
764 if (!shader->load_local_constsF)
766 TRACE("No need to load local float constants for this shader.\n");
767 return;
770 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
771 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
773 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
775 checkGLcall("glUniform4fv()");
778 /* Context activation is done by the caller. */
779 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
780 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
782 unsigned int i;
783 struct list* ptr;
785 for (i = 0; constants_set; constants_set >>= 1, ++i)
787 if (!(constants_set & 1)) continue;
789 /* We found this uniform name in the program - go ahead and send the data */
790 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
793 /* Load immediate constants */
794 ptr = list_head(&shader->constantsI);
795 while (ptr)
797 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
798 unsigned int idx = lconst->idx;
799 const GLint *values = (const GLint *)lconst->value;
801 /* We found this uniform name in the program - go ahead and send the data */
802 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
803 ptr = list_next(&shader->constantsI, ptr);
805 checkGLcall("glUniform4iv()");
808 /* Context activation is done by the caller. */
809 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
810 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
812 unsigned int i;
813 struct list* ptr;
815 for (i = 0; constants_set; constants_set >>= 1, ++i)
817 if (!(constants_set & 1)) continue;
819 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
822 /* Load immediate constants */
823 ptr = list_head(&shader->constantsB);
824 while (ptr)
826 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
827 unsigned int idx = lconst->idx;
828 const GLint *values = (const GLint *)lconst->value;
830 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
831 ptr = list_next(&shader->constantsB, ptr);
833 checkGLcall("glUniform1iv()");
836 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
838 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
841 /* Context activation is done by the caller (state handler). */
842 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
843 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
845 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
846 UINT fixup = ps->np2_fixup_info->active;
847 UINT i;
849 for (i = 0; fixup; fixup >>= 1, ++i)
851 const struct wined3d_texture *tex = state->textures[i];
852 unsigned char idx = ps->np2_fixup_info->idx[i];
853 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
855 if (!tex)
857 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
858 continue;
861 if (idx % 2)
863 tex_dim[2] = tex->pow2_matrix[0];
864 tex_dim[3] = tex->pow2_matrix[5];
866 else
868 tex_dim[0] = tex->pow2_matrix[0];
869 tex_dim[1] = tex->pow2_matrix[5];
873 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
876 /* Taken and adapted from Mesa. */
877 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
879 float pos, neg, t, det;
880 struct wined3d_matrix temp;
882 /* Calculate the determinant of upper left 3x3 submatrix and
883 * determine if the matrix is singular. */
884 pos = neg = 0.0f;
885 t = in->_11 * in->_22 * in->_33;
886 if (t >= 0.0f)
887 pos += t;
888 else
889 neg += t;
891 t = in->_21 * in->_32 * in->_13;
892 if (t >= 0.0f)
893 pos += t;
894 else
895 neg += t;
896 t = in->_31 * in->_12 * in->_23;
897 if (t >= 0.0f)
898 pos += t;
899 else
900 neg += t;
902 t = -in->_31 * in->_22 * in->_13;
903 if (t >= 0.0f)
904 pos += t;
905 else
906 neg += t;
907 t = -in->_21 * in->_12 * in->_33;
908 if (t >= 0.0f)
909 pos += t;
910 else
911 neg += t;
913 t = -in->_11 * in->_32 * in->_23;
914 if (t >= 0.0f)
915 pos += t;
916 else
917 neg += t;
919 det = pos + neg;
921 if (fabsf(det) < 1e-25f)
922 return FALSE;
924 det = 1.0f / det;
925 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
926 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
927 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
928 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
929 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
930 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
931 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
932 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
933 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
935 *out = temp;
936 return TRUE;
939 static void swap_rows(float **a, float **b)
941 float *tmp = *a;
943 *a = *b;
944 *b = tmp;
947 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
949 float wtmp[4][8];
950 float m0, m1, m2, m3, s;
951 float *r0, *r1, *r2, *r3;
953 r0 = wtmp[0];
954 r1 = wtmp[1];
955 r2 = wtmp[2];
956 r3 = wtmp[3];
958 r0[0] = m->_11;
959 r0[1] = m->_12;
960 r0[2] = m->_13;
961 r0[3] = m->_14;
962 r0[4] = 1.0f;
963 r0[5] = r0[6] = r0[7] = 0.0f;
965 r1[0] = m->_21;
966 r1[1] = m->_22;
967 r1[2] = m->_23;
968 r1[3] = m->_24;
969 r1[5] = 1.0f;
970 r1[4] = r1[6] = r1[7] = 0.0f;
972 r2[0] = m->_31;
973 r2[1] = m->_32;
974 r2[2] = m->_33;
975 r2[3] = m->_34;
976 r2[6] = 1.0f;
977 r2[4] = r2[5] = r2[7] = 0.0f;
979 r3[0] = m->_41;
980 r3[1] = m->_42;
981 r3[2] = m->_43;
982 r3[3] = m->_44;
983 r3[7] = 1.0f;
984 r3[4] = r3[5] = r3[6] = 0.0f;
986 /* Choose pivot - or die. */
987 if (fabsf(r3[0]) > fabsf(r2[0]))
988 swap_rows(&r3, &r2);
989 if (fabsf(r2[0]) > fabsf(r1[0]))
990 swap_rows(&r2, &r1);
991 if (fabsf(r1[0]) > fabsf(r0[0]))
992 swap_rows(&r1, &r0);
993 if (r0[0] == 0.0f)
994 return FALSE;
996 /* Eliminate first variable. */
997 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
998 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
999 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1000 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1001 s = r0[4];
1002 if (s != 0.0f)
1004 r1[4] -= m1 * s;
1005 r2[4] -= m2 * s;
1006 r3[4] -= m3 * s;
1008 s = r0[5];
1009 if (s != 0.0f)
1011 r1[5] -= m1 * s;
1012 r2[5] -= m2 * s;
1013 r3[5] -= m3 * s;
1015 s = r0[6];
1016 if (s != 0.0f)
1018 r1[6] -= m1 * s;
1019 r2[6] -= m2 * s;
1020 r3[6] -= m3 * s;
1022 s = r0[7];
1023 if (s != 0.0f)
1025 r1[7] -= m1 * s;
1026 r2[7] -= m2 * s;
1027 r3[7] -= m3 * s;
1030 /* Choose pivot - or die. */
1031 if (fabsf(r3[1]) > fabsf(r2[1]))
1032 swap_rows(&r3, &r2);
1033 if (fabsf(r2[1]) > fabsf(r1[1]))
1034 swap_rows(&r2, &r1);
1035 if (r1[1] == 0.0f)
1036 return FALSE;
1038 /* Eliminate second variable. */
1039 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1040 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1041 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1042 s = r1[4];
1043 if (s != 0.0f)
1045 r2[4] -= m2 * s;
1046 r3[4] -= m3 * s;
1048 s = r1[5];
1049 if (s != 0.0f)
1051 r2[5] -= m2 * s;
1052 r3[5] -= m3 * s;
1054 s = r1[6];
1055 if (s != 0.0f)
1057 r2[6] -= m2 * s;
1058 r3[6] -= m3 * s;
1060 s = r1[7];
1061 if (s != 0.0f)
1063 r2[7] -= m2 * s;
1064 r3[7] -= m3 * s;
1067 /* Choose pivot - or die. */
1068 if (fabsf(r3[2]) > fabsf(r2[2]))
1069 swap_rows(&r3, &r2);
1070 if (r2[2] == 0.0f)
1071 return FALSE;
1073 /* Eliminate third variable. */
1074 m3 = r3[2] / r2[2];
1075 r3[3] -= m3 * r2[3];
1076 r3[4] -= m3 * r2[4];
1077 r3[5] -= m3 * r2[5];
1078 r3[6] -= m3 * r2[6];
1079 r3[7] -= m3 * r2[7];
1081 /* Last check. */
1082 if (r3[3] == 0.0f)
1083 return FALSE;
1085 /* Back substitute row 3. */
1086 s = 1.0f / r3[3];
1087 r3[4] *= s;
1088 r3[5] *= s;
1089 r3[6] *= s;
1090 r3[7] *= s;
1092 /* Back substitute row 2. */
1093 m2 = r2[3];
1094 s = 1.0f / r2[2];
1095 r2[4] = s * (r2[4] - r3[4] * m2);
1096 r2[5] = s * (r2[5] - r3[5] * m2);
1097 r2[6] = s * (r2[6] - r3[6] * m2);
1098 r2[7] = s * (r2[7] - r3[7] * m2);
1099 m1 = r1[3];
1100 r1[4] -= r3[4] * m1;
1101 r1[5] -= r3[5] * m1;
1102 r1[6] -= r3[6] * m1;
1103 r1[7] -= r3[7] * m1;
1104 m0 = r0[3];
1105 r0[4] -= r3[4] * m0;
1106 r0[5] -= r3[5] * m0;
1107 r0[6] -= r3[6] * m0;
1108 r0[7] -= r3[7] * m0;
1110 /* Back substitute row 1. */
1111 m1 = r1[2];
1112 s = 1.0f / r1[1];
1113 r1[4] = s * (r1[4] - r2[4] * m1);
1114 r1[5] = s * (r1[5] - r2[5] * m1);
1115 r1[6] = s * (r1[6] - r2[6] * m1);
1116 r1[7] = s * (r1[7] - r2[7] * m1);
1117 m0 = r0[2];
1118 r0[4] -= r2[4] * m0;
1119 r0[5] -= r2[5] * m0;
1120 r0[6] -= r2[6] * m0;
1121 r0[7] -= r2[7] * m0;
1123 /* Back substitute row 0. */
1124 m0 = r0[1];
1125 s = 1.0f / r0[0];
1126 r0[4] = s * (r0[4] - r1[4] * m0);
1127 r0[5] = s * (r0[5] - r1[5] * m0);
1128 r0[6] = s * (r0[6] - r1[6] * m0);
1129 r0[7] = s * (r0[7] - r1[7] * m0);
1131 out->_11 = r0[4];
1132 out->_12 = r0[5];
1133 out->_13 = r0[6];
1134 out->_14 = r0[7];
1135 out->_21 = r1[4];
1136 out->_22 = r1[5];
1137 out->_23 = r1[6];
1138 out->_24 = r1[7];
1139 out->_31 = r2[4];
1140 out->_32 = r2[5];
1141 out->_33 = r2[6];
1142 out->_34 = r2[7];
1143 out->_41 = r3[4];
1144 out->_42 = r3[5];
1145 out->_43 = r3[6];
1146 out->_44 = r3[7];
1148 return TRUE;
1151 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1152 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1154 const struct wined3d_gl_info *gl_info = context->gl_info;
1155 float mat[3 * 3];
1156 struct wined3d_matrix mv;
1157 unsigned int i, j;
1159 if (prog->vs.normal_matrix_location == -1)
1160 return;
1162 get_modelview_matrix(context, state, 0, &mv);
1163 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1164 invert_matrix_3d(&mv, &mv);
1165 else
1166 invert_matrix(&mv, &mv);
1167 /* Tests show that singular modelview matrices are used unchanged as normal
1168 * matrices on D3D3 and older. There seems to be no clearly consistent
1169 * behavior on newer D3D versions so always follow older ddraw behavior. */
1170 for (i = 0; i < 3; ++i)
1171 for (j = 0; j < 3; ++j)
1172 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1174 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1175 checkGLcall("glUniformMatrix3fv");
1178 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1179 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1181 const struct wined3d_gl_info *gl_info = context->gl_info;
1182 struct wined3d_matrix mat;
1184 if (tex >= MAX_TEXTURES)
1185 return;
1186 if (prog->vs.texture_matrix_location[tex] == -1)
1187 return;
1189 get_texture_matrix(context, state, tex, &mat);
1190 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1191 checkGLcall("glUniformMatrix4fv");
1194 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1195 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1197 const struct wined3d_gl_info *gl_info = context->gl_info;
1199 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1201 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1202 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1204 else
1206 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1208 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1210 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1211 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1212 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1213 checkGLcall("setting FFP material uniforms");
1216 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1217 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1219 const struct wined3d_gl_info *gl_info = context->gl_info;
1220 struct wined3d_color color;
1222 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1223 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1224 checkGLcall("glUniform3fv");
1227 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1228 const struct wined3d_matrix *src2)
1230 struct wined3d_vec4 temp;
1232 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1233 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1234 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1235 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1237 *dest = temp;
1240 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1241 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1243 const struct wined3d_gl_info *gl_info = context->gl_info;
1244 const struct wined3d_light_info *light_info = state->lights[light];
1245 struct wined3d_vec4 vec4;
1246 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1248 if (!light_info)
1249 return;
1251 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1252 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1253 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1255 switch (light_info->OriginalParms.type)
1257 case WINED3D_LIGHT_POINT:
1258 multiply_vector_matrix(&vec4, &light_info->position, view);
1259 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1260 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1261 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1262 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1263 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1264 break;
1266 case WINED3D_LIGHT_SPOT:
1267 multiply_vector_matrix(&vec4, &light_info->position, view);
1268 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1270 multiply_vector_matrix(&vec4, &light_info->direction, view);
1271 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1273 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1274 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1275 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1276 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1277 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1278 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1279 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1280 break;
1282 case WINED3D_LIGHT_DIRECTIONAL:
1283 multiply_vector_matrix(&vec4, &light_info->direction, view);
1284 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1285 break;
1287 case WINED3D_LIGHT_PARALLELPOINT:
1288 multiply_vector_matrix(&vec4, &light_info->position, view);
1289 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1290 break;
1292 default:
1293 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1295 checkGLcall("setting FFP lights uniforms");
1298 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1299 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1301 const struct wined3d_gl_info *gl_info = context->gl_info;
1302 float min, max;
1303 float size, att[3];
1305 get_pointsize_minmax(context, state, &min, &max);
1307 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1308 checkGLcall("glUniform1f");
1309 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1310 checkGLcall("glUniform1f");
1312 get_pointsize(context, state, &size, att);
1314 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1315 checkGLcall("glUniform1f");
1316 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1317 checkGLcall("glUniform1f");
1318 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1319 checkGLcall("glUniform1f");
1320 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1321 checkGLcall("glUniform1f");
1324 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1325 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1327 const struct wined3d_gl_info *gl_info = context->gl_info;
1328 struct wined3d_color color;
1329 float start, end, scale;
1330 union
1332 DWORD d;
1333 float f;
1334 } tmpvalue;
1336 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1337 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1338 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1339 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1340 get_fog_start_end(context, state, &start, &end);
1341 scale = 1.0f / (end - start);
1342 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1343 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1344 checkGLcall("fog emulation uniforms");
1347 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1348 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1350 const struct wined3d_gl_info *gl_info = context->gl_info;
1351 struct wined3d_vec4 plane;
1353 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1354 if (!use_vs(state))
1355 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1356 else
1357 plane = state->clip_planes[index];
1359 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1362 /* Context activation is done by the caller (state handler). */
1363 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1364 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1366 struct wined3d_color float_key[2];
1367 const struct wined3d_texture *texture = state->textures[0];
1369 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1370 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1373 /* Context activation is done by the caller (state handler). */
1374 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1375 const struct wined3d_state *state)
1377 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1378 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1379 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1380 const struct wined3d_gl_info *gl_info = context->gl_info;
1381 struct shader_glsl_priv *priv = shader_priv;
1382 float position_fixup[4];
1383 DWORD update_mask;
1385 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1386 UINT constant_version;
1387 int i;
1389 if (!prog) {
1390 /* No GLSL program set - nothing to do. */
1391 return;
1393 constant_version = prog->constant_version;
1394 update_mask = context->constant_update_mask & prog->constant_update_mask;
1396 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1397 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1398 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1400 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1401 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1402 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1404 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1405 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1406 vshader->reg_maps.boolean_constants);
1408 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1410 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1411 shader_glsl_clip_plane_uniform(context, state, i, prog);
1414 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1415 shader_glsl_pointsize_uniform(context, state, prog);
1417 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1419 shader_get_position_fixup(context, state, position_fixup);
1420 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1421 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1422 else
1423 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1424 checkGLcall("glUniform4fv");
1427 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1429 struct wined3d_matrix mat;
1431 get_modelview_matrix(context, state, 0, &mat);
1432 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1433 checkGLcall("glUniformMatrix4fv");
1435 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1438 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1440 struct wined3d_matrix mat;
1442 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1444 if (prog->vs.modelview_matrix_location[i] == -1)
1445 break;
1447 get_modelview_matrix(context, state, i, &mat);
1448 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1449 checkGLcall("glUniformMatrix4fv");
1453 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1455 struct wined3d_matrix projection;
1457 get_projection_matrix(context, state, &projection);
1458 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1459 checkGLcall("glUniformMatrix4fv");
1462 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1464 for (i = 0; i < MAX_TEXTURES; ++i)
1465 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1468 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1469 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1471 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1473 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1474 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1475 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1478 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1479 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1480 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1482 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1483 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1484 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1486 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1487 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1488 pshader->reg_maps.boolean_constants);
1490 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1492 for (i = 0; i < MAX_TEXTURES; ++i)
1494 if (prog->ps.bumpenv_mat_location[i] == -1)
1495 continue;
1497 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1498 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1500 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1502 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1503 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1504 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1505 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1509 checkGLcall("bump env uniforms");
1512 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1514 const struct wined3d_vec4 correction_params =
1516 /* Position is relative to the framebuffer, not the viewport. */
1517 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1518 context->render_offscreen ? 1.0f : -1.0f,
1519 0.0f,
1520 0.0f,
1523 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1526 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1527 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1528 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1529 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1531 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1533 struct wined3d_color color;
1535 if (prog->ps.tex_factor_location != -1)
1537 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1538 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1541 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1542 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1543 else
1544 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1546 for (i = 0; i < MAX_TEXTURES; ++i)
1548 if (prog->ps.tss_constant_location[i] == -1)
1549 continue;
1551 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1552 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1555 checkGLcall("fixed function uniforms");
1558 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1559 shader_glsl_load_fog_uniform(context, state, prog);
1561 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1563 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1565 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1566 checkGLcall("alpha test emulation uniform");
1569 if (priv->next_constant_version == UINT_MAX)
1571 TRACE("Max constant version reached, resetting to 0.\n");
1572 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1573 priv->next_constant_version = 1;
1575 else
1577 prog->constant_version = priv->next_constant_version++;
1581 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1583 struct constant_entry *entries = heap->entries;
1584 unsigned int *positions = heap->positions;
1585 unsigned int heap_idx, parent_idx;
1587 if (!heap->contained[idx])
1589 heap_idx = heap->size++;
1590 heap->contained[idx] = TRUE;
1592 else
1594 heap_idx = positions[idx];
1597 while (heap_idx > 1)
1599 parent_idx = heap_idx >> 1;
1601 if (new_version <= entries[parent_idx].version) break;
1603 entries[heap_idx] = entries[parent_idx];
1604 positions[entries[parent_idx].idx] = heap_idx;
1605 heap_idx = parent_idx;
1608 entries[heap_idx].version = new_version;
1609 entries[heap_idx].idx = idx;
1610 positions[idx] = heap_idx;
1613 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1615 struct shader_glsl_priv *priv = device->shader_priv;
1616 struct constant_heap *heap = &priv->vconst_heap;
1617 UINT i;
1619 for (i = start; i < count + start; ++i)
1621 update_heap_entry(heap, i, priv->next_constant_version);
1625 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1627 struct shader_glsl_priv *priv = device->shader_priv;
1628 struct constant_heap *heap = &priv->pconst_heap;
1629 UINT i;
1631 for (i = start; i < count + start; ++i)
1633 update_heap_entry(heap, i, priv->next_constant_version);
1637 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1639 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1640 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1641 if(shader_major > 3) return ret;
1643 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1644 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1645 return ret;
1648 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1650 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1653 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1655 return !needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION];
1658 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1660 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1663 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1664 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1666 va_list args;
1667 int ret;
1669 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1670 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1671 for (;;)
1673 va_start(args, format);
1674 ret = shader_vaddline(buffer, format, args);
1675 va_end(args);
1676 if (!ret)
1677 return;
1678 if (!string_buffer_resize(buffer, ret))
1679 return;
1683 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1684 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1686 va_list args;
1687 int ret;
1689 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1690 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1691 for (;;)
1693 va_start(args, format);
1694 ret = shader_vaddline(buffer, format, args);
1695 va_end(args);
1696 if (!ret)
1697 return;
1698 if (!string_buffer_resize(buffer, ret))
1699 return;
1703 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1705 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1708 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
1710 switch (primitive_type)
1712 case WINED3D_PT_POINTLIST:
1713 return "points";
1715 case WINED3D_PT_LINELIST:
1716 return "lines";
1718 case WINED3D_PT_LINESTRIP:
1719 return "line_strip";
1721 case WINED3D_PT_TRIANGLELIST:
1722 return "triangles";
1724 case WINED3D_PT_TRIANGLESTRIP:
1725 return "triangle_strip";
1727 case WINED3D_PT_LINELIST_ADJ:
1728 return "lines_adjacency";
1730 case WINED3D_PT_TRIANGLELIST_ADJ:
1731 return "triangles_adjacency";
1733 default:
1734 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
1735 return "";
1739 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1741 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1742 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1743 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1744 unsigned int i;
1746 if (reg_maps->shader_version.major < 3)
1747 return input_reg_used[idx];
1749 for (i = 0; i < input_signature->element_count; ++i)
1751 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1753 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1754 continue;
1756 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1757 && input->semantic_idx == idx)
1759 if (input_reg_used[input->register_idx])
1760 return TRUE;
1761 else
1762 return FALSE;
1765 return FALSE;
1768 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1769 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1771 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1773 if (version->major >= 4)
1774 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1775 else
1776 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1779 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1780 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1781 unsigned int index)
1783 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1784 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1785 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1786 index, scalar_type, scalar_type, index);
1789 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
1790 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
1792 unsigned int index = e->register_idx;
1794 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1796 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1797 index);
1798 return;
1800 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1802 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1803 index);
1804 return;
1806 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
1807 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
1809 if (shader_glsl_use_explicit_attrib_location(gl_info))
1810 shader_addline(buffer, "layout(location = %u) ", index);
1812 switch (e->component_type)
1814 case WINED3D_TYPE_UINT:
1815 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
1816 break;
1817 case WINED3D_TYPE_INT:
1818 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
1819 break;
1821 default:
1822 FIXME("Unhandled type %#x.\n", e->component_type);
1823 /* Fall through. */
1824 case WINED3D_TYPE_UNKNOWN:
1825 case WINED3D_TYPE_FLOAT:
1826 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
1827 break;
1831 /** Generate the variable & register declarations for the GLSL output target */
1832 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1833 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1834 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1836 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1837 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1838 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1839 const struct wined3d_gl_info *gl_info = context->gl_info;
1840 const struct wined3d_shader_indexable_temp *idx_temp_reg;
1841 unsigned int i, extra_constants_needed = 0;
1842 const struct wined3d_shader_lconst *lconst;
1843 const char *prefix;
1844 DWORD map;
1846 prefix = shader_glsl_get_prefix(version->type);
1848 /* Prototype the subroutines */
1849 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1851 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1854 /* Declare the constants (aka uniforms) */
1855 if (shader->limits->constant_float > 0)
1857 unsigned max_constantsF;
1859 /* Unless the shader uses indirect addressing, always declare the
1860 * maximum array size and ignore that we need some uniforms privately.
1861 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1862 * and immediate values, still declare VC[256]. If the shader needs
1863 * more uniforms than we have it won't work in any case. If it uses
1864 * less, the compiler will figure out which uniforms are really used
1865 * and strip them out. This allows a shader to use c255 on a dx9 card,
1866 * as long as it doesn't also use all the other constants.
1868 * If the shader uses indirect addressing the compiler must assume
1869 * that all declared uniforms are used. In this case, declare only the
1870 * amount that we're assured to have.
1872 * Thus we run into problems in these two cases:
1873 * 1) The shader really uses more uniforms than supported.
1874 * 2) The shader uses indirect addressing, less constants than
1875 * supported, but uses a constant index > #supported consts. */
1876 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1878 /* No indirect addressing here. */
1879 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1881 else
1883 if (reg_maps->usesrelconstF)
1885 /* Subtract the other potential uniforms from the max
1886 * available (bools, ints, and 1 row of projection matrix).
1887 * Subtract another uniform for immediate values, which have
1888 * to be loaded via uniform by the driver as well. The shader
1889 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1890 * shader code, so one vec4 should be enough. (Unfortunately
1891 * the Nvidia driver doesn't store 128 and -128 in one float).
1893 * Writing gl_ClipVertex requires one uniform for each
1894 * clipplane as well. */
1895 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1896 if (vs_args->clip_enabled)
1897 max_constantsF -= gl_info->limits.user_clip_distances;
1898 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1899 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1900 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1901 * for now take this into account when calculating the number of available constants
1903 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1904 /* Set by driver quirks in directx.c */
1905 max_constantsF -= gl_info->reserved_glsl_constants;
1907 if (max_constantsF < shader->limits->constant_float)
1909 static unsigned int once;
1911 if (!once++)
1912 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1913 " it may not render correctly.\n");
1914 else
1915 WARN("The hardware does not support enough uniform components to run this shader.\n");
1918 else
1920 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1923 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1924 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1927 /* Always declare the full set of constants, the compiler can remove the
1928 * unused ones because d3d doesn't (yet) support indirect int and bool
1929 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1930 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1931 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1933 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1934 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1936 /* Declare immediate constant buffer */
1937 if (reg_maps->icb)
1938 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
1940 /* Declare constant buffers */
1941 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1943 if (reg_maps->cb_sizes[i])
1944 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1945 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1948 /* Declare texture samplers */
1949 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1951 struct wined3d_shader_sampler_map_entry *entry;
1952 const char *sampler_type_prefix, *sampler_type;
1953 BOOL shadow_sampler, tex_rect;
1955 entry = &reg_maps->sampler_map.entries[i];
1957 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1959 ERR("Invalid resource index %u.\n", entry->resource_idx);
1960 continue;
1963 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1965 case WINED3D_DATA_FLOAT:
1966 case WINED3D_DATA_UNORM:
1967 case WINED3D_DATA_SNORM:
1968 sampler_type_prefix = "";
1969 break;
1971 case WINED3D_DATA_INT:
1972 sampler_type_prefix = "i";
1973 break;
1975 case WINED3D_DATA_UINT:
1976 sampler_type_prefix = "u";
1977 break;
1979 default:
1980 sampler_type_prefix = "";
1981 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1982 break;
1985 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
1986 switch (reg_maps->resource_info[entry->resource_idx].type)
1988 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1989 if (shadow_sampler)
1990 sampler_type = "sampler1DShadow";
1991 else
1992 sampler_type = "sampler1D";
1993 break;
1995 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1996 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1997 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1998 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1999 if (shadow_sampler)
2001 if (tex_rect)
2002 sampler_type = "sampler2DRectShadow";
2003 else
2004 sampler_type = "sampler2DShadow";
2006 else
2008 if (tex_rect)
2009 sampler_type = "sampler2DRect";
2010 else
2011 sampler_type = "sampler2D";
2013 break;
2015 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2016 if (shadow_sampler)
2017 FIXME("Unsupported 3D shadow sampler.\n");
2018 sampler_type = "sampler3D";
2019 break;
2021 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2022 if (shadow_sampler)
2023 FIXME("Unsupported Cube shadow sampler.\n");
2024 sampler_type = "samplerCube";
2025 break;
2027 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2028 if (shadow_sampler)
2029 sampler_type = "sampler2DArrayShadow";
2030 else
2031 sampler_type = "sampler2DArray";
2032 break;
2034 default:
2035 sampler_type = "unsupported_sampler";
2036 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2037 break;
2039 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2040 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2043 /* Declare uniforms for NP2 texcoord fixup:
2044 * This is NOT done inside the loop that declares the texture samplers
2045 * since the NP2 fixup code is currently only used for the GeforceFX
2046 * series and when forcing the ARB_npot extension off. Modern cards just
2047 * skip the code anyway, so put it inside a separate loop. */
2048 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
2050 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
2051 UINT cur = 0;
2053 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2054 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2055 * samplerNP2Fixup stores texture dimensions and is updated through
2056 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2058 for (i = 0; i < shader->limits->sampler; ++i)
2060 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
2061 continue;
2063 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
2065 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2066 continue;
2069 fixup->idx[i] = cur++;
2072 fixup->num_consts = (cur + 1) >> 1;
2073 fixup->active = ps_args->np2_fixup;
2074 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
2077 /* Declare address variables */
2078 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2080 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2083 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2085 for (i = 0; i < shader->input_signature.element_count; ++i)
2086 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
2088 if (vs_args->point_size && !vs_args->per_vertex_point_size)
2090 shader_addline(buffer, "uniform struct\n{\n");
2091 shader_addline(buffer, " float size;\n");
2092 shader_addline(buffer, " float size_min;\n");
2093 shader_addline(buffer, " float size_max;\n");
2094 shader_addline(buffer, "} ffp_point;\n");
2097 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2099 if (vs_args->clip_enabled)
2100 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
2102 if (version->major < 3)
2104 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2105 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2106 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2107 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2111 if (version->major < 4)
2112 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2114 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2116 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2118 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2120 else
2122 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2123 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2124 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2125 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2128 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2130 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2132 shader_addline(buffer, "uniform struct\n{\n");
2133 shader_addline(buffer, " vec4 color;\n");
2134 shader_addline(buffer, " float density;\n");
2135 shader_addline(buffer, " float end;\n");
2136 shader_addline(buffer, " float scale;\n");
2137 shader_addline(buffer, "} ffp_fog;\n");
2139 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2141 if (glsl_is_color_reg_read(shader, 0))
2142 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2143 if (glsl_is_color_reg_read(shader, 1))
2144 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2145 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2146 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2148 else
2150 if (glsl_is_color_reg_read(shader, 0))
2151 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2152 if (glsl_is_color_reg_read(shader, 1))
2153 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2154 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2155 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2156 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2160 if (version->major >= 3)
2162 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2164 if (ps_args->vp_mode == vertexshader)
2165 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2166 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2169 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2171 if (!(map & 1))
2172 continue;
2174 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2176 if (reg_maps->luminanceparams & (1u << i))
2178 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2179 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2180 extra_constants_needed++;
2183 extra_constants_needed++;
2186 if (ps_args->srgb_correction)
2188 shader_addline(buffer, "const vec4 srgb_const0 = ");
2189 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2190 shader_addline(buffer, ";\n");
2191 shader_addline(buffer, "const vec4 srgb_const1 = ");
2192 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2193 shader_addline(buffer, ";\n");
2195 if (reg_maps->vpos || reg_maps->usesdsy)
2197 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2199 ++extra_constants_needed;
2200 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2202 if (reg_maps->vpos)
2204 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2206 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
2207 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2208 ps_args->render_offscreen ? "" : "origin_upper_left, ");
2209 else if (!ps_args->render_offscreen)
2210 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2212 shader_addline(buffer, "vec4 vpos;\n");
2216 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2217 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2219 if (!needs_legacy_glsl_syntax(gl_info))
2220 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2222 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2223 FIXME("Insufficient uniforms to run this shader.\n");
2226 /* Declare output register temporaries */
2227 if (shader->limits->packed_output)
2228 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2230 /* Declare temporary variables */
2231 if (reg_maps->temporary_count)
2233 for (i = 0; i < reg_maps->temporary_count; ++i)
2234 shader_addline(buffer, "vec4 R%u;\n", i);
2236 else
2238 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2240 if (map & 1)
2241 shader_addline(buffer, "vec4 R%u;\n", i);
2245 /* Declare indexable temporary variables */
2246 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2248 if (idx_temp_reg->component_count != 4)
2249 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2250 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2253 /* Declare loop registers aLx */
2254 if (version->major < 4)
2256 for (i = 0; i < reg_maps->loop_depth; ++i)
2258 shader_addline(buffer, "int aL%u;\n", i);
2259 shader_addline(buffer, "int tmpInt%u;\n", i);
2263 /* Temporary variables for matrix operations */
2264 shader_addline(buffer, "vec4 tmp0;\n");
2265 shader_addline(buffer, "vec4 tmp1;\n");
2267 if (!shader->load_local_constsF)
2269 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2271 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2272 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2273 shader_addline(buffer, ";\n");
2278 /*****************************************************************************
2279 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2281 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2282 ****************************************************************************/
2284 /* Prototypes */
2285 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2286 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2288 /** Used for opcode modifiers - They multiply the result by the specified amount */
2289 static const char * const shift_glsl_tab[] = {
2290 "", /* 0 (none) */
2291 "2.0 * ", /* 1 (x2) */
2292 "4.0 * ", /* 2 (x4) */
2293 "8.0 * ", /* 3 (x8) */
2294 "16.0 * ", /* 4 (x16) */
2295 "32.0 * ", /* 5 (x32) */
2296 "", /* 6 (x64) */
2297 "", /* 7 (x128) */
2298 "", /* 8 (d256) */
2299 "", /* 9 (d128) */
2300 "", /* 10 (d64) */
2301 "", /* 11 (d32) */
2302 "0.0625 * ", /* 12 (d16) */
2303 "0.125 * ", /* 13 (d8) */
2304 "0.25 * ", /* 14 (d4) */
2305 "0.5 * " /* 15 (d2) */
2308 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2309 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2310 const char *in_reg, const char *in_regswizzle, char *out_str)
2312 switch (src_modifier)
2314 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2315 case WINED3DSPSM_DW:
2316 case WINED3DSPSM_NONE:
2317 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2318 break;
2319 case WINED3DSPSM_NEG:
2320 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2321 break;
2322 case WINED3DSPSM_NOT:
2323 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2324 break;
2325 case WINED3DSPSM_BIAS:
2326 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2327 break;
2328 case WINED3DSPSM_BIASNEG:
2329 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2330 break;
2331 case WINED3DSPSM_SIGN:
2332 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2333 break;
2334 case WINED3DSPSM_SIGNNEG:
2335 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2336 break;
2337 case WINED3DSPSM_COMP:
2338 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2339 break;
2340 case WINED3DSPSM_X2:
2341 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2342 break;
2343 case WINED3DSPSM_X2NEG:
2344 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2345 break;
2346 case WINED3DSPSM_ABS:
2347 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2348 break;
2349 case WINED3DSPSM_ABSNEG:
2350 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2351 break;
2352 default:
2353 FIXME("Unhandled modifier %u\n", src_modifier);
2354 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2358 /** Writes the GLSL variable name that corresponds to the register that the
2359 * DX opcode parameter is trying to access */
2360 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2361 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2363 /* oPos, oFog and oPts in D3D */
2364 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2366 const struct wined3d_shader *shader = ins->ctx->shader;
2367 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2368 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2369 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2370 const char *prefix = shader_glsl_get_prefix(version->type);
2371 struct glsl_src_param rel_param0, rel_param1;
2372 char imm_str[4][17];
2374 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2375 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2376 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2377 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2378 *is_color = FALSE;
2380 switch (reg->type)
2382 case WINED3DSPR_TEMP:
2383 sprintf(register_name, "R%u", reg->idx[0].offset);
2384 break;
2386 case WINED3DSPR_INPUT:
2387 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2389 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2391 if (reg->idx[0].rel_addr)
2392 FIXME("VS3+ input registers relative addressing.\n");
2393 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2394 *is_color = TRUE;
2395 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2396 break;
2399 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2401 if (reg->idx[0].rel_addr)
2403 if (reg->idx[1].rel_addr)
2404 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2405 rel_param0.param_str, reg->idx[0].offset,
2406 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2407 rel_param1.param_str, reg->idx[1].offset);
2408 else
2409 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2410 rel_param0.param_str, reg->idx[0].offset,
2411 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2412 reg->idx[1].offset);
2414 else if (reg->idx[1].rel_addr)
2415 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2416 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2417 rel_param1.param_str, reg->idx[1].offset);
2418 else
2419 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2420 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2421 reg->idx[1].offset);
2422 break;
2425 /* pixel shaders >= 3.0 */
2426 if (version->major >= 3)
2428 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2429 unsigned int in_count = vec4_varyings(version->major, gl_info);
2431 if (reg->idx[0].rel_addr)
2433 /* Removing a + 0 would be an obvious optimization, but
2434 * OS X doesn't see the NOP operation there. */
2435 if (idx)
2437 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2438 && shader->u.ps.declared_in_count > in_count)
2440 sprintf(register_name,
2441 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2442 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2443 prefix, rel_param0.param_str, idx);
2445 else
2447 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2450 else
2452 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2453 && shader->u.ps.declared_in_count > in_count)
2455 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2456 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2457 prefix, rel_param0.param_str);
2459 else
2461 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2465 else
2467 if (idx == in_count) sprintf(register_name, "gl_Color");
2468 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2469 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2472 else
2474 if (!reg->idx[0].offset)
2475 strcpy(register_name, "ffp_varying_diffuse");
2476 else
2477 strcpy(register_name, "ffp_varying_specular");
2478 break;
2480 break;
2482 case WINED3DSPR_CONST:
2484 /* Relative addressing */
2485 if (reg->idx[0].rel_addr)
2487 if (wined3d_settings.check_float_constants)
2488 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2489 rel_param0.param_str, reg->idx[0].offset,
2490 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2491 prefix, rel_param0.param_str, reg->idx[0].offset);
2492 else if (reg->idx[0].offset)
2493 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2494 else
2495 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2497 else
2499 if (shader_constant_is_local(shader, reg->idx[0].offset))
2500 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2501 else
2502 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2505 break;
2507 case WINED3DSPR_CONSTINT:
2508 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2509 break;
2511 case WINED3DSPR_CONSTBOOL:
2512 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2513 break;
2515 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2516 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2517 sprintf(register_name, "T%u", reg->idx[0].offset);
2518 else
2519 sprintf(register_name, "A%u", reg->idx[0].offset);
2520 break;
2522 case WINED3DSPR_LOOP:
2523 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2524 break;
2526 case WINED3DSPR_SAMPLER:
2527 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2528 break;
2530 case WINED3DSPR_COLOROUT:
2531 if (reg->idx[0].offset >= gl_info->limits.buffers)
2532 WARN("Write to render target %u, only %d supported.\n",
2533 reg->idx[0].offset, gl_info->limits.buffers);
2535 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2536 break;
2538 case WINED3DSPR_RASTOUT:
2539 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2540 break;
2542 case WINED3DSPR_DEPTHOUT:
2543 sprintf(register_name, "gl_FragDepth");
2544 break;
2546 case WINED3DSPR_ATTROUT:
2547 if (!reg->idx[0].offset)
2548 sprintf(register_name, "%s_out[8]", prefix);
2549 else
2550 sprintf(register_name, "%s_out[9]", prefix);
2551 break;
2553 case WINED3DSPR_TEXCRDOUT:
2554 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2555 if (reg->idx[0].rel_addr)
2556 FIXME("VS3 output registers relative addressing.\n");
2557 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2558 break;
2560 case WINED3DSPR_MISCTYPE:
2561 if (!reg->idx[0].offset)
2563 /* vPos */
2564 sprintf(register_name, "vpos");
2566 else if (reg->idx[0].offset == 1)
2568 /* Note that gl_FrontFacing is a bool, while vFace is
2569 * a float for which the sign determines front/back */
2570 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2572 else
2574 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2575 sprintf(register_name, "unrecognized_register");
2577 break;
2579 case WINED3DSPR_IMMCONST:
2580 switch (reg->immconst_type)
2582 case WINED3D_IMMCONST_SCALAR:
2583 switch (reg->data_type)
2585 case WINED3D_DATA_FLOAT:
2586 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2587 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->immconst_data[0]);
2588 else
2589 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2590 break;
2591 case WINED3D_DATA_INT:
2592 sprintf(register_name, "%#x", reg->immconst_data[0]);
2593 break;
2594 case WINED3D_DATA_RESOURCE:
2595 case WINED3D_DATA_SAMPLER:
2596 case WINED3D_DATA_UINT:
2597 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2598 break;
2599 default:
2600 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2601 break;
2603 break;
2605 case WINED3D_IMMCONST_VEC4:
2606 switch (reg->data_type)
2608 case WINED3D_DATA_FLOAT:
2609 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2611 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2612 reg->immconst_data[0], reg->immconst_data[1],
2613 reg->immconst_data[2], reg->immconst_data[3]);
2615 else
2617 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2618 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2619 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2620 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2621 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2622 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2624 break;
2625 case WINED3D_DATA_INT:
2626 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2627 reg->immconst_data[0], reg->immconst_data[1],
2628 reg->immconst_data[2], reg->immconst_data[3]);
2629 break;
2630 case WINED3D_DATA_RESOURCE:
2631 case WINED3D_DATA_SAMPLER:
2632 case WINED3D_DATA_UINT:
2633 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2634 reg->immconst_data[0], reg->immconst_data[1],
2635 reg->immconst_data[2], reg->immconst_data[3]);
2636 break;
2637 default:
2638 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2639 break;
2641 break;
2643 default:
2644 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2645 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2647 break;
2649 case WINED3DSPR_CONSTBUFFER:
2650 if (reg->idx[1].rel_addr)
2651 sprintf(register_name, "%s_cb%u[%s + %u]",
2652 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2653 else
2654 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2655 break;
2657 case WINED3DSPR_IMMCONSTBUFFER:
2658 if (reg->idx[0].rel_addr)
2659 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2660 else
2661 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2662 break;
2664 case WINED3DSPR_PRIMID:
2665 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2666 break;
2668 case WINED3DSPR_IDXTEMP:
2669 if (reg->idx[1].rel_addr)
2670 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2671 else
2672 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2673 break;
2675 default:
2676 FIXME("Unhandled register type %#x.\n", reg->type);
2677 sprintf(register_name, "unrecognized_register");
2678 break;
2682 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2684 *str++ = '.';
2685 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2686 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2687 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2688 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2689 *str = '\0';
2692 /* Get the GLSL write mask for the destination register */
2693 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2695 DWORD mask = param->write_mask;
2697 if (shader_is_scalar(&param->reg))
2699 mask = WINED3DSP_WRITEMASK_0;
2700 *write_mask = '\0';
2702 else
2704 shader_glsl_write_mask_to_str(mask, write_mask);
2707 return mask;
2710 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2711 unsigned int size = 0;
2713 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2714 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2715 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2716 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2718 return size;
2721 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2723 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2724 * but addressed as "rgba". To fix this we need to swap the register's x
2725 * and z components. */
2726 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2728 *str++ = '.';
2729 /* swizzle bits fields: wwzzyyxx */
2730 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2731 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2732 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2733 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2734 *str = '\0';
2737 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2738 BOOL fixup, DWORD mask, char *swizzle_str)
2740 if (shader_is_scalar(&param->reg))
2741 *swizzle_str = '\0';
2742 else
2743 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2746 /* From a given parameter token, generate the corresponding GLSL string.
2747 * Also, return the actual register name and swizzle in case the
2748 * caller needs this information as well. */
2749 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2750 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2752 BOOL is_color = FALSE;
2753 char swizzle_str[6];
2755 glsl_src->reg_name[0] = '\0';
2756 glsl_src->param_str[0] = '\0';
2757 swizzle_str[0] = '\0';
2759 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2760 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2762 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2764 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2766 else
2768 char reg_name[200];
2770 switch (wined3d_src->reg.data_type)
2772 case WINED3D_DATA_FLOAT:
2773 sprintf(reg_name, "%s", glsl_src->reg_name);
2774 break;
2775 case WINED3D_DATA_INT:
2776 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2777 break;
2778 case WINED3D_DATA_RESOURCE:
2779 case WINED3D_DATA_SAMPLER:
2780 case WINED3D_DATA_UINT:
2781 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2782 break;
2783 default:
2784 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2785 sprintf(reg_name, "%s", glsl_src->reg_name);
2786 break;
2789 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2793 /* From a given parameter token, generate the corresponding GLSL string.
2794 * Also, return the actual register name and swizzle in case the
2795 * caller needs this information as well. */
2796 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2797 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2799 BOOL is_color = FALSE;
2801 glsl_dst->mask_str[0] = '\0';
2802 glsl_dst->reg_name[0] = '\0';
2804 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2805 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2808 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2809 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2810 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2811 enum wined3d_data_type data_type)
2813 struct glsl_dst_param glsl_dst;
2814 DWORD mask;
2816 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2818 switch (data_type)
2820 case WINED3D_DATA_FLOAT:
2821 shader_addline(buffer, "%s%s = %s(",
2822 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2823 break;
2824 case WINED3D_DATA_INT:
2825 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2826 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2827 break;
2828 case WINED3D_DATA_RESOURCE:
2829 case WINED3D_DATA_SAMPLER:
2830 case WINED3D_DATA_UINT:
2831 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2832 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2833 break;
2834 default:
2835 FIXME("Unhandled data type %#x.\n", data_type);
2836 shader_addline(buffer, "%s%s = %s(",
2837 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2838 break;
2842 return mask;
2845 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2846 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2848 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2851 /** Process GLSL instruction modifiers */
2852 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2854 struct glsl_dst_param dst_param;
2855 DWORD modifiers;
2857 if (!ins->dst_count) return;
2859 modifiers = ins->dst[0].modifiers;
2860 if (!modifiers) return;
2862 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2864 if (modifiers & WINED3DSPDM_SATURATE)
2866 /* _SAT means to clamp the value of the register to between 0 and 1 */
2867 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2868 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2871 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2873 FIXME("_centroid modifier not handled\n");
2876 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2878 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2882 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2884 switch (op)
2886 case WINED3D_SHADER_REL_OP_GT: return ">";
2887 case WINED3D_SHADER_REL_OP_EQ: return "==";
2888 case WINED3D_SHADER_REL_OP_GE: return ">=";
2889 case WINED3D_SHADER_REL_OP_LT: return "<";
2890 case WINED3D_SHADER_REL_OP_NE: return "!=";
2891 case WINED3D_SHADER_REL_OP_LE: return "<=";
2892 default:
2893 FIXME("Unrecognized operator %#x.\n", op);
2894 return "(\?\?)";
2898 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info,
2899 const struct wined3d_shader_version *version)
2901 return shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
2904 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2905 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
2907 static const struct
2909 unsigned int coord_size;
2910 const char *type_part;
2912 resource_types[] =
2914 {0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2915 {1, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2916 {1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2917 {2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2918 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2919 {3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2920 {3, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2921 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2922 {3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2923 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2925 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2926 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2927 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2928 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
2929 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2930 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2931 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
2932 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2933 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2934 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2935 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2936 const char *base = "texture", *type_part = "", *suffix = "";
2937 unsigned int coord_size, deriv_size;
2938 BOOL array;
2940 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2942 if (resource_type >= ARRAY_SIZE(resource_types))
2944 ERR("Unexpected resource type %#x.\n", resource_type);
2945 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2947 array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
2948 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
2950 /* Note that there's no such thing as a projected cube texture. */
2951 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2952 projected = FALSE;
2954 if (needs_legacy_glsl_syntax(gl_info))
2956 if (shadow)
2957 base = "shadow";
2959 type_part = resource_types[resource_type].type_part;
2960 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2961 type_part = "2DRect";
2962 if (!type_part[0])
2963 FIXME("Unhandled resource type %#x.\n", resource_type);
2965 if (!lod && grad && !shader_glsl_has_core_grad(gl_info, &ctx->shader->reg_maps.shader_version))
2967 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2968 suffix = "ARB";
2969 else
2970 FIXME("Unsupported grad function.\n");
2974 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2976 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2977 if (flags & ~texel_fetch_flags)
2978 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
2980 base = "texelFetch";
2981 type_part = "";
2984 sample_function->name = string_buffer_get(priv->string_buffers);
2985 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2986 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2988 coord_size = resource_types[resource_type].coord_size;
2989 deriv_size = coord_size;
2990 if (shadow)
2991 ++coord_size;
2992 if (array)
2993 --deriv_size;
2994 sample_function->offset_size = offset ? deriv_size : 0;
2995 sample_function->coord_mask = (1u << coord_size) - 1;
2996 sample_function->deriv_mask = (1u << deriv_size) - 1;
2997 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3000 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3001 struct glsl_sample_function *sample_function)
3003 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3005 string_buffer_release(priv->string_buffers, sample_function->name);
3008 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3009 BOOL sign_fixup, enum fixup_channel_source channel_source)
3011 switch(channel_source)
3013 case CHANNEL_SOURCE_ZERO:
3014 strcat(arguments, "0.0");
3015 break;
3017 case CHANNEL_SOURCE_ONE:
3018 strcat(arguments, "1.0");
3019 break;
3021 case CHANNEL_SOURCE_X:
3022 strcat(arguments, reg_name);
3023 strcat(arguments, ".x");
3024 break;
3026 case CHANNEL_SOURCE_Y:
3027 strcat(arguments, reg_name);
3028 strcat(arguments, ".y");
3029 break;
3031 case CHANNEL_SOURCE_Z:
3032 strcat(arguments, reg_name);
3033 strcat(arguments, ".z");
3034 break;
3036 case CHANNEL_SOURCE_W:
3037 strcat(arguments, reg_name);
3038 strcat(arguments, ".w");
3039 break;
3041 default:
3042 FIXME("Unhandled channel source %#x\n", channel_source);
3043 strcat(arguments, "undefined");
3044 break;
3047 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3050 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3051 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3053 unsigned int mask_size, remaining;
3054 DWORD fixup_mask = 0;
3055 char arguments[256];
3056 char mask_str[6];
3058 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3059 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3060 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3061 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3062 if (!(mask &= fixup_mask))
3063 return;
3065 if (is_complex_fixup(fixup))
3067 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3068 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3069 return;
3072 shader_glsl_write_mask_to_str(mask, mask_str);
3073 mask_size = shader_glsl_get_write_mask_size(mask);
3075 arguments[0] = '\0';
3076 remaining = mask_size;
3077 if (mask & WINED3DSP_WRITEMASK_0)
3079 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3080 if (--remaining) strcat(arguments, ", ");
3082 if (mask & WINED3DSP_WRITEMASK_1)
3084 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3085 if (--remaining) strcat(arguments, ", ");
3087 if (mask & WINED3DSP_WRITEMASK_2)
3089 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3090 if (--remaining) strcat(arguments, ", ");
3092 if (mask & WINED3DSP_WRITEMASK_3)
3094 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3095 if (--remaining) strcat(arguments, ", ");
3098 if (mask_size > 1)
3099 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3100 else
3101 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3104 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3106 char reg_name[256];
3107 BOOL is_color;
3109 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3110 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3113 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3114 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3115 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3116 const char *coord_reg_fmt, ...)
3118 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3119 char dst_swizzle[6];
3120 struct color_fixup_desc fixup;
3121 BOOL np2_fixup = FALSE;
3122 va_list args;
3123 int ret;
3125 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3127 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3128 * We actually rely on it for vertex shaders and SM4+. */
3129 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3131 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3132 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3134 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3135 np2_fixup = TRUE;
3137 else
3139 fixup = COLOR_FIXUP_IDENTITY;
3142 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3144 if (sample_function->output_single_component)
3145 shader_addline(ins->ctx->buffer, "vec4(");
3147 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3148 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3150 for (;;)
3152 va_start(args, coord_reg_fmt);
3153 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3154 va_end(args);
3155 if (!ret)
3156 break;
3157 if (!string_buffer_resize(ins->ctx->buffer, ret))
3158 break;
3161 if (np2_fixup)
3163 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3164 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3166 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3168 case 1:
3169 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3170 idx >> 1, (idx % 2) ? "z" : "x");
3171 break;
3172 case 2:
3173 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3174 idx >> 1, (idx % 2) ? "zw" : "xy");
3175 break;
3176 case 3:
3177 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3178 idx >> 1, (idx % 2) ? "zw" : "xy");
3179 break;
3180 case 4:
3181 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3182 idx >> 1, (idx % 2) ? "zw" : "xy");
3183 break;
3186 if (dx && dy)
3187 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3188 else if (bias)
3189 shader_addline(ins->ctx->buffer, ", %s", bias);
3190 if (sample_function->offset_size)
3192 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3193 shader_addline(ins->ctx->buffer, ", ");
3194 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3196 shader_addline(ins->ctx->buffer, ")");
3198 if (sample_function->output_single_component)
3199 shader_addline(ins->ctx->buffer, ")");
3201 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3203 if (!is_identity_fixup(fixup))
3204 shader_glsl_color_correction(ins, fixup);
3207 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3209 /* Write the final position.
3211 * OpenGL coordinates specify the center of the pixel while D3D coords
3212 * specify the corner. The offsets are stored in z and w in
3213 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3214 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3215 * a MAD. */
3216 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3217 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3219 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3220 * in utils.c
3222 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3223 * shaders are run before the homogeneous divide, so we have to take the w
3224 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3225 * z = z * 2 - w. */
3226 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3229 /*****************************************************************************
3230 * Begin processing individual instruction opcodes
3231 ****************************************************************************/
3233 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3235 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3236 struct glsl_src_param src0_param;
3237 struct glsl_src_param src1_param;
3238 DWORD write_mask;
3239 const char *op;
3241 /* Determine the GLSL operator to use based on the opcode */
3242 switch (ins->handler_idx)
3244 case WINED3DSIH_ADD: op = "+"; break;
3245 case WINED3DSIH_AND: op = "&"; break;
3246 case WINED3DSIH_DIV: op = "/"; break;
3247 case WINED3DSIH_IADD: op = "+"; break;
3248 case WINED3DSIH_ISHL: op = "<<"; break;
3249 case WINED3DSIH_ISHR: op = ">>"; break;
3250 case WINED3DSIH_MUL: op = "*"; break;
3251 case WINED3DSIH_OR: op = "|"; break;
3252 case WINED3DSIH_SUB: op = "-"; break;
3253 case WINED3DSIH_USHR: op = ">>"; break;
3254 case WINED3DSIH_XOR: op = "^"; break;
3255 default:
3256 op = "<unhandled operator>";
3257 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3258 break;
3261 write_mask = shader_glsl_append_dst(buffer, ins);
3262 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3263 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3264 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3267 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3269 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3270 struct glsl_src_param src0_param;
3271 struct glsl_src_param src1_param;
3272 unsigned int mask_size;
3273 DWORD write_mask;
3274 const char *op;
3276 write_mask = shader_glsl_append_dst(buffer, ins);
3277 mask_size = shader_glsl_get_write_mask_size(write_mask);
3278 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3279 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3281 if (mask_size > 1)
3283 switch (ins->handler_idx)
3285 case WINED3DSIH_EQ: op = "equal"; break;
3286 case WINED3DSIH_IEQ: op = "equal"; break;
3287 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3288 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3289 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3290 case WINED3DSIH_LT: op = "lessThan"; break;
3291 case WINED3DSIH_ILT: op = "lessThan"; break;
3292 case WINED3DSIH_ULT: op = "lessThan"; break;
3293 case WINED3DSIH_NE: op = "notEqual"; break;
3294 case WINED3DSIH_INE: op = "notEqual"; break;
3295 default:
3296 op = "<unhandled operator>";
3297 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3298 break;
3301 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3302 mask_size, op, src0_param.param_str, src1_param.param_str);
3304 else
3306 switch (ins->handler_idx)
3308 case WINED3DSIH_EQ: op = "=="; break;
3309 case WINED3DSIH_IEQ: op = "=="; break;
3310 case WINED3DSIH_GE: op = ">="; break;
3311 case WINED3DSIH_IGE: op = ">="; break;
3312 case WINED3DSIH_UGE: op = ">="; break;
3313 case WINED3DSIH_LT: op = "<"; break;
3314 case WINED3DSIH_ILT: op = "<"; break;
3315 case WINED3DSIH_ULT: op = "<"; break;
3316 case WINED3DSIH_NE: op = "!="; break;
3317 case WINED3DSIH_INE: op = "!="; break;
3318 default:
3319 op = "<unhandled operator>";
3320 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3321 break;
3324 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3325 src0_param.param_str, op, src1_param.param_str);
3329 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3331 struct glsl_src_param src_param;
3332 DWORD write_mask;
3333 const char *op;
3335 switch (ins->handler_idx)
3337 case WINED3DSIH_INEG: op = "-"; break;
3338 case WINED3DSIH_NOT: op = "~"; break;
3339 default:
3340 op = "<unhandled operator>";
3341 ERR("Unhandled opcode %s.\n",
3342 debug_d3dshaderinstructionhandler(ins->handler_idx));
3343 break;
3346 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3347 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3348 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3351 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3353 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3354 struct glsl_src_param src0_param;
3355 struct glsl_src_param src1_param;
3356 DWORD write_mask;
3358 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3359 * not, we can emulate it. */
3360 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3361 FIXME("64-bit integer multiplies not implemented.\n");
3363 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3365 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3366 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3367 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3369 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3370 src0_param.param_str, src1_param.param_str);
3374 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3376 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3377 struct glsl_src_param src0_param, src1_param;
3378 DWORD write_mask;
3380 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3382 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3384 char dst_mask[6];
3386 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3387 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3388 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3389 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3390 dst_mask, src0_param.param_str, src1_param.param_str);
3392 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3393 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3394 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3395 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3397 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3398 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3400 else
3402 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3403 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3404 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3405 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3408 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3410 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3411 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3412 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3413 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3417 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3418 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3420 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3421 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3422 struct glsl_src_param src0_param;
3423 DWORD write_mask;
3425 write_mask = shader_glsl_append_dst(buffer, ins);
3426 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3428 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3429 * shader versions WINED3DSIO_MOVA is used for this. */
3430 if (ins->ctx->reg_maps->shader_version.major == 1
3431 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3432 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3434 /* This is a simple floor() */
3435 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3436 if (mask_size > 1) {
3437 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3438 } else {
3439 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3442 else if (ins->handler_idx == WINED3DSIH_MOVA)
3444 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
3445 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3447 if (shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3449 if (mask_size > 1)
3450 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3451 else
3452 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3454 else
3456 if (mask_size > 1)
3457 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3458 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3459 else
3460 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3461 src0_param.param_str, src0_param.param_str);
3464 else
3466 shader_addline(buffer, "%s);\n", src0_param.param_str);
3470 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3471 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3473 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3474 struct glsl_src_param src0_param;
3475 struct glsl_src_param src1_param;
3476 DWORD dst_write_mask, src_write_mask;
3477 unsigned int dst_size;
3479 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3480 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3482 /* dp4 works on vec4, dp3 on vec3, etc. */
3483 if (ins->handler_idx == WINED3DSIH_DP4)
3484 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3485 else if (ins->handler_idx == WINED3DSIH_DP3)
3486 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3487 else
3488 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3490 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3491 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3493 if (dst_size > 1) {
3494 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3495 } else {
3496 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3500 /* Note that this instruction has some restrictions. The destination write mask
3501 * can't contain the w component, and the source swizzles have to be .xyzw */
3502 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3504 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3505 struct glsl_src_param src0_param;
3506 struct glsl_src_param src1_param;
3507 char dst_mask[6];
3509 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3510 shader_glsl_append_dst(ins->ctx->buffer, ins);
3511 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3512 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3513 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3516 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3518 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3520 if (!stream)
3521 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3522 else
3523 FIXME("Unhandled primitive stream %u.\n", stream);
3526 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3527 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3528 * GLSL uses the value as-is. */
3529 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3531 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3532 struct glsl_src_param src0_param;
3533 struct glsl_src_param src1_param;
3534 DWORD dst_write_mask;
3535 unsigned int dst_size;
3537 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3538 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3540 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3541 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3543 if (dst_size > 1)
3545 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3546 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3548 else
3550 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3551 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3555 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3556 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3558 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3559 struct glsl_src_param src_param;
3560 const char *instruction;
3561 DWORD write_mask;
3562 unsigned i;
3564 /* Determine the GLSL function to use based on the opcode */
3565 /* TODO: Possibly make this a table for faster lookups */
3566 switch (ins->handler_idx)
3568 case WINED3DSIH_ABS: instruction = "abs"; break;
3569 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3570 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
3571 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
3572 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3573 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
3574 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
3575 case WINED3DSIH_FRC: instruction = "fract"; break;
3576 case WINED3DSIH_IMAX: instruction = "max"; break;
3577 case WINED3DSIH_IMIN: instruction = "min"; break;
3578 case WINED3DSIH_MAX: instruction = "max"; break;
3579 case WINED3DSIH_MIN: instruction = "min"; break;
3580 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3581 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3582 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3583 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3584 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3585 case WINED3DSIH_UMAX: instruction = "max"; break;
3586 case WINED3DSIH_UMIN: instruction = "min"; break;
3587 default: instruction = "";
3588 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3589 break;
3592 write_mask = shader_glsl_append_dst(buffer, ins);
3594 shader_addline(buffer, "%s(", instruction);
3596 if (ins->src_count)
3598 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3599 shader_addline(buffer, "%s", src_param.param_str);
3600 for (i = 1; i < ins->src_count; ++i)
3602 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3603 shader_addline(buffer, ", %s", src_param.param_str);
3607 shader_addline(buffer, "));\n");
3610 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3612 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3614 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3615 struct glsl_src_param src_param;
3616 unsigned int mask_size;
3617 DWORD write_mask;
3618 char dst_mask[6];
3620 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3621 mask_size = shader_glsl_get_write_mask_size(write_mask);
3622 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3624 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3625 src_param.param_str, src_param.param_str);
3626 shader_glsl_append_dst(buffer, ins);
3628 if (mask_size > 1)
3630 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3631 mask_size, src_param.param_str);
3633 else
3635 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3636 src_param.param_str);
3640 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3642 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3643 ins->ctx->reg_maps->shader_version.minor);
3644 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3645 struct glsl_src_param src0_param;
3646 const char *prefix, *suffix;
3647 unsigned int dst_size;
3648 DWORD dst_write_mask;
3650 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3651 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3653 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3654 dst_write_mask = WINED3DSP_WRITEMASK_3;
3656 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3658 switch (ins->handler_idx)
3660 case WINED3DSIH_EXP:
3661 case WINED3DSIH_EXPP:
3662 prefix = "exp2(";
3663 suffix = ")";
3664 break;
3666 case WINED3DSIH_LOG:
3667 case WINED3DSIH_LOGP:
3668 prefix = "log2(abs(";
3669 suffix = "))";
3670 break;
3672 case WINED3DSIH_RCP:
3673 prefix = "1.0 / ";
3674 suffix = "";
3675 break;
3677 case WINED3DSIH_RSQ:
3678 prefix = "inversesqrt(abs(";
3679 suffix = "))";
3680 break;
3682 default:
3683 prefix = "";
3684 suffix = "";
3685 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3686 break;
3689 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3690 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3691 else
3692 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3695 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3696 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3697 * dst.x = 2^(floor(src))
3698 * dst.y = src - floor(src)
3699 * dst.z = 2^src (partial precision is allowed, but optional)
3700 * dst.w = 1.0;
3701 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3702 * dst = 2^src; (partial precision is allowed, but optional)
3704 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3706 if (ins->ctx->reg_maps->shader_version.major < 2)
3708 struct glsl_src_param src_param;
3709 char dst_mask[6];
3711 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3713 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3714 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3715 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3716 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3718 shader_glsl_append_dst(ins->ctx->buffer, ins);
3719 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3720 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3721 return;
3724 shader_glsl_scalar_op(ins);
3727 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3728 const char *vector_constructor, const char *scalar_constructor)
3730 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3731 struct glsl_src_param src_param;
3732 unsigned int mask_size;
3733 DWORD write_mask;
3735 write_mask = shader_glsl_append_dst(buffer, ins);
3736 mask_size = shader_glsl_get_write_mask_size(write_mask);
3737 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3739 if (mask_size > 1)
3740 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3741 else
3742 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3745 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3747 shader_glsl_cast(ins, "ivec", "int");
3750 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3752 shader_glsl_cast(ins, "uvec", "uint");
3755 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3757 shader_glsl_cast(ins, "vec", "float");
3760 /** Process signed comparison opcodes in GLSL. */
3761 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3763 struct glsl_src_param src0_param;
3764 struct glsl_src_param src1_param;
3765 DWORD write_mask;
3766 unsigned int mask_size;
3768 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3769 mask_size = shader_glsl_get_write_mask_size(write_mask);
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);
3773 if (mask_size > 1) {
3774 const char *compare;
3776 switch(ins->handler_idx)
3778 case WINED3DSIH_SLT: compare = "lessThan"; break;
3779 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3780 default: compare = "";
3781 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3784 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3785 src0_param.param_str, src1_param.param_str);
3786 } else {
3787 switch(ins->handler_idx)
3789 case WINED3DSIH_SLT:
3790 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3791 * to return 0.0 but step returns 1.0 because step is not < x
3792 * An alternative is a bvec compare padded with an unused second component.
3793 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3794 * issue. Playing with not() is not possible either because not() does not accept
3795 * a scalar.
3797 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3798 src0_param.param_str, src1_param.param_str);
3799 break;
3800 case WINED3DSIH_SGE:
3801 /* Here we can use the step() function and safe a conditional */
3802 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3803 break;
3804 default:
3805 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3811 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3813 const char *condition_prefix, *condition_suffix;
3814 struct wined3d_shader_dst_param dst;
3815 struct glsl_src_param src0_param;
3816 struct glsl_src_param src1_param;
3817 struct glsl_src_param src2_param;
3818 BOOL temp_destination = FALSE;
3819 DWORD cmp_channel = 0;
3820 unsigned int i, j;
3821 char mask_char[6];
3822 DWORD write_mask;
3824 switch (ins->handler_idx)
3826 case WINED3DSIH_CMP:
3827 condition_prefix = "";
3828 condition_suffix = " >= 0.0";
3829 break;
3831 case WINED3DSIH_CND:
3832 condition_prefix = "";
3833 condition_suffix = " > 0.5";
3834 break;
3836 case WINED3DSIH_MOVC:
3837 condition_prefix = "bool(";
3838 condition_suffix = ")";
3839 break;
3841 default:
3842 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3843 condition_prefix = "<unhandled prefix>";
3844 condition_suffix = "<unhandled suffix>";
3845 break;
3848 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3850 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3851 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3852 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3853 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3855 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3856 condition_prefix, src0_param.param_str, condition_suffix,
3857 src1_param.param_str, src2_param.param_str);
3858 return;
3861 dst = ins->dst[0];
3863 /* Splitting the instruction up in multiple lines imposes a problem:
3864 * The first lines may overwrite source parameters of the following lines.
3865 * Deal with that by using a temporary destination register if needed. */
3866 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3867 && ins->src[0].reg.type == dst.reg.type)
3868 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3869 && ins->src[1].reg.type == dst.reg.type)
3870 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3871 && ins->src[2].reg.type == dst.reg.type))
3872 temp_destination = TRUE;
3874 /* Cycle through all source0 channels. */
3875 for (i = 0; i < 4; ++i)
3877 write_mask = 0;
3878 /* Find the destination channels which use the current source0 channel. */
3879 for (j = 0; j < 4; ++j)
3881 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3883 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3884 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3887 dst.write_mask = ins->dst[0].write_mask & write_mask;
3889 if (temp_destination)
3891 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3892 continue;
3893 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3895 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3896 continue;
3898 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3899 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3900 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3902 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3903 condition_prefix, src0_param.param_str, condition_suffix,
3904 src1_param.param_str, src2_param.param_str);
3907 if (temp_destination)
3909 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3910 shader_glsl_append_dst(ins->ctx->buffer, ins);
3911 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3915 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3916 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3917 * the compare is done per component of src0. */
3918 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3920 struct glsl_src_param src0_param;
3921 struct glsl_src_param src1_param;
3922 struct glsl_src_param src2_param;
3923 DWORD write_mask;
3924 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3925 ins->ctx->reg_maps->shader_version.minor);
3927 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3929 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3930 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3931 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3932 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3934 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3935 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3936 else
3937 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3938 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3939 return;
3942 shader_glsl_conditional_move(ins);
3945 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3946 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3948 struct glsl_src_param src0_param;
3949 struct glsl_src_param src1_param;
3950 struct glsl_src_param src2_param;
3951 DWORD write_mask;
3953 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3954 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3955 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3956 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3957 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3958 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3961 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3962 Vertex shaders to GLSL codes */
3963 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3965 int i;
3966 int nComponents = 0;
3967 struct wined3d_shader_dst_param tmp_dst = {{0}};
3968 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3969 struct wined3d_shader_instruction tmp_ins;
3971 memset(&tmp_ins, 0, sizeof(tmp_ins));
3973 /* Set constants for the temporary argument */
3974 tmp_ins.ctx = ins->ctx;
3975 tmp_ins.dst_count = 1;
3976 tmp_ins.dst = &tmp_dst;
3977 tmp_ins.src_count = 2;
3978 tmp_ins.src = tmp_src;
3980 switch(ins->handler_idx)
3982 case WINED3DSIH_M4x4:
3983 nComponents = 4;
3984 tmp_ins.handler_idx = WINED3DSIH_DP4;
3985 break;
3986 case WINED3DSIH_M4x3:
3987 nComponents = 3;
3988 tmp_ins.handler_idx = WINED3DSIH_DP4;
3989 break;
3990 case WINED3DSIH_M3x4:
3991 nComponents = 4;
3992 tmp_ins.handler_idx = WINED3DSIH_DP3;
3993 break;
3994 case WINED3DSIH_M3x3:
3995 nComponents = 3;
3996 tmp_ins.handler_idx = WINED3DSIH_DP3;
3997 break;
3998 case WINED3DSIH_M3x2:
3999 nComponents = 2;
4000 tmp_ins.handler_idx = WINED3DSIH_DP3;
4001 break;
4002 default:
4003 break;
4006 tmp_dst = ins->dst[0];
4007 tmp_src[0] = ins->src[0];
4008 tmp_src[1] = ins->src[1];
4009 for (i = 0; i < nComponents; ++i)
4011 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4012 shader_glsl_dot(&tmp_ins);
4013 ++tmp_src[1].reg.idx[0].offset;
4018 The LRP instruction performs a component-wise linear interpolation
4019 between the second and third operands using the first operand as the
4020 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4021 This is equivalent to mix(src2, src1, src0);
4023 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4025 struct glsl_src_param src0_param;
4026 struct glsl_src_param src1_param;
4027 struct glsl_src_param src2_param;
4028 DWORD write_mask;
4030 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4032 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4033 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4034 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4036 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4037 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4040 /** Process the WINED3DSIO_LIT instruction in GLSL:
4041 * dst.x = dst.w = 1.0
4042 * dst.y = (src0.x > 0) ? src0.x
4043 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4044 * where src.w is clamped at +- 128
4046 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4048 struct glsl_src_param src0_param;
4049 struct glsl_src_param src1_param;
4050 struct glsl_src_param src3_param;
4051 char dst_mask[6];
4053 shader_glsl_append_dst(ins->ctx->buffer, ins);
4054 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4056 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4057 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4058 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4060 /* The sdk specifies the instruction like this
4061 * dst.x = 1.0;
4062 * if(src.x > 0.0) dst.y = src.x
4063 * else dst.y = 0.0.
4064 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4065 * else dst.z = 0.0;
4066 * dst.w = 1.0;
4067 * (where power = src.w clamped between -128 and 128)
4069 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4070 * dst.x = 1.0 ... No further explanation needed
4071 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4072 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4073 * dst.w = 1.0. ... Nothing fancy.
4075 * So we still have one conditional in there. So do this:
4076 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4078 * 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),
4079 * 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.
4080 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4082 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4083 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4084 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4086 shader_addline(ins->ctx->buffer,
4087 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4088 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4089 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4090 src0_param.param_str, src3_param.param_str, dst_mask);
4093 /** Process the WINED3DSIO_DST instruction in GLSL:
4094 * dst.x = 1.0
4095 * dst.y = src0.x * src0.y
4096 * dst.z = src0.z
4097 * dst.w = src1.w
4099 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4101 struct glsl_src_param src0y_param;
4102 struct glsl_src_param src0z_param;
4103 struct glsl_src_param src1y_param;
4104 struct glsl_src_param src1w_param;
4105 char dst_mask[6];
4107 shader_glsl_append_dst(ins->ctx->buffer, ins);
4108 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4110 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4111 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4112 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4113 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4115 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4116 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4119 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4120 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4121 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4123 * dst.x = cos(src0.?)
4124 * dst.y = sin(src0.?)
4125 * dst.z = dst.z
4126 * dst.w = dst.w
4128 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4130 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4131 struct glsl_src_param src0_param;
4132 DWORD write_mask;
4134 if (ins->ctx->reg_maps->shader_version.major < 4)
4136 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4138 write_mask = shader_glsl_append_dst(buffer, ins);
4139 switch (write_mask)
4141 case WINED3DSP_WRITEMASK_0:
4142 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4143 break;
4145 case WINED3DSP_WRITEMASK_1:
4146 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4147 break;
4149 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4150 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4151 src0_param.param_str, src0_param.param_str);
4152 break;
4154 default:
4155 ERR("Write mask should be .x, .y or .xy\n");
4156 break;
4159 return;
4162 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4165 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4167 char dst_mask[6];
4169 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4170 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4171 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4173 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4174 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4175 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4177 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4178 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4180 else
4182 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4183 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4184 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4187 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4189 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4190 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4191 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4195 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4196 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4197 * generate invalid code
4199 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4201 struct glsl_src_param src0_param;
4202 DWORD write_mask;
4204 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4205 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4207 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4210 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4211 * Start a for() loop where src1.y is the initial value of aL,
4212 * increment aL by src1.z for a total of src1.x iterations.
4213 * Need to use a temporary variable for this operation.
4215 /* FIXME: I don't think nested loops will work correctly this way. */
4216 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4218 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4219 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4220 const struct wined3d_shader *shader = ins->ctx->shader;
4221 const struct wined3d_shader_lconst *constant;
4222 struct glsl_src_param src1_param;
4223 const DWORD *control_values = NULL;
4225 if (ins->ctx->reg_maps->shader_version.major < 4)
4227 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4229 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4230 * class hardware doesn't support real varying indexing, but Microsoft
4231 * designed this feature for Shader model 2.x+. If the loop control is
4232 * known at compile time, the GLSL compiler can unroll the loop, and
4233 * replace indirect addressing with direct addressing. */
4234 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4236 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4238 if (constant->idx == ins->src[1].reg.idx[0].offset)
4240 control_values = constant->value;
4241 break;
4246 if (control_values)
4248 struct wined3d_shader_loop_control loop_control;
4249 loop_control.count = control_values[0];
4250 loop_control.start = control_values[1];
4251 loop_control.step = (int)control_values[2];
4253 if (loop_control.step > 0)
4255 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4256 loop_state->current_depth, loop_control.start,
4257 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4258 loop_state->current_depth, loop_control.step);
4260 else if (loop_control.step < 0)
4262 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4263 loop_state->current_depth, loop_control.start,
4264 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4265 loop_state->current_depth, loop_control.step);
4267 else
4269 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4270 loop_state->current_depth, loop_control.start, loop_state->current_depth,
4271 loop_state->current_depth, loop_control.count,
4272 loop_state->current_depth);
4275 else
4277 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4278 loop_state->current_depth, loop_state->current_reg,
4279 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4280 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4283 ++loop_state->current_reg;
4285 else
4287 shader_addline(buffer, "for (;;)\n{\n");
4290 ++loop_state->current_depth;
4293 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4295 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4297 shader_addline(ins->ctx->buffer, "}\n");
4299 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4301 --loop_state->current_depth;
4302 --loop_state->current_reg;
4305 if (ins->handler_idx == WINED3DSIH_ENDREP)
4307 --loop_state->current_depth;
4311 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4313 const struct wined3d_shader *shader = ins->ctx->shader;
4314 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4315 const struct wined3d_shader_lconst *constant;
4316 struct glsl_src_param src0_param;
4317 const DWORD *control_values = NULL;
4319 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4320 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4322 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4324 if (constant->idx == ins->src[0].reg.idx[0].offset)
4326 control_values = constant->value;
4327 break;
4332 if (control_values)
4334 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4335 loop_state->current_depth, loop_state->current_depth,
4336 control_values[0], loop_state->current_depth);
4338 else
4340 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4341 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4342 loop_state->current_depth, loop_state->current_depth,
4343 src0_param.param_str, loop_state->current_depth);
4346 ++loop_state->current_depth;
4349 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4351 struct glsl_src_param src0_param;
4353 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4354 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4357 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4359 struct glsl_src_param src0_param;
4361 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4362 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4365 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4367 shader_addline(ins->ctx->buffer, "default:\n");
4370 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4372 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4373 struct glsl_src_param src0_param;
4375 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4376 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4379 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4381 struct glsl_src_param src0_param;
4382 struct glsl_src_param src1_param;
4384 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4385 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4387 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4388 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4391 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4393 shader_addline(ins->ctx->buffer, "} else {\n");
4396 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4398 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4400 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4401 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
4402 shader_glsl_fixup_position(ins->ctx->buffer);
4404 if (!stream)
4405 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4406 else
4407 FIXME("Unhandled primitive stream %u.\n", stream);
4410 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4412 shader_addline(ins->ctx->buffer, "break;\n");
4415 /* FIXME: According to MSDN the compare is done per component. */
4416 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4418 struct glsl_src_param src0_param;
4419 struct glsl_src_param src1_param;
4421 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4422 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4424 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4425 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4428 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4430 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4431 struct glsl_src_param src_param;
4433 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4434 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4437 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
4439 shader_addline(ins->ctx->buffer, "continue;\n");
4442 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4444 shader_addline(ins->ctx->buffer, "}\n");
4445 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4448 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4450 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4453 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4455 struct glsl_src_param src1_param;
4457 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4458 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4459 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4462 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4464 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4465 * function only suppresses the unhandled instruction warning
4469 /*********************************************
4470 * Pixel Shader Specific Code begins here
4471 ********************************************/
4472 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4474 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4475 ins->ctx->reg_maps->shader_version.minor);
4476 struct glsl_sample_function sample_function;
4477 DWORD sample_flags = 0;
4478 DWORD resource_idx;
4479 DWORD mask = 0, swizzle;
4480 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4482 /* 1.0-1.4: Use destination register as sampler source.
4483 * 2.0+: Use provided sampler source. */
4484 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4485 resource_idx = ins->dst[0].reg.idx[0].offset;
4486 else
4487 resource_idx = ins->src[1].reg.idx[0].offset;
4489 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4491 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4492 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4493 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4495 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4496 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4498 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4499 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4501 case WINED3D_TTFF_COUNT1:
4502 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4503 break;
4504 case WINED3D_TTFF_COUNT2:
4505 mask = WINED3DSP_WRITEMASK_1;
4506 break;
4507 case WINED3D_TTFF_COUNT3:
4508 mask = WINED3DSP_WRITEMASK_2;
4509 break;
4510 case WINED3D_TTFF_COUNT4:
4511 case WINED3D_TTFF_DISABLE:
4512 mask = WINED3DSP_WRITEMASK_3;
4513 break;
4517 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4519 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4521 if (src_mod == WINED3DSPSM_DZ) {
4522 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4523 mask = WINED3DSP_WRITEMASK_2;
4524 } else if (src_mod == WINED3DSPSM_DW) {
4525 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4526 mask = WINED3DSP_WRITEMASK_3;
4529 else
4531 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4532 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4534 /* ps 2.0 texldp instruction always divides by the fourth component. */
4535 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4536 mask = WINED3DSP_WRITEMASK_3;
4540 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4541 mask |= sample_function.coord_mask;
4542 sample_function.coord_mask = mask;
4544 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4545 else swizzle = ins->src[1].swizzle;
4547 /* 1.0-1.3: Use destination register as coordinate source.
4548 1.4+: Use provided coordinate source register. */
4549 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4551 char coord_mask[6];
4552 shader_glsl_write_mask_to_str(mask, coord_mask);
4553 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4554 "T%u%s", resource_idx, coord_mask);
4556 else
4558 struct glsl_src_param coord_param;
4559 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4560 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4562 struct glsl_src_param bias;
4563 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4564 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4565 NULL, "%s", coord_param.param_str);
4566 } else {
4567 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4568 "%s", coord_param.param_str);
4571 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4574 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4576 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4577 struct glsl_src_param coord_param, dx_param, dy_param;
4578 struct glsl_sample_function sample_function;
4579 DWORD sampler_idx;
4580 DWORD swizzle = ins->src[1].swizzle;
4582 if (!shader_glsl_has_core_grad(gl_info, &ins->ctx->shader->reg_maps.shader_version)
4583 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4585 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4586 shader_glsl_tex(ins);
4587 return;
4590 sampler_idx = ins->src[1].reg.idx[0].offset;
4592 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4593 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4594 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
4595 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
4597 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4598 NULL, NULL, "%s", coord_param.param_str);
4599 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4602 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4604 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
4605 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4606 struct glsl_src_param coord_param, lod_param;
4607 struct glsl_sample_function sample_function;
4608 DWORD swizzle = ins->src[1].swizzle;
4609 DWORD sampler_idx;
4611 sampler_idx = ins->src[1].reg.idx[0].offset;
4613 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4614 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4616 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4618 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info, shader_version)
4619 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4621 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4622 * However, the NVIDIA drivers allow them in fragment shaders as well,
4623 * even without the appropriate extension. */
4624 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4626 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4627 "%s", coord_param.param_str);
4628 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4631 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4632 unsigned int resource_idx, unsigned int sampler_idx)
4634 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4635 unsigned int i;
4637 for (i = 0; i < sampler_map->count; ++i)
4639 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4640 return entries[i].bind_idx;
4643 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4645 return ~0u;
4648 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4650 static const unsigned int texture_size_component_count[] =
4652 0, /* WINED3D_SHADER_RESOURCE_NONE */
4653 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4654 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4655 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4656 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4657 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4658 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4659 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4660 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4661 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4664 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4665 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4666 enum wined3d_shader_resource_type resource_type;
4667 unsigned int resource_idx, sampler_bind_idx, i;
4668 enum wined3d_data_type dst_data_type;
4669 struct glsl_src_param lod_param;
4670 char dst_swizzle[6];
4671 DWORD write_mask;
4673 dst_data_type = ins->dst[0].reg.data_type;
4674 if (ins->flags == WINED3DSI_RESINFO_UINT)
4675 dst_data_type = WINED3D_DATA_UINT;
4676 else if (ins->flags)
4677 FIXME("Unhandled flags %#x.\n", ins->flags);
4679 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4680 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4682 resource_idx = ins->src[1].reg.idx[0].offset;
4683 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4684 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4685 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4686 resource_idx, WINED3D_SAMPLER_DEFAULT);
4688 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4690 ERR("Unexpected resource type %#x.\n", resource_type);
4691 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4694 if (dst_data_type == WINED3D_DATA_UINT)
4695 shader_addline(ins->ctx->buffer, "uvec4(");
4696 else
4697 shader_addline(ins->ctx->buffer, "vec4(");
4699 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4700 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4702 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4703 shader_addline(ins->ctx->buffer, "0, ");
4705 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4707 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4708 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4710 else
4712 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4713 shader_addline(ins->ctx->buffer, "1");
4716 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4719 /* FIXME: The current implementation does not handle multisample textures correctly. */
4720 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4722 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4723 struct glsl_src_param coord_param, lod_param;
4724 struct glsl_sample_function sample_function;
4725 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4727 if (wined3d_shader_instruction_has_texel_offset(ins))
4728 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4730 resource_idx = ins->src[1].reg.idx[0].offset;
4731 sampler_idx = WINED3D_SAMPLER_DEFAULT;
4733 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4734 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4735 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4736 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4737 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4738 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4739 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4742 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4744 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4745 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4746 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4747 struct glsl_sample_function sample_function;
4748 DWORD flags = 0;
4750 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4751 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4752 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4753 flags |= WINED3D_GLSL_SAMPLE_LOD;
4754 if (wined3d_shader_instruction_has_texel_offset(ins))
4755 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4757 resource_idx = ins->src[1].reg.idx[0].offset;
4758 sampler_idx = ins->src[2].reg.idx[0].offset;
4760 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4761 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4763 switch (ins->handler_idx)
4765 case WINED3DSIH_SAMPLE:
4766 break;
4767 case WINED3DSIH_SAMPLE_B:
4768 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4769 lod_param_str = lod_param.param_str;
4770 break;
4771 case WINED3DSIH_SAMPLE_GRAD:
4772 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
4773 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
4774 dx_param_str = dx_param.param_str;
4775 dy_param_str = dy_param.param_str;
4776 break;
4777 case WINED3DSIH_SAMPLE_LOD:
4778 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4779 lod_param_str = lod_param.param_str;
4780 break;
4781 default:
4782 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4783 break;
4786 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4787 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4788 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4789 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4792 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
4794 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4795 struct glsl_src_param coord_param, compare_param;
4796 struct glsl_sample_function sample_function;
4797 const char *lod_param = NULL;
4798 DWORD flags = 0;
4799 UINT coord_size;
4801 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
4803 lod_param = "0";
4804 flags |= WINED3D_GLSL_SAMPLE_LOD;
4807 if (wined3d_shader_instruction_has_texel_offset(ins))
4808 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4810 resource_idx = ins->src[1].reg.idx[0].offset;
4811 sampler_idx = ins->src[2].reg.idx[0].offset;
4813 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4814 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4815 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
4816 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
4817 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4818 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4819 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
4820 coord_size, coord_param.param_str, compare_param.param_str);
4821 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4824 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4826 /* FIXME: Make this work for more than just 2D textures */
4827 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4828 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4830 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4832 char dst_mask[6];
4834 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4835 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4836 ins->dst[0].reg.idx[0].offset, dst_mask);
4838 else
4840 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4841 DWORD reg = ins->src[0].reg.idx[0].offset;
4842 char dst_swizzle[6];
4844 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4846 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4848 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4849 struct glsl_src_param div_param;
4850 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4852 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4854 if (mask_size > 1)
4855 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4856 else
4857 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4859 else
4861 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4866 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4867 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4868 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4869 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4871 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4872 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4873 struct glsl_sample_function sample_function;
4874 struct glsl_src_param src0_param;
4875 UINT mask_size;
4877 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4879 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4880 * scalar, and projected sampling would require 4.
4882 * It is a dependent read - not valid with conditional NP2 textures
4884 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4885 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4887 switch(mask_size)
4889 case 1:
4890 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4891 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4892 break;
4894 case 2:
4895 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4896 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4897 break;
4899 case 3:
4900 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4901 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4902 break;
4904 default:
4905 FIXME("Unexpected mask size %u\n", mask_size);
4906 break;
4908 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4911 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4912 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4913 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4915 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4916 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4917 struct glsl_src_param src0_param;
4918 DWORD dst_mask;
4919 unsigned int mask_size;
4921 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4922 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4923 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4925 if (mask_size > 1) {
4926 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4927 } else {
4928 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4932 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4933 * Calculate the depth as dst.x / dst.y */
4934 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4936 struct glsl_dst_param dst_param;
4938 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4940 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4941 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4942 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4943 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4944 * >= 1.0 or < 0.0
4946 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4947 dst_param.reg_name, dst_param.reg_name);
4950 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4951 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4952 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4953 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4955 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4957 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4958 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4959 struct glsl_src_param src0_param;
4961 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4963 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4964 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4967 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4968 * Calculate the 1st of a 2-row matrix multiplication. */
4969 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4971 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4972 DWORD reg = ins->dst[0].reg.idx[0].offset;
4973 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4974 struct glsl_src_param src0_param;
4976 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4977 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4980 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4981 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4982 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4984 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4985 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4986 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4987 DWORD reg = ins->dst[0].reg.idx[0].offset;
4988 struct glsl_src_param src0_param;
4990 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4991 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4992 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4995 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4997 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4998 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4999 struct glsl_sample_function sample_function;
5000 DWORD reg = ins->dst[0].reg.idx[0].offset;
5001 struct glsl_src_param src0_param;
5003 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5004 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5006 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5008 /* Sample the texture using the calculated coordinates */
5009 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
5010 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5013 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5014 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5015 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
5017 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5018 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5019 struct glsl_sample_function sample_function;
5020 DWORD reg = ins->dst[0].reg.idx[0].offset;
5021 struct glsl_src_param src0_param;
5023 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5024 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5026 /* Dependent read, not valid with conditional NP2 */
5027 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5029 /* Sample the texture using the calculated coordinates */
5030 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
5031 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5033 tex_mx->current_row = 0;
5036 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5037 * Perform the 3rd row of a 3x3 matrix multiply */
5038 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
5040 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5041 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5042 DWORD reg = ins->dst[0].reg.idx[0].offset;
5043 struct glsl_src_param src0_param;
5044 char dst_mask[6];
5046 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5048 shader_glsl_append_dst(ins->ctx->buffer, ins);
5049 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5050 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5052 tex_mx->current_row = 0;
5055 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5056 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5057 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5059 struct glsl_src_param src0_param;
5060 struct glsl_src_param src1_param;
5061 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5062 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5063 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5064 struct glsl_sample_function sample_function;
5065 DWORD reg = ins->dst[0].reg.idx[0].offset;
5066 char coord_mask[6];
5068 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5069 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5071 /* Perform the last matrix multiply operation */
5072 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5073 /* Reflection calculation */
5074 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5076 /* Dependent read, not valid with conditional NP2 */
5077 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5078 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5080 /* Sample the texture */
5081 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5082 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5083 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5085 tex_mx->current_row = 0;
5088 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5089 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5090 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5092 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5093 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5094 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5095 struct glsl_sample_function sample_function;
5096 DWORD reg = ins->dst[0].reg.idx[0].offset;
5097 struct glsl_src_param src0_param;
5098 char coord_mask[6];
5100 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5102 /* Perform the last matrix multiply operation */
5103 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
5105 /* Construct the eye-ray vector from w coordinates */
5106 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5107 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
5108 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5110 /* Dependent read, not valid with conditional NP2 */
5111 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5112 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5114 /* Sample the texture using the calculated coordinates */
5115 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5116 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5117 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5119 tex_mx->current_row = 0;
5122 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5123 * Apply a fake bump map transform.
5124 * texbem is pshader <= 1.3 only, this saves a few version checks
5126 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
5128 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5129 struct glsl_sample_function sample_function;
5130 struct glsl_src_param coord_param;
5131 DWORD sampler_idx;
5132 DWORD mask;
5133 DWORD flags;
5134 char coord_mask[6];
5136 sampler_idx = ins->dst[0].reg.idx[0].offset;
5137 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5138 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5140 /* Dependent read, not valid with conditional NP2 */
5141 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5142 mask = sample_function.coord_mask;
5144 shader_glsl_write_mask_to_str(mask, coord_mask);
5146 /* With projected textures, texbem only divides the static texture coord,
5147 * not the displacement, so we can't let GL handle this. */
5148 if (flags & WINED3D_PSARGS_PROJECTED)
5150 DWORD div_mask=0;
5151 char coord_div_mask[3];
5152 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5154 case WINED3D_TTFF_COUNT1:
5155 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5156 break;
5157 case WINED3D_TTFF_COUNT2:
5158 div_mask = WINED3DSP_WRITEMASK_1;
5159 break;
5160 case WINED3D_TTFF_COUNT3:
5161 div_mask = WINED3DSP_WRITEMASK_2;
5162 break;
5163 case WINED3D_TTFF_COUNT4:
5164 case WINED3D_TTFF_DISABLE:
5165 div_mask = WINED3DSP_WRITEMASK_3;
5166 break;
5168 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
5169 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
5172 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
5174 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5175 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
5176 coord_param.param_str, coord_mask);
5178 if (ins->handler_idx == WINED3DSIH_TEXBEML)
5180 struct glsl_src_param luminance_param;
5181 struct glsl_dst_param dst_param;
5183 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
5184 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5186 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
5187 dst_param.reg_name, dst_param.mask_str,
5188 luminance_param.param_str, sampler_idx, sampler_idx);
5190 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5193 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
5195 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5196 struct glsl_src_param src0_param, src1_param;
5198 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5199 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5201 shader_glsl_append_dst(ins->ctx->buffer, ins);
5202 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
5203 src0_param.param_str, sampler_idx, src1_param.param_str);
5206 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5207 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5208 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
5210 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5211 struct glsl_sample_function sample_function;
5212 struct glsl_src_param src0_param;
5214 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5216 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5217 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5218 "%s.wx", src0_param.reg_name);
5219 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5222 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5223 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5224 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
5226 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5227 struct glsl_sample_function sample_function;
5228 struct glsl_src_param src0_param;
5230 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5232 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5233 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5234 "%s.yz", src0_param.reg_name);
5235 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5238 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5239 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5240 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
5242 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5243 struct glsl_sample_function sample_function;
5244 struct glsl_src_param src0_param;
5246 /* Dependent read, not valid with conditional NP2 */
5247 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5248 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
5250 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5251 "%s", src0_param.param_str);
5252 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5255 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5256 * If any of the first 3 components are < 0, discard this pixel */
5257 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
5259 if (ins->ctx->reg_maps->shader_version.major >= 4)
5261 struct glsl_src_param src_param;
5263 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5264 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
5266 else
5268 struct glsl_dst_param dst_param;
5270 /* The argument is a destination parameter, and no writemasks are allowed */
5271 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5273 /* 2.0 shaders compare all 4 components in texkill. */
5274 if (ins->ctx->reg_maps->shader_version.major >= 2)
5275 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
5276 /* 1.x shaders only compare the first 3 components, probably due to
5277 * the nature of the texkill instruction as a tex* instruction, and
5278 * phase, which kills all .w components. Even if all 4 components are
5279 * defined, only the first 3 are used. */
5280 else
5281 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
5285 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5286 * dst = dot2(src0, src1) + src2 */
5287 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5289 struct glsl_src_param src0_param;
5290 struct glsl_src_param src1_param;
5291 struct glsl_src_param src2_param;
5292 DWORD write_mask;
5293 unsigned int mask_size;
5295 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5296 mask_size = shader_glsl_get_write_mask_size(write_mask);
5298 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5299 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5300 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5302 if (mask_size > 1) {
5303 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5304 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5305 } else {
5306 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5307 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5311 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5312 const struct wined3d_shader_signature *input_signature,
5313 const struct wined3d_shader_reg_maps *reg_maps,
5314 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5316 unsigned int i;
5318 for (i = 0; i < input_signature->element_count; ++i)
5320 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5321 const char *semantic_name;
5322 UINT semantic_idx;
5323 char reg_mask[6];
5325 /* Unused */
5326 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5327 continue;
5329 semantic_name = input->semantic_name;
5330 semantic_idx = input->semantic_idx;
5331 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5333 if (args->vp_mode == vertexshader)
5335 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5337 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5338 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5340 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5342 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5344 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
5346 shader_addline(buffer, "ps_in[%u] = vec4("
5347 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
5348 input->register_idx);
5350 else
5352 if (input->sysval_semantic)
5353 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
5354 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5355 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5356 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5359 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5361 if (args->pointsprite)
5362 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5363 shader->u.ps.input_reg_map[input->register_idx]);
5364 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5365 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5366 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5367 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5368 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5369 else
5370 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5371 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5373 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5375 if (!semantic_idx)
5376 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5377 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5378 else if (semantic_idx == 1)
5379 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5380 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5381 else
5382 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5383 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5385 else
5387 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5388 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5393 /*********************************************
5394 * Vertex Shader Specific Code begins here
5395 ********************************************/
5397 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5399 struct glsl_program_key key;
5401 key.vs_id = entry->vs.id;
5402 key.gs_id = entry->gs.id;
5403 key.ps_id = entry->ps.id;
5405 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5407 ERR("Failed to insert program entry.\n");
5411 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5412 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5414 struct wine_rb_entry *entry;
5415 struct glsl_program_key key;
5417 key.vs_id = vs_id;
5418 key.gs_id = gs_id;
5419 key.ps_id = ps_id;
5421 entry = wine_rb_get(&priv->program_lookup, &key);
5422 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5425 /* Context activation is done by the caller. */
5426 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5427 struct glsl_shader_prog_link *entry)
5429 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
5431 GL_EXTCALL(glDeleteProgram(entry->id));
5432 if (entry->vs.id)
5433 list_remove(&entry->vs.shader_entry);
5434 if (entry->gs.id)
5435 list_remove(&entry->gs.shader_entry);
5436 if (entry->ps.id)
5437 list_remove(&entry->ps.shader_entry);
5438 HeapFree(GetProcessHeap(), 0, entry);
5441 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
5442 const struct wined3d_gl_info *gl_info, const DWORD *map,
5443 const struct wined3d_shader_signature *input_signature,
5444 const struct wined3d_shader_reg_maps *reg_maps_in,
5445 const struct wined3d_shader_signature *output_signature,
5446 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5448 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5449 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5450 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5451 unsigned int in_count = vec4_varyings(3, gl_info);
5452 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5453 DWORD in_idx, *set = NULL;
5454 unsigned int i, j;
5455 char reg_mask[6];
5457 set = wined3d_calloc(max_varyings, sizeof(*set));
5459 for (i = 0; i < input_signature->element_count; ++i)
5461 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5463 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5464 continue;
5466 in_idx = map[input->register_idx];
5467 /* Declared, but not read register */
5468 if (in_idx == ~0u)
5469 continue;
5470 if (in_idx >= max_varyings)
5472 FIXME("More input varyings declared than supported, expect issues.\n");
5473 continue;
5476 if (in_idx == in_count)
5477 string_buffer_sprintf(destination, "gl_FrontColor");
5478 else if (in_idx == in_count + 1)
5479 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5480 else
5481 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
5483 if (!set[in_idx])
5484 set[in_idx] = ~0u;
5486 for (j = 0; j < output_signature->element_count; ++j)
5488 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5489 DWORD mask;
5491 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5492 || input->semantic_idx != output->semantic_idx
5493 || strcmp(input->semantic_name, output->semantic_name)
5494 || !(mask = input->mask & output->mask))
5495 continue;
5497 if (set[in_idx] == ~0u)
5498 set[in_idx] = 0;
5499 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5500 shader_glsl_write_mask_to_str(mask, reg_mask);
5502 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5503 destination->buffer, reg_mask, output->register_idx, reg_mask);
5507 for (i = 0; i < max_varyings; ++i)
5509 unsigned int size;
5511 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5512 continue;
5514 if (set[i] == ~0u)
5515 set[i] = 0;
5517 size = 0;
5518 if (!(set[i] & WINED3DSP_WRITEMASK_0))
5519 reg_mask[size++] = 'x';
5520 if (!(set[i] & WINED3DSP_WRITEMASK_1))
5521 reg_mask[size++] = 'y';
5522 if (!(set[i] & WINED3DSP_WRITEMASK_2))
5523 reg_mask[size++] = 'z';
5524 if (!(set[i] & WINED3DSP_WRITEMASK_3))
5525 reg_mask[size++] = 'w';
5526 reg_mask[size] = '\0';
5528 if (i == in_count)
5529 string_buffer_sprintf(destination, "gl_FrontColor");
5530 else if (i == in_count + 1)
5531 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5532 else
5533 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
5535 if (size == 1)
5536 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5537 else
5538 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5541 HeapFree(GetProcessHeap(), 0, set);
5542 string_buffer_release(&priv->string_buffers, destination);
5545 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
5546 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
5547 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5549 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5550 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5551 char reg_mask[6];
5552 unsigned int i;
5554 for (i = 0; i < output_signature->element_count; ++i)
5556 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5558 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5559 continue;
5561 if (output->register_idx >= input_count)
5562 continue;
5564 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
5566 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5568 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5569 destination->buffer, reg_mask, output->register_idx, reg_mask);
5572 string_buffer_release(&priv->string_buffers, destination);
5575 /* Context activation is done by the caller. */
5576 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
5577 const struct wined3d_shader *vs, unsigned int input_count,
5578 const struct wined3d_gl_info *gl_info)
5580 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5581 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5583 if (legacy_context)
5584 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
5585 else
5586 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
5587 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5589 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
5590 legacy_context ? "gs_in" : "gs_in.gs_in");
5592 shader_addline(buffer, "}\n");
5595 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
5596 const struct wined3d_gl_info *gl_info, const DWORD *map,
5597 const struct wined3d_shader_signature *input_signature,
5598 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
5599 const struct wined3d_shader_signature *output_signature,
5600 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
5602 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5603 const char *semantic_name;
5604 UINT semantic_idx;
5605 char reg_mask[6];
5606 unsigned int i;
5608 /* First, sort out position and point size system values. */
5609 for (i = 0; i < output_signature->element_count; ++i)
5611 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5613 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5614 continue;
5616 semantic_name = output->semantic_name;
5617 semantic_idx = output->semantic_idx;
5618 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5620 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5622 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5623 reg_mask, output->register_idx, reg_mask);
5625 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5627 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5628 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5630 else if (output->sysval_semantic)
5632 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
5636 /* Then, setup the pixel shader input. */
5637 if (reg_maps_out->shader_version.major < 4)
5638 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
5639 output_signature, reg_maps_out, "ps_link");
5640 else
5641 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
5644 /* Context activation is done by the caller. */
5645 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
5646 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5647 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5649 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5650 GLuint ret;
5651 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5652 unsigned int i;
5653 const char *semantic_name;
5654 UINT semantic_idx;
5655 char reg_mask[6];
5656 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5658 string_buffer_clear(buffer);
5660 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &vs->reg_maps.shader_version));
5662 if (per_vertex_point_size)
5664 shader_addline(buffer, "uniform struct\n{\n");
5665 shader_addline(buffer, " float size_min;\n");
5666 shader_addline(buffer, " float size_max;\n");
5667 shader_addline(buffer, "} ffp_point;\n");
5670 if (ps_major < 3)
5672 DWORD colors_written_mask[2] = {0};
5673 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5675 if (!legacy_context)
5677 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5678 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5679 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5680 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5683 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5685 for (i = 0; i < vs->output_signature.element_count; ++i)
5687 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5688 DWORD write_mask;
5690 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5691 continue;
5693 semantic_name = output->semantic_name;
5694 semantic_idx = output->semantic_idx;
5695 write_mask = output->mask;
5696 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5698 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5700 if (legacy_context)
5701 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
5702 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5703 else
5704 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
5705 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5707 colors_written_mask[semantic_idx] = write_mask;
5709 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5711 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5712 reg_mask, output->register_idx, reg_mask);
5714 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5716 if (semantic_idx < MAX_TEXTURES)
5718 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
5719 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5720 semantic_idx, reg_mask, output->register_idx, reg_mask);
5721 texcoords_written_mask[semantic_idx] = write_mask;
5724 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5726 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5727 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5729 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5731 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
5732 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5733 output->register_idx, reg_mask[1]);
5737 for (i = 0; i < 2; ++i)
5739 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5741 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5742 if (!i)
5743 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5744 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5745 reg_mask, reg_mask);
5746 else
5747 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5748 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5749 reg_mask, reg_mask);
5752 for (i = 0; i < MAX_TEXTURES; ++i)
5754 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5755 continue;
5757 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5759 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5760 && !texcoords_written_mask[i])
5761 continue;
5763 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5764 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5765 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5769 else
5771 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5773 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5774 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5775 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5776 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
5779 shader_addline(buffer, "}\n");
5781 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5782 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5783 shader_glsl_compile(gl_info, ret, buffer->buffer);
5785 return ret;
5788 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
5789 const struct wined3d_shader *shader, unsigned int input_count,
5790 const struct wined3d_gl_info *gl_info)
5792 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5794 if (input_count)
5795 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
5797 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
5798 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
5800 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
5801 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
5803 shader_addline(buffer, "}\n");
5806 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
5807 const struct wined3d_gl_info *gl_info)
5809 const char *output = get_fragment_output(gl_info);
5811 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
5812 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5813 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
5814 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
5815 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
5816 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
5819 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
5820 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
5822 const char *output = get_fragment_output(gl_info);
5824 switch (mode)
5826 case WINED3D_FFP_PS_FOG_OFF:
5827 return;
5829 case WINED3D_FFP_PS_FOG_LINEAR:
5830 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5831 break;
5833 case WINED3D_FFP_PS_FOG_EXP:
5834 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5835 break;
5837 case WINED3D_FFP_PS_FOG_EXP2:
5838 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5839 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5840 break;
5842 default:
5843 ERR("Invalid fog mode %#x.\n", mode);
5844 return;
5847 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
5848 output, output);
5851 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
5852 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
5854 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
5855 * flipping all the operators here, just negate the comparison below. */
5856 static const char * const comparison_operator[] =
5858 "", /* WINED3D_CMP_NEVER */
5859 "<", /* WINED3D_CMP_LESS */
5860 "==", /* WINED3D_CMP_EQUAL */
5861 "<=", /* WINED3D_CMP_LESSEQUAL */
5862 ">", /* WINED3D_CMP_GREATER */
5863 "!=", /* WINED3D_CMP_NOTEQUAL */
5864 ">=", /* WINED3D_CMP_GREATEREQUAL */
5865 "" /* WINED3D_CMP_ALWAYS */
5868 if (alpha_func == WINED3D_CMP_ALWAYS)
5869 return;
5871 if (alpha_func != WINED3D_CMP_NEVER)
5872 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
5873 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
5874 shader_addline(buffer, " discard;\n");
5877 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
5878 const struct wined3d_gl_info *gl_info)
5880 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5881 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5882 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5883 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5884 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5885 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5886 if (gl_info->supported[EXT_GPU_SHADER4])
5887 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5888 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5889 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5892 /* Context activation is done by the caller. */
5893 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5894 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5895 const struct wined3d_shader *shader,
5896 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5898 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5899 const struct wined3d_gl_info *gl_info = context->gl_info;
5900 const DWORD *function = shader->function;
5901 struct shader_glsl_ctx_priv priv_ctx;
5902 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5904 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5905 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5907 memset(&priv_ctx, 0, sizeof(priv_ctx));
5908 priv_ctx.cur_ps_args = args;
5909 priv_ctx.cur_np2fixup_info = np2fixup_info;
5910 priv_ctx.string_buffers = string_buffers;
5912 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
5914 shader_glsl_enable_extensions(buffer, gl_info);
5915 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
5916 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
5917 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
5918 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
5919 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5920 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5921 /* The spec says that it doesn't have to be explicitly enabled, but the
5922 * nvidia drivers write a warning if we don't do so. */
5923 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5924 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5926 /* Base Declarations */
5927 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5929 shader_addline(buffer, "void main()\n{\n");
5931 /* Direct3D applications expect integer vPos values, while OpenGL drivers
5932 * add approximately 0.5. This causes off-by-one problems as spotted by
5933 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
5934 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
5935 * causes precision troubles when we just subtract 0.5.
5937 * To deal with that, just floor() the position. This will eliminate the
5938 * fraction on all cards.
5940 * TODO: Test how this behaves with multisampling.
5942 * An advantage of floor is that it works even if the driver doesn't add
5943 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
5944 * to return in gl_FragCoord, even though coordinates specify the pixel
5945 * centers instead of the pixel corners. This code will behave correctly
5946 * on drivers that returns integer values. */
5947 if (reg_maps->vpos)
5949 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
5950 shader_addline(buffer, "vpos = gl_FragCoord;\n");
5951 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
5952 shader_addline(buffer,
5953 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
5954 else
5955 shader_addline(buffer,
5956 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
5959 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5961 unsigned int i;
5962 WORD map = reg_maps->texcoord;
5964 if (legacy_context)
5966 if (glsl_is_color_reg_read(shader, 0))
5967 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5968 if (glsl_is_color_reg_read(shader, 1))
5969 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5972 for (i = 0; map; map >>= 1, ++i)
5974 if (map & 1)
5976 if (args->pointsprite)
5977 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5978 else if (args->texcoords_initialized & (1u << i))
5979 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5980 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5981 else
5982 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5983 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5987 if (legacy_context)
5988 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5991 /* Pack 3.0 inputs */
5992 if (reg_maps->shader_version.major >= 3)
5993 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5995 /* Base Shader Body */
5996 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5998 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5999 if (reg_maps->shader_version.major < 2)
6000 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
6002 if (args->srgb_correction)
6003 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6005 /* SM < 3 does not replace the fog stage. */
6006 if (reg_maps->shader_version.major < 3)
6007 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
6009 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
6011 shader_addline(buffer, "}\n");
6013 TRACE("Compiling shader object %u.\n", shader_id);
6014 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6016 return shader_id;
6019 /* Context activation is done by the caller. */
6020 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
6021 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
6023 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6024 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6025 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6026 const struct wined3d_gl_info *gl_info = context->gl_info;
6027 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6028 const DWORD *function = shader->function;
6029 struct shader_glsl_ctx_priv priv_ctx;
6030 unsigned int i;
6032 /* Create the hw GLSL shader program and assign it as the shader->prgId */
6033 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6035 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6037 shader_glsl_enable_extensions(buffer, gl_info);
6038 if (gl_info->supported[ARB_DRAW_INSTANCED])
6039 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
6040 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6041 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6043 memset(&priv_ctx, 0, sizeof(priv_ctx));
6044 priv_ctx.cur_vs_args = args;
6045 priv_ctx.string_buffers = string_buffers;
6047 /* Base Declarations */
6048 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6050 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6051 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6053 if (reg_maps->shader_version.major >= 4)
6055 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6056 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
6057 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
6058 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
6061 shader_addline(buffer, "void main()\n{\n");
6063 /* Base Shader Body */
6064 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6066 /* Unpack outputs */
6067 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6069 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6070 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6071 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6072 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
6074 if (reg_maps->shader_version.major < 3)
6076 if (args->fog_src == VS_FOG_Z)
6077 shader_addline(buffer, "%s = gl_Position.z;\n",
6078 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6079 else if (!reg_maps->fog)
6080 shader_addline(buffer, "%s = 0.0;\n",
6081 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6084 /* We always store the clipplanes without y inversion */
6085 if (args->clip_enabled)
6087 if (legacy_context)
6088 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6089 else
6090 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6091 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6094 if (args->point_size && !args->per_vertex_point_size)
6095 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6097 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6098 shader_glsl_fixup_position(buffer);
6100 shader_addline(buffer, "}\n");
6102 TRACE("Compiling shader object %u.\n", shader_id);
6103 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6105 return shader_id;
6108 /* Context activation is done by the caller. */
6109 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
6110 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
6112 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6113 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6114 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6115 const struct wined3d_gl_info *gl_info = context->gl_info;
6116 const DWORD *function = shader->function;
6117 struct shader_glsl_ctx_priv priv_ctx;
6118 GLuint shader_id;
6120 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
6122 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6124 shader_glsl_enable_extensions(buffer, gl_info);
6125 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
6126 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
6128 memset(&priv_ctx, 0, sizeof(priv_ctx));
6129 priv_ctx.string_buffers = string_buffers;
6130 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6131 if (!gl_info->supported[ARB_CLIP_CONTROL])
6132 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6133 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
6134 shader_addline(buffer, "void main()\n{\n");
6135 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6136 shader_addline(buffer, "}\n");
6138 TRACE("Compiling shader object %u.\n", shader_id);
6139 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6141 return shader_id;
6144 static GLuint find_glsl_pshader(const struct wined3d_context *context,
6145 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6146 struct wined3d_shader *shader,
6147 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
6149 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
6150 struct glsl_shader_private *shader_data;
6151 struct ps_np2fixup_info *np2fixup;
6152 UINT i;
6153 DWORD new_size;
6154 GLuint ret;
6156 if (!shader->backend_data)
6158 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6159 if (!shader->backend_data)
6161 ERR("Failed to allocate backend data.\n");
6162 return 0;
6165 shader_data = shader->backend_data;
6166 gl_shaders = shader_data->gl_shaders.ps;
6168 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6169 * so a linear search is more performant than a hashmap or a binary search
6170 * (cache coherency etc)
6172 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6174 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6176 if (args->np2_fixup)
6177 *np2fixup_info = &gl_shaders[i].np2fixup;
6178 return gl_shaders[i].id;
6182 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6183 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6184 if (shader_data->num_gl_shaders)
6186 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6187 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
6188 new_size * sizeof(*gl_shaders));
6190 else
6192 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6193 new_size = 1;
6196 if(!new_array) {
6197 ERR("Out of memory\n");
6198 return 0;
6200 shader_data->gl_shaders.ps = new_array;
6201 shader_data->shader_array_size = new_size;
6202 gl_shaders = new_array;
6205 gl_shaders[shader_data->num_gl_shaders].args = *args;
6207 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
6208 memset(np2fixup, 0, sizeof(*np2fixup));
6209 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
6211 pixelshader_update_resource_types(shader, args->tex_types);
6213 string_buffer_clear(buffer);
6214 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
6215 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6217 return ret;
6220 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
6221 const DWORD use_map)
6223 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
6224 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
6225 if (stored->point_size != new->point_size)
6226 return FALSE;
6227 if (stored->per_vertex_point_size != new->per_vertex_point_size)
6228 return FALSE;
6229 if (stored->flatshading != new->flatshading)
6230 return FALSE;
6231 if (stored->next_shader_type != new->next_shader_type)
6232 return FALSE;
6233 if (stored->next_shader_input_count != new->next_shader_input_count)
6234 return FALSE;
6235 return stored->fog_src == new->fog_src;
6238 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
6239 struct wined3d_shader *shader, const struct vs_compile_args *args)
6241 UINT i;
6242 DWORD new_size;
6243 DWORD use_map = context->stream_info.use_map;
6244 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
6245 struct glsl_shader_private *shader_data;
6246 GLuint ret;
6248 if (!shader->backend_data)
6250 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6251 if (!shader->backend_data)
6253 ERR("Failed to allocate backend data.\n");
6254 return 0;
6257 shader_data = shader->backend_data;
6258 gl_shaders = shader_data->gl_shaders.vs;
6260 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6261 * so a linear search is more performant than a hashmap or a binary search
6262 * (cache coherency etc)
6264 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6266 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
6267 return gl_shaders[i].id;
6270 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6272 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6273 if (shader_data->num_gl_shaders)
6275 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6276 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
6277 new_size * sizeof(*gl_shaders));
6279 else
6281 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6282 new_size = 1;
6285 if(!new_array) {
6286 ERR("Out of memory\n");
6287 return 0;
6289 shader_data->gl_shaders.vs = new_array;
6290 shader_data->shader_array_size = new_size;
6291 gl_shaders = new_array;
6294 gl_shaders[shader_data->num_gl_shaders].args = *args;
6296 string_buffer_clear(&priv->shader_buffer);
6297 ret = shader_glsl_generate_vshader(context, priv, shader, args);
6298 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6300 return ret;
6303 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
6304 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
6306 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
6307 struct glsl_shader_private *shader_data;
6308 unsigned int i, new_size;
6309 GLuint ret;
6311 if (!shader->backend_data)
6313 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6315 ERR("Failed to allocate backend data.\n");
6316 return 0;
6319 shader_data = shader->backend_data;
6320 gl_shaders = shader_data->gl_shaders.gs;
6322 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6324 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6325 return gl_shaders[i].id;
6328 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6330 if (shader_data->num_gl_shaders)
6332 new_size = shader_data->shader_array_size + 1;
6333 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
6334 new_size * sizeof(*new_array));
6336 else
6338 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
6339 new_size = 1;
6342 if (!new_array)
6344 ERR("Failed to allocate GL shaders array.\n");
6345 return 0;
6347 shader_data->gl_shaders.gs = new_array;
6348 shader_data->shader_array_size = new_size;
6349 gl_shaders = new_array;
6351 string_buffer_clear(&priv->shader_buffer);
6352 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
6353 gl_shaders[shader_data->num_gl_shaders].args = *args;
6354 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6356 return ret;
6359 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
6361 switch (mcs)
6363 case WINED3D_MCS_MATERIAL:
6364 return material;
6365 case WINED3D_MCS_COLOR1:
6366 return "ffp_attrib_diffuse";
6367 case WINED3D_MCS_COLOR2:
6368 return "ffp_attrib_specular";
6369 default:
6370 ERR("Invalid material color source %#x.\n", mcs);
6371 return "<invalid>";
6375 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
6376 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
6378 const char *diffuse, *specular, *emissive, *ambient;
6379 enum wined3d_light_type light_type;
6380 unsigned int i;
6382 if (!settings->lighting)
6384 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6385 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
6386 return;
6389 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
6390 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
6391 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
6392 shader_addline(buffer, "vec3 dir, dst;\n");
6393 shader_addline(buffer, "float att, t;\n");
6395 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
6396 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
6397 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
6398 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
6400 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6402 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
6403 switch (light_type)
6405 case WINED3D_LIGHT_POINT:
6406 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6407 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6408 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6409 shader_addline(buffer, "dst.x = 1.0;\n");
6410 if (legacy_lighting)
6412 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6413 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6415 else
6417 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6419 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6420 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
6421 if (!legacy_lighting)
6422 shader_addline(buffer, "att = 1.0 / att;\n");
6423 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6424 if (!settings->normal)
6426 if (!legacy_lighting)
6427 shader_addline(buffer, "}\n");
6428 break;
6430 shader_addline(buffer, "dir = normalize(dir);\n");
6431 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6432 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6433 if (settings->localviewer)
6434 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6435 else
6436 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6437 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6438 " * ffp_light[%u].specular) * att;\n", i);
6439 if (!legacy_lighting)
6440 shader_addline(buffer, "}\n");
6441 break;
6443 case WINED3D_LIGHT_SPOT:
6444 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6445 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6446 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6447 shader_addline(buffer, "dst.x = 1.0;\n");
6448 if (legacy_lighting)
6450 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6451 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6453 else
6455 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6457 shader_addline(buffer, "dir = normalize(dir);\n");
6458 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
6459 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
6460 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
6461 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
6462 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
6463 i, i, i, i);
6464 if (legacy_lighting)
6465 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6466 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6467 i, i, i);
6468 else
6469 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6470 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6471 i, i, i);
6472 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6473 if (!settings->normal)
6475 if (!legacy_lighting)
6476 shader_addline(buffer, "}\n");
6477 break;
6479 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6480 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6481 if (settings->localviewer)
6482 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6483 else
6484 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6485 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6486 " * ffp_light[%u].specular) * att;\n", i);
6487 if (!legacy_lighting)
6488 shader_addline(buffer, "}\n");
6489 break;
6491 case WINED3D_LIGHT_DIRECTIONAL:
6492 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6493 if (!settings->normal)
6494 break;
6495 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
6496 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6497 " * ffp_light[%u].diffuse.xyz;\n", i);
6498 /* TODO: In the non-local viewer case the halfvector is constant
6499 * and could be precomputed and stored in a uniform. */
6500 if (settings->localviewer)
6501 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6502 else
6503 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6504 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6505 " * ffp_light[%u].specular;\n", i);
6506 break;
6508 case WINED3D_LIGHT_PARALLELPOINT:
6509 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6510 if (!settings->normal)
6511 break;
6512 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
6513 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6514 " * ffp_light[%u].diffuse.xyz;\n", i);
6515 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6516 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6517 " * ffp_light[%u].specular;\n", i);
6518 break;
6520 default:
6521 if (light_type)
6522 FIXME("Unhandled light type %#x.\n", light_type);
6523 continue;
6527 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6528 ambient, diffuse, emissive);
6529 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6530 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6533 /* Context activation is done by the caller. */
6534 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6535 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6537 static const struct attrib_info
6539 const char type[6];
6540 const char name[24];
6542 attrib_info[] =
6544 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6545 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6546 /* TODO: Indexed vertex blending */
6547 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6548 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6549 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6550 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6551 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6553 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6554 BOOL legacy_lighting = priv->legacy_lighting;
6555 GLuint shader_obj;
6556 unsigned int i;
6557 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6558 BOOL output_legacy_fogcoord = legacy_context;
6560 string_buffer_clear(buffer);
6562 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, NULL));
6564 if (shader_glsl_use_explicit_attrib_location(gl_info))
6565 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6567 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6569 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6571 if (shader_glsl_use_explicit_attrib_location(gl_info))
6572 shader_addline(buffer, "layout(location = %u) ", i);
6573 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
6575 shader_addline(buffer, "\n");
6577 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6578 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6579 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6580 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6582 shader_addline(buffer, "uniform struct\n{\n");
6583 shader_addline(buffer, " vec4 emissive;\n");
6584 shader_addline(buffer, " vec4 ambient;\n");
6585 shader_addline(buffer, " vec4 diffuse;\n");
6586 shader_addline(buffer, " vec4 specular;\n");
6587 shader_addline(buffer, " float shininess;\n");
6588 shader_addline(buffer, "} ffp_material;\n");
6590 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6591 shader_addline(buffer, "uniform struct\n{\n");
6592 shader_addline(buffer, " vec4 diffuse;\n");
6593 shader_addline(buffer, " vec4 specular;\n");
6594 shader_addline(buffer, " vec4 ambient;\n");
6595 shader_addline(buffer, " vec4 position;\n");
6596 shader_addline(buffer, " vec3 direction;\n");
6597 shader_addline(buffer, " float range;\n");
6598 shader_addline(buffer, " float falloff;\n");
6599 shader_addline(buffer, " float c_att;\n");
6600 shader_addline(buffer, " float l_att;\n");
6601 shader_addline(buffer, " float q_att;\n");
6602 shader_addline(buffer, " float cos_htheta;\n");
6603 shader_addline(buffer, " float cos_hphi;\n");
6604 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6606 if (settings->point_size)
6608 shader_addline(buffer, "uniform struct\n{\n");
6609 shader_addline(buffer, " float size;\n");
6610 shader_addline(buffer, " float size_min;\n");
6611 shader_addline(buffer, " float size_max;\n");
6612 shader_addline(buffer, " float c_att;\n");
6613 shader_addline(buffer, " float l_att;\n");
6614 shader_addline(buffer, " float q_att;\n");
6615 shader_addline(buffer, "} ffp_point;\n");
6618 if (legacy_context)
6620 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6621 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6622 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6623 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6625 else
6627 if (settings->clipping)
6628 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
6630 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6631 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6632 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6633 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6636 shader_addline(buffer, "\nvoid main()\n{\n");
6637 shader_addline(buffer, "float m;\n");
6638 shader_addline(buffer, "vec3 r;\n");
6640 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6642 if (attrib_info[i].name[0])
6643 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
6644 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
6646 for (i = 0; i < MAX_TEXTURES; ++i)
6648 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6649 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6650 && settings->texcoords & (1u << i))
6651 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6654 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6656 if (settings->transformed)
6658 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6659 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6660 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6662 else
6664 for (i = 0; i < settings->vertexblends; ++i)
6665 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6667 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6668 for (i = 0; i < settings->vertexblends + 1; ++i)
6669 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6671 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6672 if (settings->clipping)
6674 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
6675 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6676 else
6677 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6678 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
6680 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6683 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6684 if (settings->normal)
6686 if (!settings->vertexblends)
6688 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6690 else
6692 for (i = 0; i < settings->vertexblends + 1; ++i)
6693 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6696 if (settings->normalize)
6697 shader_addline(buffer, "normal = normalize(normal);\n");
6700 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6701 if (legacy_context)
6703 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6704 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6706 else
6708 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
6709 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
6712 for (i = 0; i < MAX_TEXTURES; ++i)
6714 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6716 switch (settings->texgen[i] & 0xffff0000)
6718 case WINED3DTSS_TCI_PASSTHRU:
6719 if (settings->texcoords & (1u << i))
6720 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6721 i, i, i);
6722 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6723 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6724 else
6725 output_legacy_texcoord = FALSE;
6726 break;
6728 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6729 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6730 break;
6732 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6733 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6734 break;
6736 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6737 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6738 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6739 break;
6741 case WINED3DTSS_TCI_SPHEREMAP:
6742 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6743 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6744 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6745 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6746 break;
6748 default:
6749 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6750 break;
6752 if (output_legacy_texcoord)
6753 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6756 switch (settings->fog_mode)
6758 case WINED3D_FFP_VS_FOG_OFF:
6759 output_legacy_fogcoord = FALSE;
6760 break;
6762 case WINED3D_FFP_VS_FOG_FOGCOORD:
6763 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6764 break;
6766 case WINED3D_FFP_VS_FOG_RANGE:
6767 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6768 break;
6770 case WINED3D_FFP_VS_FOG_DEPTH:
6771 if (settings->ortho_fog)
6773 if (gl_info->supported[ARB_CLIP_CONTROL])
6774 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
6775 else
6776 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6777 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6779 else if (settings->transformed)
6781 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6783 else
6785 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6787 break;
6789 default:
6790 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6791 break;
6793 if (output_legacy_fogcoord)
6794 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6796 if (settings->point_size)
6798 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6799 " + ffp_point.l_att * length(ec_pos.xyz)"
6800 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6801 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6802 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6805 shader_addline(buffer, "}\n");
6807 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6808 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6810 return shader_obj;
6813 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6814 DWORD argnum, unsigned int stage, DWORD arg)
6816 const char *ret;
6818 if (arg == ARG_UNUSED)
6819 return "<unused arg>";
6821 switch (arg & WINED3DTA_SELECTMASK)
6823 case WINED3DTA_DIFFUSE:
6824 ret = "ffp_varying_diffuse";
6825 break;
6827 case WINED3DTA_CURRENT:
6828 ret = "ret";
6829 break;
6831 case WINED3DTA_TEXTURE:
6832 switch (stage)
6834 case 0: ret = "tex0"; break;
6835 case 1: ret = "tex1"; break;
6836 case 2: ret = "tex2"; break;
6837 case 3: ret = "tex3"; break;
6838 case 4: ret = "tex4"; break;
6839 case 5: ret = "tex5"; break;
6840 case 6: ret = "tex6"; break;
6841 case 7: ret = "tex7"; break;
6842 default:
6843 ret = "<invalid texture>";
6844 break;
6846 break;
6848 case WINED3DTA_TFACTOR:
6849 ret = "tex_factor";
6850 break;
6852 case WINED3DTA_SPECULAR:
6853 ret = "ffp_varying_specular";
6854 break;
6856 case WINED3DTA_TEMP:
6857 ret = "temp_reg";
6858 break;
6860 case WINED3DTA_CONSTANT:
6861 switch (stage)
6863 case 0: ret = "tss_const0"; break;
6864 case 1: ret = "tss_const1"; break;
6865 case 2: ret = "tss_const2"; break;
6866 case 3: ret = "tss_const3"; break;
6867 case 4: ret = "tss_const4"; break;
6868 case 5: ret = "tss_const5"; break;
6869 case 6: ret = "tss_const6"; break;
6870 case 7: ret = "tss_const7"; break;
6871 default:
6872 ret = "<invalid constant>";
6873 break;
6875 break;
6877 default:
6878 return "<unhandled arg>";
6881 if (arg & WINED3DTA_COMPLEMENT)
6883 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6884 if (argnum == 0)
6885 ret = "arg0";
6886 else if (argnum == 1)
6887 ret = "arg1";
6888 else if (argnum == 2)
6889 ret = "arg2";
6892 if (arg & WINED3DTA_ALPHAREPLICATE)
6894 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6895 if (argnum == 0)
6896 ret = "arg0";
6897 else if (argnum == 1)
6898 ret = "arg1";
6899 else if (argnum == 2)
6900 ret = "arg2";
6903 return ret;
6906 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6907 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6909 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6911 if (color && alpha)
6912 dstmask = "";
6913 else if (color)
6914 dstmask = ".xyz";
6915 else
6916 dstmask = ".w";
6918 if (dst == tempreg)
6919 dstreg = "temp_reg";
6920 else
6921 dstreg = "ret";
6923 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6924 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6925 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6927 switch (op)
6929 case WINED3D_TOP_DISABLE:
6930 break;
6932 case WINED3D_TOP_SELECT_ARG1:
6933 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6934 break;
6936 case WINED3D_TOP_SELECT_ARG2:
6937 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6938 break;
6940 case WINED3D_TOP_MODULATE:
6941 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6942 break;
6944 case WINED3D_TOP_MODULATE_4X:
6945 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6946 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6947 break;
6949 case WINED3D_TOP_MODULATE_2X:
6950 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6951 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6952 break;
6954 case WINED3D_TOP_ADD:
6955 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6956 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6957 break;
6959 case WINED3D_TOP_ADD_SIGNED:
6960 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6961 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6962 break;
6964 case WINED3D_TOP_ADD_SIGNED_2X:
6965 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6966 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6967 break;
6969 case WINED3D_TOP_SUBTRACT:
6970 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6971 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6972 break;
6974 case WINED3D_TOP_ADD_SMOOTH:
6975 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6976 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6977 break;
6979 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6980 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6981 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6982 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6983 break;
6985 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6986 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6987 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6988 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6989 break;
6991 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6992 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6993 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6994 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6995 break;
6997 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6998 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6999 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7000 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
7001 break;
7003 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
7004 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
7005 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7006 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7007 break;
7009 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
7010 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
7011 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7012 break;
7014 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
7015 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
7016 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7017 break;
7019 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
7020 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7021 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7022 break;
7023 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
7024 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
7025 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7026 break;
7028 case WINED3D_TOP_BUMPENVMAP:
7029 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7030 /* These are handled in the first pass, nothing to do. */
7031 break;
7033 case WINED3D_TOP_DOTPRODUCT3:
7034 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
7035 dstreg, dstmask, arg1, arg2, dstmask);
7036 break;
7038 case WINED3D_TOP_MULTIPLY_ADD:
7039 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
7040 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
7041 break;
7043 case WINED3D_TOP_LERP:
7044 /* MSDN isn't quite right here. */
7045 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
7046 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
7047 break;
7049 default:
7050 FIXME("Unhandled operation %#x.\n", op);
7051 break;
7055 /* Context activation is done by the caller. */
7056 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
7057 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
7059 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
7060 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
7061 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7062 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7063 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
7064 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
7065 UINT lowest_disabled_stage;
7066 GLuint shader_id;
7067 DWORD arg0, arg1, arg2;
7068 unsigned int stage;
7070 string_buffer_clear(buffer);
7072 /* Find out which textures are read */
7073 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7075 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7076 break;
7078 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
7079 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
7080 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
7082 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
7083 || (stage == 0 && settings->color_key_enabled))
7084 tex_map |= 1u << stage;
7085 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7086 tfactor_used = TRUE;
7087 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7088 tempreg_used = TRUE;
7089 if (settings->op[stage].dst == tempreg)
7090 tempreg_used = TRUE;
7091 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7092 tss_const_map |= 1u << stage;
7094 switch (settings->op[stage].cop)
7096 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7097 lum_map |= 1u << stage;
7098 /* fall through */
7099 case WINED3D_TOP_BUMPENVMAP:
7100 bump_map |= 1u << stage;
7101 /* fall through */
7102 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7103 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7104 tex_map |= 1u << stage;
7105 break;
7107 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7108 tfactor_used = TRUE;
7109 break;
7111 default:
7112 break;
7115 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7116 continue;
7118 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
7119 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
7120 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
7122 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
7123 tex_map |= 1u << stage;
7124 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7125 tfactor_used = TRUE;
7126 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7127 tempreg_used = TRUE;
7128 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7129 tss_const_map |= 1u << stage;
7131 lowest_disabled_stage = stage;
7133 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, NULL));
7135 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7136 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7138 if (!needs_legacy_glsl_syntax(gl_info))
7139 shader_addline(buffer, "out vec4 ps_out[1];\n");
7141 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
7142 shader_addline(buffer, "vec4 ret;\n");
7143 if (tempreg_used || settings->sRGB_write)
7144 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
7145 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
7147 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7149 if (tss_const_map & (1u << stage))
7150 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
7152 if (!(tex_map & (1u << stage)))
7153 continue;
7155 switch (settings->op[stage].tex_type)
7157 case WINED3D_GL_RES_TYPE_TEX_1D:
7158 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
7159 break;
7160 case WINED3D_GL_RES_TYPE_TEX_2D:
7161 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
7162 break;
7163 case WINED3D_GL_RES_TYPE_TEX_3D:
7164 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
7165 break;
7166 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7167 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
7168 break;
7169 case WINED3D_GL_RES_TYPE_TEX_RECT:
7170 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
7171 break;
7172 default:
7173 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
7174 break;
7177 shader_addline(buffer, "vec4 tex%u;\n", stage);
7179 if (!(bump_map & (1u << stage)))
7180 continue;
7181 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
7183 if (!(lum_map & (1u << stage)))
7184 continue;
7185 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
7186 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
7188 if (tfactor_used)
7189 shader_addline(buffer, "uniform vec4 tex_factor;\n");
7190 if (settings->color_key_enabled)
7191 shader_addline(buffer, "uniform vec4 color_key[2];\n");
7192 shader_addline(buffer, "uniform vec4 specular_enable;\n");
7194 if (settings->sRGB_write)
7196 shader_addline(buffer, "const vec4 srgb_const0 = ");
7197 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7198 shader_addline(buffer, ";\n");
7199 shader_addline(buffer, "const vec4 srgb_const1 = ");
7200 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7201 shader_addline(buffer, ";\n");
7204 shader_addline(buffer, "uniform struct\n{\n");
7205 shader_addline(buffer, " vec4 color;\n");
7206 shader_addline(buffer, " float density;\n");
7207 shader_addline(buffer, " float end;\n");
7208 shader_addline(buffer, " float scale;\n");
7209 shader_addline(buffer, "} ffp_fog;\n");
7211 if (alpha_test_func != WINED3D_CMP_ALWAYS)
7212 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7214 if (legacy_context)
7216 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7217 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7218 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7219 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7220 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7222 else
7224 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7225 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7226 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7227 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7228 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7231 shader_addline(buffer, "void main()\n{\n");
7233 if (legacy_context)
7235 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7236 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7239 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7241 if (tex_map & (1u << stage))
7243 if (settings->pointsprite)
7244 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
7245 else if (settings->texcoords_initialized & (1u << stage))
7246 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
7247 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
7248 else
7249 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
7253 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
7254 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7256 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
7257 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
7259 /* Generate texture sampling instructions */
7260 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
7262 const char *texture_function, *coord_mask;
7263 BOOL proj;
7265 if (!(tex_map & (1u << stage)))
7266 continue;
7268 if (settings->op[stage].projected == proj_none)
7270 proj = FALSE;
7272 else if (settings->op[stage].projected == proj_count4
7273 || settings->op[stage].projected == proj_count3)
7275 proj = TRUE;
7277 else
7279 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
7280 proj = TRUE;
7283 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
7284 proj = FALSE;
7286 switch (settings->op[stage].tex_type)
7288 case WINED3D_GL_RES_TYPE_TEX_1D:
7289 if (proj)
7291 texture_function = "texture1DProj";
7292 coord_mask = "xw";
7294 else
7296 texture_function = "texture1D";
7297 coord_mask = "x";
7299 break;
7300 case WINED3D_GL_RES_TYPE_TEX_2D:
7301 if (proj)
7303 texture_function = "texture2DProj";
7304 coord_mask = "xyw";
7306 else
7308 texture_function = "texture2D";
7309 coord_mask = "xy";
7311 break;
7312 case WINED3D_GL_RES_TYPE_TEX_3D:
7313 if (proj)
7315 texture_function = "texture3DProj";
7316 coord_mask = "xyzw";
7318 else
7320 texture_function = "texture3D";
7321 coord_mask = "xyz";
7323 break;
7324 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7325 texture_function = "textureCube";
7326 coord_mask = "xyz";
7327 break;
7328 case WINED3D_GL_RES_TYPE_TEX_RECT:
7329 if (proj)
7331 texture_function = "texture2DRectProj";
7332 coord_mask = "xyw";
7334 else
7336 texture_function = "texture2DRect";
7337 coord_mask = "xy";
7339 break;
7340 default:
7341 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
7342 texture_function = "";
7343 coord_mask = "xyzw";
7344 break;
7346 if (!needs_legacy_glsl_syntax(gl_info))
7347 texture_function = proj ? "textureProj" : "texture";
7349 if (stage > 0
7350 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
7351 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
7353 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
7355 /* With projective textures, texbem only divides the static
7356 * texture coord, not the displacement, so multiply the
7357 * displacement with the dividing parameter before passing it to
7358 * TXP. */
7359 if (settings->op[stage].projected != proj_none)
7361 if (settings->op[stage].projected == proj_count4)
7363 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7364 stage, stage);
7365 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
7367 else
7369 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7370 stage, stage);
7371 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
7374 else
7376 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
7379 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7380 stage, texture_function, stage, coord_mask);
7382 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7383 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7384 stage, stage - 1, stage - 1, stage - 1);
7386 else if (settings->op[stage].projected == proj_count3)
7388 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7389 stage, texture_function, stage, stage);
7391 else
7393 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7394 stage, texture_function, stage, stage, coord_mask);
7397 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
7398 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
7399 settings->op[stage].color_fixup);
7402 if (settings->color_key_enabled)
7404 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7405 shader_addline(buffer, " discard;\n");
7408 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
7410 /* Generate the main shader */
7411 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7413 BOOL op_equal;
7415 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7416 break;
7418 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7419 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7420 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
7421 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7422 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7423 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
7424 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7425 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7426 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
7427 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7428 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7429 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
7430 else
7431 op_equal = settings->op[stage].aop == settings->op[stage].cop
7432 && settings->op[stage].carg0 == settings->op[stage].aarg0
7433 && settings->op[stage].carg1 == settings->op[stage].aarg1
7434 && settings->op[stage].carg2 == settings->op[stage].aarg2;
7436 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7438 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7439 settings->op[stage].cop, settings->op[stage].carg0,
7440 settings->op[stage].carg1, settings->op[stage].carg2);
7442 else if (op_equal)
7444 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
7445 settings->op[stage].cop, settings->op[stage].carg0,
7446 settings->op[stage].carg1, settings->op[stage].carg2);
7448 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
7449 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7451 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7452 settings->op[stage].cop, settings->op[stage].carg0,
7453 settings->op[stage].carg1, settings->op[stage].carg2);
7454 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
7455 settings->op[stage].aop, settings->op[stage].aarg0,
7456 settings->op[stage].aarg1, settings->op[stage].aarg2);
7460 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
7461 get_fragment_output(gl_info));
7463 if (settings->sRGB_write)
7464 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7466 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
7468 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
7470 shader_addline(buffer, "}\n");
7472 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7473 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7475 string_buffer_release(&priv->string_buffers, tex_reg_name);
7476 return shader_id;
7479 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
7480 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
7482 struct glsl_ffp_vertex_shader *shader;
7483 const struct wine_rb_entry *entry;
7485 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
7486 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
7488 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
7489 return NULL;
7491 shader->desc.settings = *settings;
7492 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
7493 list_init(&shader->linked_programs);
7494 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
7495 ERR("Failed to insert ffp vertex shader.\n");
7497 return shader;
7500 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
7501 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
7503 struct glsl_ffp_fragment_shader *glsl_desc;
7504 const struct ffp_frag_desc *desc;
7506 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
7507 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
7509 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
7510 return NULL;
7512 glsl_desc->entry.settings = *args;
7513 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
7514 list_init(&glsl_desc->linked_programs);
7515 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
7517 return glsl_desc;
7521 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
7522 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
7524 unsigned int i;
7525 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7527 for (i = 0; i < vs_c_count; ++i)
7529 string_buffer_sprintf(name, "vs_c[%u]", i);
7530 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7532 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
7534 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7536 string_buffer_sprintf(name, "vs_i[%u]", i);
7537 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7540 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7542 string_buffer_sprintf(name, "vs_b[%u]", i);
7543 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7546 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7548 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7550 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7551 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7553 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7554 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7555 for (i = 0; i < MAX_TEXTURES; ++i)
7557 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7558 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7560 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7561 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7562 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7563 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7564 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7565 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7566 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7568 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7569 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7570 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7571 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7572 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7573 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7574 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7575 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7576 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7577 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7578 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7579 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7580 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7581 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7582 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7583 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7584 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7585 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7586 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7587 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7588 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7589 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7590 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7591 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7593 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7594 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7595 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7596 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7597 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7598 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7599 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
7601 string_buffer_release(&priv->string_buffers, name);
7604 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
7605 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
7607 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7610 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7611 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7613 unsigned int i;
7614 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7616 for (i = 0; i < ps_c_count; ++i)
7618 string_buffer_sprintf(name, "ps_c[%u]", i);
7619 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7621 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
7623 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7625 string_buffer_sprintf(name, "ps_i[%u]", i);
7626 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7629 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7631 string_buffer_sprintf(name, "ps_b[%u]", i);
7632 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7635 for (i = 0; i < MAX_TEXTURES; ++i)
7637 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7638 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7639 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7640 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7641 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7642 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7643 string_buffer_sprintf(name, "tss_const%u", i);
7644 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7647 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7648 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7650 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7651 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7652 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7653 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7655 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
7657 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7658 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7659 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7661 string_buffer_release(&priv->string_buffers, name);
7664 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7665 struct shader_glsl_priv *priv, GLuint program_id,
7666 const struct wined3d_shader_reg_maps *reg_maps)
7668 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7669 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7670 unsigned int i, base, count;
7671 GLuint block_idx;
7673 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
7674 for (i = 0; i < count; ++i)
7676 if (!reg_maps->cb_sizes[i])
7677 continue;
7679 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7680 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7681 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7683 checkGLcall("glUniformBlockBinding");
7684 string_buffer_release(&priv->string_buffers, name);
7687 /* Context activation is done by the caller. */
7688 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7689 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7691 const struct wined3d_gl_info *gl_info = context->gl_info;
7692 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7693 const struct ps_np2fixup_info *np2fixup_info = NULL;
7694 struct glsl_shader_prog_link *entry = NULL;
7695 struct wined3d_shader *vshader = NULL;
7696 struct wined3d_shader *gshader = NULL;
7697 struct wined3d_shader *pshader = NULL;
7698 GLuint program_id;
7699 GLuint reorder_shader_id = 0;
7700 unsigned int i;
7701 GLuint vs_id = 0;
7702 GLuint gs_id = 0;
7703 GLuint ps_id = 0;
7704 struct list *ps_list, *vs_list;
7705 WORD attribs_map;
7706 struct wined3d_string_buffer *tmp_name;
7708 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7710 vs_id = ctx_data->glsl_program->vs.id;
7711 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7713 if (use_vs(state))
7715 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7716 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7718 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7719 && ctx_data->glsl_program->gs.id)
7721 gs_id = ctx_data->glsl_program->gs.id;
7723 else if (gshader)
7725 struct gs_compile_args args;
7727 find_gs_compile_args(state, gshader, &args);
7728 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
7732 else if (use_vs(state))
7734 struct vs_compile_args vs_compile_args;
7736 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7737 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7739 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7740 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
7741 vs_list = &vshader->linked_programs;
7743 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7744 && ctx_data->glsl_program->gs.id)
7746 gs_id = ctx_data->glsl_program->gs.id;
7748 else if (gshader)
7750 struct gs_compile_args gs_compile_args;
7752 find_gs_compile_args(state, gshader, &gs_compile_args);
7753 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
7756 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7758 struct glsl_ffp_vertex_shader *ffp_shader;
7759 struct wined3d_ffp_vs_settings settings;
7761 wined3d_ffp_get_vs_settings(context, state, &settings);
7762 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7763 vs_id = ffp_shader->id;
7764 vs_list = &ffp_shader->linked_programs;
7767 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7769 ps_id = ctx_data->glsl_program->ps.id;
7770 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7772 if (use_ps(state))
7773 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7775 else if (use_ps(state))
7777 struct ps_compile_args ps_compile_args;
7778 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7779 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7780 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7781 pshader, &ps_compile_args, &np2fixup_info);
7782 ps_list = &pshader->linked_programs;
7784 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7786 struct glsl_ffp_fragment_shader *ffp_shader;
7787 struct ffp_frag_settings settings;
7789 gen_ffp_frag_op(context, state, &settings, FALSE);
7790 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7791 ps_id = ffp_shader->id;
7792 ps_list = &ffp_shader->linked_programs;
7795 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7797 ctx_data->glsl_program = entry;
7798 return;
7801 /* If we get to this point, then no matching program exists, so we create one */
7802 program_id = GL_EXTCALL(glCreateProgram());
7803 TRACE("Created new GLSL shader program %u.\n", program_id);
7805 /* Create the entry */
7806 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7807 entry->id = program_id;
7808 entry->vs.id = vs_id;
7809 entry->gs.id = gs_id;
7810 entry->ps.id = ps_id;
7811 entry->constant_version = 0;
7812 entry->ps.np2_fixup_info = np2fixup_info;
7813 /* Add the hash table entry */
7814 add_glsl_program_entry(priv, entry);
7816 /* Set the current program */
7817 ctx_data->glsl_program = entry;
7819 /* Attach GLSL vshader */
7820 if (vs_id)
7822 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7823 GL_EXTCALL(glAttachShader(program_id, vs_id));
7824 checkGLcall("glAttachShader");
7826 list_add_head(vs_list, &entry->vs.shader_entry);
7829 if (vshader)
7831 attribs_map = vshader->reg_maps.input_registers;
7832 if (vshader->reg_maps.shader_version.major < 4)
7834 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
7835 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7836 d3d_info->emulated_flatshading
7837 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7838 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7839 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7840 checkGLcall("glAttachShader");
7841 /* Flag the reorder function for deletion, it will be freed
7842 * automatically when the program is destroyed. */
7843 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7846 else
7848 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7851 if (!shader_glsl_use_explicit_attrib_location(gl_info))
7853 /* Bind vertex attributes to a corresponding index number to match
7854 * the same index numbers as ARB_vertex_programs (makes loading
7855 * vertex attributes simpler). With this method, we can use the
7856 * exact same code to load the attributes later for both ARB and
7857 * GLSL shaders.
7859 * We have to do this here because we need to know the Program ID
7860 * in order to make the bindings work, and it has to be done prior
7861 * to linking the GLSL program. */
7862 tmp_name = string_buffer_get(&priv->string_buffers);
7863 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7865 if (!(attribs_map & 1))
7866 continue;
7868 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7869 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7870 if (vshader && vshader->reg_maps.shader_version.major >= 4)
7872 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
7873 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7874 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
7875 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7878 checkGLcall("glBindAttribLocation");
7879 string_buffer_release(&priv->string_buffers, tmp_name);
7882 if (gshader)
7884 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7885 GL_EXTCALL(glAttachShader(program_id, gs_id));
7886 checkGLcall("glAttachShader");
7888 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7890 TRACE("input type %s, output type %s, vertices out %u.\n",
7891 debug_d3dprimitivetype(gshader->u.gs.input_type),
7892 debug_d3dprimitivetype(gshader->u.gs.output_type),
7893 gshader->u.gs.vertices_out);
7894 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7895 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7896 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7897 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7898 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7899 gshader->u.gs.vertices_out));
7900 checkGLcall("glProgramParameteriARB");
7903 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7906 /* Attach GLSL pshader */
7907 if (ps_id)
7909 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7910 GL_EXTCALL(glAttachShader(program_id, ps_id));
7911 checkGLcall("glAttachShader");
7913 list_add_head(ps_list, &entry->ps.shader_entry);
7916 /* Link the program */
7917 TRACE("Linking GLSL shader program %u.\n", program_id);
7918 GL_EXTCALL(glLinkProgram(program_id));
7919 shader_glsl_validate_link(gl_info, program_id);
7921 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7922 vshader ? vshader->limits->constant_float : 0);
7923 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
7924 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7925 pshader ? pshader->limits->constant_float : 0);
7926 checkGLcall("Find glsl program uniform locations");
7928 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7930 if (pshader && pshader->reg_maps.shader_version.major >= 3
7931 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7933 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7934 entry->vs.vertex_color_clamp = GL_FALSE;
7936 else
7938 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7941 else
7943 /* With core profile we never change vertex_color_clamp from
7944 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
7945 * glClampColorARB(). */
7946 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7949 /* Set the shader to allow uniform loading on it */
7950 GL_EXTCALL(glUseProgram(program_id));
7951 checkGLcall("glUseProgram");
7953 /* Texture unit mapping is set up to be the same each time the shader
7954 * program is used so we can hardcode the sampler uniform values. */
7955 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7957 entry->constant_update_mask = 0;
7958 if (vshader)
7960 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7961 if (vshader->reg_maps.integer_constants)
7962 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7963 if (vshader->reg_maps.boolean_constants)
7964 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7965 if (entry->vs.pos_fixup_location != -1)
7966 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
7968 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps);
7969 shader_glsl_load_icb(gl_info, priv, program_id, &vshader->reg_maps);
7971 else
7973 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7974 | WINED3D_SHADER_CONST_FFP_PROJ;
7976 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7978 if (entry->vs.modelview_matrix_location[i] != -1)
7980 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7981 break;
7985 for (i = 0; i < MAX_TEXTURES; ++i)
7987 if (entry->vs.texture_matrix_location[i] != -1)
7989 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7990 break;
7993 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7994 || entry->vs.material_specular_location != -1
7995 || entry->vs.material_emissive_location != -1
7996 || entry->vs.material_shininess_location != -1)
7997 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7998 if (entry->vs.light_ambient_location != -1)
7999 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8001 if (entry->vs.clip_planes_location != -1)
8002 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
8003 if (entry->vs.pointsize_min_location != -1)
8004 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8006 if (gshader)
8008 if (entry->gs.pos_fixup_location != -1)
8009 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8010 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps);
8011 shader_glsl_load_icb(gl_info, priv, program_id, &gshader->reg_maps);
8014 if (ps_id)
8016 if (pshader)
8018 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
8019 if (pshader->reg_maps.integer_constants)
8020 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
8021 if (pshader->reg_maps.boolean_constants)
8022 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
8023 if (entry->ps.ycorrection_location != -1)
8024 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
8026 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps);
8027 shader_glsl_load_icb(gl_info, priv, program_id, &pshader->reg_maps);
8029 else
8031 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8034 for (i = 0; i < MAX_TEXTURES; ++i)
8036 if (entry->ps.bumpenv_mat_location[i] != -1)
8038 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
8039 break;
8043 if (entry->ps.fog_color_location != -1)
8044 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8045 if (entry->ps.alpha_test_ref_location != -1)
8046 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
8047 if (entry->ps.np2_fixup_location != -1)
8048 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
8049 if (entry->ps.color_key_location != -1)
8050 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8054 /* Context activation is done by the caller. */
8055 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
8056 BOOL masked)
8058 GLuint program_id;
8059 GLuint vshader_id, pshader_id;
8060 const char *blt_pshader;
8062 static const char blt_vshader[] =
8063 "#version 120\n"
8064 "void main(void)\n"
8065 "{\n"
8066 " gl_Position = gl_Vertex;\n"
8067 " gl_FrontColor = vec4(1.0);\n"
8068 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
8069 "}\n";
8071 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
8073 /* WINED3D_GL_RES_TYPE_TEX_1D */
8074 NULL,
8075 /* WINED3D_GL_RES_TYPE_TEX_2D */
8076 "#version 120\n"
8077 "uniform sampler2D sampler;\n"
8078 "void main(void)\n"
8079 "{\n"
8080 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
8081 "}\n",
8082 /* WINED3D_GL_RES_TYPE_TEX_3D */
8083 NULL,
8084 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
8085 "#version 120\n"
8086 "uniform samplerCube sampler;\n"
8087 "void main(void)\n"
8088 "{\n"
8089 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
8090 "}\n",
8091 /* WINED3D_GL_RES_TYPE_TEX_RECT */
8092 "#version 120\n"
8093 "#extension GL_ARB_texture_rectangle : enable\n"
8094 "uniform sampler2DRect sampler;\n"
8095 "void main(void)\n"
8096 "{\n"
8097 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
8098 "}\n",
8099 /* WINED3D_GL_RES_TYPE_BUFFER */
8100 NULL,
8101 /* WINED3D_GL_RES_TYPE_RB */
8102 NULL,
8105 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
8107 /* WINED3D_GL_RES_TYPE_TEX_1D */
8108 NULL,
8109 /* WINED3D_GL_RES_TYPE_TEX_2D */
8110 "#version 120\n"
8111 "uniform sampler2D sampler;\n"
8112 "uniform vec4 mask;\n"
8113 "void main(void)\n"
8114 "{\n"
8115 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
8116 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
8117 "}\n",
8118 /* WINED3D_GL_RES_TYPE_TEX_3D */
8119 NULL,
8120 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
8121 "#version 120\n"
8122 "uniform samplerCube sampler;\n"
8123 "uniform vec4 mask;\n"
8124 "void main(void)\n"
8125 "{\n"
8126 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
8127 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
8128 "}\n",
8129 /* WINED3D_GL_RES_TYPE_TEX_RECT */
8130 "#version 120\n"
8131 "#extension GL_ARB_texture_rectangle : enable\n"
8132 "uniform sampler2DRect sampler;\n"
8133 "uniform vec4 mask;\n"
8134 "void main(void)\n"
8135 "{\n"
8136 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
8137 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
8138 "}\n",
8139 /* WINED3D_GL_RES_TYPE_BUFFER */
8140 NULL,
8141 /* WINED3D_GL_RES_TYPE_RB */
8142 NULL,
8145 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
8146 if (!blt_pshader)
8148 FIXME("tex_type %#x not supported\n", tex_type);
8149 return 0;
8152 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8153 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
8155 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8156 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
8158 program_id = GL_EXTCALL(glCreateProgram());
8159 GL_EXTCALL(glAttachShader(program_id, vshader_id));
8160 GL_EXTCALL(glAttachShader(program_id, pshader_id));
8161 GL_EXTCALL(glLinkProgram(program_id));
8163 shader_glsl_validate_link(gl_info, program_id);
8165 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
8166 * is destroyed
8168 GL_EXTCALL(glDeleteShader(vshader_id));
8169 GL_EXTCALL(glDeleteShader(pshader_id));
8170 return program_id;
8173 /* Context activation is done by the caller. */
8174 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
8175 const struct wined3d_state *state)
8177 struct glsl_context_data *ctx_data = context->shader_backend_data;
8178 const struct wined3d_gl_info *gl_info = context->gl_info;
8179 struct shader_glsl_priv *priv = shader_priv;
8180 GLuint program_id = 0, prev_id = 0;
8181 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
8183 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
8184 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
8186 if (ctx_data->glsl_program)
8188 prev_id = ctx_data->glsl_program->id;
8189 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8191 else
8193 prev_id = 0;
8194 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8197 set_glsl_shader_program(context, state, priv, ctx_data);
8199 if (ctx_data->glsl_program)
8201 program_id = ctx_data->glsl_program->id;
8202 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8204 else
8206 program_id = 0;
8207 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8210 if (old_vertex_color_clamp != current_vertex_color_clamp)
8212 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8214 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
8215 checkGLcall("glClampColorARB");
8217 else
8219 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
8223 TRACE("Using GLSL program %u.\n", program_id);
8225 if (prev_id != program_id)
8227 GL_EXTCALL(glUseProgram(program_id));
8228 checkGLcall("glUseProgram");
8230 if (program_id)
8231 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
8235 /* "context" is not necessarily the currently active context. */
8236 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
8238 struct glsl_context_data *ctx_data = context->shader_backend_data;
8240 ctx_data->glsl_program = NULL;
8241 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
8242 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8243 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8244 | (1u << WINED3D_SHADER_TYPE_HULL)
8245 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
8246 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
8249 /* Context activation is done by the caller. */
8250 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
8252 const struct wined3d_gl_info *gl_info = context->gl_info;
8253 struct shader_glsl_priv *priv = shader_priv;
8255 shader_glsl_invalidate_current_program(context);
8256 GL_EXTCALL(glUseProgram(0));
8257 checkGLcall("glUseProgram");
8259 priv->vertex_pipe->vp_enable(gl_info, FALSE);
8260 priv->fragment_pipe->enable_extension(gl_info, FALSE);
8262 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8264 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
8265 checkGLcall("glClampColorARB");
8269 /* Context activation is done by the caller. */
8270 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
8271 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
8273 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
8274 struct shader_glsl_priv *priv = shader_priv;
8275 GLuint *blt_program;
8276 GLint loc;
8278 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
8279 if (!*blt_program)
8281 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
8282 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
8283 GL_EXTCALL(glUseProgram(*blt_program));
8284 GL_EXTCALL(glUniform1i(loc, 0));
8286 else
8288 GL_EXTCALL(glUseProgram(*blt_program));
8291 if (masked)
8293 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
8294 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
8298 /* Context activation is done by the caller. */
8299 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
8301 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
8302 GLuint program_id;
8304 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8305 if (program_id) TRACE("Using GLSL program %u\n", program_id);
8307 GL_EXTCALL(glUseProgram(program_id));
8308 checkGLcall("glUseProgram");
8311 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
8312 const struct glsl_shader_prog_link *program)
8314 const struct glsl_context_data *ctx_data;
8315 struct wined3d_context *context;
8316 unsigned int i;
8318 for (i = 0; i < device->context_count; ++i)
8320 context = device->contexts[i];
8321 ctx_data = context->shader_backend_data;
8323 if (ctx_data->glsl_program == program)
8324 shader_glsl_invalidate_current_program(context);
8328 static void shader_glsl_destroy(struct wined3d_shader *shader)
8330 struct glsl_shader_private *shader_data = shader->backend_data;
8331 struct wined3d_device *device = shader->device;
8332 struct shader_glsl_priv *priv = device->shader_priv;
8333 const struct wined3d_gl_info *gl_info;
8334 const struct list *linked_programs;
8335 struct wined3d_context *context;
8337 if (!shader_data || !shader_data->num_gl_shaders)
8339 HeapFree(GetProcessHeap(), 0, shader_data);
8340 shader->backend_data = NULL;
8341 return;
8344 context = context_acquire(device, NULL);
8345 gl_info = context->gl_info;
8347 TRACE("Deleting linked programs.\n");
8348 linked_programs = &shader->linked_programs;
8349 if (linked_programs->next)
8351 struct glsl_shader_prog_link *entry, *entry2;
8352 UINT i;
8354 switch (shader->reg_maps.shader_version.type)
8356 case WINED3D_SHADER_TYPE_PIXEL:
8358 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
8360 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8362 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
8363 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8364 checkGLcall("glDeleteShader");
8366 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
8368 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8369 struct glsl_shader_prog_link, ps.shader_entry)
8371 shader_glsl_invalidate_contexts_program(device, entry);
8372 delete_glsl_program_entry(priv, gl_info, entry);
8375 break;
8378 case WINED3D_SHADER_TYPE_VERTEX:
8380 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
8382 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8384 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
8385 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8386 checkGLcall("glDeleteShader");
8388 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
8390 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8391 struct glsl_shader_prog_link, vs.shader_entry)
8393 shader_glsl_invalidate_contexts_program(device, entry);
8394 delete_glsl_program_entry(priv, gl_info, entry);
8397 break;
8400 case WINED3D_SHADER_TYPE_GEOMETRY:
8402 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
8404 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8406 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
8407 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8408 checkGLcall("glDeleteShader");
8410 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
8412 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8413 struct glsl_shader_prog_link, gs.shader_entry)
8415 shader_glsl_invalidate_contexts_program(device, entry);
8416 delete_glsl_program_entry(priv, gl_info, entry);
8419 break;
8422 default:
8423 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8424 break;
8428 HeapFree(GetProcessHeap(), 0, shader->backend_data);
8429 shader->backend_data = NULL;
8431 context_release(context);
8434 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
8436 const struct glsl_program_key *k = key;
8437 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
8438 const struct glsl_shader_prog_link, program_lookup_entry);
8440 if (k->vs_id > prog->vs.id) return 1;
8441 else if (k->vs_id < prog->vs.id) return -1;
8443 if (k->gs_id > prog->gs.id) return 1;
8444 else if (k->gs_id < prog->gs.id) return -1;
8446 if (k->ps_id > prog->ps.id) return 1;
8447 else if (k->ps_id < prog->ps.id) return -1;
8449 return 0;
8452 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
8454 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
8455 + constant_count * sizeof(*heap->contained)
8456 + constant_count * sizeof(*heap->positions);
8457 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
8459 if (!mem)
8461 ERR("Failed to allocate memory\n");
8462 return FALSE;
8465 heap->entries = mem;
8466 heap->entries[1].version = 0;
8467 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
8468 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
8469 heap->positions = (unsigned int *)(heap->contained + constant_count);
8470 heap->size = 1;
8472 return TRUE;
8475 static void constant_heap_free(struct constant_heap *heap)
8477 HeapFree(GetProcessHeap(), 0, heap->entries);
8480 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
8481 const struct fragment_pipeline *fragment_pipe)
8483 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8484 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
8485 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
8486 struct fragment_caps fragment_caps;
8487 void *vertex_priv, *fragment_priv;
8489 string_buffer_list_init(&priv->string_buffers);
8491 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
8493 ERR("Failed to initialize vertex pipe.\n");
8494 HeapFree(GetProcessHeap(), 0, priv);
8495 return E_FAIL;
8498 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
8500 ERR("Failed to initialize fragment pipe.\n");
8501 vertex_pipe->vp_free(device);
8502 HeapFree(GetProcessHeap(), 0, priv);
8503 return E_FAIL;
8506 if (!string_buffer_init(&priv->shader_buffer))
8508 ERR("Failed to initialize shader buffer.\n");
8509 goto fail;
8512 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
8514 ERR("Failed to allocate memory.\n");
8515 goto fail;
8518 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
8520 ERR("Failed to initialize vertex shader constant heap\n");
8521 goto fail;
8524 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
8526 ERR("Failed to initialize pixel shader constant heap\n");
8527 goto fail;
8530 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
8532 priv->next_constant_version = 1;
8533 priv->vertex_pipe = vertex_pipe;
8534 priv->fragment_pipe = fragment_pipe;
8535 fragment_pipe->get_caps(gl_info, &fragment_caps);
8536 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
8537 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
8539 device->vertex_priv = vertex_priv;
8540 device->fragment_priv = fragment_priv;
8541 device->shader_priv = priv;
8543 return WINED3D_OK;
8545 fail:
8546 constant_heap_free(&priv->pconst_heap);
8547 constant_heap_free(&priv->vconst_heap);
8548 HeapFree(GetProcessHeap(), 0, priv->stack);
8549 string_buffer_free(&priv->shader_buffer);
8550 fragment_pipe->free_private(device);
8551 vertex_pipe->vp_free(device);
8552 HeapFree(GetProcessHeap(), 0, priv);
8553 return E_OUTOFMEMORY;
8556 /* Context activation is done by the caller. */
8557 static void shader_glsl_free(struct wined3d_device *device)
8559 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8560 struct shader_glsl_priv *priv = device->shader_priv;
8561 int i;
8563 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
8565 if (priv->depth_blt_program_full[i])
8567 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
8569 if (priv->depth_blt_program_masked[i])
8571 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
8575 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
8576 constant_heap_free(&priv->pconst_heap);
8577 constant_heap_free(&priv->vconst_heap);
8578 HeapFree(GetProcessHeap(), 0, priv->stack);
8579 string_buffer_list_cleanup(&priv->string_buffers);
8580 string_buffer_free(&priv->shader_buffer);
8581 priv->fragment_pipe->free_private(device);
8582 priv->vertex_pipe->vp_free(device);
8584 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8585 device->shader_priv = NULL;
8588 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8590 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8591 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8594 static void shader_glsl_free_context_data(struct wined3d_context *context)
8596 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8599 static void shader_glsl_init_context_state(struct wined3d_context *context)
8601 const struct wined3d_gl_info *gl_info = context->gl_info;
8603 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8604 checkGLcall("GL_PROGRAM_POINT_SIZE");
8607 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8609 UINT shader_model;
8611 /* FIXME: Check for the specific extensions required for SM5 support
8612 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8613 * soon as we introduce them, adjusting the GL / GLSL version checks
8614 * accordingly. */
8615 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
8616 && gl_info->supported[ARB_DERIVATIVE_CONTROL])
8617 shader_model = 5;
8618 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8619 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8620 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8621 shader_model = 4;
8622 /* Support for texldd and texldl instructions in pixel shaders is required
8623 * for SM3. */
8624 else if (shader_glsl_has_core_grad(gl_info, NULL) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
8625 shader_model = 3;
8626 else
8627 shader_model = 2;
8628 TRACE("Shader model %u.\n", shader_model);
8630 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8631 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
8632 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
8633 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8634 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8635 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
8637 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
8638 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
8639 caps->varying_count = gl_info->limits.glsl_varyings;
8641 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8642 * Direct3D minimum requirement.
8644 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8645 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8647 * The problem is that the refrast clamps temporary results in the shader to
8648 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8649 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8650 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8651 * offer a way to query this.
8653 if (shader_model >= 4)
8654 caps->ps_1x_max_value = FLT_MAX;
8655 else
8656 caps->ps_1x_max_value = 1024.0f;
8658 /* Ideally we'd only set caps like sRGB writes here if supported by both
8659 * the shader backend and the fragment pipe, but we can get called before
8660 * shader_glsl_alloc(). */
8661 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8662 | WINED3D_SHADER_CAP_SRGB_WRITE;
8665 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8667 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8669 TRACE("Checking support for fixup:\n");
8670 dump_color_fixup_desc(fixup);
8673 /* We support everything except YUV conversions. */
8674 if (!is_complex_fixup(fixup))
8676 TRACE("[OK]\n");
8677 return TRUE;
8680 TRACE("[FAILED]\n");
8681 return FALSE;
8684 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8686 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8687 /* WINED3DSIH_ADD */ shader_glsl_binop,
8688 /* WINED3DSIH_AND */ shader_glsl_binop,
8689 /* WINED3DSIH_BEM */ shader_glsl_bem,
8690 /* WINED3DSIH_BFI */ NULL,
8691 /* WINED3DSIH_BFREV */ NULL,
8692 /* WINED3DSIH_BREAK */ shader_glsl_break,
8693 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8694 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8695 /* WINED3DSIH_BUFINFO */ NULL,
8696 /* WINED3DSIH_CALL */ shader_glsl_call,
8697 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8698 /* WINED3DSIH_CASE */ shader_glsl_case,
8699 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8700 /* WINED3DSIH_CND */ shader_glsl_cnd,
8701 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
8702 /* WINED3DSIH_CRS */ shader_glsl_cross,
8703 /* WINED3DSIH_CUT */ shader_glsl_cut,
8704 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
8705 /* WINED3DSIH_DCL */ shader_glsl_nop,
8706 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8707 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8708 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
8709 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
8710 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
8711 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
8712 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8713 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
8714 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8715 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8716 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8717 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8718 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8719 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8720 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8721 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
8722 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8723 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8724 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
8725 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8726 /* WINED3DSIH_DCL_STREAM */ NULL,
8727 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8728 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
8729 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
8730 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
8731 /* WINED3DSIH_DCL_TGSM_RAW */ NULL,
8732 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL,
8733 /* WINED3DSIH_DCL_THREAD_GROUP */ NULL,
8734 /* WINED3DSIH_DCL_UAV_TYPED */ NULL,
8735 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8736 /* WINED3DSIH_DEF */ shader_glsl_nop,
8737 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
8738 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8739 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8740 /* WINED3DSIH_DIV */ shader_glsl_binop,
8741 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8742 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8743 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8744 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8745 /* WINED3DSIH_DST */ shader_glsl_dst,
8746 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8747 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
8748 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
8749 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8750 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
8751 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
8752 /* WINED3DSIH_ELSE */ shader_glsl_else,
8753 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8754 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
8755 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8756 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8757 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8758 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
8759 /* WINED3DSIH_EQ */ shader_glsl_relop,
8760 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8761 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8762 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8763 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8764 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8765 /* WINED3DSIH_GATHER4 */ NULL,
8766 /* WINED3DSIH_GATHER4_C */ NULL,
8767 /* WINED3DSIH_GE */ shader_glsl_relop,
8768 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
8769 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
8770 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
8771 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
8772 /* WINED3DSIH_IADD */ shader_glsl_binop,
8773 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8774 /* WINED3DSIH_IF */ shader_glsl_if,
8775 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8776 /* WINED3DSIH_IGE */ shader_glsl_relop,
8777 /* WINED3DSIH_ILT */ shader_glsl_relop,
8778 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8779 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8780 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8781 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL,
8782 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL,
8783 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8784 /* WINED3DSIH_INE */ shader_glsl_relop,
8785 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8786 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8787 /* WINED3DSIH_ISHR */ shader_glsl_binop,
8788 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8789 /* WINED3DSIH_LABEL */ shader_glsl_label,
8790 /* WINED3DSIH_LD */ shader_glsl_ld,
8791 /* WINED3DSIH_LD2DMS */ NULL,
8792 /* WINED3DSIH_LD_RAW */ NULL,
8793 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8794 /* WINED3DSIH_LD_UAV_TYPED */ NULL,
8795 /* WINED3DSIH_LIT */ shader_glsl_lit,
8796 /* WINED3DSIH_LOD */ NULL,
8797 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8798 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8799 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8800 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8801 /* WINED3DSIH_LT */ shader_glsl_relop,
8802 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8803 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8804 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8805 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8806 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8807 /* WINED3DSIH_MAD */ shader_glsl_mad,
8808 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8809 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8810 /* WINED3DSIH_MOV */ shader_glsl_mov,
8811 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8812 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8813 /* WINED3DSIH_MUL */ shader_glsl_binop,
8814 /* WINED3DSIH_NE */ shader_glsl_relop,
8815 /* WINED3DSIH_NOP */ shader_glsl_nop,
8816 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8817 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8818 /* WINED3DSIH_OR */ shader_glsl_binop,
8819 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8820 /* WINED3DSIH_POW */ shader_glsl_pow,
8821 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8822 /* WINED3DSIH_REP */ shader_glsl_rep,
8823 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8824 /* WINED3DSIH_RET */ shader_glsl_ret,
8825 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
8826 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8827 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8828 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8829 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8830 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8831 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8832 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
8833 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
8834 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8835 /* WINED3DSIH_SAMPLE_INFO */ NULL,
8836 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8837 /* WINED3DSIH_SAMPLE_POS */ NULL,
8838 /* WINED3DSIH_SETP */ NULL,
8839 /* WINED3DSIH_SGE */ shader_glsl_compare,
8840 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8841 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8842 /* WINED3DSIH_SLT */ shader_glsl_compare,
8843 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8844 /* WINED3DSIH_STORE_RAW */ NULL,
8845 /* WINED3DSIH_STORE_STRUCTURED */ NULL,
8846 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
8847 /* WINED3DSIH_SUB */ shader_glsl_binop,
8848 /* WINED3DSIH_SWAPC */ NULL,
8849 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
8850 /* WINED3DSIH_TEX */ shader_glsl_tex,
8851 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8852 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8853 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8854 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8855 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8856 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8857 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8858 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8859 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8860 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8861 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8862 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8863 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8864 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8865 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8866 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8867 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8868 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8869 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8870 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8871 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8872 /* WINED3DSIH_UBFE */ NULL,
8873 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8874 /* WINED3DSIH_UGE */ shader_glsl_relop,
8875 /* WINED3DSIH_ULT */ shader_glsl_relop,
8876 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
8877 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
8878 /* WINED3DSIH_USHR */ shader_glsl_binop,
8879 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8880 /* WINED3DSIH_XOR */ shader_glsl_binop,
8883 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8884 SHADER_HANDLER hw_fct;
8886 /* Select handler */
8887 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8889 /* Unhandled opcode */
8890 if (!hw_fct)
8892 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8893 return;
8895 hw_fct(ins);
8897 shader_glsl_add_instruction_modifiers(ins);
8900 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8902 struct shader_glsl_priv *priv = shader_priv;
8904 return priv->ffp_proj_control;
8907 const struct wined3d_shader_backend_ops glsl_shader_backend =
8909 shader_glsl_handle_instruction,
8910 shader_glsl_select,
8911 shader_glsl_disable,
8912 shader_glsl_select_depth_blt,
8913 shader_glsl_deselect_depth_blt,
8914 shader_glsl_update_float_vertex_constants,
8915 shader_glsl_update_float_pixel_constants,
8916 shader_glsl_load_constants,
8917 shader_glsl_destroy,
8918 shader_glsl_alloc,
8919 shader_glsl_free,
8920 shader_glsl_allocate_context_data,
8921 shader_glsl_free_context_data,
8922 shader_glsl_init_context_state,
8923 shader_glsl_get_caps,
8924 shader_glsl_color_fixup_supported,
8925 shader_glsl_has_ffp_proj_control,
8928 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8930 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8932 caps->xyzrhw = TRUE;
8933 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8934 caps->ffp_generic_attributes = TRUE;
8935 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8936 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8937 caps->max_vertex_blend_matrix_index = 0;
8938 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8939 | WINED3DVTXPCAPS_MATERIALSOURCE7
8940 | WINED3DVTXPCAPS_VERTEXFOG
8941 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8942 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8943 | WINED3DVTXPCAPS_LOCALVIEWER
8944 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8945 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8946 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
8947 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8950 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8952 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8953 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8954 return 0;
8957 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8959 struct shader_glsl_priv *priv;
8961 if (shader_backend == &glsl_shader_backend)
8963 priv = shader_priv;
8964 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
8965 return priv;
8968 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8970 return NULL;
8973 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8975 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8976 struct glsl_ffp_vertex_shader, desc.entry);
8977 struct glsl_shader_prog_link *program, *program2;
8978 struct glsl_ffp_destroy_ctx *ctx = context;
8980 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8981 struct glsl_shader_prog_link, vs.shader_entry)
8983 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8985 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8986 HeapFree(GetProcessHeap(), 0, shader);
8989 /* Context activation is done by the caller. */
8990 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8992 struct shader_glsl_priv *priv = device->vertex_priv;
8993 struct glsl_ffp_destroy_ctx ctx;
8995 ctx.priv = priv;
8996 ctx.gl_info = &device->adapter->gl_info;
8997 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
9000 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
9001 const struct wined3d_state *state, DWORD state_id) {}
9003 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
9004 const struct wined3d_state *state, DWORD state_id)
9006 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9009 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
9010 const struct wined3d_state *state, DWORD state_id)
9012 const struct wined3d_gl_info *gl_info = context->gl_info;
9013 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
9014 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9015 BOOL transformed = context->stream_info.position_transformed;
9016 BOOL wasrhw = context->last_was_rhw;
9017 unsigned int i;
9019 context->last_was_rhw = transformed;
9021 /* If the vertex declaration contains a transformed position attribute,
9022 * the draw uses the fixed function vertex pipeline regardless of any
9023 * vertex shader set by the application. */
9024 if (transformed != wasrhw
9025 || context->stream_info.swizzle_map != context->last_swizzle_map)
9026 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9028 context->last_swizzle_map = context->stream_info.swizzle_map;
9030 if (!use_vs(state))
9032 if (context->last_was_vshader)
9034 if (legacy_context)
9035 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9036 clipplane(context, state, STATE_CLIPPLANE(i));
9037 else
9038 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9041 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9043 /* Because of settings->texcoords, we have to regenerate the vertex
9044 * shader on a vdecl change if there aren't enough varyings to just
9045 * always output all the texture coordinates. */
9046 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
9047 || normal != context->last_was_normal)
9048 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9050 if (use_ps(state)
9051 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
9052 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
9053 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9055 else
9057 if (!context->last_was_vshader)
9059 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
9060 if (legacy_context)
9061 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9062 clipplane(context, state, STATE_CLIPPLANE(i));
9063 else
9064 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9068 context->last_was_vshader = use_vs(state);
9069 context->last_was_normal = normal;
9072 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
9073 const struct wined3d_state *state, DWORD state_id)
9075 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9076 /* Different vertex shaders potentially require a different vertex attributes setup. */
9077 if (!isStateDirty(context, STATE_VDECL))
9078 context_apply_state(context, state, STATE_VDECL);
9081 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
9082 const struct wined3d_state *state, DWORD state_id)
9084 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9085 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9086 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9089 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
9090 const struct wined3d_state *state, DWORD state_id)
9092 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
9093 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
9094 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9095 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9096 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9099 static void glsl_vertex_pipe_world(struct wined3d_context *context,
9100 const struct wined3d_state *state, DWORD state_id)
9102 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
9105 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
9106 const struct wined3d_state *state, DWORD state_id)
9108 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9111 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
9113 const struct wined3d_gl_info *gl_info = context->gl_info;
9114 unsigned int k;
9116 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
9117 | WINED3D_SHADER_CONST_FFP_LIGHTS
9118 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9120 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9122 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
9124 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
9125 clipplane(context, state, STATE_CLIPPLANE(k));
9128 else
9130 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9134 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
9135 const struct wined3d_state *state, DWORD state_id)
9137 /* Table fog behavior depends on the projection matrix. */
9138 if (state->render_states[WINED3D_RS_FOGENABLE]
9139 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
9140 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9141 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
9144 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
9145 const struct wined3d_state *state, DWORD state_id)
9147 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
9148 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
9149 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
9150 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
9151 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9152 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9155 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
9156 const struct wined3d_state *state, DWORD state_id)
9158 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9161 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
9162 const struct wined3d_state *state, DWORD state_id)
9164 DWORD sampler = state_id - STATE_SAMPLER(0);
9165 const struct wined3d_texture *texture = state->textures[sampler];
9166 BOOL np2;
9168 if (!texture)
9169 return;
9171 if (sampler >= MAX_TEXTURES)
9172 return;
9174 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
9175 || context->lastWasPow2Texture & (1u << sampler))
9177 if (np2)
9178 context->lastWasPow2Texture |= 1u << sampler;
9179 else
9180 context->lastWasPow2Texture &= ~(1u << sampler);
9182 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9186 static void glsl_vertex_pipe_material(struct wined3d_context *context,
9187 const struct wined3d_state *state, DWORD state_id)
9189 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9192 static void glsl_vertex_pipe_light(struct wined3d_context *context,
9193 const struct wined3d_state *state, DWORD state_id)
9195 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9198 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
9199 const struct wined3d_state *state, DWORD state_id)
9201 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9204 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
9205 const struct wined3d_state *state, DWORD state_id)
9207 if (!use_vs(state))
9208 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9211 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
9212 const struct wined3d_state *state, DWORD state_id)
9214 static unsigned int once;
9216 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
9217 FIXME("Non-point sprite points not supported in core profile.\n");
9220 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
9221 const struct wined3d_state *state, DWORD state_id)
9223 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9226 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
9227 const struct wined3d_state *state, DWORD state_id)
9229 const struct wined3d_gl_info *gl_info = context->gl_info;
9230 UINT index = state_id - STATE_CLIPPLANE(0);
9232 if (index >= gl_info->limits.user_clip_distances)
9233 return;
9235 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9238 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
9240 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9241 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
9242 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
9243 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
9244 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
9245 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
9246 /* Clip planes */
9247 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9248 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9249 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9250 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9251 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9252 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9253 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9254 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9255 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9256 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9257 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9258 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9259 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9260 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9261 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9262 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9263 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9264 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9265 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9266 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9267 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9268 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9269 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9270 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9271 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9272 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9273 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9274 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9275 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9276 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9277 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9278 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9279 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9280 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9281 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9282 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9283 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9284 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9285 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9286 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9287 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9288 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9289 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9290 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9291 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9292 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9293 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9294 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9295 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9296 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9297 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9298 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9299 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9300 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9301 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9302 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9303 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9304 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9305 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9306 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9307 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9308 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9309 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9310 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9311 /* Lights */
9312 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9313 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9314 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9315 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9316 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9317 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9318 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9319 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9320 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9321 /* Viewport */
9322 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
9323 /* Transform states */
9324 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
9325 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
9326 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9327 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9328 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9329 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9330 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9331 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9332 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9333 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9334 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
9335 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9336 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9337 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9338 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9339 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9340 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9341 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9342 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9343 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9344 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9345 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9346 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9347 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9348 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9349 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9350 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9351 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9352 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9353 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9354 /* Fog */
9355 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9356 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9357 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9358 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9359 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
9360 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
9361 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9362 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9363 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9364 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9365 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9366 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9367 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9368 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9369 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9370 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9371 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9372 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
9373 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
9374 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
9375 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
9376 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
9377 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9378 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9379 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9380 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9381 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9382 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9383 /* NP2 texture matrix fixups. They are not needed if
9384 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
9385 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
9386 * matrix. */
9387 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9388 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9389 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9390 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9391 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9392 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9393 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9394 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9395 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9396 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9397 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9398 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9399 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9400 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9401 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9402 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9403 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9404 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9405 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9406 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9407 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9408 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9409 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9410 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9411 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9412 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
9413 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
9414 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
9417 /* TODO:
9418 * - Implement vertex tweening. */
9419 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
9421 glsl_vertex_pipe_vp_enable,
9422 glsl_vertex_pipe_vp_get_caps,
9423 glsl_vertex_pipe_vp_get_emul_mask,
9424 glsl_vertex_pipe_vp_alloc,
9425 glsl_vertex_pipe_vp_free,
9426 glsl_vertex_pipe_vp_states,
9429 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
9431 /* Nothing to do. */
9434 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
9436 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
9437 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
9438 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
9439 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
9440 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
9441 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
9442 | WINED3DTEXOPCAPS_SELECTARG1
9443 | WINED3DTEXOPCAPS_SELECTARG2
9444 | WINED3DTEXOPCAPS_MODULATE4X
9445 | WINED3DTEXOPCAPS_MODULATE2X
9446 | WINED3DTEXOPCAPS_MODULATE
9447 | WINED3DTEXOPCAPS_ADDSIGNED2X
9448 | WINED3DTEXOPCAPS_ADDSIGNED
9449 | WINED3DTEXOPCAPS_ADD
9450 | WINED3DTEXOPCAPS_SUBTRACT
9451 | WINED3DTEXOPCAPS_ADDSMOOTH
9452 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
9453 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
9454 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
9455 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
9456 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
9457 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
9458 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
9459 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
9460 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
9461 | WINED3DTEXOPCAPS_DOTPRODUCT3
9462 | WINED3DTEXOPCAPS_MULTIPLYADD
9463 | WINED3DTEXOPCAPS_LERP
9464 | WINED3DTEXOPCAPS_BUMPENVMAP
9465 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
9466 caps->MaxTextureBlendStages = MAX_TEXTURES;
9467 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, MAX_TEXTURES);
9470 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
9472 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9473 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9474 return 0;
9477 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9479 struct shader_glsl_priv *priv;
9481 if (shader_backend == &glsl_shader_backend)
9483 priv = shader_priv;
9484 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
9485 return priv;
9488 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
9490 return NULL;
9493 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
9495 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9496 struct glsl_ffp_fragment_shader, entry.entry);
9497 struct glsl_shader_prog_link *program, *program2;
9498 struct glsl_ffp_destroy_ctx *ctx = context;
9500 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9501 struct glsl_shader_prog_link, ps.shader_entry)
9503 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9505 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9506 HeapFree(GetProcessHeap(), 0, shader);
9509 /* Context activation is done by the caller. */
9510 static void glsl_fragment_pipe_free(struct wined3d_device *device)
9512 struct shader_glsl_priv *priv = device->fragment_priv;
9513 struct glsl_ffp_destroy_ctx ctx;
9515 ctx.priv = priv;
9516 ctx.gl_info = &device->adapter->gl_info;
9517 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
9520 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
9521 const struct wined3d_state *state, DWORD state_id)
9523 context->last_was_pshader = use_ps(state);
9525 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9528 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
9529 const struct wined3d_state *state, DWORD state_id)
9531 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9534 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
9535 const struct wined3d_state *state, DWORD state_id)
9537 BOOL use_vshader = use_vs(state);
9538 enum fogsource new_source;
9539 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
9540 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
9542 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9544 if (!state->render_states[WINED3D_RS_FOGENABLE])
9545 return;
9547 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
9549 if (use_vshader)
9550 new_source = FOGSOURCE_VS;
9551 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
9552 new_source = FOGSOURCE_COORD;
9553 else
9554 new_source = FOGSOURCE_FFP;
9556 else
9558 new_source = FOGSOURCE_FFP;
9561 if (new_source != context->fog_source || fogstart == fogend)
9563 context->fog_source = new_source;
9564 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9568 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
9569 const struct wined3d_state *state, DWORD state_id)
9571 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9572 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9573 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9575 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
9576 glsl_fragment_pipe_fog(context, state, state_id);
9579 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
9580 const struct wined3d_state *state, DWORD state_id)
9582 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9583 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9584 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9587 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
9588 const struct wined3d_state *state, DWORD state_id)
9590 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9593 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
9594 const struct wined3d_state *state, DWORD state_id)
9596 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
9599 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
9600 const struct wined3d_state *state, DWORD state_id)
9602 const struct wined3d_gl_info *gl_info = context->gl_info;
9603 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
9604 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
9606 if (func)
9608 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
9609 checkGLcall("glAlphaFunc");
9613 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
9614 const struct wined3d_state *state, DWORD state_id)
9616 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9619 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
9620 const struct wined3d_state *state, DWORD state_id)
9622 const struct wined3d_gl_info *gl_info = context->gl_info;
9624 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
9626 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
9627 checkGLcall("glEnable(GL_ALPHA_TEST)");
9629 else
9631 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
9632 checkGLcall("glDisable(GL_ALPHA_TEST)");
9636 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
9637 const struct wined3d_state *state, DWORD state_id)
9639 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
9642 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
9643 const struct wined3d_state *state, DWORD state_id)
9645 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
9648 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
9649 const struct wined3d_state *state, DWORD state_id)
9651 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9654 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
9656 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9657 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
9658 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9659 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9660 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9661 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9662 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9663 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9664 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9665 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9666 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9667 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9668 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9669 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9670 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9671 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9672 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9673 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9674 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9675 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9676 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9677 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9678 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9679 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9680 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9681 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9682 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9683 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9684 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9685 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9686 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9687 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9688 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9689 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9690 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9691 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9692 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9693 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9694 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9695 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9696 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9697 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9698 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9699 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9700 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9701 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9702 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9703 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9704 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9705 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9706 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9707 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9708 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9709 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9710 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9711 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9712 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9713 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9714 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9715 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9716 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9717 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9718 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9719 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9720 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9721 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9722 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9723 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9724 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9725 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9726 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9727 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9728 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9729 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9730 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9731 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9732 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
9733 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9734 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
9735 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
9736 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
9737 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
9738 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9739 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9740 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9741 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9742 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9743 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9744 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9745 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9746 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9747 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9748 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9749 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9750 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
9751 {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 },
9752 {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 },
9753 {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 },
9754 {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 },
9755 {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 },
9756 {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 },
9757 {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 },
9758 {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 },
9759 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9760 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9761 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9762 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9763 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9764 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9765 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9766 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9767 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9768 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9769 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9770 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9771 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9774 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9776 return TRUE;
9779 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9783 const struct fragment_pipeline glsl_fragment_pipe =
9785 glsl_fragment_pipe_enable,
9786 glsl_fragment_pipe_get_caps,
9787 glsl_fragment_pipe_get_emul_mask,
9788 glsl_fragment_pipe_alloc,
9789 glsl_fragment_pipe_free,
9790 glsl_fragment_pipe_alloc_context_data,
9791 glsl_fragment_pipe_free_context_data,
9792 shader_glsl_color_fixup_supported,
9793 glsl_fragment_pipe_state_template,