user32: Scale the button size with screen resolution.
[wine.git] / dlls / wined3d / glsl_shader.c
blob5ccb027bdd6e97f64606c972329d363fd1910737
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 const char *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 "#version 150";
323 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
324 return "#version 130";
325 else
326 return "#version 120";
329 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
331 char str[4][17];
333 wined3d_ftoa(values[0], str[0]);
334 wined3d_ftoa(values[1], str[1]);
335 wined3d_ftoa(values[2], str[2]);
336 wined3d_ftoa(values[3], str[3]);
337 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
340 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
341 const int *values, unsigned int size)
343 int i;
345 if (!size || size > 4)
347 ERR("Invalid vector size %u.\n", size);
348 return;
351 if (size > 1)
352 shader_addline(buffer, "ivec%u(", size);
354 for (i = 0; i < size; ++i)
355 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
357 if (size > 1)
358 shader_addline(buffer, ")");
361 static const char *get_info_log_line(const char **ptr)
363 const char *p, *q;
365 p = *ptr;
366 if (!(q = strstr(p, "\n")))
368 if (!*p) return NULL;
369 *ptr += strlen(p);
370 return p;
372 *ptr = q + 1;
374 return p;
377 /* Context activation is done by the caller. */
378 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
380 int length = 0;
381 char *log;
383 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
384 return;
386 if (program)
387 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
388 else
389 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
391 /* A size of 1 is just a null-terminated string, so the log should be bigger than
392 * that if there are errors. */
393 if (length > 1)
395 const char *ptr, *line;
397 log = HeapAlloc(GetProcessHeap(), 0, length);
398 /* The info log is supposed to be zero-terminated, but at least some
399 * versions of fglrx don't terminate the string properly. The reported
400 * length does include the terminator, so explicitly set it to zero
401 * here. */
402 log[length - 1] = 0;
403 if (program)
404 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
405 else
406 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
408 ptr = log;
409 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
411 WARN("Info log received from GLSL shader #%u:\n", id);
412 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
414 else
416 FIXME("Info log received from GLSL shader #%u:\n", id);
417 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
419 HeapFree(GetProcessHeap(), 0, log);
423 /* Context activation is done by the caller. */
424 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
426 const char *ptr, *line;
428 TRACE("Compiling shader object %u.\n", shader);
430 if (TRACE_ON(d3d_shader))
432 ptr = src;
433 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
436 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
437 checkGLcall("glShaderSource");
438 GL_EXTCALL(glCompileShader(shader));
439 checkGLcall("glCompileShader");
440 print_glsl_info_log(gl_info, shader, FALSE);
443 /* Context activation is done by the caller. */
444 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
446 GLint i, shader_count, source_size = -1;
447 GLuint *shaders;
448 char *source = NULL;
450 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
451 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
453 ERR("Failed to allocate shader array memory.\n");
454 return;
457 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
458 for (i = 0; i < shader_count; ++i)
460 const char *ptr, *line;
461 GLint tmp;
463 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
465 if (source_size < tmp)
467 HeapFree(GetProcessHeap(), 0, source);
469 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
470 if (!source)
472 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
473 HeapFree(GetProcessHeap(), 0, shaders);
474 return;
476 source_size = tmp;
479 FIXME("Shader %u:\n", shaders[i]);
480 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
481 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
482 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
483 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
484 FIXME("\n");
486 ptr = source;
487 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
488 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
489 FIXME("\n");
492 HeapFree(GetProcessHeap(), 0, source);
493 HeapFree(GetProcessHeap(), 0, shaders);
496 /* Context activation is done by the caller. */
497 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
499 GLint tmp;
501 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
502 return;
504 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
505 if (!tmp)
507 FIXME("Program %u link status invalid.\n", program);
508 shader_glsl_dump_program_source(gl_info, program);
511 print_glsl_info_log(gl_info, program, TRUE);
514 /* Context activation is done by the caller. */
515 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
516 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
518 unsigned int mapped_unit;
519 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
520 const char *prefix;
521 unsigned int i, j;
522 GLint name_loc;
524 static const struct
526 enum wined3d_shader_type type;
527 unsigned int base_idx;
528 unsigned int count;
530 sampler_info[] =
532 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
533 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
536 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
538 prefix = shader_glsl_get_prefix(sampler_info[i].type);
540 for (j = 0; j < sampler_info[i].count; ++j)
542 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
543 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
544 if (name_loc == -1)
545 continue;
547 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
548 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
550 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
551 continue;
554 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
555 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
558 checkGLcall("glUniform1i");
559 string_buffer_release(&priv->string_buffers, sampler_name);
562 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
563 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
565 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
567 if (icb)
569 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
570 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
571 GLint icb_location;
573 string_buffer_sprintf(icb_name, "%s_icb", prefix);
574 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
575 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
576 checkGLcall("Load immediate constant buffer");
578 string_buffer_release(&priv->string_buffers, icb_name);
582 /* Context activation is done by the caller. */
583 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
584 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
586 unsigned int start = ~0U, end = 0;
587 int stack_idx = 0;
588 unsigned int heap_idx = 1;
589 unsigned int idx;
591 if (heap->entries[heap_idx].version <= version) return;
593 idx = heap->entries[heap_idx].idx;
594 if (constant_locations[idx] != -1)
595 start = end = idx;
596 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
598 while (stack_idx >= 0)
600 /* Note that we fall through to the next case statement. */
601 switch(stack[stack_idx])
603 case HEAP_NODE_TRAVERSE_LEFT:
605 unsigned int left_idx = heap_idx << 1;
606 if (left_idx < heap->size && heap->entries[left_idx].version > version)
608 heap_idx = left_idx;
609 idx = heap->entries[heap_idx].idx;
610 if (constant_locations[idx] != -1)
612 if (start > idx)
613 start = idx;
614 if (end < idx)
615 end = idx;
618 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
619 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
620 break;
624 case HEAP_NODE_TRAVERSE_RIGHT:
626 unsigned int right_idx = (heap_idx << 1) + 1;
627 if (right_idx < heap->size && heap->entries[right_idx].version > version)
629 heap_idx = right_idx;
630 idx = heap->entries[heap_idx].idx;
631 if (constant_locations[idx] != -1)
633 if (start > idx)
634 start = idx;
635 if (end < idx)
636 end = idx;
639 stack[stack_idx++] = HEAP_NODE_POP;
640 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
641 break;
645 case HEAP_NODE_POP:
646 heap_idx >>= 1;
647 --stack_idx;
648 break;
651 if (start <= end)
652 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
653 checkGLcall("walk_constant_heap()");
656 /* Context activation is done by the caller. */
657 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
658 GLint location, const struct wined3d_vec4 *data)
660 GLfloat clamped_constant[4];
662 if (location == -1) return;
664 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
665 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
666 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
667 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
669 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
672 /* Context activation is done by the caller. */
673 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
674 const struct wined3d_vec4 *constants, const GLint *constant_locations,
675 const struct constant_heap *heap, unsigned char *stack, DWORD version)
677 int stack_idx = 0;
678 unsigned int heap_idx = 1;
679 unsigned int idx;
681 if (heap->entries[heap_idx].version <= version) return;
683 idx = heap->entries[heap_idx].idx;
684 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
685 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
687 while (stack_idx >= 0)
689 /* Note that we fall through to the next case statement. */
690 switch(stack[stack_idx])
692 case HEAP_NODE_TRAVERSE_LEFT:
694 unsigned int left_idx = heap_idx << 1;
695 if (left_idx < heap->size && heap->entries[left_idx].version > version)
697 heap_idx = left_idx;
698 idx = heap->entries[heap_idx].idx;
699 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
701 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
702 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
703 break;
707 case HEAP_NODE_TRAVERSE_RIGHT:
709 unsigned int right_idx = (heap_idx << 1) + 1;
710 if (right_idx < heap->size && heap->entries[right_idx].version > version)
712 heap_idx = right_idx;
713 idx = heap->entries[heap_idx].idx;
714 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
716 stack[stack_idx++] = HEAP_NODE_POP;
717 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
718 break;
722 case HEAP_NODE_POP:
723 heap_idx >>= 1;
724 --stack_idx;
725 break;
728 checkGLcall("walk_constant_heap_clamped()");
731 /* Context activation is done by the caller. */
732 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
733 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
734 unsigned char *stack, unsigned int version)
736 const struct wined3d_shader_lconst *lconst;
738 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
739 if (shader->reg_maps.shader_version.major == 1
740 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
741 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
742 else
743 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
745 if (!shader->load_local_constsF)
747 TRACE("No need to load local float constants for this shader.\n");
748 return;
751 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
752 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
754 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
756 checkGLcall("glUniform4fv()");
759 /* Context activation is done by the caller. */
760 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
761 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
763 unsigned int i;
764 struct list* ptr;
766 for (i = 0; constants_set; constants_set >>= 1, ++i)
768 if (!(constants_set & 1)) continue;
770 /* We found this uniform name in the program - go ahead and send the data */
771 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
774 /* Load immediate constants */
775 ptr = list_head(&shader->constantsI);
776 while (ptr)
778 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
779 unsigned int idx = lconst->idx;
780 const GLint *values = (const GLint *)lconst->value;
782 /* We found this uniform name in the program - go ahead and send the data */
783 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
784 ptr = list_next(&shader->constantsI, ptr);
786 checkGLcall("glUniform4iv()");
789 /* Context activation is done by the caller. */
790 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
791 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
793 unsigned int i;
794 struct list* ptr;
796 for (i = 0; constants_set; constants_set >>= 1, ++i)
798 if (!(constants_set & 1)) continue;
800 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
803 /* Load immediate constants */
804 ptr = list_head(&shader->constantsB);
805 while (ptr)
807 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
808 unsigned int idx = lconst->idx;
809 const GLint *values = (const GLint *)lconst->value;
811 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
812 ptr = list_next(&shader->constantsB, ptr);
814 checkGLcall("glUniform1iv()");
817 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
819 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
822 /* Context activation is done by the caller (state handler). */
823 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
824 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
826 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
827 UINT fixup = ps->np2_fixup_info->active;
828 UINT i;
830 for (i = 0; fixup; fixup >>= 1, ++i)
832 const struct wined3d_texture *tex = state->textures[i];
833 unsigned char idx = ps->np2_fixup_info->idx[i];
834 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
836 if (!tex)
838 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
839 continue;
842 if (idx % 2)
844 tex_dim[2] = tex->pow2_matrix[0];
845 tex_dim[3] = tex->pow2_matrix[5];
847 else
849 tex_dim[0] = tex->pow2_matrix[0];
850 tex_dim[1] = tex->pow2_matrix[5];
854 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
857 /* Taken and adapted from Mesa. */
858 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
860 float pos, neg, t, det;
861 struct wined3d_matrix temp;
863 /* Calculate the determinant of upper left 3x3 submatrix and
864 * determine if the matrix is singular. */
865 pos = neg = 0.0f;
866 t = in->_11 * in->_22 * in->_33;
867 if (t >= 0.0f)
868 pos += t;
869 else
870 neg += t;
872 t = in->_21 * in->_32 * in->_13;
873 if (t >= 0.0f)
874 pos += t;
875 else
876 neg += t;
877 t = in->_31 * in->_12 * in->_23;
878 if (t >= 0.0f)
879 pos += t;
880 else
881 neg += t;
883 t = -in->_31 * in->_22 * in->_13;
884 if (t >= 0.0f)
885 pos += t;
886 else
887 neg += t;
888 t = -in->_21 * in->_12 * in->_33;
889 if (t >= 0.0f)
890 pos += t;
891 else
892 neg += t;
894 t = -in->_11 * in->_32 * in->_23;
895 if (t >= 0.0f)
896 pos += t;
897 else
898 neg += t;
900 det = pos + neg;
902 if (fabsf(det) < 1e-25f)
903 return FALSE;
905 det = 1.0f / det;
906 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
907 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
908 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
909 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
910 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
911 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
912 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
913 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
914 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
916 *out = temp;
917 return TRUE;
920 static void swap_rows(float **a, float **b)
922 float *tmp = *a;
924 *a = *b;
925 *b = tmp;
928 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
930 float wtmp[4][8];
931 float m0, m1, m2, m3, s;
932 float *r0, *r1, *r2, *r3;
934 r0 = wtmp[0];
935 r1 = wtmp[1];
936 r2 = wtmp[2];
937 r3 = wtmp[3];
939 r0[0] = m->_11;
940 r0[1] = m->_12;
941 r0[2] = m->_13;
942 r0[3] = m->_14;
943 r0[4] = 1.0f;
944 r0[5] = r0[6] = r0[7] = 0.0f;
946 r1[0] = m->_21;
947 r1[1] = m->_22;
948 r1[2] = m->_23;
949 r1[3] = m->_24;
950 r1[5] = 1.0f;
951 r1[4] = r1[6] = r1[7] = 0.0f;
953 r2[0] = m->_31;
954 r2[1] = m->_32;
955 r2[2] = m->_33;
956 r2[3] = m->_34;
957 r2[6] = 1.0f;
958 r2[4] = r2[5] = r2[7] = 0.0f;
960 r3[0] = m->_41;
961 r3[1] = m->_42;
962 r3[2] = m->_43;
963 r3[3] = m->_44;
964 r3[7] = 1.0f;
965 r3[4] = r3[5] = r3[6] = 0.0f;
967 /* Choose pivot - or die. */
968 if (fabsf(r3[0]) > fabsf(r2[0]))
969 swap_rows(&r3, &r2);
970 if (fabsf(r2[0]) > fabsf(r1[0]))
971 swap_rows(&r2, &r1);
972 if (fabsf(r1[0]) > fabsf(r0[0]))
973 swap_rows(&r1, &r0);
974 if (r0[0] == 0.0f)
975 return FALSE;
977 /* Eliminate first variable. */
978 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
979 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
980 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
981 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
982 s = r0[4];
983 if (s != 0.0f)
985 r1[4] -= m1 * s;
986 r2[4] -= m2 * s;
987 r3[4] -= m3 * s;
989 s = r0[5];
990 if (s != 0.0f)
992 r1[5] -= m1 * s;
993 r2[5] -= m2 * s;
994 r3[5] -= m3 * s;
996 s = r0[6];
997 if (s != 0.0f)
999 r1[6] -= m1 * s;
1000 r2[6] -= m2 * s;
1001 r3[6] -= m3 * s;
1003 s = r0[7];
1004 if (s != 0.0f)
1006 r1[7] -= m1 * s;
1007 r2[7] -= m2 * s;
1008 r3[7] -= m3 * s;
1011 /* Choose pivot - or die. */
1012 if (fabsf(r3[1]) > fabsf(r2[1]))
1013 swap_rows(&r3, &r2);
1014 if (fabsf(r2[1]) > fabsf(r1[1]))
1015 swap_rows(&r2, &r1);
1016 if (r1[1] == 0.0f)
1017 return FALSE;
1019 /* Eliminate second variable. */
1020 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1021 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1022 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1023 s = r1[4];
1024 if (s != 0.0f)
1026 r2[4] -= m2 * s;
1027 r3[4] -= m3 * s;
1029 s = r1[5];
1030 if (s != 0.0f)
1032 r2[5] -= m2 * s;
1033 r3[5] -= m3 * s;
1035 s = r1[6];
1036 if (s != 0.0f)
1038 r2[6] -= m2 * s;
1039 r3[6] -= m3 * s;
1041 s = r1[7];
1042 if (s != 0.0f)
1044 r2[7] -= m2 * s;
1045 r3[7] -= m3 * s;
1048 /* Choose pivot - or die. */
1049 if (fabsf(r3[2]) > fabsf(r2[2]))
1050 swap_rows(&r3, &r2);
1051 if (r2[2] == 0.0f)
1052 return FALSE;
1054 /* Eliminate third variable. */
1055 m3 = r3[2] / r2[2];
1056 r3[3] -= m3 * r2[3];
1057 r3[4] -= m3 * r2[4];
1058 r3[5] -= m3 * r2[5];
1059 r3[6] -= m3 * r2[6];
1060 r3[7] -= m3 * r2[7];
1062 /* Last check. */
1063 if (r3[3] == 0.0f)
1064 return FALSE;
1066 /* Back substitute row 3. */
1067 s = 1.0f / r3[3];
1068 r3[4] *= s;
1069 r3[5] *= s;
1070 r3[6] *= s;
1071 r3[7] *= s;
1073 /* Back substitute row 2. */
1074 m2 = r2[3];
1075 s = 1.0f / r2[2];
1076 r2[4] = s * (r2[4] - r3[4] * m2);
1077 r2[5] = s * (r2[5] - r3[5] * m2);
1078 r2[6] = s * (r2[6] - r3[6] * m2);
1079 r2[7] = s * (r2[7] - r3[7] * m2);
1080 m1 = r1[3];
1081 r1[4] -= r3[4] * m1;
1082 r1[5] -= r3[5] * m1;
1083 r1[6] -= r3[6] * m1;
1084 r1[7] -= r3[7] * m1;
1085 m0 = r0[3];
1086 r0[4] -= r3[4] * m0;
1087 r0[5] -= r3[5] * m0;
1088 r0[6] -= r3[6] * m0;
1089 r0[7] -= r3[7] * m0;
1091 /* Back substitute row 1. */
1092 m1 = r1[2];
1093 s = 1.0f / r1[1];
1094 r1[4] = s * (r1[4] - r2[4] * m1);
1095 r1[5] = s * (r1[5] - r2[5] * m1);
1096 r1[6] = s * (r1[6] - r2[6] * m1);
1097 r1[7] = s * (r1[7] - r2[7] * m1);
1098 m0 = r0[2];
1099 r0[4] -= r2[4] * m0;
1100 r0[5] -= r2[5] * m0;
1101 r0[6] -= r2[6] * m0;
1102 r0[7] -= r2[7] * m0;
1104 /* Back substitute row 0. */
1105 m0 = r0[1];
1106 s = 1.0f / r0[0];
1107 r0[4] = s * (r0[4] - r1[4] * m0);
1108 r0[5] = s * (r0[5] - r1[5] * m0);
1109 r0[6] = s * (r0[6] - r1[6] * m0);
1110 r0[7] = s * (r0[7] - r1[7] * m0);
1112 out->_11 = r0[4];
1113 out->_12 = r0[5];
1114 out->_13 = r0[6];
1115 out->_14 = r0[7];
1116 out->_21 = r1[4];
1117 out->_22 = r1[5];
1118 out->_23 = r1[6];
1119 out->_24 = r1[7];
1120 out->_31 = r2[4];
1121 out->_32 = r2[5];
1122 out->_33 = r2[6];
1123 out->_34 = r2[7];
1124 out->_41 = r3[4];
1125 out->_42 = r3[5];
1126 out->_43 = r3[6];
1127 out->_44 = r3[7];
1129 return TRUE;
1132 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1133 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1135 const struct wined3d_gl_info *gl_info = context->gl_info;
1136 float mat[3 * 3];
1137 struct wined3d_matrix mv;
1138 unsigned int i, j;
1140 if (prog->vs.normal_matrix_location == -1)
1141 return;
1143 get_modelview_matrix(context, state, 0, &mv);
1144 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1145 invert_matrix_3d(&mv, &mv);
1146 else
1147 invert_matrix(&mv, &mv);
1148 /* Tests show that singular modelview matrices are used unchanged as normal
1149 * matrices on D3D3 and older. There seems to be no clearly consistent
1150 * behavior on newer D3D versions so always follow older ddraw behavior. */
1151 for (i = 0; i < 3; ++i)
1152 for (j = 0; j < 3; ++j)
1153 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1155 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1156 checkGLcall("glUniformMatrix3fv");
1159 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1160 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1162 const struct wined3d_gl_info *gl_info = context->gl_info;
1163 struct wined3d_matrix mat;
1165 if (tex >= MAX_TEXTURES)
1166 return;
1167 if (prog->vs.texture_matrix_location[tex] == -1)
1168 return;
1170 get_texture_matrix(context, state, tex, &mat);
1171 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1172 checkGLcall("glUniformMatrix4fv");
1175 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1176 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1178 const struct wined3d_gl_info *gl_info = context->gl_info;
1180 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1182 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1183 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1185 else
1187 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1189 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1191 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1192 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1193 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1194 checkGLcall("setting FFP material uniforms");
1197 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1198 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1200 const struct wined3d_gl_info *gl_info = context->gl_info;
1201 struct wined3d_color color;
1203 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1204 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1205 checkGLcall("glUniform3fv");
1208 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1209 const struct wined3d_matrix *src2)
1211 struct wined3d_vec4 temp;
1213 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1214 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1215 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1216 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1218 *dest = temp;
1221 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1222 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1224 const struct wined3d_gl_info *gl_info = context->gl_info;
1225 const struct wined3d_light_info *light_info = state->lights[light];
1226 struct wined3d_vec4 vec4;
1227 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1229 if (!light_info)
1230 return;
1232 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1233 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1234 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1236 switch (light_info->OriginalParms.type)
1238 case WINED3D_LIGHT_POINT:
1239 multiply_vector_matrix(&vec4, &light_info->position, view);
1240 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1241 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1242 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1243 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1244 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1245 break;
1247 case WINED3D_LIGHT_SPOT:
1248 multiply_vector_matrix(&vec4, &light_info->position, view);
1249 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1251 multiply_vector_matrix(&vec4, &light_info->direction, view);
1252 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1254 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1255 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1256 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1257 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1258 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1259 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1260 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1261 break;
1263 case WINED3D_LIGHT_DIRECTIONAL:
1264 multiply_vector_matrix(&vec4, &light_info->direction, view);
1265 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1266 break;
1268 case WINED3D_LIGHT_PARALLELPOINT:
1269 multiply_vector_matrix(&vec4, &light_info->position, view);
1270 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1271 break;
1273 default:
1274 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1276 checkGLcall("setting FFP lights uniforms");
1279 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1280 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1282 const struct wined3d_gl_info *gl_info = context->gl_info;
1283 float min, max;
1284 float size, att[3];
1286 get_pointsize_minmax(context, state, &min, &max);
1288 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1289 checkGLcall("glUniform1f");
1290 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1291 checkGLcall("glUniform1f");
1293 get_pointsize(context, state, &size, att);
1295 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1296 checkGLcall("glUniform1f");
1297 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1298 checkGLcall("glUniform1f");
1299 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1300 checkGLcall("glUniform1f");
1301 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1302 checkGLcall("glUniform1f");
1305 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1306 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1308 const struct wined3d_gl_info *gl_info = context->gl_info;
1309 struct wined3d_color color;
1310 float start, end, scale;
1311 union
1313 DWORD d;
1314 float f;
1315 } tmpvalue;
1317 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1318 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1319 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1320 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1321 get_fog_start_end(context, state, &start, &end);
1322 scale = 1.0f / (end - start);
1323 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1324 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1325 checkGLcall("fog emulation uniforms");
1328 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1329 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1331 const struct wined3d_gl_info *gl_info = context->gl_info;
1332 struct wined3d_vec4 plane;
1334 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1335 if (!use_vs(state))
1336 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1337 else
1338 plane = state->clip_planes[index];
1340 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1343 /* Context activation is done by the caller (state handler). */
1344 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1345 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1347 struct wined3d_color float_key[2];
1348 const struct wined3d_texture *texture = state->textures[0];
1350 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1351 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1354 /* Context activation is done by the caller (state handler). */
1355 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1356 const struct wined3d_state *state)
1358 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1359 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1360 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1361 const struct wined3d_gl_info *gl_info = context->gl_info;
1362 struct shader_glsl_priv *priv = shader_priv;
1363 float position_fixup[4];
1364 DWORD update_mask;
1366 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1367 UINT constant_version;
1368 int i;
1370 if (!prog) {
1371 /* No GLSL program set - nothing to do. */
1372 return;
1374 constant_version = prog->constant_version;
1375 update_mask = context->constant_update_mask & prog->constant_update_mask;
1377 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1378 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1379 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1381 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1382 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1383 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1385 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1386 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1387 vshader->reg_maps.boolean_constants);
1389 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1391 for (i = 0; i < gl_info->limits.clipplanes; ++i)
1392 shader_glsl_clip_plane_uniform(context, state, i, prog);
1395 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1396 shader_glsl_pointsize_uniform(context, state, prog);
1398 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1400 shader_get_position_fixup(context, state, position_fixup);
1401 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1402 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1403 else
1404 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1405 checkGLcall("glUniform4fv");
1408 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1410 struct wined3d_matrix mat;
1412 get_modelview_matrix(context, state, 0, &mat);
1413 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1414 checkGLcall("glUniformMatrix4fv");
1416 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1419 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1421 struct wined3d_matrix mat;
1423 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1425 if (prog->vs.modelview_matrix_location[i] == -1)
1426 break;
1428 get_modelview_matrix(context, state, i, &mat);
1429 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1430 checkGLcall("glUniformMatrix4fv");
1434 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1436 struct wined3d_matrix projection;
1438 get_projection_matrix(context, state, &projection);
1439 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1440 checkGLcall("glUniformMatrix4fv");
1443 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1445 for (i = 0; i < MAX_TEXTURES; ++i)
1446 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1449 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1450 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1452 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1454 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1455 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1456 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1459 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1460 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1461 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1463 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1464 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1465 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1467 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1468 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1469 pshader->reg_maps.boolean_constants);
1471 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1473 for (i = 0; i < MAX_TEXTURES; ++i)
1475 if (prog->ps.bumpenv_mat_location[i] == -1)
1476 continue;
1478 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1479 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1481 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1483 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1484 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1485 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1486 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1490 checkGLcall("bump env uniforms");
1493 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1495 const struct wined3d_vec4 correction_params =
1497 /* Position is relative to the framebuffer, not the viewport. */
1498 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1499 context->render_offscreen ? 1.0f : -1.0f,
1500 0.0f,
1501 0.0f,
1504 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1507 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1508 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1509 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1510 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1512 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1514 struct wined3d_color color;
1516 if (prog->ps.tex_factor_location != -1)
1518 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1519 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1522 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1523 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1524 else
1525 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1527 for (i = 0; i < MAX_TEXTURES; ++i)
1529 if (prog->ps.tss_constant_location[i] == -1)
1530 continue;
1532 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1533 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1536 checkGLcall("fixed function uniforms");
1539 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1540 shader_glsl_load_fog_uniform(context, state, prog);
1542 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1544 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1546 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1547 checkGLcall("alpha test emulation uniform");
1550 if (priv->next_constant_version == UINT_MAX)
1552 TRACE("Max constant version reached, resetting to 0.\n");
1553 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1554 priv->next_constant_version = 1;
1556 else
1558 prog->constant_version = priv->next_constant_version++;
1562 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1564 struct constant_entry *entries = heap->entries;
1565 unsigned int *positions = heap->positions;
1566 unsigned int heap_idx, parent_idx;
1568 if (!heap->contained[idx])
1570 heap_idx = heap->size++;
1571 heap->contained[idx] = TRUE;
1573 else
1575 heap_idx = positions[idx];
1578 while (heap_idx > 1)
1580 parent_idx = heap_idx >> 1;
1582 if (new_version <= entries[parent_idx].version) break;
1584 entries[heap_idx] = entries[parent_idx];
1585 positions[entries[parent_idx].idx] = heap_idx;
1586 heap_idx = parent_idx;
1589 entries[heap_idx].version = new_version;
1590 entries[heap_idx].idx = idx;
1591 positions[idx] = heap_idx;
1594 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1596 struct shader_glsl_priv *priv = device->shader_priv;
1597 struct constant_heap *heap = &priv->vconst_heap;
1598 UINT i;
1600 for (i = start; i < count + start; ++i)
1602 update_heap_entry(heap, i, priv->next_constant_version);
1606 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1608 struct shader_glsl_priv *priv = device->shader_priv;
1609 struct constant_heap *heap = &priv->pconst_heap;
1610 UINT i;
1612 for (i = start; i < count + start; ++i)
1614 update_heap_entry(heap, i, priv->next_constant_version);
1618 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1620 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1621 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1622 if(shader_major > 3) return ret;
1624 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1625 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1626 return ret;
1629 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1631 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1634 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1636 return !needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION];
1639 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1641 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1644 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1645 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1647 va_list args;
1648 int ret;
1650 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1651 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1652 for (;;)
1654 va_start(args, format);
1655 ret = shader_vaddline(buffer, format, args);
1656 va_end(args);
1657 if (!ret)
1658 return;
1659 if (!string_buffer_resize(buffer, ret))
1660 return;
1664 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1665 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1667 va_list args;
1668 int ret;
1670 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1671 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1672 for (;;)
1674 va_start(args, format);
1675 ret = shader_vaddline(buffer, format, args);
1676 va_end(args);
1677 if (!ret)
1678 return;
1679 if (!string_buffer_resize(buffer, ret))
1680 return;
1684 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1686 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1689 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
1691 switch (primitive_type)
1693 case WINED3D_PT_POINTLIST:
1694 return "points";
1696 case WINED3D_PT_LINELIST:
1697 return "lines";
1699 case WINED3D_PT_LINESTRIP:
1700 return "line_strip";
1702 case WINED3D_PT_TRIANGLELIST:
1703 return "triangles";
1705 case WINED3D_PT_TRIANGLESTRIP:
1706 return "triangle_strip";
1708 case WINED3D_PT_LINELIST_ADJ:
1709 return "lines_adjacency";
1711 case WINED3D_PT_TRIANGLELIST_ADJ:
1712 return "triangles_adjacency";
1714 default:
1715 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
1716 return "";
1720 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1722 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1723 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1724 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1725 unsigned int i;
1727 if (reg_maps->shader_version.major < 3)
1728 return input_reg_used[idx];
1730 for (i = 0; i < input_signature->element_count; ++i)
1732 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1734 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1735 continue;
1737 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1738 && input->semantic_idx == idx)
1740 if (input_reg_used[input->register_idx])
1741 return TRUE;
1742 else
1743 return FALSE;
1746 return FALSE;
1749 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1750 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1752 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1754 if (version->major >= 4)
1755 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1756 else
1757 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1760 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1761 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1762 unsigned int index)
1764 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1765 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1766 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1767 index, scalar_type, scalar_type, index);
1770 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
1771 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
1773 unsigned int index = e->register_idx;
1775 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1777 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1778 index);
1779 return;
1781 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1783 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1784 index);
1785 return;
1787 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
1788 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
1790 if (shader_glsl_use_explicit_attrib_location(gl_info))
1791 shader_addline(buffer, "layout(location = %u) ", index);
1793 switch (e->component_type)
1795 case WINED3D_TYPE_UINT:
1796 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
1797 break;
1798 case WINED3D_TYPE_INT:
1799 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
1800 break;
1802 default:
1803 FIXME("Unhandled type %#x.\n", e->component_type);
1804 /* Fall through. */
1805 case WINED3D_TYPE_UNKNOWN:
1806 case WINED3D_TYPE_FLOAT:
1807 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
1808 break;
1812 /** Generate the variable & register declarations for the GLSL output target */
1813 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1814 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1815 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1817 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1818 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1819 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1820 const struct wined3d_gl_info *gl_info = context->gl_info;
1821 const struct wined3d_shader_indexable_temp *idx_temp_reg;
1822 unsigned int i, extra_constants_needed = 0;
1823 const struct wined3d_shader_lconst *lconst;
1824 const char *prefix;
1825 DWORD map;
1827 prefix = shader_glsl_get_prefix(version->type);
1829 /* Prototype the subroutines */
1830 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1832 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1835 /* Declare the constants (aka uniforms) */
1836 if (shader->limits->constant_float > 0)
1838 unsigned max_constantsF;
1840 /* Unless the shader uses indirect addressing, always declare the
1841 * maximum array size and ignore that we need some uniforms privately.
1842 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1843 * and immediate values, still declare VC[256]. If the shader needs
1844 * more uniforms than we have it won't work in any case. If it uses
1845 * less, the compiler will figure out which uniforms are really used
1846 * and strip them out. This allows a shader to use c255 on a dx9 card,
1847 * as long as it doesn't also use all the other constants.
1849 * If the shader uses indirect addressing the compiler must assume
1850 * that all declared uniforms are used. In this case, declare only the
1851 * amount that we're assured to have.
1853 * Thus we run into problems in these two cases:
1854 * 1) The shader really uses more uniforms than supported.
1855 * 2) The shader uses indirect addressing, less constants than
1856 * supported, but uses a constant index > #supported consts. */
1857 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1859 /* No indirect addressing here. */
1860 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1862 else
1864 if (reg_maps->usesrelconstF)
1866 /* Subtract the other potential uniforms from the max
1867 * available (bools, ints, and 1 row of projection matrix).
1868 * Subtract another uniform for immediate values, which have
1869 * to be loaded via uniform by the driver as well. The shader
1870 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1871 * shader code, so one vec4 should be enough. (Unfortunately
1872 * the Nvidia driver doesn't store 128 and -128 in one float).
1874 * Writing gl_ClipVertex requires one uniform for each
1875 * clipplane as well. */
1876 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1877 if (vs_args->clip_enabled)
1878 max_constantsF -= gl_info->limits.clipplanes;
1879 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1880 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1881 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1882 * for now take this into account when calculating the number of available constants
1884 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1885 /* Set by driver quirks in directx.c */
1886 max_constantsF -= gl_info->reserved_glsl_constants;
1888 if (max_constantsF < shader->limits->constant_float)
1890 static unsigned int once;
1892 if (!once++)
1893 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1894 " it may not render correctly.\n");
1895 else
1896 WARN("The hardware does not support enough uniform components to run this shader.\n");
1899 else
1901 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1904 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1905 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1908 /* Always declare the full set of constants, the compiler can remove the
1909 * unused ones because d3d doesn't (yet) support indirect int and bool
1910 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1911 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1912 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1914 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1915 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1917 /* Declare immediate constant buffer */
1918 if (reg_maps->icb)
1919 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
1921 /* Declare constant buffers */
1922 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1924 if (reg_maps->cb_sizes[i])
1925 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1926 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1929 /* Declare texture samplers */
1930 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1932 struct wined3d_shader_sampler_map_entry *entry;
1933 const char *sampler_type_prefix, *sampler_type;
1934 BOOL shadow_sampler, tex_rect;
1936 entry = &reg_maps->sampler_map.entries[i];
1938 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1940 ERR("Invalid resource index %u.\n", entry->resource_idx);
1941 continue;
1944 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1946 case WINED3D_DATA_FLOAT:
1947 case WINED3D_DATA_UNORM:
1948 case WINED3D_DATA_SNORM:
1949 sampler_type_prefix = "";
1950 break;
1952 case WINED3D_DATA_INT:
1953 sampler_type_prefix = "i";
1954 break;
1956 case WINED3D_DATA_UINT:
1957 sampler_type_prefix = "u";
1958 break;
1960 default:
1961 sampler_type_prefix = "";
1962 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1963 break;
1966 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
1967 switch (reg_maps->resource_info[entry->resource_idx].type)
1969 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1970 if (shadow_sampler)
1971 sampler_type = "sampler1DShadow";
1972 else
1973 sampler_type = "sampler1D";
1974 break;
1976 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1977 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1978 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1979 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1980 if (shadow_sampler)
1982 if (tex_rect)
1983 sampler_type = "sampler2DRectShadow";
1984 else
1985 sampler_type = "sampler2DShadow";
1987 else
1989 if (tex_rect)
1990 sampler_type = "sampler2DRect";
1991 else
1992 sampler_type = "sampler2D";
1994 break;
1996 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1997 if (shadow_sampler)
1998 FIXME("Unsupported 3D shadow sampler.\n");
1999 sampler_type = "sampler3D";
2000 break;
2002 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2003 if (shadow_sampler)
2004 FIXME("Unsupported Cube shadow sampler.\n");
2005 sampler_type = "samplerCube";
2006 break;
2008 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2009 if (shadow_sampler)
2010 sampler_type = "sampler2DArrayShadow";
2011 else
2012 sampler_type = "sampler2DArray";
2013 break;
2015 default:
2016 sampler_type = "unsupported_sampler";
2017 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2018 break;
2020 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2021 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2024 /* Declare uniforms for NP2 texcoord fixup:
2025 * This is NOT done inside the loop that declares the texture samplers
2026 * since the NP2 fixup code is currently only used for the GeforceFX
2027 * series and when forcing the ARB_npot extension off. Modern cards just
2028 * skip the code anyway, so put it inside a separate loop. */
2029 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
2031 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
2032 UINT cur = 0;
2034 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2035 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2036 * samplerNP2Fixup stores texture dimensions and is updated through
2037 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2039 for (i = 0; i < shader->limits->sampler; ++i)
2041 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
2042 continue;
2044 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
2046 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2047 continue;
2050 fixup->idx[i] = cur++;
2053 fixup->num_consts = (cur + 1) >> 1;
2054 fixup->active = ps_args->np2_fixup;
2055 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
2058 /* Declare address variables */
2059 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2061 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2064 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2066 for (i = 0; i < shader->input_signature.element_count; ++i)
2067 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
2069 if (vs_args->point_size && !vs_args->per_vertex_point_size)
2071 shader_addline(buffer, "uniform struct\n{\n");
2072 shader_addline(buffer, " float size;\n");
2073 shader_addline(buffer, " float size_min;\n");
2074 shader_addline(buffer, " float size_max;\n");
2075 shader_addline(buffer, "} ffp_point;\n");
2078 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2080 if (vs_args->clip_enabled)
2081 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.clipplanes);
2083 if (version->major < 3)
2085 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2086 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2087 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2088 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2092 if (version->major < 4)
2093 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2095 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2097 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2099 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2101 else
2103 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2104 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2105 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2106 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2109 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2111 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2113 shader_addline(buffer, "uniform struct\n{\n");
2114 shader_addline(buffer, " vec4 color;\n");
2115 shader_addline(buffer, " float density;\n");
2116 shader_addline(buffer, " float end;\n");
2117 shader_addline(buffer, " float scale;\n");
2118 shader_addline(buffer, "} ffp_fog;\n");
2120 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2122 if (glsl_is_color_reg_read(shader, 0))
2123 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2124 if (glsl_is_color_reg_read(shader, 1))
2125 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2126 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2127 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2129 else
2131 if (glsl_is_color_reg_read(shader, 0))
2132 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2133 if (glsl_is_color_reg_read(shader, 1))
2134 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2135 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2136 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2137 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2141 if (version->major >= 3)
2143 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2145 if (ps_args->vp_mode == vertexshader)
2146 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2147 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2150 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2152 if (!(map & 1))
2153 continue;
2155 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2157 if (reg_maps->luminanceparams & (1u << i))
2159 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2160 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2161 extra_constants_needed++;
2164 extra_constants_needed++;
2167 if (ps_args->srgb_correction)
2169 shader_addline(buffer, "const vec4 srgb_const0 = ");
2170 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2171 shader_addline(buffer, ";\n");
2172 shader_addline(buffer, "const vec4 srgb_const1 = ");
2173 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2174 shader_addline(buffer, ";\n");
2176 if (reg_maps->vpos || reg_maps->usesdsy)
2178 ++extra_constants_needed;
2179 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2180 shader_addline(buffer, "vec4 vpos;\n");
2183 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2184 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2186 if (!needs_legacy_glsl_syntax(gl_info))
2187 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2189 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2190 FIXME("Insufficient uniforms to run this shader.\n");
2193 /* Declare output register temporaries */
2194 if (shader->limits->packed_output)
2195 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2197 /* Declare temporary variables */
2198 if (reg_maps->temporary_count)
2200 for (i = 0; i < reg_maps->temporary_count; ++i)
2201 shader_addline(buffer, "vec4 R%u;\n", i);
2203 else
2205 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2207 if (map & 1)
2208 shader_addline(buffer, "vec4 R%u;\n", i);
2212 /* Declare indexable temporary variables */
2213 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2215 if (idx_temp_reg->component_count != 4)
2216 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2217 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2220 /* Declare loop registers aLx */
2221 if (version->major < 4)
2223 for (i = 0; i < reg_maps->loop_depth; ++i)
2225 shader_addline(buffer, "int aL%u;\n", i);
2226 shader_addline(buffer, "int tmpInt%u;\n", i);
2230 /* Temporary variables for matrix operations */
2231 shader_addline(buffer, "vec4 tmp0;\n");
2232 shader_addline(buffer, "vec4 tmp1;\n");
2234 if (!shader->load_local_constsF)
2236 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2238 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2239 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2240 shader_addline(buffer, ";\n");
2245 /*****************************************************************************
2246 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2248 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2249 ****************************************************************************/
2251 /* Prototypes */
2252 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2253 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2255 /** Used for opcode modifiers - They multiply the result by the specified amount */
2256 static const char * const shift_glsl_tab[] = {
2257 "", /* 0 (none) */
2258 "2.0 * ", /* 1 (x2) */
2259 "4.0 * ", /* 2 (x4) */
2260 "8.0 * ", /* 3 (x8) */
2261 "16.0 * ", /* 4 (x16) */
2262 "32.0 * ", /* 5 (x32) */
2263 "", /* 6 (x64) */
2264 "", /* 7 (x128) */
2265 "", /* 8 (d256) */
2266 "", /* 9 (d128) */
2267 "", /* 10 (d64) */
2268 "", /* 11 (d32) */
2269 "0.0625 * ", /* 12 (d16) */
2270 "0.125 * ", /* 13 (d8) */
2271 "0.25 * ", /* 14 (d4) */
2272 "0.5 * " /* 15 (d2) */
2275 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2276 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2277 const char *in_reg, const char *in_regswizzle, char *out_str)
2279 switch (src_modifier)
2281 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2282 case WINED3DSPSM_DW:
2283 case WINED3DSPSM_NONE:
2284 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2285 break;
2286 case WINED3DSPSM_NEG:
2287 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2288 break;
2289 case WINED3DSPSM_NOT:
2290 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2291 break;
2292 case WINED3DSPSM_BIAS:
2293 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2294 break;
2295 case WINED3DSPSM_BIASNEG:
2296 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2297 break;
2298 case WINED3DSPSM_SIGN:
2299 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2300 break;
2301 case WINED3DSPSM_SIGNNEG:
2302 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2303 break;
2304 case WINED3DSPSM_COMP:
2305 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2306 break;
2307 case WINED3DSPSM_X2:
2308 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2309 break;
2310 case WINED3DSPSM_X2NEG:
2311 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2312 break;
2313 case WINED3DSPSM_ABS:
2314 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2315 break;
2316 case WINED3DSPSM_ABSNEG:
2317 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2318 break;
2319 default:
2320 FIXME("Unhandled modifier %u\n", src_modifier);
2321 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2325 /** Writes the GLSL variable name that corresponds to the register that the
2326 * DX opcode parameter is trying to access */
2327 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2328 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2330 /* oPos, oFog and oPts in D3D */
2331 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2333 const struct wined3d_shader *shader = ins->ctx->shader;
2334 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2335 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2336 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2337 const char *prefix = shader_glsl_get_prefix(version->type);
2338 struct glsl_src_param rel_param0, rel_param1;
2339 char imm_str[4][17];
2341 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2342 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2343 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2344 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2345 *is_color = FALSE;
2347 switch (reg->type)
2349 case WINED3DSPR_TEMP:
2350 sprintf(register_name, "R%u", reg->idx[0].offset);
2351 break;
2353 case WINED3DSPR_INPUT:
2354 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2356 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2358 if (reg->idx[0].rel_addr)
2359 FIXME("VS3+ input registers relative addressing.\n");
2360 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2361 *is_color = TRUE;
2362 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2363 break;
2366 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2368 if (reg->idx[0].rel_addr)
2370 if (reg->idx[1].rel_addr)
2371 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2372 rel_param0.param_str, reg->idx[0].offset,
2373 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2374 rel_param1.param_str, reg->idx[1].offset);
2375 else
2376 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2377 rel_param0.param_str, reg->idx[0].offset,
2378 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2379 reg->idx[1].offset);
2381 else if (reg->idx[1].rel_addr)
2382 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2383 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2384 rel_param1.param_str, reg->idx[1].offset);
2385 else
2386 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2387 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2388 reg->idx[1].offset);
2389 break;
2392 /* pixel shaders >= 3.0 */
2393 if (version->major >= 3)
2395 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2396 unsigned int in_count = vec4_varyings(version->major, gl_info);
2398 if (reg->idx[0].rel_addr)
2400 /* Removing a + 0 would be an obvious optimization, but
2401 * OS X doesn't see the NOP operation there. */
2402 if (idx)
2404 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2405 && shader->u.ps.declared_in_count > in_count)
2407 sprintf(register_name,
2408 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2409 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2410 prefix, rel_param0.param_str, idx);
2412 else
2414 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2417 else
2419 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2420 && shader->u.ps.declared_in_count > in_count)
2422 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2423 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2424 prefix, rel_param0.param_str);
2426 else
2428 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2432 else
2434 if (idx == in_count) sprintf(register_name, "gl_Color");
2435 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2436 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2439 else
2441 if (!reg->idx[0].offset)
2442 strcpy(register_name, "ffp_varying_diffuse");
2443 else
2444 strcpy(register_name, "ffp_varying_specular");
2445 break;
2447 break;
2449 case WINED3DSPR_CONST:
2451 /* Relative addressing */
2452 if (reg->idx[0].rel_addr)
2454 if (wined3d_settings.check_float_constants)
2455 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2456 rel_param0.param_str, reg->idx[0].offset,
2457 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2458 prefix, rel_param0.param_str, reg->idx[0].offset);
2459 else if (reg->idx[0].offset)
2460 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2461 else
2462 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2464 else
2466 if (shader_constant_is_local(shader, reg->idx[0].offset))
2467 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2468 else
2469 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2472 break;
2474 case WINED3DSPR_CONSTINT:
2475 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2476 break;
2478 case WINED3DSPR_CONSTBOOL:
2479 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2480 break;
2482 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2483 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2484 sprintf(register_name, "T%u", reg->idx[0].offset);
2485 else
2486 sprintf(register_name, "A%u", reg->idx[0].offset);
2487 break;
2489 case WINED3DSPR_LOOP:
2490 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2491 break;
2493 case WINED3DSPR_SAMPLER:
2494 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2495 break;
2497 case WINED3DSPR_COLOROUT:
2498 if (reg->idx[0].offset >= gl_info->limits.buffers)
2499 WARN("Write to render target %u, only %d supported.\n",
2500 reg->idx[0].offset, gl_info->limits.buffers);
2502 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2503 break;
2505 case WINED3DSPR_RASTOUT:
2506 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2507 break;
2509 case WINED3DSPR_DEPTHOUT:
2510 sprintf(register_name, "gl_FragDepth");
2511 break;
2513 case WINED3DSPR_ATTROUT:
2514 if (!reg->idx[0].offset)
2515 sprintf(register_name, "%s_out[8]", prefix);
2516 else
2517 sprintf(register_name, "%s_out[9]", prefix);
2518 break;
2520 case WINED3DSPR_TEXCRDOUT:
2521 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2522 if (reg->idx[0].rel_addr)
2523 FIXME("VS3 output registers relative addressing.\n");
2524 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2525 break;
2527 case WINED3DSPR_MISCTYPE:
2528 if (!reg->idx[0].offset)
2530 /* vPos */
2531 sprintf(register_name, "vpos");
2533 else if (reg->idx[0].offset == 1)
2535 /* Note that gl_FrontFacing is a bool, while vFace is
2536 * a float for which the sign determines front/back */
2537 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2539 else
2541 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2542 sprintf(register_name, "unrecognized_register");
2544 break;
2546 case WINED3DSPR_IMMCONST:
2547 switch (reg->immconst_type)
2549 case WINED3D_IMMCONST_SCALAR:
2550 switch (reg->data_type)
2552 case WINED3D_DATA_FLOAT:
2553 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2554 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->immconst_data[0]);
2555 else
2556 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2557 break;
2558 case WINED3D_DATA_INT:
2559 sprintf(register_name, "%#x", reg->immconst_data[0]);
2560 break;
2561 case WINED3D_DATA_RESOURCE:
2562 case WINED3D_DATA_SAMPLER:
2563 case WINED3D_DATA_UINT:
2564 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2565 break;
2566 default:
2567 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2568 break;
2570 break;
2572 case WINED3D_IMMCONST_VEC4:
2573 switch (reg->data_type)
2575 case WINED3D_DATA_FLOAT:
2576 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2578 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2579 reg->immconst_data[0], reg->immconst_data[1],
2580 reg->immconst_data[2], reg->immconst_data[3]);
2582 else
2584 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2585 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2586 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2587 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2588 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2589 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2591 break;
2592 case WINED3D_DATA_INT:
2593 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2594 reg->immconst_data[0], reg->immconst_data[1],
2595 reg->immconst_data[2], reg->immconst_data[3]);
2596 break;
2597 case WINED3D_DATA_RESOURCE:
2598 case WINED3D_DATA_SAMPLER:
2599 case WINED3D_DATA_UINT:
2600 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2601 reg->immconst_data[0], reg->immconst_data[1],
2602 reg->immconst_data[2], reg->immconst_data[3]);
2603 break;
2604 default:
2605 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2606 break;
2608 break;
2610 default:
2611 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2612 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2614 break;
2616 case WINED3DSPR_CONSTBUFFER:
2617 if (reg->idx[1].rel_addr)
2618 sprintf(register_name, "%s_cb%u[%s + %u]",
2619 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2620 else
2621 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2622 break;
2624 case WINED3DSPR_IMMCONSTBUFFER:
2625 if (reg->idx[0].rel_addr)
2626 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2627 else
2628 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2629 break;
2631 case WINED3DSPR_PRIMID:
2632 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2633 break;
2635 case WINED3DSPR_IDXTEMP:
2636 if (reg->idx[1].rel_addr)
2637 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2638 else
2639 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2640 break;
2642 default:
2643 FIXME("Unhandled register type %#x.\n", reg->type);
2644 sprintf(register_name, "unrecognized_register");
2645 break;
2649 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2651 *str++ = '.';
2652 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2653 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2654 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2655 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2656 *str = '\0';
2659 /* Get the GLSL write mask for the destination register */
2660 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2662 DWORD mask = param->write_mask;
2664 if (shader_is_scalar(&param->reg))
2666 mask = WINED3DSP_WRITEMASK_0;
2667 *write_mask = '\0';
2669 else
2671 shader_glsl_write_mask_to_str(mask, write_mask);
2674 return mask;
2677 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2678 unsigned int size = 0;
2680 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2681 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2682 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2683 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2685 return size;
2688 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2690 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2691 * but addressed as "rgba". To fix this we need to swap the register's x
2692 * and z components. */
2693 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2695 *str++ = '.';
2696 /* swizzle bits fields: wwzzyyxx */
2697 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2698 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2699 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2700 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2701 *str = '\0';
2704 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2705 BOOL fixup, DWORD mask, char *swizzle_str)
2707 if (shader_is_scalar(&param->reg))
2708 *swizzle_str = '\0';
2709 else
2710 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2713 /* From a given parameter token, generate the corresponding GLSL string.
2714 * Also, return the actual register name and swizzle in case the
2715 * caller needs this information as well. */
2716 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2717 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2719 BOOL is_color = FALSE;
2720 char swizzle_str[6];
2722 glsl_src->reg_name[0] = '\0';
2723 glsl_src->param_str[0] = '\0';
2724 swizzle_str[0] = '\0';
2726 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2727 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2729 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2731 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2733 else
2735 char reg_name[200];
2737 switch (wined3d_src->reg.data_type)
2739 case WINED3D_DATA_FLOAT:
2740 sprintf(reg_name, "%s", glsl_src->reg_name);
2741 break;
2742 case WINED3D_DATA_INT:
2743 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2744 break;
2745 case WINED3D_DATA_RESOURCE:
2746 case WINED3D_DATA_SAMPLER:
2747 case WINED3D_DATA_UINT:
2748 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2749 break;
2750 default:
2751 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2752 sprintf(reg_name, "%s", glsl_src->reg_name);
2753 break;
2756 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2760 /* From a given parameter token, generate the corresponding GLSL string.
2761 * Also, return the actual register name and swizzle in case the
2762 * caller needs this information as well. */
2763 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2764 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2766 BOOL is_color = FALSE;
2768 glsl_dst->mask_str[0] = '\0';
2769 glsl_dst->reg_name[0] = '\0';
2771 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2772 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2775 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2776 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2777 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2778 enum wined3d_data_type data_type)
2780 struct glsl_dst_param glsl_dst;
2781 DWORD mask;
2783 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2785 switch (data_type)
2787 case WINED3D_DATA_FLOAT:
2788 shader_addline(buffer, "%s%s = %s(",
2789 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2790 break;
2791 case WINED3D_DATA_INT:
2792 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2793 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2794 break;
2795 case WINED3D_DATA_RESOURCE:
2796 case WINED3D_DATA_SAMPLER:
2797 case WINED3D_DATA_UINT:
2798 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2799 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2800 break;
2801 default:
2802 FIXME("Unhandled data type %#x.\n", data_type);
2803 shader_addline(buffer, "%s%s = %s(",
2804 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2805 break;
2809 return mask;
2812 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2813 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2815 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2818 /** Process GLSL instruction modifiers */
2819 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2821 struct glsl_dst_param dst_param;
2822 DWORD modifiers;
2824 if (!ins->dst_count) return;
2826 modifiers = ins->dst[0].modifiers;
2827 if (!modifiers) return;
2829 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2831 if (modifiers & WINED3DSPDM_SATURATE)
2833 /* _SAT means to clamp the value of the register to between 0 and 1 */
2834 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2835 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2838 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2840 FIXME("_centroid modifier not handled\n");
2843 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2845 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2849 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2851 switch (op)
2853 case WINED3D_SHADER_REL_OP_GT: return ">";
2854 case WINED3D_SHADER_REL_OP_EQ: return "==";
2855 case WINED3D_SHADER_REL_OP_GE: return ">=";
2856 case WINED3D_SHADER_REL_OP_LT: return "<";
2857 case WINED3D_SHADER_REL_OP_NE: return "!=";
2858 case WINED3D_SHADER_REL_OP_LE: return "<=";
2859 default:
2860 FIXME("Unrecognized operator %#x.\n", op);
2861 return "(\?\?)";
2865 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2866 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
2868 static const struct
2870 unsigned int coord_size;
2871 const char *type_part;
2873 resource_types[] =
2875 {0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2876 {1, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2877 {1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2878 {2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2879 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2880 {3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2881 {3, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2882 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2883 {3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2884 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2886 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2887 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2888 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2889 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
2890 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2891 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2892 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
2893 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2894 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2895 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2896 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2897 const char *base = "texture", *type_part = "", *suffix = "";
2898 unsigned int coord_size, deriv_size;
2899 BOOL array;
2901 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2903 if (resource_type >= ARRAY_SIZE(resource_types))
2905 ERR("Unexpected resource type %#x.\n", resource_type);
2906 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2908 array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
2909 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
2911 /* Note that there's no such thing as a projected cube texture. */
2912 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2913 projected = FALSE;
2915 if (needs_legacy_glsl_syntax(gl_info))
2917 if (shadow)
2918 base = "shadow";
2920 type_part = resource_types[resource_type].type_part;
2921 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2922 type_part = "2DRect";
2923 if (!type_part[0])
2924 FIXME("Unhandled resource type %#x.\n", resource_type);
2926 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2928 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2929 suffix = "ARB";
2930 else
2931 FIXME("Unsupported grad function.\n");
2935 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2937 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2938 if (flags & ~texel_fetch_flags)
2939 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
2941 base = "texelFetch";
2942 type_part = "";
2945 sample_function->name = string_buffer_get(priv->string_buffers);
2946 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2947 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2949 coord_size = resource_types[resource_type].coord_size;
2950 deriv_size = coord_size;
2951 if (shadow)
2952 ++coord_size;
2953 if (array)
2954 --deriv_size;
2955 sample_function->offset_size = offset ? deriv_size : 0;
2956 sample_function->coord_mask = (1u << coord_size) - 1;
2957 sample_function->deriv_mask = (1u << deriv_size) - 1;
2958 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2961 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2962 struct glsl_sample_function *sample_function)
2964 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2966 string_buffer_release(priv->string_buffers, sample_function->name);
2969 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2970 BOOL sign_fixup, enum fixup_channel_source channel_source)
2972 switch(channel_source)
2974 case CHANNEL_SOURCE_ZERO:
2975 strcat(arguments, "0.0");
2976 break;
2978 case CHANNEL_SOURCE_ONE:
2979 strcat(arguments, "1.0");
2980 break;
2982 case CHANNEL_SOURCE_X:
2983 strcat(arguments, reg_name);
2984 strcat(arguments, ".x");
2985 break;
2987 case CHANNEL_SOURCE_Y:
2988 strcat(arguments, reg_name);
2989 strcat(arguments, ".y");
2990 break;
2992 case CHANNEL_SOURCE_Z:
2993 strcat(arguments, reg_name);
2994 strcat(arguments, ".z");
2995 break;
2997 case CHANNEL_SOURCE_W:
2998 strcat(arguments, reg_name);
2999 strcat(arguments, ".w");
3000 break;
3002 default:
3003 FIXME("Unhandled channel source %#x\n", channel_source);
3004 strcat(arguments, "undefined");
3005 break;
3008 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3011 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3012 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3014 unsigned int mask_size, remaining;
3015 DWORD fixup_mask = 0;
3016 char arguments[256];
3017 char mask_str[6];
3019 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3020 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3021 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3022 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3023 if (!(mask &= fixup_mask))
3024 return;
3026 if (is_complex_fixup(fixup))
3028 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3029 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3030 return;
3033 shader_glsl_write_mask_to_str(mask, mask_str);
3034 mask_size = shader_glsl_get_write_mask_size(mask);
3036 arguments[0] = '\0';
3037 remaining = mask_size;
3038 if (mask & WINED3DSP_WRITEMASK_0)
3040 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3041 if (--remaining) strcat(arguments, ", ");
3043 if (mask & WINED3DSP_WRITEMASK_1)
3045 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3046 if (--remaining) strcat(arguments, ", ");
3048 if (mask & WINED3DSP_WRITEMASK_2)
3050 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3051 if (--remaining) strcat(arguments, ", ");
3053 if (mask & WINED3DSP_WRITEMASK_3)
3055 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3056 if (--remaining) strcat(arguments, ", ");
3059 if (mask_size > 1)
3060 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3061 else
3062 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3065 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3067 char reg_name[256];
3068 BOOL is_color;
3070 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3071 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3074 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3075 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3076 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3077 const char *coord_reg_fmt, ...)
3079 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3080 char dst_swizzle[6];
3081 struct color_fixup_desc fixup;
3082 BOOL np2_fixup = FALSE;
3083 va_list args;
3084 int ret;
3086 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3088 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3089 * We actually rely on it for vertex shaders and SM4+. */
3090 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3092 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3093 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3095 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3096 np2_fixup = TRUE;
3098 else
3100 fixup = COLOR_FIXUP_IDENTITY;
3103 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3105 if (sample_function->output_single_component)
3106 shader_addline(ins->ctx->buffer, "vec4(");
3108 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3109 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3111 for (;;)
3113 va_start(args, coord_reg_fmt);
3114 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3115 va_end(args);
3116 if (!ret)
3117 break;
3118 if (!string_buffer_resize(ins->ctx->buffer, ret))
3119 break;
3122 if (np2_fixup)
3124 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3125 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3127 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3129 case 1:
3130 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3131 idx >> 1, (idx % 2) ? "z" : "x");
3132 break;
3133 case 2:
3134 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3135 idx >> 1, (idx % 2) ? "zw" : "xy");
3136 break;
3137 case 3:
3138 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3139 idx >> 1, (idx % 2) ? "zw" : "xy");
3140 break;
3141 case 4:
3142 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3143 idx >> 1, (idx % 2) ? "zw" : "xy");
3144 break;
3147 if (dx && dy)
3148 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3149 else if (bias)
3150 shader_addline(ins->ctx->buffer, ", %s", bias);
3151 if (sample_function->offset_size)
3153 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3154 shader_addline(ins->ctx->buffer, ", ");
3155 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3157 shader_addline(ins->ctx->buffer, ")");
3159 if (sample_function->output_single_component)
3160 shader_addline(ins->ctx->buffer, ")");
3162 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3164 if (!is_identity_fixup(fixup))
3165 shader_glsl_color_correction(ins, fixup);
3168 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3170 /* Write the final position.
3172 * OpenGL coordinates specify the center of the pixel while D3D coords
3173 * specify the corner. The offsets are stored in z and w in
3174 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3175 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3176 * a MAD. */
3177 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3178 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3180 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3181 * in utils.c
3183 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3184 * shaders are run before the homogeneous divide, so we have to take the w
3185 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3186 * z = z * 2 - w. */
3187 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3190 /*****************************************************************************
3191 * Begin processing individual instruction opcodes
3192 ****************************************************************************/
3194 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3196 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3197 struct glsl_src_param src0_param;
3198 struct glsl_src_param src1_param;
3199 DWORD write_mask;
3200 const char *op;
3202 /* Determine the GLSL operator to use based on the opcode */
3203 switch (ins->handler_idx)
3205 case WINED3DSIH_ADD: op = "+"; break;
3206 case WINED3DSIH_AND: op = "&"; break;
3207 case WINED3DSIH_DIV: op = "/"; break;
3208 case WINED3DSIH_IADD: op = "+"; break;
3209 case WINED3DSIH_ISHL: op = "<<"; break;
3210 case WINED3DSIH_ISHR: op = ">>"; break;
3211 case WINED3DSIH_MUL: op = "*"; break;
3212 case WINED3DSIH_OR: op = "|"; break;
3213 case WINED3DSIH_SUB: op = "-"; break;
3214 case WINED3DSIH_USHR: op = ">>"; break;
3215 case WINED3DSIH_XOR: op = "^"; break;
3216 default:
3217 op = "<unhandled operator>";
3218 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3219 break;
3222 write_mask = shader_glsl_append_dst(buffer, ins);
3223 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3224 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3225 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3228 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3230 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3231 struct glsl_src_param src0_param;
3232 struct glsl_src_param src1_param;
3233 unsigned int mask_size;
3234 DWORD write_mask;
3235 const char *op;
3237 write_mask = shader_glsl_append_dst(buffer, ins);
3238 mask_size = shader_glsl_get_write_mask_size(write_mask);
3239 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3240 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3242 if (mask_size > 1)
3244 switch (ins->handler_idx)
3246 case WINED3DSIH_EQ: op = "equal"; break;
3247 case WINED3DSIH_IEQ: op = "equal"; break;
3248 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3249 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3250 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3251 case WINED3DSIH_LT: op = "lessThan"; break;
3252 case WINED3DSIH_ILT: op = "lessThan"; break;
3253 case WINED3DSIH_ULT: op = "lessThan"; break;
3254 case WINED3DSIH_NE: op = "notEqual"; break;
3255 case WINED3DSIH_INE: op = "notEqual"; break;
3256 default:
3257 op = "<unhandled operator>";
3258 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3259 break;
3262 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3263 mask_size, op, src0_param.param_str, src1_param.param_str);
3265 else
3267 switch (ins->handler_idx)
3269 case WINED3DSIH_EQ: op = "=="; break;
3270 case WINED3DSIH_IEQ: op = "=="; break;
3271 case WINED3DSIH_GE: op = ">="; break;
3272 case WINED3DSIH_IGE: op = ">="; break;
3273 case WINED3DSIH_UGE: op = ">="; break;
3274 case WINED3DSIH_LT: op = "<"; break;
3275 case WINED3DSIH_ILT: op = "<"; break;
3276 case WINED3DSIH_ULT: op = "<"; break;
3277 case WINED3DSIH_NE: op = "!="; break;
3278 case WINED3DSIH_INE: op = "!="; break;
3279 default:
3280 op = "<unhandled operator>";
3281 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3282 break;
3285 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3286 src0_param.param_str, op, src1_param.param_str);
3290 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3292 struct glsl_src_param src_param;
3293 DWORD write_mask;
3294 const char *op;
3296 switch (ins->handler_idx)
3298 case WINED3DSIH_INEG: op = "-"; break;
3299 case WINED3DSIH_NOT: op = "~"; break;
3300 default:
3301 op = "<unhandled operator>";
3302 ERR("Unhandled opcode %s.\n",
3303 debug_d3dshaderinstructionhandler(ins->handler_idx));
3304 break;
3307 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3308 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3309 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3312 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3314 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3315 struct glsl_src_param src0_param;
3316 struct glsl_src_param src1_param;
3317 DWORD write_mask;
3319 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3320 * not, we can emulate it. */
3321 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3322 FIXME("64-bit integer multiplies not implemented.\n");
3324 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3326 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3327 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3328 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3330 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3331 src0_param.param_str, src1_param.param_str);
3335 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3337 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3338 struct glsl_src_param src0_param, src1_param;
3339 DWORD write_mask;
3341 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3343 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3345 char dst_mask[6];
3347 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3348 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3349 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3350 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3351 dst_mask, src0_param.param_str, src1_param.param_str);
3353 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3354 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3355 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3356 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3358 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3359 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3361 else
3363 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3364 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3365 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3366 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3369 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3371 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3372 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3373 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3374 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3378 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3379 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3381 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3382 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3383 struct glsl_src_param src0_param;
3384 DWORD write_mask;
3386 write_mask = shader_glsl_append_dst(buffer, ins);
3387 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3389 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3390 * shader versions WINED3DSIO_MOVA is used for this. */
3391 if (ins->ctx->reg_maps->shader_version.major == 1
3392 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3393 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3395 /* This is a simple floor() */
3396 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3397 if (mask_size > 1) {
3398 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3399 } else {
3400 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3403 else if(ins->handler_idx == WINED3DSIH_MOVA)
3405 /* We need to *round* to the nearest int here. */
3406 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3408 if (gl_info->supported[EXT_GPU_SHADER4])
3410 if (mask_size > 1)
3411 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3412 else
3413 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3415 else
3417 if (mask_size > 1)
3418 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3419 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3420 else
3421 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3422 src0_param.param_str, src0_param.param_str);
3425 else
3427 shader_addline(buffer, "%s);\n", src0_param.param_str);
3431 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3432 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3434 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3435 struct glsl_src_param src0_param;
3436 struct glsl_src_param src1_param;
3437 DWORD dst_write_mask, src_write_mask;
3438 unsigned int dst_size;
3440 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3441 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3443 /* dp4 works on vec4, dp3 on vec3, etc. */
3444 if (ins->handler_idx == WINED3DSIH_DP4)
3445 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3446 else if (ins->handler_idx == WINED3DSIH_DP3)
3447 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3448 else
3449 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3451 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3452 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3454 if (dst_size > 1) {
3455 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3456 } else {
3457 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3461 /* Note that this instruction has some restrictions. The destination write mask
3462 * can't contain the w component, and the source swizzles have to be .xyzw */
3463 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3465 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3466 struct glsl_src_param src0_param;
3467 struct glsl_src_param src1_param;
3468 char dst_mask[6];
3470 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3471 shader_glsl_append_dst(ins->ctx->buffer, ins);
3472 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3473 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3474 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3477 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3479 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3481 if (!stream)
3482 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3483 else
3484 FIXME("Unhandled primitive stream %u.\n", stream);
3487 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3488 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3489 * GLSL uses the value as-is. */
3490 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3492 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3493 struct glsl_src_param src0_param;
3494 struct glsl_src_param src1_param;
3495 DWORD dst_write_mask;
3496 unsigned int dst_size;
3498 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3499 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3501 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3502 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3504 if (dst_size > 1)
3506 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3507 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3509 else
3511 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3512 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3516 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3517 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3519 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3520 struct glsl_src_param src_param;
3521 const char *instruction;
3522 DWORD write_mask;
3523 unsigned i;
3525 /* Determine the GLSL function to use based on the opcode */
3526 /* TODO: Possibly make this a table for faster lookups */
3527 switch (ins->handler_idx)
3529 case WINED3DSIH_ABS: instruction = "abs"; break;
3530 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3531 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
3532 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
3533 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3534 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
3535 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
3536 case WINED3DSIH_FRC: instruction = "fract"; break;
3537 case WINED3DSIH_IMAX: instruction = "max"; break;
3538 case WINED3DSIH_IMIN: instruction = "min"; break;
3539 case WINED3DSIH_MAX: instruction = "max"; break;
3540 case WINED3DSIH_MIN: instruction = "min"; break;
3541 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3542 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3543 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3544 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3545 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3546 case WINED3DSIH_UMAX: instruction = "max"; break;
3547 case WINED3DSIH_UMIN: instruction = "min"; break;
3548 default: instruction = "";
3549 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3550 break;
3553 write_mask = shader_glsl_append_dst(buffer, ins);
3555 shader_addline(buffer, "%s(", instruction);
3557 if (ins->src_count)
3559 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3560 shader_addline(buffer, "%s", src_param.param_str);
3561 for (i = 1; i < ins->src_count; ++i)
3563 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3564 shader_addline(buffer, ", %s", src_param.param_str);
3568 shader_addline(buffer, "));\n");
3571 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3573 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3575 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3576 struct glsl_src_param src_param;
3577 unsigned int mask_size;
3578 DWORD write_mask;
3579 char dst_mask[6];
3581 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3582 mask_size = shader_glsl_get_write_mask_size(write_mask);
3583 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3585 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3586 src_param.param_str, src_param.param_str);
3587 shader_glsl_append_dst(buffer, ins);
3589 if (mask_size > 1)
3591 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3592 mask_size, src_param.param_str);
3594 else
3596 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3597 src_param.param_str);
3601 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3603 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3604 ins->ctx->reg_maps->shader_version.minor);
3605 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3606 struct glsl_src_param src0_param;
3607 const char *prefix, *suffix;
3608 unsigned int dst_size;
3609 DWORD dst_write_mask;
3611 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3612 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3614 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3615 dst_write_mask = WINED3DSP_WRITEMASK_3;
3617 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3619 switch (ins->handler_idx)
3621 case WINED3DSIH_EXP:
3622 case WINED3DSIH_EXPP:
3623 prefix = "exp2(";
3624 suffix = ")";
3625 break;
3627 case WINED3DSIH_LOG:
3628 case WINED3DSIH_LOGP:
3629 prefix = "log2(abs(";
3630 suffix = "))";
3631 break;
3633 case WINED3DSIH_RCP:
3634 prefix = "1.0 / ";
3635 suffix = "";
3636 break;
3638 case WINED3DSIH_RSQ:
3639 prefix = "inversesqrt(abs(";
3640 suffix = "))";
3641 break;
3643 default:
3644 prefix = "";
3645 suffix = "";
3646 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3647 break;
3650 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3651 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3652 else
3653 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3656 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3657 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3658 * dst.x = 2^(floor(src))
3659 * dst.y = src - floor(src)
3660 * dst.z = 2^src (partial precision is allowed, but optional)
3661 * dst.w = 1.0;
3662 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3663 * dst = 2^src; (partial precision is allowed, but optional)
3665 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3667 if (ins->ctx->reg_maps->shader_version.major < 2)
3669 struct glsl_src_param src_param;
3670 char dst_mask[6];
3672 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3674 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3675 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3676 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3677 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3679 shader_glsl_append_dst(ins->ctx->buffer, ins);
3680 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3681 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3682 return;
3685 shader_glsl_scalar_op(ins);
3688 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3689 const char *vector_constructor, const char *scalar_constructor)
3691 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3692 struct glsl_src_param src_param;
3693 unsigned int mask_size;
3694 DWORD write_mask;
3696 write_mask = shader_glsl_append_dst(buffer, ins);
3697 mask_size = shader_glsl_get_write_mask_size(write_mask);
3698 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3700 if (mask_size > 1)
3701 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3702 else
3703 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3706 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3708 shader_glsl_cast(ins, "ivec", "int");
3711 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3713 shader_glsl_cast(ins, "uvec", "uint");
3716 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3718 shader_glsl_cast(ins, "vec", "float");
3721 /** Process signed comparison opcodes in GLSL. */
3722 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3724 struct glsl_src_param src0_param;
3725 struct glsl_src_param src1_param;
3726 DWORD write_mask;
3727 unsigned int mask_size;
3729 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3730 mask_size = shader_glsl_get_write_mask_size(write_mask);
3731 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3732 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3734 if (mask_size > 1) {
3735 const char *compare;
3737 switch(ins->handler_idx)
3739 case WINED3DSIH_SLT: compare = "lessThan"; break;
3740 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3741 default: compare = "";
3742 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3745 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3746 src0_param.param_str, src1_param.param_str);
3747 } else {
3748 switch(ins->handler_idx)
3750 case WINED3DSIH_SLT:
3751 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3752 * to return 0.0 but step returns 1.0 because step is not < x
3753 * An alternative is a bvec compare padded with an unused second component.
3754 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3755 * issue. Playing with not() is not possible either because not() does not accept
3756 * a scalar.
3758 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3759 src0_param.param_str, src1_param.param_str);
3760 break;
3761 case WINED3DSIH_SGE:
3762 /* Here we can use the step() function and safe a conditional */
3763 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3764 break;
3765 default:
3766 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3772 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3774 const char *condition_prefix, *condition_suffix;
3775 struct wined3d_shader_dst_param dst;
3776 struct glsl_src_param src0_param;
3777 struct glsl_src_param src1_param;
3778 struct glsl_src_param src2_param;
3779 BOOL temp_destination = FALSE;
3780 DWORD cmp_channel = 0;
3781 unsigned int i, j;
3782 char mask_char[6];
3783 DWORD write_mask;
3785 switch (ins->handler_idx)
3787 case WINED3DSIH_CMP:
3788 condition_prefix = "";
3789 condition_suffix = " >= 0.0";
3790 break;
3792 case WINED3DSIH_CND:
3793 condition_prefix = "";
3794 condition_suffix = " > 0.5";
3795 break;
3797 case WINED3DSIH_MOVC:
3798 condition_prefix = "bool(";
3799 condition_suffix = ")";
3800 break;
3802 default:
3803 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3804 condition_prefix = "<unhandled prefix>";
3805 condition_suffix = "<unhandled suffix>";
3806 break;
3809 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3811 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3812 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3813 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3814 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3816 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3817 condition_prefix, src0_param.param_str, condition_suffix,
3818 src1_param.param_str, src2_param.param_str);
3819 return;
3822 dst = ins->dst[0];
3824 /* Splitting the instruction up in multiple lines imposes a problem:
3825 * The first lines may overwrite source parameters of the following lines.
3826 * Deal with that by using a temporary destination register if needed. */
3827 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3828 && ins->src[0].reg.type == dst.reg.type)
3829 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3830 && ins->src[1].reg.type == dst.reg.type)
3831 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3832 && ins->src[2].reg.type == dst.reg.type))
3833 temp_destination = TRUE;
3835 /* Cycle through all source0 channels. */
3836 for (i = 0; i < 4; ++i)
3838 write_mask = 0;
3839 /* Find the destination channels which use the current source0 channel. */
3840 for (j = 0; j < 4; ++j)
3842 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3844 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3845 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3848 dst.write_mask = ins->dst[0].write_mask & write_mask;
3850 if (temp_destination)
3852 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3853 continue;
3854 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3856 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3857 continue;
3859 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3860 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3861 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3863 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3864 condition_prefix, src0_param.param_str, condition_suffix,
3865 src1_param.param_str, src2_param.param_str);
3868 if (temp_destination)
3870 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3871 shader_glsl_append_dst(ins->ctx->buffer, ins);
3872 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3876 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3877 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3878 * the compare is done per component of src0. */
3879 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3881 struct glsl_src_param src0_param;
3882 struct glsl_src_param src1_param;
3883 struct glsl_src_param src2_param;
3884 DWORD write_mask;
3885 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3886 ins->ctx->reg_maps->shader_version.minor);
3888 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3890 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3891 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3892 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3893 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3895 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3896 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3897 else
3898 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3899 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3900 return;
3903 shader_glsl_conditional_move(ins);
3906 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3907 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3909 struct glsl_src_param src0_param;
3910 struct glsl_src_param src1_param;
3911 struct glsl_src_param src2_param;
3912 DWORD write_mask;
3914 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3915 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3916 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3917 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3918 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3919 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3922 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3923 Vertex shaders to GLSL codes */
3924 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3926 int i;
3927 int nComponents = 0;
3928 struct wined3d_shader_dst_param tmp_dst = {{0}};
3929 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3930 struct wined3d_shader_instruction tmp_ins;
3932 memset(&tmp_ins, 0, sizeof(tmp_ins));
3934 /* Set constants for the temporary argument */
3935 tmp_ins.ctx = ins->ctx;
3936 tmp_ins.dst_count = 1;
3937 tmp_ins.dst = &tmp_dst;
3938 tmp_ins.src_count = 2;
3939 tmp_ins.src = tmp_src;
3941 switch(ins->handler_idx)
3943 case WINED3DSIH_M4x4:
3944 nComponents = 4;
3945 tmp_ins.handler_idx = WINED3DSIH_DP4;
3946 break;
3947 case WINED3DSIH_M4x3:
3948 nComponents = 3;
3949 tmp_ins.handler_idx = WINED3DSIH_DP4;
3950 break;
3951 case WINED3DSIH_M3x4:
3952 nComponents = 4;
3953 tmp_ins.handler_idx = WINED3DSIH_DP3;
3954 break;
3955 case WINED3DSIH_M3x3:
3956 nComponents = 3;
3957 tmp_ins.handler_idx = WINED3DSIH_DP3;
3958 break;
3959 case WINED3DSIH_M3x2:
3960 nComponents = 2;
3961 tmp_ins.handler_idx = WINED3DSIH_DP3;
3962 break;
3963 default:
3964 break;
3967 tmp_dst = ins->dst[0];
3968 tmp_src[0] = ins->src[0];
3969 tmp_src[1] = ins->src[1];
3970 for (i = 0; i < nComponents; ++i)
3972 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3973 shader_glsl_dot(&tmp_ins);
3974 ++tmp_src[1].reg.idx[0].offset;
3979 The LRP instruction performs a component-wise linear interpolation
3980 between the second and third operands using the first operand as the
3981 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3982 This is equivalent to mix(src2, src1, src0);
3984 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3986 struct glsl_src_param src0_param;
3987 struct glsl_src_param src1_param;
3988 struct glsl_src_param src2_param;
3989 DWORD write_mask;
3991 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3993 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3994 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3995 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3997 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3998 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4001 /** Process the WINED3DSIO_LIT instruction in GLSL:
4002 * dst.x = dst.w = 1.0
4003 * dst.y = (src0.x > 0) ? src0.x
4004 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4005 * where src.w is clamped at +- 128
4007 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4009 struct glsl_src_param src0_param;
4010 struct glsl_src_param src1_param;
4011 struct glsl_src_param src3_param;
4012 char dst_mask[6];
4014 shader_glsl_append_dst(ins->ctx->buffer, ins);
4015 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4017 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4018 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4019 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4021 /* The sdk specifies the instruction like this
4022 * dst.x = 1.0;
4023 * if(src.x > 0.0) dst.y = src.x
4024 * else dst.y = 0.0.
4025 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4026 * else dst.z = 0.0;
4027 * dst.w = 1.0;
4028 * (where power = src.w clamped between -128 and 128)
4030 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4031 * dst.x = 1.0 ... No further explanation needed
4032 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4033 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4034 * dst.w = 1.0. ... Nothing fancy.
4036 * So we still have one conditional in there. So do this:
4037 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4039 * 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),
4040 * 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.
4041 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4043 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4044 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4045 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4047 shader_addline(ins->ctx->buffer,
4048 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4049 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4050 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4051 src0_param.param_str, src3_param.param_str, dst_mask);
4054 /** Process the WINED3DSIO_DST instruction in GLSL:
4055 * dst.x = 1.0
4056 * dst.y = src0.x * src0.y
4057 * dst.z = src0.z
4058 * dst.w = src1.w
4060 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4062 struct glsl_src_param src0y_param;
4063 struct glsl_src_param src0z_param;
4064 struct glsl_src_param src1y_param;
4065 struct glsl_src_param src1w_param;
4066 char dst_mask[6];
4068 shader_glsl_append_dst(ins->ctx->buffer, ins);
4069 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4071 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4072 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4073 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4074 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4076 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4077 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4080 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4081 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4082 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4084 * dst.x = cos(src0.?)
4085 * dst.y = sin(src0.?)
4086 * dst.z = dst.z
4087 * dst.w = dst.w
4089 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4091 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4092 struct glsl_src_param src0_param;
4093 DWORD write_mask;
4095 if (ins->ctx->reg_maps->shader_version.major < 4)
4097 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4099 write_mask = shader_glsl_append_dst(buffer, ins);
4100 switch (write_mask)
4102 case WINED3DSP_WRITEMASK_0:
4103 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4104 break;
4106 case WINED3DSP_WRITEMASK_1:
4107 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4108 break;
4110 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4111 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4112 src0_param.param_str, src0_param.param_str);
4113 break;
4115 default:
4116 ERR("Write mask should be .x, .y or .xy\n");
4117 break;
4120 return;
4123 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4126 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4128 char dst_mask[6];
4130 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4131 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4132 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4134 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4135 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4136 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4138 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4139 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4141 else
4143 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4144 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4145 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4148 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4150 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4151 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4152 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4156 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4157 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4158 * generate invalid code
4160 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4162 struct glsl_src_param src0_param;
4163 DWORD write_mask;
4165 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4166 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4168 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4171 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4172 * Start a for() loop where src1.y is the initial value of aL,
4173 * increment aL by src1.z for a total of src1.x iterations.
4174 * Need to use a temporary variable for this operation.
4176 /* FIXME: I don't think nested loops will work correctly this way. */
4177 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4179 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4180 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4181 const struct wined3d_shader *shader = ins->ctx->shader;
4182 const struct wined3d_shader_lconst *constant;
4183 struct glsl_src_param src1_param;
4184 const DWORD *control_values = NULL;
4186 if (ins->ctx->reg_maps->shader_version.major < 4)
4188 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4190 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4191 * class hardware doesn't support real varying indexing, but Microsoft
4192 * designed this feature for Shader model 2.x+. If the loop control is
4193 * known at compile time, the GLSL compiler can unroll the loop, and
4194 * replace indirect addressing with direct addressing. */
4195 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4197 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4199 if (constant->idx == ins->src[1].reg.idx[0].offset)
4201 control_values = constant->value;
4202 break;
4207 if (control_values)
4209 struct wined3d_shader_loop_control loop_control;
4210 loop_control.count = control_values[0];
4211 loop_control.start = control_values[1];
4212 loop_control.step = (int)control_values[2];
4214 if (loop_control.step > 0)
4216 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4217 loop_state->current_depth, loop_control.start,
4218 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4219 loop_state->current_depth, loop_control.step);
4221 else if (loop_control.step < 0)
4223 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4224 loop_state->current_depth, loop_control.start,
4225 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4226 loop_state->current_depth, loop_control.step);
4228 else
4230 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4231 loop_state->current_depth, loop_control.start, loop_state->current_depth,
4232 loop_state->current_depth, loop_control.count,
4233 loop_state->current_depth);
4236 else
4238 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4239 loop_state->current_depth, loop_state->current_reg,
4240 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4241 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4244 ++loop_state->current_reg;
4246 else
4248 shader_addline(buffer, "for (;;)\n{\n");
4251 ++loop_state->current_depth;
4254 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4256 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4258 shader_addline(ins->ctx->buffer, "}\n");
4260 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4262 --loop_state->current_depth;
4263 --loop_state->current_reg;
4266 if (ins->handler_idx == WINED3DSIH_ENDREP)
4268 --loop_state->current_depth;
4272 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4274 const struct wined3d_shader *shader = ins->ctx->shader;
4275 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4276 const struct wined3d_shader_lconst *constant;
4277 struct glsl_src_param src0_param;
4278 const DWORD *control_values = NULL;
4280 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4281 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4283 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4285 if (constant->idx == ins->src[0].reg.idx[0].offset)
4287 control_values = constant->value;
4288 break;
4293 if (control_values)
4295 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4296 loop_state->current_depth, loop_state->current_depth,
4297 control_values[0], loop_state->current_depth);
4299 else
4301 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4302 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4303 loop_state->current_depth, loop_state->current_depth,
4304 src0_param.param_str, loop_state->current_depth);
4307 ++loop_state->current_depth;
4310 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4312 struct glsl_src_param src0_param;
4314 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4315 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4318 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4320 struct glsl_src_param src0_param;
4322 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4323 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4326 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4328 shader_addline(ins->ctx->buffer, "default:\n");
4331 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4333 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4334 struct glsl_src_param src0_param;
4336 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4337 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4340 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4342 struct glsl_src_param src0_param;
4343 struct glsl_src_param src1_param;
4345 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4346 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4348 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4349 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4352 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4354 shader_addline(ins->ctx->buffer, "} else {\n");
4357 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4359 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4361 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4362 shader_glsl_fixup_position(ins->ctx->buffer);
4364 if (!stream)
4365 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4366 else
4367 FIXME("Unhandled primitive stream %u.\n", stream);
4370 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4372 shader_addline(ins->ctx->buffer, "break;\n");
4375 /* FIXME: According to MSDN the compare is done per component. */
4376 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4378 struct glsl_src_param src0_param;
4379 struct glsl_src_param src1_param;
4381 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4382 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4384 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4385 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4388 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4390 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4391 struct glsl_src_param src_param;
4393 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4394 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4397 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
4399 shader_addline(ins->ctx->buffer, "continue;\n");
4402 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4404 shader_addline(ins->ctx->buffer, "}\n");
4405 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4408 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4410 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4413 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4415 struct glsl_src_param src1_param;
4417 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4418 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4419 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4422 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4424 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4425 * function only suppresses the unhandled instruction warning
4429 /*********************************************
4430 * Pixel Shader Specific Code begins here
4431 ********************************************/
4432 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4434 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4435 ins->ctx->reg_maps->shader_version.minor);
4436 struct glsl_sample_function sample_function;
4437 DWORD sample_flags = 0;
4438 DWORD resource_idx;
4439 DWORD mask = 0, swizzle;
4440 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4442 /* 1.0-1.4: Use destination register as sampler source.
4443 * 2.0+: Use provided sampler source. */
4444 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4445 resource_idx = ins->dst[0].reg.idx[0].offset;
4446 else
4447 resource_idx = ins->src[1].reg.idx[0].offset;
4449 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4451 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4452 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4453 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4455 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4456 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4458 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4459 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4461 case WINED3D_TTFF_COUNT1:
4462 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4463 break;
4464 case WINED3D_TTFF_COUNT2:
4465 mask = WINED3DSP_WRITEMASK_1;
4466 break;
4467 case WINED3D_TTFF_COUNT3:
4468 mask = WINED3DSP_WRITEMASK_2;
4469 break;
4470 case WINED3D_TTFF_COUNT4:
4471 case WINED3D_TTFF_DISABLE:
4472 mask = WINED3DSP_WRITEMASK_3;
4473 break;
4477 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4479 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4481 if (src_mod == WINED3DSPSM_DZ) {
4482 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4483 mask = WINED3DSP_WRITEMASK_2;
4484 } else if (src_mod == WINED3DSPSM_DW) {
4485 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4486 mask = WINED3DSP_WRITEMASK_3;
4489 else
4491 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4492 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4494 /* ps 2.0 texldp instruction always divides by the fourth component. */
4495 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4496 mask = WINED3DSP_WRITEMASK_3;
4500 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4501 mask |= sample_function.coord_mask;
4502 sample_function.coord_mask = mask;
4504 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4505 else swizzle = ins->src[1].swizzle;
4507 /* 1.0-1.3: Use destination register as coordinate source.
4508 1.4+: Use provided coordinate source register. */
4509 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4511 char coord_mask[6];
4512 shader_glsl_write_mask_to_str(mask, coord_mask);
4513 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4514 "T%u%s", resource_idx, coord_mask);
4516 else
4518 struct glsl_src_param coord_param;
4519 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4520 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4522 struct glsl_src_param bias;
4523 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4524 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4525 NULL, "%s", coord_param.param_str);
4526 } else {
4527 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4528 "%s", coord_param.param_str);
4531 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4534 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4536 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4537 struct glsl_src_param coord_param, dx_param, dy_param;
4538 struct glsl_sample_function sample_function;
4539 DWORD sampler_idx;
4540 DWORD swizzle = ins->src[1].swizzle;
4542 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4544 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4545 shader_glsl_tex(ins);
4546 return;
4549 sampler_idx = ins->src[1].reg.idx[0].offset;
4551 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4552 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4553 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
4554 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
4556 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4557 NULL, NULL, "%s", coord_param.param_str);
4558 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4561 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4563 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4564 struct glsl_src_param coord_param, lod_param;
4565 struct glsl_sample_function sample_function;
4566 DWORD sampler_idx;
4567 DWORD swizzle = ins->src[1].swizzle;
4569 sampler_idx = ins->src[1].reg.idx[0].offset;
4571 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4572 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4574 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4576 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4577 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4579 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4580 * However, the NVIDIA drivers allow them in fragment shaders as well,
4581 * even without the appropriate extension. */
4582 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4584 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4585 "%s", coord_param.param_str);
4586 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4589 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4590 unsigned int resource_idx, unsigned int sampler_idx)
4592 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4593 unsigned int i;
4595 for (i = 0; i < sampler_map->count; ++i)
4597 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4598 return entries[i].bind_idx;
4601 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4603 return ~0u;
4606 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4608 static const unsigned int texture_size_component_count[] =
4610 0, /* WINED3D_SHADER_RESOURCE_NONE */
4611 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4612 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4613 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4614 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4615 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4616 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4617 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4618 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4619 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4622 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4623 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4624 enum wined3d_shader_resource_type resource_type;
4625 unsigned int resource_idx, sampler_bind_idx, i;
4626 enum wined3d_data_type dst_data_type;
4627 struct glsl_src_param lod_param;
4628 char dst_swizzle[6];
4629 DWORD write_mask;
4631 dst_data_type = ins->dst[0].reg.data_type;
4632 if (ins->flags == WINED3DSI_RESINFO_UINT)
4633 dst_data_type = WINED3D_DATA_UINT;
4634 else if (ins->flags)
4635 FIXME("Unhandled flags %#x.\n", ins->flags);
4637 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4638 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4640 resource_idx = ins->src[1].reg.idx[0].offset;
4641 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4642 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4643 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4644 resource_idx, WINED3D_SAMPLER_DEFAULT);
4646 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4648 ERR("Unexpected resource type %#x.\n", resource_type);
4649 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4652 if (dst_data_type == WINED3D_DATA_UINT)
4653 shader_addline(ins->ctx->buffer, "uvec4(");
4654 else
4655 shader_addline(ins->ctx->buffer, "vec4(");
4657 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4658 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4660 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4661 shader_addline(ins->ctx->buffer, "0, ");
4663 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4665 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4666 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4668 else
4670 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4671 shader_addline(ins->ctx->buffer, "1");
4674 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4677 /* FIXME: The current implementation does not handle multisample textures correctly. */
4678 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4680 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4681 struct glsl_src_param coord_param, lod_param;
4682 struct glsl_sample_function sample_function;
4683 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4685 if (wined3d_shader_instruction_has_texel_offset(ins))
4686 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4688 resource_idx = ins->src[1].reg.idx[0].offset;
4689 sampler_idx = WINED3D_SAMPLER_DEFAULT;
4691 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4692 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4693 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4694 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4695 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4696 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4697 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4700 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4702 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4703 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4704 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4705 struct glsl_sample_function sample_function;
4706 DWORD flags = 0;
4708 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4709 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4710 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4711 flags |= WINED3D_GLSL_SAMPLE_LOD;
4712 if (wined3d_shader_instruction_has_texel_offset(ins))
4713 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4715 resource_idx = ins->src[1].reg.idx[0].offset;
4716 sampler_idx = ins->src[2].reg.idx[0].offset;
4718 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4719 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4721 switch (ins->handler_idx)
4723 case WINED3DSIH_SAMPLE:
4724 break;
4725 case WINED3DSIH_SAMPLE_B:
4726 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4727 lod_param_str = lod_param.param_str;
4728 break;
4729 case WINED3DSIH_SAMPLE_GRAD:
4730 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
4731 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
4732 dx_param_str = dx_param.param_str;
4733 dy_param_str = dy_param.param_str;
4734 break;
4735 case WINED3DSIH_SAMPLE_LOD:
4736 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4737 lod_param_str = lod_param.param_str;
4738 break;
4739 default:
4740 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4741 break;
4744 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4745 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4746 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4747 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4750 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
4752 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4753 struct glsl_src_param coord_param, compare_param;
4754 struct glsl_sample_function sample_function;
4755 const char *lod_param = NULL;
4756 DWORD flags = 0;
4757 UINT coord_size;
4759 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
4761 lod_param = "0";
4762 flags |= WINED3D_GLSL_SAMPLE_LOD;
4765 if (wined3d_shader_instruction_has_texel_offset(ins))
4766 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4768 resource_idx = ins->src[1].reg.idx[0].offset;
4769 sampler_idx = ins->src[2].reg.idx[0].offset;
4771 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4772 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4773 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
4774 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
4775 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4776 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4777 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
4778 coord_size, coord_param.param_str, compare_param.param_str);
4779 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4782 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4784 /* FIXME: Make this work for more than just 2D textures */
4785 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4786 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4788 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4790 char dst_mask[6];
4792 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4793 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4794 ins->dst[0].reg.idx[0].offset, dst_mask);
4796 else
4798 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4799 DWORD reg = ins->src[0].reg.idx[0].offset;
4800 char dst_swizzle[6];
4802 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4804 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4806 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4807 struct glsl_src_param div_param;
4808 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4810 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4812 if (mask_size > 1)
4813 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4814 else
4815 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4817 else
4819 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4824 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4825 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4826 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4827 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4829 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4830 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4831 struct glsl_sample_function sample_function;
4832 struct glsl_src_param src0_param;
4833 UINT mask_size;
4835 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4837 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4838 * scalar, and projected sampling would require 4.
4840 * It is a dependent read - not valid with conditional NP2 textures
4842 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4843 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4845 switch(mask_size)
4847 case 1:
4848 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4849 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4850 break;
4852 case 2:
4853 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4854 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4855 break;
4857 case 3:
4858 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4859 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4860 break;
4862 default:
4863 FIXME("Unexpected mask size %u\n", mask_size);
4864 break;
4866 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4869 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4870 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4871 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4873 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4874 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4875 struct glsl_src_param src0_param;
4876 DWORD dst_mask;
4877 unsigned int mask_size;
4879 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4880 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4881 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4883 if (mask_size > 1) {
4884 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4885 } else {
4886 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4890 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4891 * Calculate the depth as dst.x / dst.y */
4892 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4894 struct glsl_dst_param dst_param;
4896 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4898 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4899 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4900 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4901 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4902 * >= 1.0 or < 0.0
4904 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4905 dst_param.reg_name, dst_param.reg_name);
4908 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4909 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4910 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4911 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4913 static void shader_glsl_texm3x2depth(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;
4919 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4921 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4922 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4925 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4926 * Calculate the 1st of a 2-row matrix multiplication. */
4927 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4929 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4930 DWORD reg = ins->dst[0].reg.idx[0].offset;
4931 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4932 struct glsl_src_param src0_param;
4934 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4935 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4938 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4939 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4940 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4942 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4943 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4944 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4945 DWORD reg = ins->dst[0].reg.idx[0].offset;
4946 struct glsl_src_param src0_param;
4948 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4949 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4950 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4953 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4955 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4956 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4957 struct glsl_sample_function sample_function;
4958 DWORD reg = 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);
4962 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4964 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4966 /* Sample the texture using the calculated coordinates */
4967 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
4968 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4971 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4972 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4973 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4975 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4976 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4977 struct glsl_sample_function sample_function;
4978 DWORD reg = ins->dst[0].reg.idx[0].offset;
4979 struct glsl_src_param src0_param;
4981 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4982 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4984 /* Dependent read, not valid with conditional NP2 */
4985 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4987 /* Sample the texture using the calculated coordinates */
4988 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
4989 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4991 tex_mx->current_row = 0;
4994 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4995 * Perform the 3rd row of a 3x3 matrix multiply */
4996 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4998 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4999 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5000 DWORD reg = ins->dst[0].reg.idx[0].offset;
5001 struct glsl_src_param src0_param;
5002 char dst_mask[6];
5004 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5006 shader_glsl_append_dst(ins->ctx->buffer, ins);
5007 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5008 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5010 tex_mx->current_row = 0;
5013 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5014 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5015 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5017 struct glsl_src_param src0_param;
5018 struct glsl_src_param src1_param;
5019 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5020 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5021 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5022 struct glsl_sample_function sample_function;
5023 DWORD reg = ins->dst[0].reg.idx[0].offset;
5024 char coord_mask[6];
5026 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5027 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5029 /* Perform the last matrix multiply operation */
5030 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5031 /* Reflection calculation */
5032 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5034 /* Dependent read, not valid with conditional NP2 */
5035 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5036 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5038 /* Sample the texture */
5039 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5040 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5041 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5043 tex_mx->current_row = 0;
5046 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5047 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5048 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5050 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5051 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5052 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5053 struct glsl_sample_function sample_function;
5054 DWORD reg = ins->dst[0].reg.idx[0].offset;
5055 struct glsl_src_param src0_param;
5056 char coord_mask[6];
5058 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5060 /* Perform the last matrix multiply operation */
5061 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
5063 /* Construct the eye-ray vector from w coordinates */
5064 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5065 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
5066 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5068 /* Dependent read, not valid with conditional NP2 */
5069 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5070 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5072 /* Sample the texture using the calculated coordinates */
5073 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5074 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5075 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5077 tex_mx->current_row = 0;
5080 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5081 * Apply a fake bump map transform.
5082 * texbem is pshader <= 1.3 only, this saves a few version checks
5084 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
5086 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5087 struct glsl_sample_function sample_function;
5088 struct glsl_src_param coord_param;
5089 DWORD sampler_idx;
5090 DWORD mask;
5091 DWORD flags;
5092 char coord_mask[6];
5094 sampler_idx = ins->dst[0].reg.idx[0].offset;
5095 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5096 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5098 /* Dependent read, not valid with conditional NP2 */
5099 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5100 mask = sample_function.coord_mask;
5102 shader_glsl_write_mask_to_str(mask, coord_mask);
5104 /* With projected textures, texbem only divides the static texture coord,
5105 * not the displacement, so we can't let GL handle this. */
5106 if (flags & WINED3D_PSARGS_PROJECTED)
5108 DWORD div_mask=0;
5109 char coord_div_mask[3];
5110 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5112 case WINED3D_TTFF_COUNT1:
5113 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5114 break;
5115 case WINED3D_TTFF_COUNT2:
5116 div_mask = WINED3DSP_WRITEMASK_1;
5117 break;
5118 case WINED3D_TTFF_COUNT3:
5119 div_mask = WINED3DSP_WRITEMASK_2;
5120 break;
5121 case WINED3D_TTFF_COUNT4:
5122 case WINED3D_TTFF_DISABLE:
5123 div_mask = WINED3DSP_WRITEMASK_3;
5124 break;
5126 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
5127 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
5130 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
5132 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5133 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
5134 coord_param.param_str, coord_mask);
5136 if (ins->handler_idx == WINED3DSIH_TEXBEML)
5138 struct glsl_src_param luminance_param;
5139 struct glsl_dst_param dst_param;
5141 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
5142 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5144 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
5145 dst_param.reg_name, dst_param.mask_str,
5146 luminance_param.param_str, sampler_idx, sampler_idx);
5148 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5151 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
5153 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5154 struct glsl_src_param src0_param, src1_param;
5156 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5157 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5159 shader_glsl_append_dst(ins->ctx->buffer, ins);
5160 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
5161 src0_param.param_str, sampler_idx, src1_param.param_str);
5164 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5165 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5166 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
5168 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5169 struct glsl_sample_function sample_function;
5170 struct glsl_src_param src0_param;
5172 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5174 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5175 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5176 "%s.wx", src0_param.reg_name);
5177 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5180 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5181 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5182 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
5184 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5185 struct glsl_sample_function sample_function;
5186 struct glsl_src_param src0_param;
5188 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5190 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5191 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5192 "%s.yz", src0_param.reg_name);
5193 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5196 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5197 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5198 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
5200 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5201 struct glsl_sample_function sample_function;
5202 struct glsl_src_param src0_param;
5204 /* Dependent read, not valid with conditional NP2 */
5205 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5206 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
5208 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5209 "%s", src0_param.param_str);
5210 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5213 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5214 * If any of the first 3 components are < 0, discard this pixel */
5215 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
5217 if (ins->ctx->reg_maps->shader_version.major >= 4)
5219 struct glsl_src_param src_param;
5221 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5222 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
5224 else
5226 struct glsl_dst_param dst_param;
5228 /* The argument is a destination parameter, and no writemasks are allowed */
5229 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5231 /* 2.0 shaders compare all 4 components in texkill. */
5232 if (ins->ctx->reg_maps->shader_version.major >= 2)
5233 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
5234 /* 1.x shaders only compare the first 3 components, probably due to
5235 * the nature of the texkill instruction as a tex* instruction, and
5236 * phase, which kills all .w components. Even if all 4 components are
5237 * defined, only the first 3 are used. */
5238 else
5239 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
5243 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5244 * dst = dot2(src0, src1) + src2 */
5245 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5247 struct glsl_src_param src0_param;
5248 struct glsl_src_param src1_param;
5249 struct glsl_src_param src2_param;
5250 DWORD write_mask;
5251 unsigned int mask_size;
5253 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5254 mask_size = shader_glsl_get_write_mask_size(write_mask);
5256 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5257 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5258 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5260 if (mask_size > 1) {
5261 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5262 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5263 } else {
5264 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5265 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5269 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5270 const struct wined3d_shader_signature *input_signature,
5271 const struct wined3d_shader_reg_maps *reg_maps,
5272 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5274 unsigned int i;
5276 for (i = 0; i < input_signature->element_count; ++i)
5278 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5279 const char *semantic_name;
5280 UINT semantic_idx;
5281 char reg_mask[6];
5283 /* Unused */
5284 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5285 continue;
5287 semantic_name = input->semantic_name;
5288 semantic_idx = input->semantic_idx;
5289 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5291 if (args->vp_mode == vertexshader)
5293 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5295 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5296 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5298 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5300 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5302 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
5304 shader_addline(buffer, "ps_in[%u] = vec4("
5305 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
5306 input->register_idx);
5308 else
5310 if (input->sysval_semantic)
5311 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
5312 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5313 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5314 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5317 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5319 if (args->pointsprite)
5320 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5321 shader->u.ps.input_reg_map[input->register_idx]);
5322 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5323 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5324 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5325 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5326 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5327 else
5328 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5329 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5331 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5333 if (!semantic_idx)
5334 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5335 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5336 else if (semantic_idx == 1)
5337 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5338 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5339 else
5340 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5341 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5343 else
5345 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5346 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5351 /*********************************************
5352 * Vertex Shader Specific Code begins here
5353 ********************************************/
5355 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5357 struct glsl_program_key key;
5359 key.vs_id = entry->vs.id;
5360 key.gs_id = entry->gs.id;
5361 key.ps_id = entry->ps.id;
5363 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5365 ERR("Failed to insert program entry.\n");
5369 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5370 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5372 struct wine_rb_entry *entry;
5373 struct glsl_program_key key;
5375 key.vs_id = vs_id;
5376 key.gs_id = gs_id;
5377 key.ps_id = ps_id;
5379 entry = wine_rb_get(&priv->program_lookup, &key);
5380 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5383 /* Context activation is done by the caller. */
5384 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5385 struct glsl_shader_prog_link *entry)
5387 struct glsl_program_key key;
5389 key.vs_id = entry->vs.id;
5390 key.gs_id = entry->gs.id;
5391 key.ps_id = entry->ps.id;
5392 wine_rb_remove(&priv->program_lookup, &key);
5394 GL_EXTCALL(glDeleteProgram(entry->id));
5395 if (entry->vs.id)
5396 list_remove(&entry->vs.shader_entry);
5397 if (entry->gs.id)
5398 list_remove(&entry->gs.shader_entry);
5399 if (entry->ps.id)
5400 list_remove(&entry->ps.shader_entry);
5401 HeapFree(GetProcessHeap(), 0, entry);
5404 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
5405 const struct wined3d_gl_info *gl_info, const DWORD *map,
5406 const struct wined3d_shader_signature *input_signature,
5407 const struct wined3d_shader_reg_maps *reg_maps_in,
5408 const struct wined3d_shader_signature *output_signature,
5409 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5411 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5412 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5413 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5414 unsigned int in_count = vec4_varyings(3, gl_info);
5415 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5416 DWORD in_idx, *set = NULL;
5417 unsigned int i, j;
5418 char reg_mask[6];
5420 set = wined3d_calloc(max_varyings, sizeof(*set));
5422 for (i = 0; i < input_signature->element_count; ++i)
5424 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5426 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5427 continue;
5429 in_idx = map[input->register_idx];
5430 /* Declared, but not read register */
5431 if (in_idx == ~0u)
5432 continue;
5433 if (in_idx >= max_varyings)
5435 FIXME("More input varyings declared than supported, expect issues.\n");
5436 continue;
5439 if (in_idx == in_count)
5440 string_buffer_sprintf(destination, "gl_FrontColor");
5441 else if (in_idx == in_count + 1)
5442 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5443 else
5444 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
5446 if (!set[in_idx])
5447 set[in_idx] = ~0u;
5449 for (j = 0; j < output_signature->element_count; ++j)
5451 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5452 DWORD mask;
5454 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5455 || input->semantic_idx != output->semantic_idx
5456 || strcmp(input->semantic_name, output->semantic_name)
5457 || !(mask = input->mask & output->mask))
5458 continue;
5460 if (set[in_idx] == ~0u)
5461 set[in_idx] = 0;
5462 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5463 shader_glsl_write_mask_to_str(mask, reg_mask);
5465 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5466 destination->buffer, reg_mask, output->register_idx, reg_mask);
5470 for (i = 0; i < max_varyings; ++i)
5472 unsigned int size;
5474 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5475 continue;
5477 if (set[i] == ~0u)
5478 set[i] = 0;
5480 size = 0;
5481 if (!(set[i] & WINED3DSP_WRITEMASK_0))
5482 reg_mask[size++] = 'x';
5483 if (!(set[i] & WINED3DSP_WRITEMASK_1))
5484 reg_mask[size++] = 'y';
5485 if (!(set[i] & WINED3DSP_WRITEMASK_2))
5486 reg_mask[size++] = 'z';
5487 if (!(set[i] & WINED3DSP_WRITEMASK_3))
5488 reg_mask[size++] = 'w';
5489 reg_mask[size] = '\0';
5491 if (i == in_count)
5492 string_buffer_sprintf(destination, "gl_FrontColor");
5493 else if (i == in_count + 1)
5494 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5495 else
5496 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
5498 if (size == 1)
5499 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5500 else
5501 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5504 HeapFree(GetProcessHeap(), 0, set);
5505 string_buffer_release(&priv->string_buffers, destination);
5508 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
5509 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
5510 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5512 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5513 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5514 char reg_mask[6];
5515 unsigned int i;
5517 for (i = 0; i < output_signature->element_count; ++i)
5519 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5521 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5522 continue;
5524 if (output->register_idx >= input_count)
5525 continue;
5527 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
5529 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5531 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5532 destination->buffer, reg_mask, output->register_idx, reg_mask);
5535 string_buffer_release(&priv->string_buffers, destination);
5538 /* Context activation is done by the caller. */
5539 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
5540 const struct wined3d_shader *vs, unsigned int input_count,
5541 const struct wined3d_gl_info *gl_info)
5543 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5544 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5546 if (legacy_context)
5547 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
5548 else
5549 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
5550 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5552 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
5553 legacy_context ? "gs_in" : "gs_in.gs_in");
5555 shader_addline(buffer, "}\n");
5558 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
5559 const struct wined3d_gl_info *gl_info, const DWORD *map,
5560 const struct wined3d_shader_signature *input_signature,
5561 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
5562 const struct wined3d_shader_signature *output_signature,
5563 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
5565 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5566 const char *semantic_name;
5567 UINT semantic_idx;
5568 char reg_mask[6];
5569 unsigned int i;
5571 /* First, sort out position and point size system values. */
5572 for (i = 0; i < output_signature->element_count; ++i)
5574 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5576 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5577 continue;
5579 semantic_name = output->semantic_name;
5580 semantic_idx = output->semantic_idx;
5581 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5583 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5585 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5586 reg_mask, output->register_idx, reg_mask);
5588 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5590 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5591 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5593 else if (output->sysval_semantic)
5595 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
5599 /* Then, setup the pixel shader input. */
5600 if (reg_maps_out->shader_version.major < 4)
5601 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
5602 output_signature, reg_maps_out, "ps_link");
5603 else
5604 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
5607 /* Context activation is done by the caller. */
5608 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
5609 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5610 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5612 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5613 GLuint ret;
5614 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5615 unsigned int i;
5616 const char *semantic_name;
5617 UINT semantic_idx;
5618 char reg_mask[6];
5619 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5621 string_buffer_clear(buffer);
5623 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5625 if (per_vertex_point_size)
5627 shader_addline(buffer, "uniform struct\n{\n");
5628 shader_addline(buffer, " float size_min;\n");
5629 shader_addline(buffer, " float size_max;\n");
5630 shader_addline(buffer, "} ffp_point;\n");
5633 if (ps_major < 3)
5635 DWORD colors_written_mask[2] = {0};
5636 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5638 if (!legacy_context)
5640 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5641 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5642 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5643 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5646 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5648 for (i = 0; i < vs->output_signature.element_count; ++i)
5650 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5651 DWORD write_mask;
5653 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5654 continue;
5656 semantic_name = output->semantic_name;
5657 semantic_idx = output->semantic_idx;
5658 write_mask = output->mask;
5659 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5661 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5663 if (legacy_context)
5664 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
5665 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5666 else
5667 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
5668 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5670 colors_written_mask[semantic_idx] = write_mask;
5672 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5674 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5675 reg_mask, output->register_idx, reg_mask);
5677 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5679 if (semantic_idx < MAX_TEXTURES)
5681 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
5682 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5683 semantic_idx, reg_mask, output->register_idx, reg_mask);
5684 texcoords_written_mask[semantic_idx] = write_mask;
5687 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5689 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5690 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5692 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5694 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
5695 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5696 output->register_idx, reg_mask[1]);
5700 for (i = 0; i < 2; ++i)
5702 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5704 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5705 if (!i)
5706 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5707 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5708 reg_mask, reg_mask);
5709 else
5710 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5711 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5712 reg_mask, reg_mask);
5715 for (i = 0; i < MAX_TEXTURES; ++i)
5717 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5718 continue;
5720 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5722 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5723 && !texcoords_written_mask[i])
5724 continue;
5726 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5727 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5728 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5732 else
5734 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5736 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5737 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5738 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5739 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
5742 shader_addline(buffer, "}\n");
5744 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5745 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5746 shader_glsl_compile(gl_info, ret, buffer->buffer);
5748 return ret;
5751 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
5752 const struct wined3d_shader *shader, unsigned int input_count,
5753 const struct wined3d_gl_info *gl_info)
5755 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5757 if (input_count)
5758 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
5760 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
5761 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
5763 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
5764 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
5766 shader_addline(buffer, "}\n");
5769 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
5770 const struct wined3d_gl_info *gl_info)
5772 const char *output = get_fragment_output(gl_info);
5774 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
5775 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5776 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
5777 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
5778 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
5779 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
5782 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
5783 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
5785 const char *output = get_fragment_output(gl_info);
5787 switch (mode)
5789 case WINED3D_FFP_PS_FOG_OFF:
5790 return;
5792 case WINED3D_FFP_PS_FOG_LINEAR:
5793 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5794 break;
5796 case WINED3D_FFP_PS_FOG_EXP:
5797 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5798 break;
5800 case WINED3D_FFP_PS_FOG_EXP2:
5801 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5802 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5803 break;
5805 default:
5806 ERR("Invalid fog mode %#x.\n", mode);
5807 return;
5810 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
5811 output, output);
5814 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
5815 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
5817 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
5818 * flipping all the operators here, just negate the comparison below. */
5819 static const char * const comparison_operator[] =
5821 "", /* WINED3D_CMP_NEVER */
5822 "<", /* WINED3D_CMP_LESS */
5823 "==", /* WINED3D_CMP_EQUAL */
5824 "<=", /* WINED3D_CMP_LESSEQUAL */
5825 ">", /* WINED3D_CMP_GREATER */
5826 "!=", /* WINED3D_CMP_NOTEQUAL */
5827 ">=", /* WINED3D_CMP_GREATEREQUAL */
5828 "" /* WINED3D_CMP_ALWAYS */
5831 if (alpha_func == WINED3D_CMP_ALWAYS)
5832 return;
5834 if (alpha_func != WINED3D_CMP_NEVER)
5835 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
5836 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
5837 shader_addline(buffer, " discard;\n");
5840 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
5841 const struct wined3d_gl_info *gl_info)
5843 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5844 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5845 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5846 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5847 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5848 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5849 if (gl_info->supported[EXT_GPU_SHADER4])
5850 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5851 if (gl_info->supported[EXT_TEXTURE_ARRAY])
5852 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
5855 /* Context activation is done by the caller. */
5856 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5857 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5858 const struct wined3d_shader *shader,
5859 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5861 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5862 const struct wined3d_gl_info *gl_info = context->gl_info;
5863 const DWORD *function = shader->function;
5864 struct shader_glsl_ctx_priv priv_ctx;
5865 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5867 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5868 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5870 memset(&priv_ctx, 0, sizeof(priv_ctx));
5871 priv_ctx.cur_ps_args = args;
5872 priv_ctx.cur_np2fixup_info = np2fixup_info;
5873 priv_ctx.string_buffers = string_buffers;
5875 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5877 shader_glsl_enable_extensions(buffer, gl_info);
5878 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
5879 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
5880 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5881 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5882 /* The spec says that it doesn't have to be explicitly enabled, but the
5883 * nvidia drivers write a warning if we don't do so. */
5884 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5885 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5887 /* Base Declarations */
5888 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5890 shader_addline(buffer, "void main()\n{\n");
5892 /* Direct3D applications expect integer vPos values, while OpenGL drivers
5893 * add approximately 0.5. This causes off-by-one problems as spotted by
5894 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
5895 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
5896 * causes precision troubles when we just subtract 0.5.
5898 * To deal with that, just floor() the position. This will eliminate the
5899 * fraction on all cards.
5901 * TODO: Test how this behaves with multisampling.
5903 * An advantage of floor is that it works even if the driver doesn't add
5904 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
5905 * to return in gl_FragCoord, even though coordinates specify the pixel
5906 * centers instead of the pixel corners. This code will behave correctly
5907 * on drivers that returns integer values. */
5908 if (reg_maps->vpos)
5910 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
5911 shader_addline(buffer,
5912 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
5913 else
5914 shader_addline(buffer,
5915 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
5918 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5920 unsigned int i;
5921 WORD map = reg_maps->texcoord;
5923 if (legacy_context)
5925 if (glsl_is_color_reg_read(shader, 0))
5926 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5927 if (glsl_is_color_reg_read(shader, 1))
5928 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5931 for (i = 0; map; map >>= 1, ++i)
5933 if (map & 1)
5935 if (args->pointsprite)
5936 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5937 else if (args->texcoords_initialized & (1u << i))
5938 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5939 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5940 else
5941 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5942 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5946 if (legacy_context)
5947 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5950 /* Pack 3.0 inputs */
5951 if (reg_maps->shader_version.major >= 3)
5952 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5954 /* Base Shader Body */
5955 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5957 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5958 if (reg_maps->shader_version.major < 2)
5959 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
5961 if (args->srgb_correction)
5962 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
5964 /* SM < 3 does not replace the fog stage. */
5965 if (reg_maps->shader_version.major < 3)
5966 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
5968 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
5970 shader_addline(buffer, "}\n");
5972 TRACE("Compiling shader object %u.\n", shader_id);
5973 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5975 return shader_id;
5978 /* Context activation is done by the caller. */
5979 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5980 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
5982 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
5983 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5984 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5985 const struct wined3d_gl_info *gl_info = context->gl_info;
5986 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5987 const DWORD *function = shader->function;
5988 struct shader_glsl_ctx_priv priv_ctx;
5989 unsigned int i;
5991 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5992 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5994 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5996 shader_glsl_enable_extensions(buffer, gl_info);
5997 if (gl_info->supported[ARB_DRAW_INSTANCED])
5998 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5999 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6000 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6002 memset(&priv_ctx, 0, sizeof(priv_ctx));
6003 priv_ctx.cur_vs_args = args;
6004 priv_ctx.string_buffers = string_buffers;
6006 /* Base Declarations */
6007 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6009 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6010 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6012 if (reg_maps->shader_version.major >= 4)
6014 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6015 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
6016 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
6017 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
6020 shader_addline(buffer, "void main()\n{\n");
6022 /* Base Shader Body */
6023 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6025 /* Unpack outputs */
6026 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6028 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6029 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6030 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6031 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
6033 if (reg_maps->shader_version.major < 3)
6035 if (args->fog_src == VS_FOG_Z)
6036 shader_addline(buffer, "%s = gl_Position.z;\n",
6037 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6038 else if (!reg_maps->fog)
6039 shader_addline(buffer, "%s = 0.0;\n",
6040 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6043 /* We always store the clipplanes without y inversion */
6044 if (args->clip_enabled)
6046 if (legacy_context)
6047 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6048 else
6049 for (i = 0; i < gl_info->limits.clipplanes; ++i)
6050 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6053 if (args->point_size && !args->per_vertex_point_size)
6054 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6056 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6057 shader_glsl_fixup_position(buffer);
6059 shader_addline(buffer, "}\n");
6061 TRACE("Compiling shader object %u.\n", shader_id);
6062 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6064 return shader_id;
6067 /* Context activation is done by the caller. */
6068 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
6069 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
6071 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6072 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6073 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6074 const struct wined3d_gl_info *gl_info = context->gl_info;
6075 const DWORD *function = shader->function;
6076 struct shader_glsl_ctx_priv priv_ctx;
6077 GLuint shader_id;
6079 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
6081 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
6083 shader_glsl_enable_extensions(buffer, gl_info);
6084 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
6085 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
6087 memset(&priv_ctx, 0, sizeof(priv_ctx));
6088 priv_ctx.string_buffers = string_buffers;
6089 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6090 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6091 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
6092 shader_addline(buffer, "void main()\n{\n");
6093 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6094 shader_addline(buffer, "}\n");
6096 TRACE("Compiling shader object %u.\n", shader_id);
6097 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6099 return shader_id;
6102 static GLuint find_glsl_pshader(const struct wined3d_context *context,
6103 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6104 struct wined3d_shader *shader,
6105 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
6107 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
6108 struct glsl_shader_private *shader_data;
6109 struct ps_np2fixup_info *np2fixup;
6110 UINT i;
6111 DWORD new_size;
6112 GLuint ret;
6114 if (!shader->backend_data)
6116 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6117 if (!shader->backend_data)
6119 ERR("Failed to allocate backend data.\n");
6120 return 0;
6123 shader_data = shader->backend_data;
6124 gl_shaders = shader_data->gl_shaders.ps;
6126 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6127 * so a linear search is more performant than a hashmap or a binary search
6128 * (cache coherency etc)
6130 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6132 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6134 if (args->np2_fixup)
6135 *np2fixup_info = &gl_shaders[i].np2fixup;
6136 return gl_shaders[i].id;
6140 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6141 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6142 if (shader_data->num_gl_shaders)
6144 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6145 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
6146 new_size * sizeof(*gl_shaders));
6148 else
6150 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6151 new_size = 1;
6154 if(!new_array) {
6155 ERR("Out of memory\n");
6156 return 0;
6158 shader_data->gl_shaders.ps = new_array;
6159 shader_data->shader_array_size = new_size;
6160 gl_shaders = new_array;
6163 gl_shaders[shader_data->num_gl_shaders].args = *args;
6165 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
6166 memset(np2fixup, 0, sizeof(*np2fixup));
6167 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
6169 pixelshader_update_resource_types(shader, args->tex_types);
6171 string_buffer_clear(buffer);
6172 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
6173 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6175 return ret;
6178 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
6179 const DWORD use_map)
6181 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
6182 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
6183 if (stored->point_size != new->point_size)
6184 return FALSE;
6185 if (stored->per_vertex_point_size != new->per_vertex_point_size)
6186 return FALSE;
6187 if (stored->flatshading != new->flatshading)
6188 return FALSE;
6189 if (stored->next_shader_type != new->next_shader_type)
6190 return FALSE;
6191 if (stored->next_shader_input_count != new->next_shader_input_count)
6192 return FALSE;
6193 return stored->fog_src == new->fog_src;
6196 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
6197 struct wined3d_shader *shader, const struct vs_compile_args *args)
6199 UINT i;
6200 DWORD new_size;
6201 DWORD use_map = context->stream_info.use_map;
6202 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
6203 struct glsl_shader_private *shader_data;
6204 GLuint ret;
6206 if (!shader->backend_data)
6208 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6209 if (!shader->backend_data)
6211 ERR("Failed to allocate backend data.\n");
6212 return 0;
6215 shader_data = shader->backend_data;
6216 gl_shaders = shader_data->gl_shaders.vs;
6218 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6219 * so a linear search is more performant than a hashmap or a binary search
6220 * (cache coherency etc)
6222 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6224 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
6225 return gl_shaders[i].id;
6228 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6230 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6231 if (shader_data->num_gl_shaders)
6233 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6234 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
6235 new_size * sizeof(*gl_shaders));
6237 else
6239 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6240 new_size = 1;
6243 if(!new_array) {
6244 ERR("Out of memory\n");
6245 return 0;
6247 shader_data->gl_shaders.vs = new_array;
6248 shader_data->shader_array_size = new_size;
6249 gl_shaders = new_array;
6252 gl_shaders[shader_data->num_gl_shaders].args = *args;
6254 string_buffer_clear(&priv->shader_buffer);
6255 ret = shader_glsl_generate_vshader(context, priv, shader, args);
6256 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6258 return ret;
6261 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
6262 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
6264 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
6265 struct glsl_shader_private *shader_data;
6266 unsigned int i, new_size;
6267 GLuint ret;
6269 if (!shader->backend_data)
6271 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6273 ERR("Failed to allocate backend data.\n");
6274 return 0;
6277 shader_data = shader->backend_data;
6278 gl_shaders = shader_data->gl_shaders.gs;
6280 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6282 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6283 return gl_shaders[i].id;
6286 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6288 if (shader_data->num_gl_shaders)
6290 new_size = shader_data->shader_array_size + 1;
6291 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
6292 new_size * sizeof(*new_array));
6294 else
6296 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
6297 new_size = 1;
6300 if (!new_array)
6302 ERR("Failed to allocate GL shaders array.\n");
6303 return 0;
6305 shader_data->gl_shaders.gs = new_array;
6306 shader_data->shader_array_size = new_size;
6307 gl_shaders = new_array;
6309 string_buffer_clear(&priv->shader_buffer);
6310 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
6311 gl_shaders[shader_data->num_gl_shaders].args = *args;
6312 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6314 return ret;
6317 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
6319 switch (mcs)
6321 case WINED3D_MCS_MATERIAL:
6322 return material;
6323 case WINED3D_MCS_COLOR1:
6324 return "ffp_attrib_diffuse";
6325 case WINED3D_MCS_COLOR2:
6326 return "ffp_attrib_specular";
6327 default:
6328 ERR("Invalid material color source %#x.\n", mcs);
6329 return "<invalid>";
6333 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
6334 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
6336 const char *diffuse, *specular, *emissive, *ambient;
6337 enum wined3d_light_type light_type;
6338 unsigned int i;
6340 if (!settings->lighting)
6342 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6343 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
6344 return;
6347 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
6348 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
6349 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
6350 shader_addline(buffer, "vec3 dir, dst;\n");
6351 shader_addline(buffer, "float att, t;\n");
6353 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
6354 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
6355 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
6356 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
6358 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6360 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
6361 switch (light_type)
6363 case WINED3D_LIGHT_POINT:
6364 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6365 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6366 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6367 shader_addline(buffer, "dst.x = 1.0;\n");
6368 if (legacy_lighting)
6370 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6371 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6373 else
6375 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6377 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6378 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
6379 if (!legacy_lighting)
6380 shader_addline(buffer, "att = 1.0 / att;\n");
6381 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6382 if (!settings->normal)
6384 if (!legacy_lighting)
6385 shader_addline(buffer, "}\n");
6386 break;
6388 shader_addline(buffer, "dir = normalize(dir);\n");
6389 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6390 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6391 if (settings->localviewer)
6392 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6393 else
6394 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6395 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6396 " * ffp_light[%u].specular) * att;\n", i);
6397 if (!legacy_lighting)
6398 shader_addline(buffer, "}\n");
6399 break;
6401 case WINED3D_LIGHT_SPOT:
6402 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
6403 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6404 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6405 shader_addline(buffer, "dst.x = 1.0;\n");
6406 if (legacy_lighting)
6408 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
6409 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6411 else
6413 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
6415 shader_addline(buffer, "dir = normalize(dir);\n");
6416 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
6417 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
6418 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
6419 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
6420 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
6421 i, i, i, i);
6422 if (legacy_lighting)
6423 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6424 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6425 i, i, i);
6426 else
6427 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6428 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6429 i, i, i);
6430 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
6431 if (!settings->normal)
6433 if (!legacy_lighting)
6434 shader_addline(buffer, "}\n");
6435 break;
6437 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6438 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6439 if (settings->localviewer)
6440 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6441 else
6442 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6443 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6444 " * ffp_light[%u].specular) * att;\n", i);
6445 if (!legacy_lighting)
6446 shader_addline(buffer, "}\n");
6447 break;
6449 case WINED3D_LIGHT_DIRECTIONAL:
6450 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6451 if (!settings->normal)
6452 break;
6453 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
6454 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6455 " * ffp_light[%u].diffuse.xyz;\n", i);
6456 /* TODO: In the non-local viewer case the halfvector is constant
6457 * and could be precomputed and stored in a uniform. */
6458 if (settings->localviewer)
6459 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6460 else
6461 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6462 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6463 " * ffp_light[%u].specular;\n", i);
6464 break;
6466 case WINED3D_LIGHT_PARALLELPOINT:
6467 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6468 if (!settings->normal)
6469 break;
6470 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
6471 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6472 " * ffp_light[%u].diffuse.xyz;\n", i);
6473 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6474 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6475 " * ffp_light[%u].specular;\n", i);
6476 break;
6478 default:
6479 if (light_type)
6480 FIXME("Unhandled light type %#x.\n", light_type);
6481 continue;
6485 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6486 ambient, diffuse, emissive);
6487 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6488 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6491 /* Context activation is done by the caller. */
6492 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6493 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6495 static const struct attrib_info
6497 const char type[6];
6498 const char name[24];
6500 attrib_info[] =
6502 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6503 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6504 /* TODO: Indexed vertex blending */
6505 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6506 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6507 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6508 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6509 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6511 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6512 BOOL legacy_lighting = priv->legacy_lighting;
6513 GLuint shader_obj;
6514 unsigned int i;
6515 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6516 BOOL output_legacy_fogcoord = legacy_context;
6518 string_buffer_clear(buffer);
6520 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6522 if (shader_glsl_use_explicit_attrib_location(gl_info))
6523 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6525 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6527 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6529 if (shader_glsl_use_explicit_attrib_location(gl_info))
6530 shader_addline(buffer, "layout(location = %u) ", i);
6531 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
6533 shader_addline(buffer, "\n");
6535 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6536 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6537 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6538 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6540 shader_addline(buffer, "uniform struct\n{\n");
6541 shader_addline(buffer, " vec4 emissive;\n");
6542 shader_addline(buffer, " vec4 ambient;\n");
6543 shader_addline(buffer, " vec4 diffuse;\n");
6544 shader_addline(buffer, " vec4 specular;\n");
6545 shader_addline(buffer, " float shininess;\n");
6546 shader_addline(buffer, "} ffp_material;\n");
6548 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6549 shader_addline(buffer, "uniform struct\n{\n");
6550 shader_addline(buffer, " vec4 diffuse;\n");
6551 shader_addline(buffer, " vec4 specular;\n");
6552 shader_addline(buffer, " vec4 ambient;\n");
6553 shader_addline(buffer, " vec4 position;\n");
6554 shader_addline(buffer, " vec3 direction;\n");
6555 shader_addline(buffer, " float range;\n");
6556 shader_addline(buffer, " float falloff;\n");
6557 shader_addline(buffer, " float c_att;\n");
6558 shader_addline(buffer, " float l_att;\n");
6559 shader_addline(buffer, " float q_att;\n");
6560 shader_addline(buffer, " float cos_htheta;\n");
6561 shader_addline(buffer, " float cos_hphi;\n");
6562 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6564 if (settings->point_size)
6566 shader_addline(buffer, "uniform struct\n{\n");
6567 shader_addline(buffer, " float size;\n");
6568 shader_addline(buffer, " float size_min;\n");
6569 shader_addline(buffer, " float size_max;\n");
6570 shader_addline(buffer, " float c_att;\n");
6571 shader_addline(buffer, " float l_att;\n");
6572 shader_addline(buffer, " float q_att;\n");
6573 shader_addline(buffer, "} ffp_point;\n");
6576 if (legacy_context)
6578 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6579 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6580 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6581 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6583 else
6585 if (settings->clipping)
6586 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.clipplanes);
6588 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6589 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6590 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6591 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6594 shader_addline(buffer, "\nvoid main()\n{\n");
6595 shader_addline(buffer, "float m;\n");
6596 shader_addline(buffer, "vec3 r;\n");
6598 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6600 if (attrib_info[i].name[0])
6601 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
6602 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
6604 for (i = 0; i < MAX_TEXTURES; ++i)
6606 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6607 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6608 && settings->texcoords & (1u << i))
6609 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6612 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6614 if (settings->transformed)
6616 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6617 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6618 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6620 else
6622 for (i = 0; i < settings->vertexblends; ++i)
6623 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6625 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6626 for (i = 0; i < settings->vertexblends + 1; ++i)
6627 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6629 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6630 if (settings->clipping)
6632 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
6633 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6634 else
6635 for (i = 0; i < gl_info->limits.clipplanes; ++i)
6636 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
6638 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6641 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6642 if (settings->normal)
6644 if (!settings->vertexblends)
6646 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6648 else
6650 for (i = 0; i < settings->vertexblends + 1; ++i)
6651 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6654 if (settings->normalize)
6655 shader_addline(buffer, "normal = normalize(normal);\n");
6658 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6659 if (legacy_context)
6661 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6662 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6664 else
6666 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
6667 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
6670 for (i = 0; i < MAX_TEXTURES; ++i)
6672 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6674 switch (settings->texgen[i] & 0xffff0000)
6676 case WINED3DTSS_TCI_PASSTHRU:
6677 if (settings->texcoords & (1u << i))
6678 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6679 i, i, i);
6680 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6681 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6682 else
6683 output_legacy_texcoord = FALSE;
6684 break;
6686 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6687 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6688 break;
6690 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6691 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6692 break;
6694 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6695 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6696 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6697 break;
6699 case WINED3DTSS_TCI_SPHEREMAP:
6700 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6701 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6702 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6703 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6704 break;
6706 default:
6707 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6708 break;
6710 if (output_legacy_texcoord)
6711 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6714 switch (settings->fog_mode)
6716 case WINED3D_FFP_VS_FOG_OFF:
6717 output_legacy_fogcoord = FALSE;
6718 break;
6720 case WINED3D_FFP_VS_FOG_FOGCOORD:
6721 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6722 break;
6724 case WINED3D_FFP_VS_FOG_RANGE:
6725 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6726 break;
6728 case WINED3D_FFP_VS_FOG_DEPTH:
6729 if (settings->ortho_fog)
6730 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6731 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6732 else if (settings->transformed)
6733 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6734 else
6735 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6736 break;
6738 default:
6739 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6740 break;
6742 if (output_legacy_fogcoord)
6743 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6745 if (settings->point_size)
6747 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6748 " + ffp_point.l_att * length(ec_pos.xyz)"
6749 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6750 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6751 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6754 shader_addline(buffer, "}\n");
6756 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6757 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6759 return shader_obj;
6762 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6763 DWORD argnum, unsigned int stage, DWORD arg)
6765 const char *ret;
6767 if (arg == ARG_UNUSED)
6768 return "<unused arg>";
6770 switch (arg & WINED3DTA_SELECTMASK)
6772 case WINED3DTA_DIFFUSE:
6773 ret = "ffp_varying_diffuse";
6774 break;
6776 case WINED3DTA_CURRENT:
6777 ret = "ret";
6778 break;
6780 case WINED3DTA_TEXTURE:
6781 switch (stage)
6783 case 0: ret = "tex0"; break;
6784 case 1: ret = "tex1"; break;
6785 case 2: ret = "tex2"; break;
6786 case 3: ret = "tex3"; break;
6787 case 4: ret = "tex4"; break;
6788 case 5: ret = "tex5"; break;
6789 case 6: ret = "tex6"; break;
6790 case 7: ret = "tex7"; break;
6791 default:
6792 ret = "<invalid texture>";
6793 break;
6795 break;
6797 case WINED3DTA_TFACTOR:
6798 ret = "tex_factor";
6799 break;
6801 case WINED3DTA_SPECULAR:
6802 ret = "ffp_varying_specular";
6803 break;
6805 case WINED3DTA_TEMP:
6806 ret = "temp_reg";
6807 break;
6809 case WINED3DTA_CONSTANT:
6810 switch (stage)
6812 case 0: ret = "tss_const0"; break;
6813 case 1: ret = "tss_const1"; break;
6814 case 2: ret = "tss_const2"; break;
6815 case 3: ret = "tss_const3"; break;
6816 case 4: ret = "tss_const4"; break;
6817 case 5: ret = "tss_const5"; break;
6818 case 6: ret = "tss_const6"; break;
6819 case 7: ret = "tss_const7"; break;
6820 default:
6821 ret = "<invalid constant>";
6822 break;
6824 break;
6826 default:
6827 return "<unhandled arg>";
6830 if (arg & WINED3DTA_COMPLEMENT)
6832 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6833 if (argnum == 0)
6834 ret = "arg0";
6835 else if (argnum == 1)
6836 ret = "arg1";
6837 else if (argnum == 2)
6838 ret = "arg2";
6841 if (arg & WINED3DTA_ALPHAREPLICATE)
6843 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6844 if (argnum == 0)
6845 ret = "arg0";
6846 else if (argnum == 1)
6847 ret = "arg1";
6848 else if (argnum == 2)
6849 ret = "arg2";
6852 return ret;
6855 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6856 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6858 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6860 if (color && alpha)
6861 dstmask = "";
6862 else if (color)
6863 dstmask = ".xyz";
6864 else
6865 dstmask = ".w";
6867 if (dst == tempreg)
6868 dstreg = "temp_reg";
6869 else
6870 dstreg = "ret";
6872 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6873 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6874 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6876 switch (op)
6878 case WINED3D_TOP_DISABLE:
6879 break;
6881 case WINED3D_TOP_SELECT_ARG1:
6882 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6883 break;
6885 case WINED3D_TOP_SELECT_ARG2:
6886 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6887 break;
6889 case WINED3D_TOP_MODULATE:
6890 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6891 break;
6893 case WINED3D_TOP_MODULATE_4X:
6894 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6895 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6896 break;
6898 case WINED3D_TOP_MODULATE_2X:
6899 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6900 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6901 break;
6903 case WINED3D_TOP_ADD:
6904 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6905 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6906 break;
6908 case WINED3D_TOP_ADD_SIGNED:
6909 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6910 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6911 break;
6913 case WINED3D_TOP_ADD_SIGNED_2X:
6914 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6915 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6916 break;
6918 case WINED3D_TOP_SUBTRACT:
6919 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6920 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6921 break;
6923 case WINED3D_TOP_ADD_SMOOTH:
6924 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6925 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6926 break;
6928 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6929 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6930 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6931 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6932 break;
6934 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6935 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6936 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6937 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6938 break;
6940 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6941 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6942 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6943 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6944 break;
6946 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6947 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6948 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6949 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6950 break;
6952 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6953 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6954 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6955 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6956 break;
6958 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6959 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6960 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6961 break;
6963 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6964 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6965 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6966 break;
6968 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6969 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6970 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6971 break;
6972 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6973 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6974 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6975 break;
6977 case WINED3D_TOP_BUMPENVMAP:
6978 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6979 /* These are handled in the first pass, nothing to do. */
6980 break;
6982 case WINED3D_TOP_DOTPRODUCT3:
6983 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6984 dstreg, dstmask, arg1, arg2, dstmask);
6985 break;
6987 case WINED3D_TOP_MULTIPLY_ADD:
6988 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6989 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6990 break;
6992 case WINED3D_TOP_LERP:
6993 /* MSDN isn't quite right here. */
6994 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6995 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6996 break;
6998 default:
6999 FIXME("Unhandled operation %#x.\n", op);
7000 break;
7004 /* Context activation is done by the caller. */
7005 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
7006 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
7008 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
7009 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
7010 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7011 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7012 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
7013 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
7014 UINT lowest_disabled_stage;
7015 GLuint shader_id;
7016 DWORD arg0, arg1, arg2;
7017 unsigned int stage;
7019 string_buffer_clear(buffer);
7021 /* Find out which textures are read */
7022 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7024 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7025 break;
7027 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
7028 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
7029 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
7031 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
7032 || (stage == 0 && settings->color_key_enabled))
7033 tex_map |= 1u << stage;
7034 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7035 tfactor_used = TRUE;
7036 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7037 tempreg_used = TRUE;
7038 if (settings->op[stage].dst == tempreg)
7039 tempreg_used = TRUE;
7040 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7041 tss_const_map |= 1u << stage;
7043 switch (settings->op[stage].cop)
7045 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7046 lum_map |= 1u << stage;
7047 /* fall through */
7048 case WINED3D_TOP_BUMPENVMAP:
7049 bump_map |= 1u << stage;
7050 /* fall through */
7051 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7052 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7053 tex_map |= 1u << stage;
7054 break;
7056 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7057 tfactor_used = TRUE;
7058 break;
7060 default:
7061 break;
7064 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7065 continue;
7067 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
7068 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
7069 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
7071 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
7072 tex_map |= 1u << stage;
7073 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7074 tfactor_used = TRUE;
7075 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7076 tempreg_used = TRUE;
7077 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7078 tss_const_map |= 1u << stage;
7080 lowest_disabled_stage = stage;
7082 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
7084 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7085 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7087 if (!needs_legacy_glsl_syntax(gl_info))
7088 shader_addline(buffer, "out vec4 ps_out[1];\n");
7090 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
7091 shader_addline(buffer, "vec4 ret;\n");
7092 if (tempreg_used || settings->sRGB_write)
7093 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
7094 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
7096 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7098 if (tss_const_map & (1u << stage))
7099 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
7101 if (!(tex_map & (1u << stage)))
7102 continue;
7104 switch (settings->op[stage].tex_type)
7106 case WINED3D_GL_RES_TYPE_TEX_1D:
7107 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
7108 break;
7109 case WINED3D_GL_RES_TYPE_TEX_2D:
7110 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
7111 break;
7112 case WINED3D_GL_RES_TYPE_TEX_3D:
7113 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
7114 break;
7115 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7116 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
7117 break;
7118 case WINED3D_GL_RES_TYPE_TEX_RECT:
7119 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
7120 break;
7121 default:
7122 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
7123 break;
7126 shader_addline(buffer, "vec4 tex%u;\n", stage);
7128 if (!(bump_map & (1u << stage)))
7129 continue;
7130 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
7132 if (!(lum_map & (1u << stage)))
7133 continue;
7134 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
7135 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
7137 if (tfactor_used)
7138 shader_addline(buffer, "uniform vec4 tex_factor;\n");
7139 if (settings->color_key_enabled)
7140 shader_addline(buffer, "uniform vec4 color_key[2];\n");
7141 shader_addline(buffer, "uniform vec4 specular_enable;\n");
7143 if (settings->sRGB_write)
7145 shader_addline(buffer, "const vec4 srgb_const0 = ");
7146 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7147 shader_addline(buffer, ";\n");
7148 shader_addline(buffer, "const vec4 srgb_const1 = ");
7149 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7150 shader_addline(buffer, ";\n");
7153 shader_addline(buffer, "uniform struct\n{\n");
7154 shader_addline(buffer, " vec4 color;\n");
7155 shader_addline(buffer, " float density;\n");
7156 shader_addline(buffer, " float end;\n");
7157 shader_addline(buffer, " float scale;\n");
7158 shader_addline(buffer, "} ffp_fog;\n");
7160 if (alpha_test_func != WINED3D_CMP_ALWAYS)
7161 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7163 if (legacy_context)
7165 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7166 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7167 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7168 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7169 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7171 else
7173 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7174 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7175 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7176 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7177 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7180 shader_addline(buffer, "void main()\n{\n");
7182 if (legacy_context)
7184 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7185 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7188 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7190 if (tex_map & (1u << stage))
7192 if (settings->pointsprite)
7193 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
7194 else if (settings->texcoords_initialized & (1u << stage))
7195 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
7196 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
7197 else
7198 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
7202 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
7203 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7205 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
7206 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
7208 /* Generate texture sampling instructions */
7209 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
7211 const char *texture_function, *coord_mask;
7212 BOOL proj;
7214 if (!(tex_map & (1u << stage)))
7215 continue;
7217 if (settings->op[stage].projected == proj_none)
7219 proj = FALSE;
7221 else if (settings->op[stage].projected == proj_count4
7222 || settings->op[stage].projected == proj_count3)
7224 proj = TRUE;
7226 else
7228 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
7229 proj = TRUE;
7232 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
7233 proj = FALSE;
7235 switch (settings->op[stage].tex_type)
7237 case WINED3D_GL_RES_TYPE_TEX_1D:
7238 if (proj)
7240 texture_function = "texture1DProj";
7241 coord_mask = "xw";
7243 else
7245 texture_function = "texture1D";
7246 coord_mask = "x";
7248 break;
7249 case WINED3D_GL_RES_TYPE_TEX_2D:
7250 if (proj)
7252 texture_function = "texture2DProj";
7253 coord_mask = "xyw";
7255 else
7257 texture_function = "texture2D";
7258 coord_mask = "xy";
7260 break;
7261 case WINED3D_GL_RES_TYPE_TEX_3D:
7262 if (proj)
7264 texture_function = "texture3DProj";
7265 coord_mask = "xyzw";
7267 else
7269 texture_function = "texture3D";
7270 coord_mask = "xyz";
7272 break;
7273 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7274 texture_function = "textureCube";
7275 coord_mask = "xyz";
7276 break;
7277 case WINED3D_GL_RES_TYPE_TEX_RECT:
7278 if (proj)
7280 texture_function = "texture2DRectProj";
7281 coord_mask = "xyw";
7283 else
7285 texture_function = "texture2DRect";
7286 coord_mask = "xy";
7288 break;
7289 default:
7290 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
7291 texture_function = "";
7292 coord_mask = "xyzw";
7293 break;
7295 if (!needs_legacy_glsl_syntax(gl_info))
7296 texture_function = proj ? "textureProj" : "texture";
7298 if (stage > 0
7299 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
7300 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
7302 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
7304 /* With projective textures, texbem only divides the static
7305 * texture coord, not the displacement, so multiply the
7306 * displacement with the dividing parameter before passing it to
7307 * TXP. */
7308 if (settings->op[stage].projected != proj_none)
7310 if (settings->op[stage].projected == proj_count4)
7312 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7313 stage, stage);
7314 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
7316 else
7318 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7319 stage, stage);
7320 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
7323 else
7325 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
7328 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7329 stage, texture_function, stage, coord_mask);
7331 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7332 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7333 stage, stage - 1, stage - 1, stage - 1);
7335 else if (settings->op[stage].projected == proj_count3)
7337 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7338 stage, texture_function, stage, stage);
7340 else
7342 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7343 stage, texture_function, stage, stage, coord_mask);
7346 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
7347 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
7348 settings->op[stage].color_fixup);
7351 if (settings->color_key_enabled)
7353 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7354 shader_addline(buffer, " discard;\n");
7357 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
7359 /* Generate the main shader */
7360 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7362 BOOL op_equal;
7364 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7365 break;
7367 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7368 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7369 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
7370 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7371 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7372 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
7373 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7374 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7375 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
7376 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7377 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7378 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
7379 else
7380 op_equal = settings->op[stage].aop == settings->op[stage].cop
7381 && settings->op[stage].carg0 == settings->op[stage].aarg0
7382 && settings->op[stage].carg1 == settings->op[stage].aarg1
7383 && settings->op[stage].carg2 == settings->op[stage].aarg2;
7385 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7387 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7388 settings->op[stage].cop, settings->op[stage].carg0,
7389 settings->op[stage].carg1, settings->op[stage].carg2);
7391 else if (op_equal)
7393 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
7394 settings->op[stage].cop, settings->op[stage].carg0,
7395 settings->op[stage].carg1, settings->op[stage].carg2);
7397 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
7398 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7400 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7401 settings->op[stage].cop, settings->op[stage].carg0,
7402 settings->op[stage].carg1, settings->op[stage].carg2);
7403 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
7404 settings->op[stage].aop, settings->op[stage].aarg0,
7405 settings->op[stage].aarg1, settings->op[stage].aarg2);
7409 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
7410 get_fragment_output(gl_info));
7412 if (settings->sRGB_write)
7413 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7415 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
7417 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
7419 shader_addline(buffer, "}\n");
7421 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7422 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7424 string_buffer_release(&priv->string_buffers, tex_reg_name);
7425 return shader_id;
7428 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
7429 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
7431 struct glsl_ffp_vertex_shader *shader;
7432 const struct wine_rb_entry *entry;
7434 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
7435 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
7437 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
7438 return NULL;
7440 shader->desc.settings = *settings;
7441 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
7442 list_init(&shader->linked_programs);
7443 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
7444 ERR("Failed to insert ffp vertex shader.\n");
7446 return shader;
7449 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
7450 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
7452 struct glsl_ffp_fragment_shader *glsl_desc;
7453 const struct ffp_frag_desc *desc;
7455 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
7456 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
7458 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
7459 return NULL;
7461 glsl_desc->entry.settings = *args;
7462 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
7463 list_init(&glsl_desc->linked_programs);
7464 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
7466 return glsl_desc;
7470 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
7471 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
7473 unsigned int i;
7474 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7476 for (i = 0; i < vs_c_count; ++i)
7478 string_buffer_sprintf(name, "vs_c[%u]", i);
7479 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7481 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
7483 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7485 string_buffer_sprintf(name, "vs_i[%u]", i);
7486 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7489 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7491 string_buffer_sprintf(name, "vs_b[%u]", i);
7492 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7495 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7497 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7499 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7500 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7502 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7503 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7504 for (i = 0; i < MAX_TEXTURES; ++i)
7506 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7507 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7509 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7510 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7511 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7512 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7513 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7514 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7515 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7517 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7518 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7519 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7520 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7521 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7522 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7523 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7524 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7525 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7526 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7527 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7528 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7529 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7530 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7531 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7532 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7533 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7534 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7535 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7536 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7537 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7538 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7539 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7540 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7542 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7543 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7544 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7545 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7546 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7547 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7548 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
7550 string_buffer_release(&priv->string_buffers, name);
7553 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
7554 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
7556 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7559 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7560 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7562 unsigned int i;
7563 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7565 for (i = 0; i < ps_c_count; ++i)
7567 string_buffer_sprintf(name, "ps_c[%u]", i);
7568 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7570 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
7572 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7574 string_buffer_sprintf(name, "ps_i[%u]", i);
7575 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7578 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7580 string_buffer_sprintf(name, "ps_b[%u]", i);
7581 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7584 for (i = 0; i < MAX_TEXTURES; ++i)
7586 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7587 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7588 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7589 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7590 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7591 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7592 string_buffer_sprintf(name, "tss_const%u", i);
7593 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7596 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7597 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7599 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7600 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7601 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7602 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7604 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
7606 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7607 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7608 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7610 string_buffer_release(&priv->string_buffers, name);
7613 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7614 struct shader_glsl_priv *priv, GLuint program_id,
7615 const struct wined3d_shader_reg_maps *reg_maps)
7617 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7618 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7619 unsigned int i, base, count;
7620 GLuint block_idx;
7622 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
7623 for (i = 0; i < count; ++i)
7625 if (!reg_maps->cb_sizes[i])
7626 continue;
7628 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7629 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7630 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7632 checkGLcall("glUniformBlockBinding");
7633 string_buffer_release(&priv->string_buffers, name);
7636 /* Context activation is done by the caller. */
7637 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7638 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7640 const struct wined3d_gl_info *gl_info = context->gl_info;
7641 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7642 const struct ps_np2fixup_info *np2fixup_info = NULL;
7643 struct glsl_shader_prog_link *entry = NULL;
7644 struct wined3d_shader *vshader = NULL;
7645 struct wined3d_shader *gshader = NULL;
7646 struct wined3d_shader *pshader = NULL;
7647 GLuint program_id;
7648 GLuint reorder_shader_id = 0;
7649 unsigned int i;
7650 GLuint vs_id = 0;
7651 GLuint gs_id = 0;
7652 GLuint ps_id = 0;
7653 struct list *ps_list, *vs_list;
7654 WORD attribs_map;
7655 struct wined3d_string_buffer *tmp_name;
7657 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7659 vs_id = ctx_data->glsl_program->vs.id;
7660 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7662 if (use_vs(state))
7664 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7665 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7667 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7668 && ctx_data->glsl_program->gs.id)
7670 gs_id = ctx_data->glsl_program->gs.id;
7672 else if (gshader)
7674 struct gs_compile_args args;
7676 find_gs_compile_args(state, gshader, &args);
7677 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
7681 else if (use_vs(state))
7683 struct vs_compile_args vs_compile_args;
7685 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7686 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7688 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7689 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
7690 vs_list = &vshader->linked_programs;
7692 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7693 && ctx_data->glsl_program->gs.id)
7695 gs_id = ctx_data->glsl_program->gs.id;
7697 else if (gshader)
7699 struct gs_compile_args gs_compile_args;
7701 find_gs_compile_args(state, gshader, &gs_compile_args);
7702 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
7705 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7707 struct glsl_ffp_vertex_shader *ffp_shader;
7708 struct wined3d_ffp_vs_settings settings;
7710 wined3d_ffp_get_vs_settings(context, state, &settings);
7711 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7712 vs_id = ffp_shader->id;
7713 vs_list = &ffp_shader->linked_programs;
7716 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7718 ps_id = ctx_data->glsl_program->ps.id;
7719 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7721 if (use_ps(state))
7722 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7724 else if (use_ps(state))
7726 struct ps_compile_args ps_compile_args;
7727 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7728 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7729 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7730 pshader, &ps_compile_args, &np2fixup_info);
7731 ps_list = &pshader->linked_programs;
7733 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7735 struct glsl_ffp_fragment_shader *ffp_shader;
7736 struct ffp_frag_settings settings;
7738 gen_ffp_frag_op(context, state, &settings, FALSE);
7739 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7740 ps_id = ffp_shader->id;
7741 ps_list = &ffp_shader->linked_programs;
7744 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7746 ctx_data->glsl_program = entry;
7747 return;
7750 /* If we get to this point, then no matching program exists, so we create one */
7751 program_id = GL_EXTCALL(glCreateProgram());
7752 TRACE("Created new GLSL shader program %u.\n", program_id);
7754 /* Create the entry */
7755 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7756 entry->id = program_id;
7757 entry->vs.id = vs_id;
7758 entry->gs.id = gs_id;
7759 entry->ps.id = ps_id;
7760 entry->constant_version = 0;
7761 entry->ps.np2_fixup_info = np2fixup_info;
7762 /* Add the hash table entry */
7763 add_glsl_program_entry(priv, entry);
7765 /* Set the current program */
7766 ctx_data->glsl_program = entry;
7768 /* Attach GLSL vshader */
7769 if (vs_id)
7771 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7772 GL_EXTCALL(glAttachShader(program_id, vs_id));
7773 checkGLcall("glAttachShader");
7775 list_add_head(vs_list, &entry->vs.shader_entry);
7778 if (vshader)
7780 attribs_map = vshader->reg_maps.input_registers;
7781 if (vshader->reg_maps.shader_version.major < 4)
7783 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
7784 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7785 d3d_info->emulated_flatshading
7786 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7787 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7788 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7789 checkGLcall("glAttachShader");
7790 /* Flag the reorder function for deletion, it will be freed
7791 * automatically when the program is destroyed. */
7792 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7795 else
7797 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7800 if (!shader_glsl_use_explicit_attrib_location(gl_info))
7802 /* Bind vertex attributes to a corresponding index number to match
7803 * the same index numbers as ARB_vertex_programs (makes loading
7804 * vertex attributes simpler). With this method, we can use the
7805 * exact same code to load the attributes later for both ARB and
7806 * GLSL shaders.
7808 * We have to do this here because we need to know the Program ID
7809 * in order to make the bindings work, and it has to be done prior
7810 * to linking the GLSL program. */
7811 tmp_name = string_buffer_get(&priv->string_buffers);
7812 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7814 if (!(attribs_map & 1))
7815 continue;
7817 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7818 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7819 if (vshader && vshader->reg_maps.shader_version.major >= 4)
7821 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
7822 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7823 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
7824 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7827 checkGLcall("glBindAttribLocation");
7828 string_buffer_release(&priv->string_buffers, tmp_name);
7831 if (gshader)
7833 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7834 GL_EXTCALL(glAttachShader(program_id, gs_id));
7835 checkGLcall("glAttachShader");
7837 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7839 TRACE("input type %s, output type %s, vertices out %u.\n",
7840 debug_d3dprimitivetype(gshader->u.gs.input_type),
7841 debug_d3dprimitivetype(gshader->u.gs.output_type),
7842 gshader->u.gs.vertices_out);
7843 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7844 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7845 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7846 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7847 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7848 gshader->u.gs.vertices_out));
7849 checkGLcall("glProgramParameteriARB");
7852 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7855 /* Attach GLSL pshader */
7856 if (ps_id)
7858 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7859 GL_EXTCALL(glAttachShader(program_id, ps_id));
7860 checkGLcall("glAttachShader");
7862 list_add_head(ps_list, &entry->ps.shader_entry);
7865 /* Link the program */
7866 TRACE("Linking GLSL shader program %u.\n", program_id);
7867 GL_EXTCALL(glLinkProgram(program_id));
7868 shader_glsl_validate_link(gl_info, program_id);
7870 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7871 vshader ? vshader->limits->constant_float : 0);
7872 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
7873 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7874 pshader ? pshader->limits->constant_float : 0);
7875 checkGLcall("Find glsl program uniform locations");
7877 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7879 if (pshader && pshader->reg_maps.shader_version.major >= 3
7880 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7882 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7883 entry->vs.vertex_color_clamp = GL_FALSE;
7885 else
7887 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7890 else
7892 /* With core profile we never change vertex_color_clamp from
7893 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
7894 * glClampColorARB(). */
7895 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7898 /* Set the shader to allow uniform loading on it */
7899 GL_EXTCALL(glUseProgram(program_id));
7900 checkGLcall("glUseProgram");
7902 /* Texture unit mapping is set up to be the same each time the shader
7903 * program is used so we can hardcode the sampler uniform values. */
7904 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7906 entry->constant_update_mask = 0;
7907 if (vshader)
7909 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7910 if (vshader->reg_maps.integer_constants)
7911 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7912 if (vshader->reg_maps.boolean_constants)
7913 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7914 if (entry->vs.pos_fixup_location != -1)
7915 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
7917 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps);
7918 shader_glsl_load_icb(gl_info, priv, program_id, &vshader->reg_maps);
7920 else
7922 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7923 | WINED3D_SHADER_CONST_FFP_PROJ;
7925 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7927 if (entry->vs.modelview_matrix_location[i] != -1)
7929 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7930 break;
7934 for (i = 0; i < MAX_TEXTURES; ++i)
7936 if (entry->vs.texture_matrix_location[i] != -1)
7938 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7939 break;
7942 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7943 || entry->vs.material_specular_location != -1
7944 || entry->vs.material_emissive_location != -1
7945 || entry->vs.material_shininess_location != -1)
7946 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7947 if (entry->vs.light_ambient_location != -1)
7948 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7950 if (entry->vs.clip_planes_location != -1)
7951 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
7952 if (entry->vs.pointsize_min_location != -1)
7953 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7955 if (gshader)
7957 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
7958 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps);
7959 shader_glsl_load_icb(gl_info, priv, program_id, &gshader->reg_maps);
7962 if (ps_id)
7964 if (pshader)
7966 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7967 if (pshader->reg_maps.integer_constants)
7968 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7969 if (pshader->reg_maps.boolean_constants)
7970 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7971 if (entry->ps.ycorrection_location != -1)
7972 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7974 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps);
7975 shader_glsl_load_icb(gl_info, priv, program_id, &pshader->reg_maps);
7977 else
7979 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7982 for (i = 0; i < MAX_TEXTURES; ++i)
7984 if (entry->ps.bumpenv_mat_location[i] != -1)
7986 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7987 break;
7991 if (entry->ps.fog_color_location != -1)
7992 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7993 if (entry->ps.alpha_test_ref_location != -1)
7994 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
7995 if (entry->ps.np2_fixup_location != -1)
7996 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7997 if (entry->ps.color_key_location != -1)
7998 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8002 /* Context activation is done by the caller. */
8003 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
8004 BOOL masked)
8006 GLuint program_id;
8007 GLuint vshader_id, pshader_id;
8008 const char *blt_pshader;
8010 static const char blt_vshader[] =
8011 "#version 120\n"
8012 "void main(void)\n"
8013 "{\n"
8014 " gl_Position = gl_Vertex;\n"
8015 " gl_FrontColor = vec4(1.0);\n"
8016 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
8017 "}\n";
8019 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
8021 /* WINED3D_GL_RES_TYPE_TEX_1D */
8022 NULL,
8023 /* WINED3D_GL_RES_TYPE_TEX_2D */
8024 "#version 120\n"
8025 "uniform sampler2D sampler;\n"
8026 "void main(void)\n"
8027 "{\n"
8028 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
8029 "}\n",
8030 /* WINED3D_GL_RES_TYPE_TEX_3D */
8031 NULL,
8032 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
8033 "#version 120\n"
8034 "uniform samplerCube sampler;\n"
8035 "void main(void)\n"
8036 "{\n"
8037 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
8038 "}\n",
8039 /* WINED3D_GL_RES_TYPE_TEX_RECT */
8040 "#version 120\n"
8041 "#extension GL_ARB_texture_rectangle : enable\n"
8042 "uniform sampler2DRect sampler;\n"
8043 "void main(void)\n"
8044 "{\n"
8045 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
8046 "}\n",
8047 /* WINED3D_GL_RES_TYPE_BUFFER */
8048 NULL,
8049 /* WINED3D_GL_RES_TYPE_RB */
8050 NULL,
8053 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
8055 /* WINED3D_GL_RES_TYPE_TEX_1D */
8056 NULL,
8057 /* WINED3D_GL_RES_TYPE_TEX_2D */
8058 "#version 120\n"
8059 "uniform sampler2D sampler;\n"
8060 "uniform vec4 mask;\n"
8061 "void main(void)\n"
8062 "{\n"
8063 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
8064 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
8065 "}\n",
8066 /* WINED3D_GL_RES_TYPE_TEX_3D */
8067 NULL,
8068 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
8069 "#version 120\n"
8070 "uniform samplerCube sampler;\n"
8071 "uniform vec4 mask;\n"
8072 "void main(void)\n"
8073 "{\n"
8074 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
8075 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
8076 "}\n",
8077 /* WINED3D_GL_RES_TYPE_TEX_RECT */
8078 "#version 120\n"
8079 "#extension GL_ARB_texture_rectangle : enable\n"
8080 "uniform sampler2DRect sampler;\n"
8081 "uniform vec4 mask;\n"
8082 "void main(void)\n"
8083 "{\n"
8084 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
8085 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
8086 "}\n",
8087 /* WINED3D_GL_RES_TYPE_BUFFER */
8088 NULL,
8089 /* WINED3D_GL_RES_TYPE_RB */
8090 NULL,
8093 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
8094 if (!blt_pshader)
8096 FIXME("tex_type %#x not supported\n", tex_type);
8097 return 0;
8100 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8101 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
8103 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8104 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
8106 program_id = GL_EXTCALL(glCreateProgram());
8107 GL_EXTCALL(glAttachShader(program_id, vshader_id));
8108 GL_EXTCALL(glAttachShader(program_id, pshader_id));
8109 GL_EXTCALL(glLinkProgram(program_id));
8111 shader_glsl_validate_link(gl_info, program_id);
8113 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
8114 * is destroyed
8116 GL_EXTCALL(glDeleteShader(vshader_id));
8117 GL_EXTCALL(glDeleteShader(pshader_id));
8118 return program_id;
8121 /* Context activation is done by the caller. */
8122 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
8123 const struct wined3d_state *state)
8125 struct glsl_context_data *ctx_data = context->shader_backend_data;
8126 const struct wined3d_gl_info *gl_info = context->gl_info;
8127 struct shader_glsl_priv *priv = shader_priv;
8128 GLuint program_id = 0, prev_id = 0;
8129 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
8131 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
8132 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
8134 if (ctx_data->glsl_program)
8136 prev_id = ctx_data->glsl_program->id;
8137 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8139 else
8141 prev_id = 0;
8142 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8145 set_glsl_shader_program(context, state, priv, ctx_data);
8147 if (ctx_data->glsl_program)
8149 program_id = ctx_data->glsl_program->id;
8150 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8152 else
8154 program_id = 0;
8155 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8158 if (old_vertex_color_clamp != current_vertex_color_clamp)
8160 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8162 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
8163 checkGLcall("glClampColorARB");
8165 else
8167 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
8171 TRACE("Using GLSL program %u.\n", program_id);
8173 if (prev_id != program_id)
8175 GL_EXTCALL(glUseProgram(program_id));
8176 checkGLcall("glUseProgram");
8178 if (program_id)
8179 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
8183 /* "context" is not necessarily the currently active context. */
8184 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
8186 struct glsl_context_data *ctx_data = context->shader_backend_data;
8188 ctx_data->glsl_program = NULL;
8189 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
8190 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8191 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8192 | (1u << WINED3D_SHADER_TYPE_HULL)
8193 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
8194 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
8197 /* Context activation is done by the caller. */
8198 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
8200 const struct wined3d_gl_info *gl_info = context->gl_info;
8201 struct shader_glsl_priv *priv = shader_priv;
8203 shader_glsl_invalidate_current_program(context);
8204 GL_EXTCALL(glUseProgram(0));
8205 checkGLcall("glUseProgram");
8207 priv->vertex_pipe->vp_enable(gl_info, FALSE);
8208 priv->fragment_pipe->enable_extension(gl_info, FALSE);
8210 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8212 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
8213 checkGLcall("glClampColorARB");
8217 /* Context activation is done by the caller. */
8218 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
8219 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
8221 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
8222 struct shader_glsl_priv *priv = shader_priv;
8223 GLuint *blt_program;
8224 GLint loc;
8226 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
8227 if (!*blt_program)
8229 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
8230 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
8231 GL_EXTCALL(glUseProgram(*blt_program));
8232 GL_EXTCALL(glUniform1i(loc, 0));
8234 else
8236 GL_EXTCALL(glUseProgram(*blt_program));
8239 if (masked)
8241 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
8242 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
8246 /* Context activation is done by the caller. */
8247 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
8249 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
8250 GLuint program_id;
8252 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8253 if (program_id) TRACE("Using GLSL program %u\n", program_id);
8255 GL_EXTCALL(glUseProgram(program_id));
8256 checkGLcall("glUseProgram");
8259 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
8260 const struct glsl_shader_prog_link *program)
8262 const struct glsl_context_data *ctx_data;
8263 struct wined3d_context *context;
8264 unsigned int i;
8266 for (i = 0; i < device->context_count; ++i)
8268 context = device->contexts[i];
8269 ctx_data = context->shader_backend_data;
8271 if (ctx_data->glsl_program == program)
8272 shader_glsl_invalidate_current_program(context);
8276 static void shader_glsl_destroy(struct wined3d_shader *shader)
8278 struct glsl_shader_private *shader_data = shader->backend_data;
8279 struct wined3d_device *device = shader->device;
8280 struct shader_glsl_priv *priv = device->shader_priv;
8281 const struct wined3d_gl_info *gl_info;
8282 const struct list *linked_programs;
8283 struct wined3d_context *context;
8285 if (!shader_data || !shader_data->num_gl_shaders)
8287 HeapFree(GetProcessHeap(), 0, shader_data);
8288 shader->backend_data = NULL;
8289 return;
8292 context = context_acquire(device, NULL);
8293 gl_info = context->gl_info;
8295 TRACE("Deleting linked programs.\n");
8296 linked_programs = &shader->linked_programs;
8297 if (linked_programs->next)
8299 struct glsl_shader_prog_link *entry, *entry2;
8300 UINT i;
8302 switch (shader->reg_maps.shader_version.type)
8304 case WINED3D_SHADER_TYPE_PIXEL:
8306 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
8308 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8310 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
8311 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8312 checkGLcall("glDeleteShader");
8314 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
8316 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8317 struct glsl_shader_prog_link, ps.shader_entry)
8319 shader_glsl_invalidate_contexts_program(device, entry);
8320 delete_glsl_program_entry(priv, gl_info, entry);
8323 break;
8326 case WINED3D_SHADER_TYPE_VERTEX:
8328 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
8330 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8332 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
8333 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8334 checkGLcall("glDeleteShader");
8336 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
8338 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8339 struct glsl_shader_prog_link, vs.shader_entry)
8341 shader_glsl_invalidate_contexts_program(device, entry);
8342 delete_glsl_program_entry(priv, gl_info, entry);
8345 break;
8348 case WINED3D_SHADER_TYPE_GEOMETRY:
8350 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
8352 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8354 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
8355 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8356 checkGLcall("glDeleteShader");
8358 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
8360 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8361 struct glsl_shader_prog_link, gs.shader_entry)
8363 shader_glsl_invalidate_contexts_program(device, entry);
8364 delete_glsl_program_entry(priv, gl_info, entry);
8367 break;
8370 default:
8371 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8372 break;
8376 HeapFree(GetProcessHeap(), 0, shader->backend_data);
8377 shader->backend_data = NULL;
8379 context_release(context);
8382 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
8384 const struct glsl_program_key *k = key;
8385 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
8386 const struct glsl_shader_prog_link, program_lookup_entry);
8388 if (k->vs_id > prog->vs.id) return 1;
8389 else if (k->vs_id < prog->vs.id) return -1;
8391 if (k->gs_id > prog->gs.id) return 1;
8392 else if (k->gs_id < prog->gs.id) return -1;
8394 if (k->ps_id > prog->ps.id) return 1;
8395 else if (k->ps_id < prog->ps.id) return -1;
8397 return 0;
8400 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
8402 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
8403 + constant_count * sizeof(*heap->contained)
8404 + constant_count * sizeof(*heap->positions);
8405 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
8407 if (!mem)
8409 ERR("Failed to allocate memory\n");
8410 return FALSE;
8413 heap->entries = mem;
8414 heap->entries[1].version = 0;
8415 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
8416 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
8417 heap->positions = (unsigned int *)(heap->contained + constant_count);
8418 heap->size = 1;
8420 return TRUE;
8423 static void constant_heap_free(struct constant_heap *heap)
8425 HeapFree(GetProcessHeap(), 0, heap->entries);
8428 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
8430 wined3d_rb_alloc,
8431 wined3d_rb_realloc,
8432 wined3d_rb_free,
8433 glsl_program_key_compare,
8436 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
8437 const struct fragment_pipeline *fragment_pipe)
8439 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8440 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
8441 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
8442 struct fragment_caps fragment_caps;
8443 void *vertex_priv, *fragment_priv;
8445 string_buffer_list_init(&priv->string_buffers);
8447 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
8449 ERR("Failed to initialize vertex pipe.\n");
8450 HeapFree(GetProcessHeap(), 0, priv);
8451 return E_FAIL;
8454 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
8456 ERR("Failed to initialize fragment pipe.\n");
8457 vertex_pipe->vp_free(device);
8458 HeapFree(GetProcessHeap(), 0, priv);
8459 return E_FAIL;
8462 if (!string_buffer_init(&priv->shader_buffer))
8464 ERR("Failed to initialize shader buffer.\n");
8465 goto fail;
8468 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
8470 ERR("Failed to allocate memory.\n");
8471 goto fail;
8474 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
8476 ERR("Failed to initialize vertex shader constant heap\n");
8477 goto fail;
8480 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
8482 ERR("Failed to initialize pixel shader constant heap\n");
8483 goto fail;
8486 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
8488 ERR("Failed to initialize rbtree.\n");
8489 goto fail;
8492 priv->next_constant_version = 1;
8493 priv->vertex_pipe = vertex_pipe;
8494 priv->fragment_pipe = fragment_pipe;
8495 fragment_pipe->get_caps(gl_info, &fragment_caps);
8496 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
8497 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
8499 device->vertex_priv = vertex_priv;
8500 device->fragment_priv = fragment_priv;
8501 device->shader_priv = priv;
8503 return WINED3D_OK;
8505 fail:
8506 constant_heap_free(&priv->pconst_heap);
8507 constant_heap_free(&priv->vconst_heap);
8508 HeapFree(GetProcessHeap(), 0, priv->stack);
8509 string_buffer_free(&priv->shader_buffer);
8510 fragment_pipe->free_private(device);
8511 vertex_pipe->vp_free(device);
8512 HeapFree(GetProcessHeap(), 0, priv);
8513 return E_OUTOFMEMORY;
8516 /* Context activation is done by the caller. */
8517 static void shader_glsl_free(struct wined3d_device *device)
8519 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8520 struct shader_glsl_priv *priv = device->shader_priv;
8521 int i;
8523 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
8525 if (priv->depth_blt_program_full[i])
8527 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
8529 if (priv->depth_blt_program_masked[i])
8531 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
8535 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
8536 constant_heap_free(&priv->pconst_heap);
8537 constant_heap_free(&priv->vconst_heap);
8538 HeapFree(GetProcessHeap(), 0, priv->stack);
8539 string_buffer_list_cleanup(&priv->string_buffers);
8540 string_buffer_free(&priv->shader_buffer);
8541 priv->fragment_pipe->free_private(device);
8542 priv->vertex_pipe->vp_free(device);
8544 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8545 device->shader_priv = NULL;
8548 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8550 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8551 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8554 static void shader_glsl_free_context_data(struct wined3d_context *context)
8556 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8559 static void shader_glsl_init_context_state(struct wined3d_context *context)
8561 const struct wined3d_gl_info *gl_info = context->gl_info;
8563 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8564 checkGLcall("GL_PROGRAM_POINT_SIZE");
8567 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8569 UINT shader_model;
8571 /* FIXME: Check for the specific extensions required for SM5 support
8572 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8573 * soon as we introduce them, adjusting the GL / GLSL version checks
8574 * accordingly. */
8575 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
8576 && gl_info->supported[ARB_DERIVATIVE_CONTROL])
8577 shader_model = 5;
8578 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8579 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8580 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8581 shader_model = 4;
8582 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
8583 * texldd and texldl instructions. */
8584 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
8585 shader_model = 3;
8586 else
8587 shader_model = 2;
8588 TRACE("Shader model %u.\n", shader_model);
8590 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8591 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
8592 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
8593 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8594 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8595 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
8597 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
8598 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
8599 caps->varying_count = gl_info->limits.glsl_varyings;
8601 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8602 * Direct3D minimum requirement.
8604 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8605 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8607 * The problem is that the refrast clamps temporary results in the shader to
8608 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8609 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8610 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8611 * offer a way to query this.
8613 if (shader_model >= 4)
8614 caps->ps_1x_max_value = FLT_MAX;
8615 else
8616 caps->ps_1x_max_value = 1024.0f;
8618 /* Ideally we'd only set caps like sRGB writes here if supported by both
8619 * the shader backend and the fragment pipe, but we can get called before
8620 * shader_glsl_alloc(). */
8621 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8622 | WINED3D_SHADER_CAP_SRGB_WRITE;
8625 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8627 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8629 TRACE("Checking support for fixup:\n");
8630 dump_color_fixup_desc(fixup);
8633 /* We support everything except YUV conversions. */
8634 if (!is_complex_fixup(fixup))
8636 TRACE("[OK]\n");
8637 return TRUE;
8640 TRACE("[FAILED]\n");
8641 return FALSE;
8644 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8646 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8647 /* WINED3DSIH_ADD */ shader_glsl_binop,
8648 /* WINED3DSIH_AND */ shader_glsl_binop,
8649 /* WINED3DSIH_BEM */ shader_glsl_bem,
8650 /* WINED3DSIH_BFI */ NULL,
8651 /* WINED3DSIH_BFREV */ NULL,
8652 /* WINED3DSIH_BREAK */ shader_glsl_break,
8653 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8654 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8655 /* WINED3DSIH_BUFINFO */ NULL,
8656 /* WINED3DSIH_CALL */ shader_glsl_call,
8657 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8658 /* WINED3DSIH_CASE */ shader_glsl_case,
8659 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8660 /* WINED3DSIH_CND */ shader_glsl_cnd,
8661 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
8662 /* WINED3DSIH_CRS */ shader_glsl_cross,
8663 /* WINED3DSIH_CUT */ shader_glsl_cut,
8664 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
8665 /* WINED3DSIH_DCL */ shader_glsl_nop,
8666 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8667 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8668 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
8669 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
8670 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
8671 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
8672 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8673 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
8674 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8675 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8676 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8677 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8678 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8679 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8680 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8681 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
8682 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8683 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8684 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
8685 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8686 /* WINED3DSIH_DCL_STREAM */ NULL,
8687 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8688 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
8689 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
8690 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
8691 /* WINED3DSIH_DCL_TGSM_RAW */ NULL,
8692 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL,
8693 /* WINED3DSIH_DCL_THREAD_GROUP */ NULL,
8694 /* WINED3DSIH_DCL_UAV_TYPED */ NULL,
8695 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8696 /* WINED3DSIH_DEF */ shader_glsl_nop,
8697 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
8698 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8699 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8700 /* WINED3DSIH_DIV */ shader_glsl_binop,
8701 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8702 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8703 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8704 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8705 /* WINED3DSIH_DST */ shader_glsl_dst,
8706 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8707 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
8708 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
8709 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8710 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
8711 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
8712 /* WINED3DSIH_ELSE */ shader_glsl_else,
8713 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8714 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
8715 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8716 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8717 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8718 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
8719 /* WINED3DSIH_EQ */ shader_glsl_relop,
8720 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8721 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8722 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8723 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8724 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8725 /* WINED3DSIH_GATHER4 */ NULL,
8726 /* WINED3DSIH_GATHER4_C */ NULL,
8727 /* WINED3DSIH_GE */ shader_glsl_relop,
8728 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
8729 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
8730 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
8731 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
8732 /* WINED3DSIH_IADD */ shader_glsl_binop,
8733 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8734 /* WINED3DSIH_IF */ shader_glsl_if,
8735 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8736 /* WINED3DSIH_IGE */ shader_glsl_relop,
8737 /* WINED3DSIH_ILT */ shader_glsl_relop,
8738 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8739 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8740 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8741 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL,
8742 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL,
8743 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8744 /* WINED3DSIH_INE */ shader_glsl_relop,
8745 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8746 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8747 /* WINED3DSIH_ISHR */ shader_glsl_binop,
8748 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8749 /* WINED3DSIH_LABEL */ shader_glsl_label,
8750 /* WINED3DSIH_LD */ shader_glsl_ld,
8751 /* WINED3DSIH_LD2DMS */ NULL,
8752 /* WINED3DSIH_LD_RAW */ NULL,
8753 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8754 /* WINED3DSIH_LD_UAV_TYPED */ NULL,
8755 /* WINED3DSIH_LIT */ shader_glsl_lit,
8756 /* WINED3DSIH_LOD */ NULL,
8757 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8758 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8759 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8760 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8761 /* WINED3DSIH_LT */ shader_glsl_relop,
8762 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8763 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8764 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8765 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8766 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8767 /* WINED3DSIH_MAD */ shader_glsl_mad,
8768 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8769 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8770 /* WINED3DSIH_MOV */ shader_glsl_mov,
8771 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8772 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8773 /* WINED3DSIH_MUL */ shader_glsl_binop,
8774 /* WINED3DSIH_NE */ shader_glsl_relop,
8775 /* WINED3DSIH_NOP */ shader_glsl_nop,
8776 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8777 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8778 /* WINED3DSIH_OR */ shader_glsl_binop,
8779 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8780 /* WINED3DSIH_POW */ shader_glsl_pow,
8781 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8782 /* WINED3DSIH_REP */ shader_glsl_rep,
8783 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8784 /* WINED3DSIH_RET */ shader_glsl_ret,
8785 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
8786 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8787 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8788 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8789 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8790 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8791 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8792 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
8793 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
8794 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8795 /* WINED3DSIH_SAMPLE_INFO */ NULL,
8796 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8797 /* WINED3DSIH_SAMPLE_POS */ NULL,
8798 /* WINED3DSIH_SETP */ NULL,
8799 /* WINED3DSIH_SGE */ shader_glsl_compare,
8800 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8801 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8802 /* WINED3DSIH_SLT */ shader_glsl_compare,
8803 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8804 /* WINED3DSIH_STORE_RAW */ NULL,
8805 /* WINED3DSIH_STORE_STRUCTURED */ NULL,
8806 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
8807 /* WINED3DSIH_SUB */ shader_glsl_binop,
8808 /* WINED3DSIH_SWAPC */ NULL,
8809 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
8810 /* WINED3DSIH_TEX */ shader_glsl_tex,
8811 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8812 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8813 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8814 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8815 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8816 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8817 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8818 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8819 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8820 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8821 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8822 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8823 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8824 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8825 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8826 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8827 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8828 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8829 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8830 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8831 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8832 /* WINED3DSIH_UBFE */ NULL,
8833 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8834 /* WINED3DSIH_UGE */ shader_glsl_relop,
8835 /* WINED3DSIH_ULT */ shader_glsl_relop,
8836 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
8837 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
8838 /* WINED3DSIH_USHR */ shader_glsl_binop,
8839 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8840 /* WINED3DSIH_XOR */ shader_glsl_binop,
8843 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8844 SHADER_HANDLER hw_fct;
8846 /* Select handler */
8847 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8849 /* Unhandled opcode */
8850 if (!hw_fct)
8852 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8853 return;
8855 hw_fct(ins);
8857 shader_glsl_add_instruction_modifiers(ins);
8860 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8862 struct shader_glsl_priv *priv = shader_priv;
8864 return priv->ffp_proj_control;
8867 const struct wined3d_shader_backend_ops glsl_shader_backend =
8869 shader_glsl_handle_instruction,
8870 shader_glsl_select,
8871 shader_glsl_disable,
8872 shader_glsl_select_depth_blt,
8873 shader_glsl_deselect_depth_blt,
8874 shader_glsl_update_float_vertex_constants,
8875 shader_glsl_update_float_pixel_constants,
8876 shader_glsl_load_constants,
8877 shader_glsl_destroy,
8878 shader_glsl_alloc,
8879 shader_glsl_free,
8880 shader_glsl_allocate_context_data,
8881 shader_glsl_free_context_data,
8882 shader_glsl_init_context_state,
8883 shader_glsl_get_caps,
8884 shader_glsl_color_fixup_supported,
8885 shader_glsl_has_ffp_proj_control,
8888 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8890 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8892 caps->xyzrhw = TRUE;
8893 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8894 caps->ffp_generic_attributes = TRUE;
8895 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8896 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8897 caps->max_vertex_blend_matrix_index = 0;
8898 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8899 | WINED3DVTXPCAPS_MATERIALSOURCE7
8900 | WINED3DVTXPCAPS_VERTEXFOG
8901 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8902 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8903 | WINED3DVTXPCAPS_LOCALVIEWER
8904 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8905 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8906 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8907 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8910 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8912 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8913 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8914 return 0;
8917 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8919 struct shader_glsl_priv *priv;
8921 if (shader_backend == &glsl_shader_backend)
8923 priv = shader_priv;
8925 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8927 ERR("Failed to initialize rbtree.\n");
8928 return NULL;
8931 return priv;
8934 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8936 return NULL;
8939 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8941 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8942 struct glsl_ffp_vertex_shader, desc.entry);
8943 struct glsl_shader_prog_link *program, *program2;
8944 struct glsl_ffp_destroy_ctx *ctx = context;
8946 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8947 struct glsl_shader_prog_link, vs.shader_entry)
8949 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8951 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8952 HeapFree(GetProcessHeap(), 0, shader);
8955 /* Context activation is done by the caller. */
8956 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8958 struct shader_glsl_priv *priv = device->vertex_priv;
8959 struct glsl_ffp_destroy_ctx ctx;
8961 ctx.priv = priv;
8962 ctx.gl_info = &device->adapter->gl_info;
8963 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8966 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8967 const struct wined3d_state *state, DWORD state_id) {}
8969 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8970 const struct wined3d_state *state, DWORD state_id)
8972 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8975 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8976 const struct wined3d_state *state, DWORD state_id)
8978 const struct wined3d_gl_info *gl_info = context->gl_info;
8979 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8980 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8981 BOOL transformed = context->stream_info.position_transformed;
8982 BOOL wasrhw = context->last_was_rhw;
8983 unsigned int i;
8985 context->last_was_rhw = transformed;
8987 /* If the vertex declaration contains a transformed position attribute,
8988 * the draw uses the fixed function vertex pipeline regardless of any
8989 * vertex shader set by the application. */
8990 if (transformed != wasrhw
8991 || context->stream_info.swizzle_map != context->last_swizzle_map)
8992 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8994 context->last_swizzle_map = context->stream_info.swizzle_map;
8996 if (!use_vs(state))
8998 if (context->last_was_vshader)
9000 if (legacy_context)
9001 for (i = 0; i < gl_info->limits.clipplanes; ++i)
9002 clipplane(context, state, STATE_CLIPPLANE(i));
9003 else
9004 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9007 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9009 /* Because of settings->texcoords, we have to regenerate the vertex
9010 * shader on a vdecl change if there aren't enough varyings to just
9011 * always output all the texture coordinates. */
9012 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
9013 || normal != context->last_was_normal)
9014 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9016 if (use_ps(state)
9017 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
9018 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
9019 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9021 else
9023 if (!context->last_was_vshader)
9025 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
9026 if (legacy_context)
9027 for (i = 0; i < gl_info->limits.clipplanes; ++i)
9028 clipplane(context, state, STATE_CLIPPLANE(i));
9029 else
9030 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9034 context->last_was_vshader = use_vs(state);
9035 context->last_was_normal = normal;
9038 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
9039 const struct wined3d_state *state, DWORD state_id)
9041 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9042 /* Different vertex shaders potentially require a different vertex attributes setup. */
9043 if (!isStateDirty(context, STATE_VDECL))
9044 context_apply_state(context, state, STATE_VDECL);
9047 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
9048 const struct wined3d_state *state, DWORD state_id)
9050 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9051 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9052 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9055 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
9056 const struct wined3d_state *state, DWORD state_id)
9058 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
9059 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
9060 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9061 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9062 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9065 static void glsl_vertex_pipe_world(struct wined3d_context *context,
9066 const struct wined3d_state *state, DWORD state_id)
9068 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
9071 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
9072 const struct wined3d_state *state, DWORD state_id)
9074 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9077 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
9079 const struct wined3d_gl_info *gl_info = context->gl_info;
9080 unsigned int k;
9082 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
9083 | WINED3D_SHADER_CONST_FFP_LIGHTS
9084 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9086 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9088 for (k = 0; k < gl_info->limits.clipplanes; ++k)
9090 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
9091 clipplane(context, state, STATE_CLIPPLANE(k));
9094 else
9096 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9100 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
9101 const struct wined3d_state *state, DWORD state_id)
9103 /* Table fog behavior depends on the projection matrix. */
9104 if (state->render_states[WINED3D_RS_FOGENABLE]
9105 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
9106 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9107 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
9110 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
9111 const struct wined3d_state *state, DWORD state_id)
9113 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
9114 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
9115 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
9116 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
9117 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9118 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9121 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
9122 const struct wined3d_state *state, DWORD state_id)
9124 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9127 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
9128 const struct wined3d_state *state, DWORD state_id)
9130 DWORD sampler = state_id - STATE_SAMPLER(0);
9131 const struct wined3d_texture *texture = state->textures[sampler];
9132 BOOL np2;
9134 if (!texture)
9135 return;
9137 if (sampler >= MAX_TEXTURES)
9138 return;
9140 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
9141 || context->lastWasPow2Texture & (1u << sampler))
9143 if (np2)
9144 context->lastWasPow2Texture |= 1u << sampler;
9145 else
9146 context->lastWasPow2Texture &= ~(1u << sampler);
9148 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9152 static void glsl_vertex_pipe_material(struct wined3d_context *context,
9153 const struct wined3d_state *state, DWORD state_id)
9155 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9158 static void glsl_vertex_pipe_light(struct wined3d_context *context,
9159 const struct wined3d_state *state, DWORD state_id)
9161 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9164 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
9165 const struct wined3d_state *state, DWORD state_id)
9167 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9170 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
9171 const struct wined3d_state *state, DWORD state_id)
9173 if (!use_vs(state))
9174 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9177 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
9178 const struct wined3d_state *state, DWORD state_id)
9180 static unsigned int once;
9182 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
9183 FIXME("Non-point sprite points not supported in core profile.\n");
9186 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
9187 const struct wined3d_state *state, DWORD state_id)
9189 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
9192 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
9193 const struct wined3d_state *state, DWORD state_id)
9195 const struct wined3d_gl_info *gl_info = context->gl_info;
9196 UINT index = state_id - STATE_CLIPPLANE(0);
9198 if (index >= gl_info->limits.clipplanes)
9199 return;
9201 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9204 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
9206 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9207 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
9208 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
9209 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
9210 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
9211 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
9212 /* Clip planes */
9213 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9214 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9215 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9216 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9217 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9218 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9219 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9220 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9221 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9222 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9223 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9224 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9225 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9226 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9227 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9228 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9229 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9230 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9231 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9232 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9233 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9234 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9235 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9236 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9237 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9238 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9239 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9240 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9241 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9242 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9243 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9244 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9245 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9246 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9247 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9248 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9249 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9250 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9251 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9252 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9253 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9254 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9255 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9256 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9257 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9258 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9259 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9260 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9261 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9262 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9263 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9264 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9265 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9266 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9267 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9268 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9269 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9270 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9271 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9272 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9273 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9274 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9275 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9276 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9277 /* Lights */
9278 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9279 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9280 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9281 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9282 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9283 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9284 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9285 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9286 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9287 /* Viewport */
9288 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
9289 /* Transform states */
9290 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
9291 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
9292 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9293 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9294 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9295 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9296 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9297 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9298 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9299 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9300 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
9301 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9302 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9303 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9304 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9305 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9306 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9307 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9308 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9309 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9310 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9311 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9312 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9313 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9314 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9315 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9316 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9317 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9318 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9319 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9320 /* Fog */
9321 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9322 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9323 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9324 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9325 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
9326 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
9327 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9328 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9329 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9330 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9331 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9332 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9333 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9334 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9335 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9336 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9337 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9338 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
9339 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
9340 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
9341 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
9342 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
9343 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9344 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9345 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9346 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9347 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9348 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9349 /* NP2 texture matrix fixups. They are not needed if
9350 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
9351 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
9352 * matrix. */
9353 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9354 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9355 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9356 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9357 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9358 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9359 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9360 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9361 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9362 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9363 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9364 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9365 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9366 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9367 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9368 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9369 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9370 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9371 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9372 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9373 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9374 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9375 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9376 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9377 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9378 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
9379 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
9380 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
9383 /* TODO:
9384 * - Implement vertex tweening. */
9385 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
9387 glsl_vertex_pipe_vp_enable,
9388 glsl_vertex_pipe_vp_get_caps,
9389 glsl_vertex_pipe_vp_get_emul_mask,
9390 glsl_vertex_pipe_vp_alloc,
9391 glsl_vertex_pipe_vp_free,
9392 glsl_vertex_pipe_vp_states,
9395 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
9397 /* Nothing to do. */
9400 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
9402 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
9403 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
9404 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
9405 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
9406 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
9407 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
9408 | WINED3DTEXOPCAPS_SELECTARG1
9409 | WINED3DTEXOPCAPS_SELECTARG2
9410 | WINED3DTEXOPCAPS_MODULATE4X
9411 | WINED3DTEXOPCAPS_MODULATE2X
9412 | WINED3DTEXOPCAPS_MODULATE
9413 | WINED3DTEXOPCAPS_ADDSIGNED2X
9414 | WINED3DTEXOPCAPS_ADDSIGNED
9415 | WINED3DTEXOPCAPS_ADD
9416 | WINED3DTEXOPCAPS_SUBTRACT
9417 | WINED3DTEXOPCAPS_ADDSMOOTH
9418 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
9419 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
9420 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
9421 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
9422 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
9423 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
9424 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
9425 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
9426 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
9427 | WINED3DTEXOPCAPS_DOTPRODUCT3
9428 | WINED3DTEXOPCAPS_MULTIPLYADD
9429 | WINED3DTEXOPCAPS_LERP
9430 | WINED3DTEXOPCAPS_BUMPENVMAP
9431 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
9432 caps->MaxTextureBlendStages = 8;
9433 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
9436 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
9438 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9439 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9440 return 0;
9443 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9445 struct shader_glsl_priv *priv;
9447 if (shader_backend == &glsl_shader_backend)
9449 priv = shader_priv;
9451 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
9453 ERR("Failed to initialize rbtree.\n");
9454 return NULL;
9457 return priv;
9460 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
9462 return NULL;
9465 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
9467 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9468 struct glsl_ffp_fragment_shader, entry.entry);
9469 struct glsl_shader_prog_link *program, *program2;
9470 struct glsl_ffp_destroy_ctx *ctx = context;
9472 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9473 struct glsl_shader_prog_link, ps.shader_entry)
9475 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9477 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9478 HeapFree(GetProcessHeap(), 0, shader);
9481 /* Context activation is done by the caller. */
9482 static void glsl_fragment_pipe_free(struct wined3d_device *device)
9484 struct shader_glsl_priv *priv = device->fragment_priv;
9485 struct glsl_ffp_destroy_ctx ctx;
9487 ctx.priv = priv;
9488 ctx.gl_info = &device->adapter->gl_info;
9489 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
9492 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
9493 const struct wined3d_state *state, DWORD state_id)
9495 context->last_was_pshader = use_ps(state);
9497 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9500 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
9501 const struct wined3d_state *state, DWORD state_id)
9503 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9506 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
9507 const struct wined3d_state *state, DWORD state_id)
9509 BOOL use_vshader = use_vs(state);
9510 enum fogsource new_source;
9511 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
9512 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
9514 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9516 if (!state->render_states[WINED3D_RS_FOGENABLE])
9517 return;
9519 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
9521 if (use_vshader)
9522 new_source = FOGSOURCE_VS;
9523 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
9524 new_source = FOGSOURCE_COORD;
9525 else
9526 new_source = FOGSOURCE_FFP;
9528 else
9530 new_source = FOGSOURCE_FFP;
9533 if (new_source != context->fog_source || fogstart == fogend)
9535 context->fog_source = new_source;
9536 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9540 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
9541 const struct wined3d_state *state, DWORD state_id)
9543 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9544 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9545 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9547 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
9548 glsl_fragment_pipe_fog(context, state, state_id);
9551 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
9552 const struct wined3d_state *state, DWORD state_id)
9554 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9555 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9556 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9559 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
9560 const struct wined3d_state *state, DWORD state_id)
9562 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9565 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
9566 const struct wined3d_state *state, DWORD state_id)
9568 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
9571 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
9572 const struct wined3d_state *state, DWORD state_id)
9574 const struct wined3d_gl_info *gl_info = context->gl_info;
9575 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
9576 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
9578 if (func)
9580 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
9581 checkGLcall("glAlphaFunc");
9585 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
9586 const struct wined3d_state *state, DWORD state_id)
9588 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9591 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
9592 const struct wined3d_state *state, DWORD state_id)
9594 const struct wined3d_gl_info *gl_info = context->gl_info;
9596 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
9598 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
9599 checkGLcall("glEnable(GL_ALPHA_TEST)");
9601 else
9603 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
9604 checkGLcall("glDisable(GL_ALPHA_TEST)");
9608 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
9609 const struct wined3d_state *state, DWORD state_id)
9611 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
9614 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
9615 const struct wined3d_state *state, DWORD state_id)
9617 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
9620 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
9621 const struct wined3d_state *state, DWORD state_id)
9623 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
9626 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
9628 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9629 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
9630 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9631 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9632 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9633 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9634 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9635 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9636 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9637 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9638 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9639 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9640 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9641 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9642 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9643 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9644 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9645 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9646 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9647 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9648 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9649 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9650 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9651 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9652 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9653 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9654 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9655 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9656 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9657 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9658 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9659 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9660 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9661 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9662 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9663 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9664 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9665 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9666 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9667 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9668 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9669 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9670 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9671 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9672 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9673 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9674 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9675 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9676 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9677 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9678 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9679 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9680 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9681 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9682 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9683 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9684 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9685 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9686 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9687 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9688 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9689 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9690 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9691 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9692 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9693 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9694 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9695 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9696 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9697 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9698 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9699 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9700 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9701 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9702 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9703 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9704 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
9705 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9706 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
9707 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
9708 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
9709 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
9710 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9711 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9712 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9713 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9714 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9715 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9716 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9717 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9718 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9719 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9720 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9721 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9722 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
9723 {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 },
9724 {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 },
9725 {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 },
9726 {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 },
9727 {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 },
9728 {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 },
9729 {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 },
9730 {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 },
9731 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9732 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9733 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9734 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9735 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9736 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9737 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9738 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9739 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9740 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9741 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9742 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9743 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9746 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9748 return TRUE;
9751 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9755 const struct fragment_pipeline glsl_fragment_pipe =
9757 glsl_fragment_pipe_enable,
9758 glsl_fragment_pipe_get_caps,
9759 glsl_fragment_pipe_get_emul_mask,
9760 glsl_fragment_pipe_alloc,
9761 glsl_fragment_pipe_free,
9762 glsl_fragment_pipe_alloc_context_data,
9763 glsl_fragment_pipe_free_context_data,
9764 shader_glsl_color_fixup_supported,
9765 glsl_fragment_pipe_state_template,