msvcrt: Improve acosf compatibility with native ucrtbase.
[wine.git] / dlls / wined3d / glsl_shader.c
blob45eebcc2c20924571df2f2f6f66d2ef2bbff176c
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>
38 #include "wined3d_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
41 WINE_DECLARE_DEBUG_CHANNEL(d3d);
42 WINE_DECLARE_DEBUG_CHANNEL(winediag);
44 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
45 #define WINED3D_GLSL_SAMPLE_LOD 0x02
46 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
47 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
48 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
50 static const struct
52 unsigned int coord_size;
53 unsigned int resinfo_size;
54 const char *type_part;
56 resource_type_info[] =
58 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
59 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
60 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
61 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
62 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
63 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
64 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
66 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
67 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
68 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
71 static const struct
73 enum wined3d_data_type data_type;
74 const char *glsl_scalar_type;
75 const char *glsl_vector_type;
77 component_type_info[] =
79 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_UNKNOWN */
80 {WINED3D_DATA_UINT, "uint", "uvec"}, /* WINED3D_TYPE_UINT */
81 {WINED3D_DATA_INT, "int", "ivec"}, /* WINED3D_TYPE_INT */
82 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_FLOAT */
85 struct glsl_dst_param
87 char reg_name[150];
88 char mask_str[6];
91 struct glsl_src_param
93 char param_str[200];
96 struct glsl_sample_function
98 struct wined3d_string_buffer *name;
99 unsigned int coord_mask;
100 unsigned int deriv_mask;
101 enum wined3d_data_type data_type;
102 BOOL output_single_component;
103 unsigned int offset_size;
106 enum heap_node_op
108 HEAP_NODE_TRAVERSE_LEFT,
109 HEAP_NODE_TRAVERSE_RIGHT,
110 HEAP_NODE_POP,
113 struct constant_entry
115 unsigned int idx;
116 unsigned int version;
119 struct constant_heap
121 struct constant_entry *entries;
122 BOOL *contained;
123 unsigned int *positions;
124 unsigned int size;
127 /* GLSL shader private data */
128 struct shader_glsl_priv
130 struct wined3d_string_buffer shader_buffer;
131 struct wined3d_string_buffer_list string_buffers;
132 struct wine_rb_tree program_lookup;
133 struct constant_heap vconst_heap;
134 struct constant_heap pconst_heap;
135 unsigned char *stack;
136 UINT next_constant_version;
138 const struct wined3d_vertex_pipe_ops *vertex_pipe;
139 const struct wined3d_fragment_pipe_ops *fragment_pipe;
140 struct wine_rb_tree ffp_vertex_shaders;
141 struct wine_rb_tree ffp_fragment_shaders;
142 BOOL ffp_proj_control;
143 BOOL legacy_lighting;
146 struct glsl_vs_program
148 struct list shader_entry;
149 GLuint id;
150 GLenum vertex_color_clamp;
151 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
152 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
153 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
154 GLint pos_fixup_location;
155 GLint base_vertex_id_location;
157 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
158 GLint projection_matrix_location;
159 GLint normal_matrix_location;
160 GLint texture_matrix_location[WINED3D_MAX_TEXTURES];
161 GLint material_ambient_location;
162 GLint material_diffuse_location;
163 GLint material_specular_location;
164 GLint material_emissive_location;
165 GLint material_shininess_location;
166 GLint light_ambient_location;
167 struct
169 GLint diffuse;
170 GLint specular;
171 GLint ambient;
172 GLint position;
173 GLint direction;
174 GLint range;
175 GLint falloff;
176 GLint c_att;
177 GLint l_att;
178 GLint q_att;
179 GLint cos_htheta;
180 GLint cos_hphi;
181 } light_location[WINED3D_MAX_ACTIVE_LIGHTS];
182 GLint pointsize_location;
183 GLint pointsize_min_location;
184 GLint pointsize_max_location;
185 GLint pointsize_c_att_location;
186 GLint pointsize_l_att_location;
187 GLint pointsize_q_att_location;
188 GLint clip_planes_location;
191 struct glsl_hs_program
193 struct list shader_entry;
194 GLuint id;
197 struct glsl_ds_program
199 struct list shader_entry;
200 GLuint id;
202 GLint pos_fixup_location;
205 struct glsl_gs_program
207 struct list shader_entry;
208 GLuint id;
210 GLint pos_fixup_location;
213 struct glsl_ps_program
215 struct list shader_entry;
216 GLuint id;
217 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
218 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
219 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
220 GLint bumpenv_mat_location[WINED3D_MAX_TEXTURES];
221 GLint bumpenv_lum_scale_location[WINED3D_MAX_TEXTURES];
222 GLint bumpenv_lum_offset_location[WINED3D_MAX_TEXTURES];
223 GLint tss_constant_location[WINED3D_MAX_TEXTURES];
224 GLint tex_factor_location;
225 GLint specular_enable_location;
226 GLint fog_color_location;
227 GLint fog_density_location;
228 GLint fog_end_location;
229 GLint fog_scale_location;
230 GLint alpha_test_ref_location;
231 GLint ycorrection_location;
232 GLint np2_fixup_location;
233 GLint color_key_location;
234 const struct ps_np2fixup_info *np2_fixup_info;
237 struct glsl_cs_program
239 struct list shader_entry;
240 GLuint id;
243 /* Struct to maintain data about a linked GLSL program */
244 struct glsl_shader_prog_link
246 struct wine_rb_entry program_lookup_entry;
247 struct glsl_vs_program vs;
248 struct glsl_hs_program hs;
249 struct glsl_ds_program ds;
250 struct glsl_gs_program gs;
251 struct glsl_ps_program ps;
252 struct glsl_cs_program cs;
253 GLuint id;
254 DWORD constant_update_mask;
255 unsigned int constant_version;
256 DWORD shader_controlled_clip_distances : 1;
257 DWORD clip_distance_mask : 8; /* WINED3D_MAX_CLIP_DISTANCES, 8 */
258 DWORD padding : 23;
261 struct glsl_program_key
263 GLuint vs_id;
264 GLuint hs_id;
265 GLuint ds_id;
266 GLuint gs_id;
267 GLuint ps_id;
268 GLuint cs_id;
271 struct shader_glsl_ctx_priv
273 const struct wined3d_gl_info *gl_info;
274 const struct vs_compile_args *cur_vs_args;
275 const struct ds_compile_args *cur_ds_args;
276 const struct ps_compile_args *cur_ps_args;
277 struct ps_np2fixup_info *cur_np2fixup_info;
278 struct wined3d_string_buffer_list *string_buffers;
281 struct glsl_context_data
283 struct glsl_shader_prog_link *glsl_program;
284 GLenum vertex_color_clamp;
285 BOOL rasterization_disabled;
288 struct glsl_ps_compiled_shader
290 struct ps_compile_args args;
291 struct ps_np2fixup_info np2fixup;
292 GLuint id;
295 struct glsl_vs_compiled_shader
297 struct vs_compile_args args;
298 GLuint id;
301 struct glsl_hs_compiled_shader
303 GLuint id;
306 struct glsl_ds_compiled_shader
308 struct ds_compile_args args;
309 GLuint id;
312 struct glsl_gs_compiled_shader
314 struct gs_compile_args args;
315 GLuint id;
318 struct glsl_cs_compiled_shader
320 GLuint id;
323 struct glsl_shader_private
325 union
327 struct glsl_vs_compiled_shader *vs;
328 struct glsl_hs_compiled_shader *hs;
329 struct glsl_ds_compiled_shader *ds;
330 struct glsl_gs_compiled_shader *gs;
331 struct glsl_ps_compiled_shader *ps;
332 struct glsl_cs_compiled_shader *cs;
333 } gl_shaders;
334 unsigned int num_gl_shaders, shader_array_size;
337 struct glsl_ffp_vertex_shader
339 struct wined3d_ffp_vs_desc desc;
340 GLuint id;
341 struct list linked_programs;
344 struct glsl_ffp_fragment_shader
346 struct ffp_frag_desc entry;
347 GLuint id;
348 struct list linked_programs;
351 struct glsl_ffp_destroy_ctx
353 struct shader_glsl_priv *priv;
354 const struct wined3d_context_gl *context_gl;
357 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
359 static const char *debug_gl_shader_type(GLenum type)
361 switch (type)
363 #define WINED3D_TO_STR(u) case u: return #u
364 WINED3D_TO_STR(GL_VERTEX_SHADER);
365 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
366 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
367 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
368 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
369 WINED3D_TO_STR(GL_COMPUTE_SHADER);
370 #undef WINED3D_TO_STR
371 default:
372 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
376 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
378 switch (type)
380 case WINED3D_SHADER_TYPE_VERTEX:
381 return "vs";
383 case WINED3D_SHADER_TYPE_HULL:
384 return "hs";
386 case WINED3D_SHADER_TYPE_DOMAIN:
387 return "ds";
389 case WINED3D_SHADER_TYPE_GEOMETRY:
390 return "gs";
392 case WINED3D_SHADER_TYPE_PIXEL:
393 return "ps";
395 case WINED3D_SHADER_TYPE_COMPUTE:
396 return "cs";
398 default:
399 FIXME("Unhandled shader type %#x.\n", type);
400 return "unknown";
404 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
406 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
407 return 440;
408 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
409 return 150;
410 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
411 return 130;
412 else
413 return 120;
416 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
417 const struct wined3d_gl_info *gl_info)
419 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
422 unsigned int shader_glsl_full_ffp_varyings(const struct wined3d_gl_info *gl_info)
424 /* On core profile we have to also count diffuse and specular colours and
425 * the fog coordinate. */
426 return gl_info->limits.glsl_varyings >= (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
427 ? WINED3D_MAX_TEXTURES * 4 : (WINED3D_MAX_TEXTURES + 2) * 4 + 1);
430 static void shader_glsl_append_imm_vec(struct wined3d_string_buffer *buffer,
431 const float *values, unsigned int size, const struct wined3d_gl_info *gl_info)
433 const int *int_values = (const int *)values;
434 unsigned int i;
435 char str[17];
437 if (!gl_info->supported[ARB_SHADER_BIT_ENCODING])
439 if (size > 1)
440 shader_addline(buffer, "vec%u(", size);
442 for (i = 0; i < size; ++i)
444 wined3d_ftoa(values[i], str);
445 shader_addline(buffer, i ? ", %s" : "%s", str);
448 if (size > 1)
449 shader_addline(buffer, ")");
451 return;
454 shader_addline(buffer, "intBitsToFloat(");
455 if (size > 1)
456 shader_addline(buffer, "ivec%u(", size);
458 for (i = 0; i < size; ++i)
460 wined3d_ftoa(values[i], str);
461 shader_addline(buffer, i ? ", %#x /* %s */" : "%#x /* %s */", int_values[i], str);
464 if (size > 1)
465 shader_addline(buffer, ")");
466 shader_addline(buffer, ")");
469 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
470 const int *values, unsigned int size)
472 int i;
474 if (!size || size > 4)
476 ERR("Invalid vector size %u.\n", size);
477 return;
480 if (size > 1)
481 shader_addline(buffer, "ivec%u(", size);
483 for (i = 0; i < size; ++i)
484 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
486 if (size > 1)
487 shader_addline(buffer, ")");
490 static const char *get_info_log_line(const char **ptr)
492 const char *p, *q;
494 p = *ptr;
495 if (!(q = strstr(p, "\n")))
497 if (!*p) return NULL;
498 *ptr += strlen(p);
499 return p;
501 *ptr = q + 1;
503 return p;
506 /* Context activation is done by the caller. */
507 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
509 int length = 0;
510 char *log;
512 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
513 return;
515 if (program)
516 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
517 else
518 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
520 /* A size of 1 is just a null-terminated string, so the log should be bigger than
521 * that if there are errors. */
522 if (length > 1)
524 const char *ptr, *line;
526 log = heap_alloc(length);
527 /* The info log is supposed to be zero-terminated, but at least some
528 * versions of fglrx don't terminate the string properly. The reported
529 * length does include the terminator, so explicitly set it to zero
530 * here. */
531 log[length - 1] = 0;
532 if (program)
533 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
534 else
535 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
537 ptr = log;
538 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
540 WARN("Info log received from GLSL shader #%u:\n", id);
541 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
543 else
545 FIXME("Info log received from GLSL shader #%u:\n", id);
546 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
548 heap_free(log);
552 /* Context activation is done by the caller. */
553 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
555 const char *ptr, *line;
557 TRACE("Compiling shader object %u.\n", shader);
559 if (TRACE_ON(d3d_shader))
561 ptr = src;
562 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
565 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
566 checkGLcall("glShaderSource");
567 GL_EXTCALL(glCompileShader(shader));
568 checkGLcall("glCompileShader");
569 print_glsl_info_log(gl_info, shader, FALSE);
572 /* Context activation is done by the caller. */
573 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
575 GLint i, shader_count, source_size = -1;
576 GLuint *shaders;
577 char *source = NULL;
579 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
580 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
582 ERR("Failed to allocate shader array memory.\n");
583 return;
586 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
587 for (i = 0; i < shader_count; ++i)
589 const char *ptr, *line;
590 GLint tmp;
592 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
594 if (source_size < tmp)
596 heap_free(source);
598 if (!(source = heap_alloc_zero(tmp)))
600 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
601 heap_free(shaders);
602 return;
604 source_size = tmp;
607 FIXME("Shader %u:\n", shaders[i]);
608 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
609 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
610 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
611 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
612 FIXME("\n");
614 ptr = source;
615 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
616 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
617 FIXME("\n");
620 heap_free(source);
621 heap_free(shaders);
624 /* Context activation is done by the caller. */
625 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
627 GLint tmp;
629 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
630 return;
632 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
633 if (!tmp)
635 FIXME("Program %u link status invalid.\n", program);
636 shader_glsl_dump_program_source(gl_info, program);
639 print_glsl_info_log(gl_info, program, TRUE);
642 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
644 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
645 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
646 * versions. */
647 return shader_glsl_get_version(gl_info) >= 140;
650 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
652 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
655 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
656 struct shader_glsl_priv *priv, GLuint program_id,
657 const struct wined3d_shader_reg_maps *reg_maps)
659 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
660 struct wined3d_string_buffer *name;
661 unsigned int i, base, count;
662 GLuint block_idx;
664 if (shader_glsl_use_layout_binding_qualifier(gl_info))
665 return;
667 name = string_buffer_get(&priv->string_buffers);
668 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
669 for (i = 0; i < count; ++i)
671 if (!reg_maps->cb_sizes[i])
672 continue;
674 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
675 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
676 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
678 checkGLcall("glUniformBlockBinding");
679 string_buffer_release(&priv->string_buffers, name);
682 /* Context activation is done by the caller. */
683 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
684 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
685 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
687 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
688 unsigned int i, mapped_unit;
689 GLint name_loc;
691 for (i = 0; i < count; ++i)
693 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
694 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
695 if (name_loc == -1)
696 continue;
698 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
699 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
701 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
702 continue;
705 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
706 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
708 checkGLcall("Load sampler bindings");
709 string_buffer_release(&priv->string_buffers, sampler_name);
712 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
713 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
715 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
716 const unsigned int *tex_unit_map;
717 unsigned int base, count;
719 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
720 if (sampler_idx >= count)
721 return WINED3D_UNMAPPED_STAGE;
722 if (!tex_unit_map)
723 return base + sampler_idx;
724 return tex_unit_map[base + sampler_idx];
727 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
728 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
729 unsigned int sampler_idx)
731 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
732 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
733 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
734 else
735 ERR("Unmapped sampler %u.\n", sampler_idx);
738 /* Context activation is done by the caller. */
739 static void shader_glsl_load_samplers(const struct wined3d_context *context,
740 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
742 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
743 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
744 const struct wined3d_shader_version *shader_version;
745 const unsigned int *tex_unit_map;
746 unsigned int base, count;
747 const char *prefix;
749 if (shader_glsl_use_layout_binding_qualifier(gl_info))
750 return;
752 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
753 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
754 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
755 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
758 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
759 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
761 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
763 if (icb)
765 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
766 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
767 GLint icb_location;
769 string_buffer_sprintf(icb_name, "%s_icb", prefix);
770 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
771 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
772 checkGLcall("Load immediate constant buffer");
774 string_buffer_release(&priv->string_buffers, icb_name);
778 /* Context activation is done by the caller. */
779 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
780 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
782 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
783 struct wined3d_string_buffer *name;
784 GLint location;
785 unsigned int i;
787 if (shader_glsl_use_layout_binding_qualifier(gl_info))
788 return;
790 name = string_buffer_get(&priv->string_buffers);
791 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
793 if (!reg_maps->uav_resource_info[i].type)
794 continue;
796 string_buffer_sprintf(name, "%s_image%u", prefix, i);
797 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
798 if (location == -1)
799 continue;
801 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
802 GL_EXTCALL(glUniform1i(location, i));
804 checkGLcall("Load image bindings");
805 string_buffer_release(&priv->string_buffers, name);
808 /* Context activation is done by the caller. */
809 static void shader_glsl_load_program_resources(const struct wined3d_context_gl *context_gl,
810 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
812 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
814 shader_glsl_init_uniform_block_bindings(context_gl->gl_info, priv, program_id, reg_maps);
815 shader_glsl_load_icb(context_gl->gl_info, priv, program_id, reg_maps);
816 /* Texture unit mapping is set up to be the same each time the shader
817 * program is used so we can hardcode the sampler uniform values. */
818 shader_glsl_load_samplers(&context_gl->c, priv, program_id, reg_maps);
821 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
822 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
824 if (varyings && *strings)
826 char *ptr = *strings;
828 varyings[*varying_count] = ptr;
830 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
831 ptr += buffer->content_size + 1;
833 *strings = ptr;
836 *strings_length += buffer->content_size + 1;
837 ++(*varying_count);
840 static void append_transform_feedback_skip_components(const char **varyings,
841 unsigned int *varying_count, char **strings, unsigned int *strings_length,
842 struct wined3d_string_buffer *buffer, unsigned int component_count)
844 unsigned int j;
846 for (j = 0; j < component_count / 4; ++j)
848 string_buffer_sprintf(buffer, "gl_SkipComponents4");
849 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
851 if (component_count % 4)
853 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
854 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
858 static BOOL shader_glsl_generate_transform_feedback_varyings(struct wined3d_string_buffer *buffer,
859 const char **varyings, unsigned int *varying_count, char *strings, unsigned int *strings_length,
860 GLenum buffer_mode, struct wined3d_shader *shader)
862 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
863 unsigned int buffer_idx, count, length, highest_output_slot, stride;
864 unsigned int i, register_idx, component_idx;
865 BOOL have_varyings_to_record = FALSE;
867 count = length = 0;
868 highest_output_slot = 0;
869 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
871 stride = 0;
873 for (i = 0; i < so_desc->element_count; ++i)
875 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
877 highest_output_slot = max(highest_output_slot, e->output_slot);
878 if (e->output_slot != buffer_idx)
879 continue;
881 if (e->stream_idx)
883 FIXME("Unhandled stream %u.\n", e->stream_idx);
884 continue;
887 stride += e->component_count;
889 if (!e->semantic_name)
891 append_transform_feedback_skip_components(varyings, &count,
892 &strings, &length, buffer, e->component_count);
893 continue;
896 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
897 continue;
899 if (component_idx || e->component_count != 4)
901 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
903 FIXME("Unsupported component range %u-%u.\n", component_idx, e->component_count);
904 append_transform_feedback_skip_components(varyings, &count,
905 &strings, &length, buffer, e->component_count);
906 continue;
909 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
910 register_idx, component_idx, component_idx + e->component_count - 1);
911 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
913 else
915 string_buffer_sprintf(buffer, "shader_in_out.reg%u", register_idx);
916 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
919 have_varyings_to_record = TRUE;
922 if (buffer_idx < so_desc->buffer_stride_count
923 && stride < so_desc->buffer_strides[buffer_idx] / 4)
925 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
926 append_transform_feedback_skip_components(varyings, &count,
927 &strings, &length, buffer, component_count);
930 if (highest_output_slot <= buffer_idx)
931 break;
933 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
935 string_buffer_sprintf(buffer, "gl_NextBuffer");
936 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
940 if (varying_count)
941 *varying_count = count;
942 if (strings_length)
943 *strings_length = length;
945 return have_varyings_to_record;
948 static void shader_glsl_init_transform_feedback(const struct wined3d_context_gl *context_gl,
949 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
951 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
952 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
953 struct wined3d_string_buffer *buffer;
954 unsigned int i, count, length;
955 const char **varyings;
956 char *strings;
957 GLenum mode;
959 if (!so_desc)
960 return;
962 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
964 mode = GL_INTERLEAVED_ATTRIBS;
966 else
968 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
970 for (i = 0; i < so_desc->element_count; ++i)
972 if (!so_desc->elements[i].semantic_name)
974 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
975 return;
977 ++element_count[so_desc->elements[i].output_slot];
980 if (element_count[0] == so_desc->element_count)
982 mode = GL_INTERLEAVED_ATTRIBS;
984 else
986 mode = GL_SEPARATE_ATTRIBS;
987 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
989 if (element_count[i] != 1)
990 break;
992 for (; i < ARRAY_SIZE(element_count); ++i)
994 if (element_count[i])
996 FIXME("Only single element per buffer is allowed in separate mode.\n");
997 return;
1003 buffer = string_buffer_get(&priv->string_buffers);
1005 if (!shader_glsl_generate_transform_feedback_varyings(buffer, NULL, &count, NULL, &length, mode, shader))
1007 FIXME("No varyings to record, disabling transform feedback.\n");
1008 shader->u.gs.so_desc = NULL;
1009 string_buffer_release(&priv->string_buffers, buffer);
1010 return;
1013 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
1015 ERR("Out of memory.\n");
1016 string_buffer_release(&priv->string_buffers, buffer);
1017 return;
1019 if (!(strings = heap_calloc(length, sizeof(*strings))))
1021 ERR("Out of memory.\n");
1022 heap_free(varyings);
1023 string_buffer_release(&priv->string_buffers, buffer);
1024 return;
1027 shader_glsl_generate_transform_feedback_varyings(buffer, varyings, NULL, strings, NULL, mode, shader);
1028 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
1029 checkGLcall("glTransformFeedbackVaryings");
1031 heap_free(varyings);
1032 heap_free(strings);
1033 string_buffer_release(&priv->string_buffers, buffer);
1036 /* Context activation is done by the caller. */
1037 static void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
1038 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
1040 unsigned int start = ~0U, end = 0;
1041 int stack_idx = 0;
1042 unsigned int heap_idx = 1;
1043 unsigned int idx;
1045 if (heap->entries[heap_idx].version <= version) return;
1047 idx = heap->entries[heap_idx].idx;
1048 if (constant_locations[idx] != -1)
1049 start = end = idx;
1050 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1052 while (stack_idx >= 0)
1054 /* Note that we fall through to the next case statement. */
1055 switch(stack[stack_idx])
1057 case HEAP_NODE_TRAVERSE_LEFT:
1059 unsigned int left_idx = heap_idx << 1;
1060 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1062 heap_idx = left_idx;
1063 idx = heap->entries[heap_idx].idx;
1064 if (constant_locations[idx] != -1)
1066 if (start > idx)
1067 start = idx;
1068 if (end < idx)
1069 end = idx;
1072 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1073 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1074 break;
1078 case HEAP_NODE_TRAVERSE_RIGHT:
1080 unsigned int right_idx = (heap_idx << 1) + 1;
1081 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1083 heap_idx = right_idx;
1084 idx = heap->entries[heap_idx].idx;
1085 if (constant_locations[idx] != -1)
1087 if (start > idx)
1088 start = idx;
1089 if (end < idx)
1090 end = idx;
1093 stack[stack_idx++] = HEAP_NODE_POP;
1094 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1095 break;
1099 case HEAP_NODE_POP:
1100 heap_idx >>= 1;
1101 --stack_idx;
1102 break;
1105 if (start <= end)
1106 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1107 checkGLcall("walk_constant_heap()");
1110 /* Context activation is done by the caller. */
1111 static void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1112 GLint location, const struct wined3d_vec4 *data)
1114 GLfloat clamped_constant[4];
1116 if (location == -1) return;
1118 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1119 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1120 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1121 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1123 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1126 /* Context activation is done by the caller. */
1127 static void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1128 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1129 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1131 int stack_idx = 0;
1132 unsigned int heap_idx = 1;
1133 unsigned int idx;
1135 if (heap->entries[heap_idx].version <= version) return;
1137 idx = heap->entries[heap_idx].idx;
1138 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1139 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1141 while (stack_idx >= 0)
1143 /* Note that we fall through to the next case statement. */
1144 switch(stack[stack_idx])
1146 case HEAP_NODE_TRAVERSE_LEFT:
1148 unsigned int left_idx = heap_idx << 1;
1149 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1151 heap_idx = left_idx;
1152 idx = heap->entries[heap_idx].idx;
1153 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1155 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1156 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1157 break;
1161 case HEAP_NODE_TRAVERSE_RIGHT:
1163 unsigned int right_idx = (heap_idx << 1) + 1;
1164 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1166 heap_idx = right_idx;
1167 idx = heap->entries[heap_idx].idx;
1168 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1170 stack[stack_idx++] = HEAP_NODE_POP;
1171 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1172 break;
1176 case HEAP_NODE_POP:
1177 heap_idx >>= 1;
1178 --stack_idx;
1179 break;
1182 checkGLcall("walk_constant_heap_clamped()");
1185 /* Context activation is done by the caller. */
1186 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1187 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1188 unsigned char *stack, unsigned int version)
1190 const struct wined3d_shader_lconst *lconst;
1192 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1193 if (shader->reg_maps.shader_version.major == 1
1194 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1195 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1196 else
1197 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1199 if (!shader->load_local_constsF)
1201 TRACE("No need to load local float constants for this shader.\n");
1202 return;
1205 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1206 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1208 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1210 checkGLcall("glUniform4fv()");
1213 /* Context activation is done by the caller. */
1214 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1215 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1217 unsigned int i;
1218 struct list* ptr;
1220 for (i = 0; constants_set; constants_set >>= 1, ++i)
1222 if (!(constants_set & 1)) continue;
1224 /* We found this uniform name in the program - go ahead and send the data */
1225 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1228 /* Load immediate constants */
1229 ptr = list_head(&shader->constantsI);
1230 while (ptr)
1232 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1233 unsigned int idx = lconst->idx;
1234 const GLint *values = (const GLint *)lconst->value;
1236 /* We found this uniform name in the program - go ahead and send the data */
1237 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1238 ptr = list_next(&shader->constantsI, ptr);
1240 checkGLcall("glUniform4iv()");
1243 /* Context activation is done by the caller. */
1244 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1245 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1247 unsigned int i;
1248 struct list* ptr;
1250 for (i = 0; constants_set; constants_set >>= 1, ++i)
1252 if (!(constants_set & 1)) continue;
1254 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1257 /* Load immediate constants */
1258 ptr = list_head(&shader->constantsB);
1259 while (ptr)
1261 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1262 unsigned int idx = lconst->idx;
1263 const GLint *values = (const GLint *)lconst->value;
1265 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1266 ptr = list_next(&shader->constantsB, ptr);
1268 checkGLcall("glUniform1iv()");
1271 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1273 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1276 /* Context activation is done by the caller (state handler). */
1277 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1278 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1280 struct
1282 float sx, sy;
1284 np2fixup_constants[WINED3D_MAX_FRAGMENT_SAMPLERS];
1285 UINT fixup = ps->np2_fixup_info->active;
1286 UINT i;
1288 for (i = 0; fixup; fixup >>= 1, ++i)
1290 const struct wined3d_texture *tex = state->textures[i];
1291 unsigned char idx = ps->np2_fixup_info->idx[i];
1293 if (!tex)
1295 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1296 continue;
1299 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1300 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1303 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1306 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1308 struct wined3d_matrix temp;
1309 unsigned int i, j;
1311 for (i = 0; i < 4; ++i)
1312 for (j = 0; j < 4; ++j)
1313 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1315 *out = temp;
1318 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context_gl *context_gl,
1319 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1321 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1322 struct wined3d_matrix mv;
1323 float mat[3 * 3];
1325 if (prog->vs.normal_matrix_location == -1)
1326 return;
1328 get_modelview_matrix(&context_gl->c, state, 0, &mv);
1329 compute_normal_matrix(mat, context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING, &mv);
1331 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1332 checkGLcall("glUniformMatrix3fv");
1335 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context_gl *context_gl,
1336 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1338 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1339 struct wined3d_matrix mat;
1341 if (tex >= WINED3D_MAX_TEXTURES)
1342 return;
1343 if (prog->vs.texture_matrix_location[tex] == -1)
1344 return;
1346 get_texture_matrix(&context_gl->c, state, tex, &mat);
1347 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1348 checkGLcall("glUniformMatrix4fv");
1351 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context_gl *context_gl,
1352 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1354 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1356 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1358 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1359 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1361 else
1363 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1365 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1367 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1368 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1369 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1370 checkGLcall("setting FFP material uniforms");
1373 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context_gl *context_gl,
1374 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1376 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1377 struct wined3d_color color;
1379 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1380 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1381 checkGLcall("glUniform3fv");
1384 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context_gl *context_gl,
1385 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1386 struct glsl_shader_prog_link *prog)
1388 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1389 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1390 struct wined3d_vec4 vec4;
1392 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1393 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1394 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1396 switch (light_info->OriginalParms.type)
1398 case WINED3D_LIGHT_POINT:
1399 wined3d_vec4_transform(&vec4, &light_info->position, view);
1400 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1401 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1402 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1403 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1404 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1405 break;
1407 case WINED3D_LIGHT_SPOT:
1408 wined3d_vec4_transform(&vec4, &light_info->position, view);
1409 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1411 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1412 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1414 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1415 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1416 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1417 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1418 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1419 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1420 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1421 break;
1423 case WINED3D_LIGHT_DIRECTIONAL:
1424 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1425 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1426 break;
1428 case WINED3D_LIGHT_PARALLELPOINT:
1429 wined3d_vec4_transform(&vec4, &light_info->position, view);
1430 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1431 break;
1433 default:
1434 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1436 checkGLcall("setting FFP lights uniforms");
1439 static void shader_glsl_pointsize_uniform(const struct wined3d_context_gl *context_gl,
1440 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1442 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1443 float size, att[3];
1444 float min, max;
1446 get_pointsize_minmax(&context_gl->c, state, &min, &max);
1448 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1449 checkGLcall("glUniform1f");
1450 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1451 checkGLcall("glUniform1f");
1453 get_pointsize(&context_gl->c, state, &size, att);
1455 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1456 checkGLcall("glUniform1f");
1457 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1458 checkGLcall("glUniform1f");
1459 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1460 checkGLcall("glUniform1f");
1461 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1462 checkGLcall("glUniform1f");
1465 static void shader_glsl_load_fog_uniform(const struct wined3d_context_gl *context_gl,
1466 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1468 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1469 struct wined3d_color color;
1470 float start, end, scale;
1471 union
1473 DWORD d;
1474 float f;
1475 } tmpvalue;
1477 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1478 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1479 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1480 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1481 get_fog_start_end(&context_gl->c, state, &start, &end);
1482 scale = 1.0f / (end - start);
1483 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1484 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1485 checkGLcall("fog emulation uniforms");
1488 static void shader_glsl_clip_plane_uniform(const struct wined3d_context_gl *context_gl,
1489 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1491 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1492 struct wined3d_matrix matrix;
1493 struct wined3d_vec4 plane;
1495 plane = state->clip_planes[index];
1497 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1498 if (!use_vs(state))
1500 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1501 transpose_matrix(&matrix, &matrix);
1502 wined3d_vec4_transform(&plane, &plane, &matrix);
1505 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1508 /* Context activation is done by the caller (state handler). */
1509 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1510 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1512 struct wined3d_color float_key[2];
1513 const struct wined3d_texture *texture = state->textures[0];
1515 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1516 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1519 /* Context activation is done by the caller (state handler). */
1520 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1521 const struct wined3d_state *state)
1523 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1524 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1525 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1526 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1527 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1528 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1529 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1530 struct shader_glsl_priv *priv = shader_priv;
1531 unsigned int constant_version;
1532 DWORD update_mask;
1533 int i;
1535 /* No GLSL program set - nothing to do. */
1536 if (!prog)
1537 return;
1539 constant_version = prog->constant_version;
1540 update_mask = context->constant_update_mask & prog->constant_update_mask;
1542 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1543 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1544 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1546 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1547 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1548 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1550 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1551 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1552 vshader->reg_maps.boolean_constants);
1554 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1556 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1557 shader_glsl_clip_plane_uniform(context_gl, state, i, prog);
1560 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1561 shader_glsl_pointsize_uniform(context_gl, state, prog);
1563 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1565 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1566 max(state->viewport_count, 1) : 1;
1567 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1568 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1569 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1570 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1571 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1572 else
1573 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1574 checkGLcall("glUniform4fv");
1577 if (update_mask & WINED3D_SHADER_CONST_BASE_VERTEX_ID)
1579 GL_EXTCALL(glUniform1i(prog->vs.base_vertex_id_location, state->base_vertex_index));
1580 checkGLcall("base vertex id");
1583 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1585 struct wined3d_matrix mat;
1587 get_modelview_matrix(context, state, 0, &mat);
1588 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1589 checkGLcall("glUniformMatrix4fv");
1591 shader_glsl_ffp_vertex_normalmatrix_uniform(context_gl, state, prog);
1594 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1596 struct wined3d_matrix mat;
1598 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1600 if (prog->vs.modelview_matrix_location[i] == -1)
1601 break;
1603 get_modelview_matrix(context, state, i, &mat);
1604 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1605 checkGLcall("glUniformMatrix4fv");
1609 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1611 struct wined3d_matrix projection;
1613 get_projection_matrix(context, state, &projection);
1614 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1615 checkGLcall("glUniformMatrix4fv");
1618 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1620 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1621 shader_glsl_ffp_vertex_texmatrix_uniform(context_gl, state, i, prog);
1624 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1625 shader_glsl_ffp_vertex_material_uniform(context_gl, state, prog);
1627 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1629 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1630 DWORD point_count = 0;
1631 DWORD spot_count = 0;
1632 DWORD directional_count = 0;
1633 DWORD parallel_point_count = 0;
1635 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1637 if (!state->light_state.lights[i])
1638 continue;
1640 switch (state->light_state.lights[i]->OriginalParms.type)
1642 case WINED3D_LIGHT_POINT:
1643 ++point_count;
1644 break;
1645 case WINED3D_LIGHT_SPOT:
1646 ++spot_count;
1647 break;
1648 case WINED3D_LIGHT_DIRECTIONAL:
1649 ++directional_count;
1650 break;
1651 case WINED3D_LIGHT_PARALLELPOINT:
1652 ++parallel_point_count;
1653 break;
1654 default:
1655 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
1656 break;
1659 point_idx = 0;
1660 spot_idx = point_idx + point_count;
1661 directional_idx = spot_idx + spot_count;
1662 parallel_point_idx = directional_idx + directional_count;
1664 shader_glsl_ffp_vertex_lightambient_uniform(context_gl, state, prog);
1665 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1667 const struct wined3d_light_info *light_info = state->light_state.lights[i];
1668 unsigned int idx;
1670 if (!light_info)
1671 continue;
1673 switch (light_info->OriginalParms.type)
1675 case WINED3D_LIGHT_POINT:
1676 idx = point_idx++;
1677 break;
1678 case WINED3D_LIGHT_SPOT:
1679 idx = spot_idx++;
1680 break;
1681 case WINED3D_LIGHT_DIRECTIONAL:
1682 idx = directional_idx++;
1683 break;
1684 case WINED3D_LIGHT_PARALLELPOINT:
1685 idx = parallel_point_idx++;
1686 break;
1687 default:
1688 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1689 continue;
1691 shader_glsl_ffp_vertex_light_uniform(context_gl, state, idx, light_info, prog);
1695 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1696 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1697 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1699 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1700 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1701 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1703 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1704 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1705 pshader->reg_maps.boolean_constants);
1707 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1709 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1711 if (prog->ps.bumpenv_mat_location[i] == -1)
1712 continue;
1714 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1715 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1717 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1719 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1720 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1721 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1722 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1726 checkGLcall("bump env uniforms");
1729 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1731 const struct wined3d_vec4 correction_params =
1733 /* Position is relative to the framebuffer, not the viewport. */
1734 context->render_offscreen ? 0.0f : (float)state->fb.render_targets[0]->height,
1735 context->render_offscreen ? 1.0f : -1.0f,
1736 0.0f,
1737 0.0f,
1740 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1743 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1744 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1745 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1746 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1748 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1750 struct wined3d_color color;
1752 if (prog->ps.tex_factor_location != -1)
1754 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1755 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1758 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1759 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1760 else
1761 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1763 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1765 if (prog->ps.tss_constant_location[i] == -1)
1766 continue;
1768 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1769 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1772 checkGLcall("fixed function uniforms");
1775 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1776 shader_glsl_load_fog_uniform(context_gl, state, prog);
1778 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1780 float ref = wined3d_alpha_ref(state);
1782 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1783 checkGLcall("alpha test emulation uniform");
1786 if (priv->next_constant_version == UINT_MAX)
1788 TRACE("Max constant version reached, resetting to 0.\n");
1789 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1790 priv->next_constant_version = 1;
1792 else
1794 prog->constant_version = priv->next_constant_version++;
1798 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1800 struct constant_entry *entries = heap->entries;
1801 unsigned int *positions = heap->positions;
1802 unsigned int heap_idx, parent_idx;
1804 if (!heap->contained[idx])
1806 heap_idx = heap->size++;
1807 heap->contained[idx] = TRUE;
1809 else
1811 heap_idx = positions[idx];
1814 while (heap_idx > 1)
1816 parent_idx = heap_idx >> 1;
1818 if (new_version <= entries[parent_idx].version) break;
1820 entries[heap_idx] = entries[parent_idx];
1821 positions[entries[parent_idx].idx] = heap_idx;
1822 heap_idx = parent_idx;
1825 entries[heap_idx].version = new_version;
1826 entries[heap_idx].idx = idx;
1827 positions[idx] = heap_idx;
1830 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1832 struct shader_glsl_priv *priv = device->shader_priv;
1833 struct constant_heap *heap = &priv->vconst_heap;
1834 UINT i;
1836 for (i = start; i < count + start; ++i)
1838 update_heap_entry(heap, i, priv->next_constant_version);
1842 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1844 struct shader_glsl_priv *priv = device->shader_priv;
1845 struct constant_heap *heap = &priv->pconst_heap;
1846 UINT i;
1848 for (i = start; i < count + start; ++i)
1850 update_heap_entry(heap, i, priv->next_constant_version);
1854 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1856 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1857 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1858 if(shader_major > 3) return ret;
1860 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1861 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1862 return ret;
1865 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1867 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
1870 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1872 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
1873 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
1876 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
1878 return shader_glsl_get_version(gl_info) >= 150;
1881 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1883 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1886 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1887 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1889 va_list args;
1890 int ret;
1892 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1893 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1894 for (;;)
1896 va_start(args, format);
1897 ret = shader_vaddline(buffer, format, args);
1898 va_end(args);
1899 if (!ret)
1900 return;
1901 if (!string_buffer_resize(buffer, ret))
1902 return;
1906 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1907 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1909 va_list args;
1910 int ret;
1912 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1913 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1914 for (;;)
1916 va_start(args, format);
1917 ret = shader_vaddline(buffer, format, args);
1918 va_end(args);
1919 if (!ret)
1920 return;
1921 if (!string_buffer_resize(buffer, ret))
1922 return;
1926 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
1928 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
1931 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
1933 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
1936 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
1938 switch (mode)
1940 case WINED3DSIM_CONSTANT:
1941 return "flat ";
1942 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
1943 return "noperspective ";
1944 default:
1945 FIXME("Unhandled interpolation mode %#x.\n", mode);
1946 case WINED3DSIM_NONE:
1947 case WINED3DSIM_LINEAR:
1948 return "";
1952 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
1953 const DWORD *packed_interpolation_mode, unsigned int register_idx)
1955 return wined3d_extract_bits(packed_interpolation_mode,
1956 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
1959 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
1960 struct wined3d_string_buffer *buffer, unsigned int element_count,
1961 const DWORD *interpolation_mode, BOOL unroll)
1963 enum wined3d_shader_interpolation_mode mode;
1964 unsigned int i;
1966 if (shader_glsl_use_interface_blocks(gl_info))
1968 if (unroll)
1970 shader_addline(buffer, "in shader_in_out {\n");
1971 for (i = 0; i < element_count; ++i)
1973 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
1974 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
1976 shader_addline(buffer, "} shader_in;\n");
1978 else
1980 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
1983 else
1985 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
1989 static BOOL needs_interpolation_qualifiers_for_shader_outputs(const struct wined3d_gl_info *gl_info)
1991 /* In GLSL 4.40+ it is fine to specify interpolation qualifiers only in
1992 * fragment shaders. In older GLSL versions interpolation qualifiers must
1993 * match between shader stages. */
1994 return gl_info->glsl_version < MAKEDWORD_VERSION(4, 40);
1997 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
1998 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
1999 const DWORD *interpolation_mode)
2001 enum wined3d_shader_interpolation_mode mode;
2002 unsigned int i;
2004 if (shader_glsl_use_interface_blocks(gl_info))
2006 if (rasterizer_setup)
2008 shader_addline(buffer, "out shader_in_out {\n");
2009 for (i = 0; i < element_count; ++i)
2011 const char *interpolation_qualifiers = "";
2012 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2014 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2015 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2017 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2019 shader_addline(buffer, "} shader_out;\n");
2021 else
2023 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2026 else
2028 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2032 static BOOL use_legacy_fragment_output(const struct wined3d_gl_info *gl_info)
2034 /* Technically 1.30 does support user-defined fragment shader outputs but
2035 * we might not have glBindFragDataLocation() available (i.e. GL version
2036 * might be < 3.0). */
2037 return gl_info->glsl_version <= MAKEDWORD_VERSION(1, 30);
2040 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2042 return use_legacy_fragment_output(gl_info) ? "gl_FragData" : "ps_out";
2045 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2047 switch (primitive_type)
2049 case WINED3D_PT_POINTLIST:
2050 return "points";
2052 case WINED3D_PT_LINELIST:
2053 return "lines";
2055 case WINED3D_PT_LINESTRIP:
2056 return "line_strip";
2058 case WINED3D_PT_TRIANGLELIST:
2059 return "triangles";
2061 case WINED3D_PT_TRIANGLESTRIP:
2062 return "triangle_strip";
2064 case WINED3D_PT_LINELIST_ADJ:
2065 return "lines_adjacency";
2067 case WINED3D_PT_TRIANGLELIST_ADJ:
2068 return "triangles_adjacency";
2070 default:
2071 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2072 return "";
2076 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2078 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2079 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2080 DWORD input_reg_used = shader->u.ps.input_reg_used;
2081 unsigned int i;
2083 if (reg_maps->shader_version.major < 3)
2084 return input_reg_used & (1u << idx);
2086 for (i = 0; i < input_signature->element_count; ++i)
2088 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2090 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2091 continue;
2093 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2094 && input->semantic_idx == idx)
2095 return input_reg_used & (1u << input->register_idx);
2097 return FALSE;
2100 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2101 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2103 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2105 if (version->major >= 4)
2106 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2107 else
2108 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2111 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2112 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2113 unsigned int index)
2115 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2116 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2117 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2118 index, scalar_type, scalar_type, index);
2121 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2122 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2124 unsigned int index = e->register_idx;
2125 enum wined3d_component_type type;
2127 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2129 shader_addline(buffer, "uniform int base_vertex_id;\n");
2130 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID - base_vertex_id), 0.0, 0.0, 0.0);\n", index);
2131 return;
2133 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2135 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2136 index);
2137 return;
2139 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2140 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2142 if (shader_glsl_use_explicit_attrib_location(gl_info))
2143 shader_addline(buffer, "layout(location = %u) ", index);
2145 type = e->component_type;
2146 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2148 FIXME("Unhandled type %#x.\n", type);
2149 type = WINED3D_TYPE_FLOAT;
2151 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2152 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2153 else
2154 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2155 component_type_info[type].glsl_vector_type,
2156 component_type_info[type].glsl_scalar_type, index);
2159 /** Generate the variable & register declarations for the GLSL output target */
2160 static void shader_generate_glsl_declarations(const struct wined3d_context_gl *context_gl,
2161 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2162 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2164 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2165 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2166 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2167 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2168 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2169 unsigned int uniform_block_base, uniform_block_count;
2170 enum wined3d_shader_resource_type resource_type;
2171 const struct wined3d_shader_lconst *lconst;
2172 const char *prefix;
2173 unsigned int i;
2174 DWORD map;
2176 if (wined3d_settings.strict_shader_math)
2177 shader_addline(buffer, "#pragma optionNV(fastmath off)\n");
2179 prefix = shader_glsl_get_prefix(version->type);
2181 /* Prototype the subroutines */
2182 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2184 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2187 /* Declare the constants (aka uniforms) */
2188 if (shader->limits->constant_float > 0)
2190 unsigned max_constantsF;
2192 /* Unless the shader uses indirect addressing, always declare the
2193 * maximum array size and ignore that we need some uniforms privately.
2194 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2195 * and immediate values, still declare VC[256]. If the shader needs
2196 * more uniforms than we have it won't work in any case. If it uses
2197 * less, the compiler will figure out which uniforms are really used
2198 * and strip them out. This allows a shader to use c255 on a dx9 card,
2199 * as long as it doesn't also use all the other constants.
2201 * If the shader uses indirect addressing the compiler must assume
2202 * that all declared uniforms are used. In this case, declare only the
2203 * amount that we're assured to have.
2205 * Thus we run into problems in these two cases:
2206 * 1) The shader really uses more uniforms than supported.
2207 * 2) The shader uses indirect addressing, less constants than
2208 * supported, but uses a constant index > #supported consts. */
2209 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2211 /* No indirect addressing here. */
2212 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2214 else
2216 if (reg_maps->usesrelconstF)
2218 /* Subtract the other potential uniforms from the max
2219 * available (bools, ints, and 1 row of projection matrix).
2220 * Subtract another uniform for immediate values, which have
2221 * to be loaded via uniform by the driver as well. The shader
2222 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2223 * shader code, so one vec4 should be enough. (Unfortunately
2224 * the Nvidia driver doesn't store 128 and -128 in one float).
2226 * Writing gl_ClipVertex requires one uniform for each
2227 * clipplane as well. */
2228 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2229 if (vs_args->clip_enabled)
2230 max_constantsF -= gl_info->limits.user_clip_distances;
2231 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2232 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2233 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2234 * for now take this into account when calculating the number of available constants
2236 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2237 /* Set by driver quirks in directx.c */
2238 max_constantsF -= gl_info->reserved_glsl_constants;
2240 if (max_constantsF < shader->limits->constant_float)
2242 static unsigned int once;
2244 if (!once++)
2245 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2246 " it may not render correctly.\n");
2247 else
2248 WARN("The hardware does not support enough uniform components to run this shader.\n");
2251 else
2253 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2256 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2257 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2260 /* Always declare the full set of constants, the compiler can remove the
2261 * unused ones because d3d doesn't (yet) support indirect int and bool
2262 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2263 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2264 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2266 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2267 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2269 /* Declare immediate constant buffer */
2270 if (reg_maps->icb)
2271 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2273 /* Declare constant buffers */
2274 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2275 &uniform_block_base, &uniform_block_count);
2276 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2278 if (reg_maps->cb_sizes[i])
2280 shader_addline(buffer, "layout(std140");
2281 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2282 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2283 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2284 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2288 /* Declare texture samplers */
2289 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2291 struct wined3d_shader_sampler_map_entry *entry;
2292 const char *sampler_type_prefix, *sampler_type;
2293 BOOL shadow_sampler, tex_rect;
2295 entry = &reg_maps->sampler_map.entries[i];
2297 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2299 ERR("Invalid resource index %u.\n", entry->resource_idx);
2300 continue;
2303 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2305 case WINED3D_DATA_FLOAT:
2306 case WINED3D_DATA_UNORM:
2307 case WINED3D_DATA_SNORM:
2308 sampler_type_prefix = "";
2309 break;
2311 case WINED3D_DATA_INT:
2312 sampler_type_prefix = "i";
2313 break;
2315 case WINED3D_DATA_UINT:
2316 sampler_type_prefix = "u";
2317 break;
2319 default:
2320 sampler_type_prefix = "";
2321 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2322 break;
2325 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2326 resource_type = version->type == WINED3D_SHADER_TYPE_PIXEL
2327 ? pixelshader_get_resource_type(reg_maps, entry->resource_idx, ps_args->tex_types)
2328 : reg_maps->resource_info[entry->resource_idx].type;
2330 switch (resource_type)
2332 case WINED3D_SHADER_RESOURCE_BUFFER:
2333 sampler_type = "samplerBuffer";
2334 break;
2336 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2337 if (shadow_sampler)
2338 sampler_type = "sampler1DShadow";
2339 else
2340 sampler_type = "sampler1D";
2341 break;
2343 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2344 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2345 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2346 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2347 if (shadow_sampler)
2349 if (tex_rect)
2350 sampler_type = "sampler2DRectShadow";
2351 else
2352 sampler_type = "sampler2DShadow";
2354 else
2356 if (tex_rect)
2357 sampler_type = "sampler2DRect";
2358 else
2359 sampler_type = "sampler2D";
2361 break;
2363 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2364 if (shadow_sampler)
2365 FIXME("Unsupported 3D shadow sampler.\n");
2366 sampler_type = "sampler3D";
2367 break;
2369 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2370 if (shadow_sampler)
2371 sampler_type = "samplerCubeShadow";
2372 else
2373 sampler_type = "samplerCube";
2374 break;
2376 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2377 if (shadow_sampler)
2378 sampler_type = "sampler1DArrayShadow";
2379 else
2380 sampler_type = "sampler1DArray";
2381 break;
2383 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2384 if (shadow_sampler)
2385 sampler_type = "sampler2DArrayShadow";
2386 else
2387 sampler_type = "sampler2DArray";
2388 break;
2390 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2391 if (shadow_sampler)
2392 sampler_type = "samplerCubeArrayShadow";
2393 else
2394 sampler_type = "samplerCubeArray";
2395 break;
2397 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2398 sampler_type = "sampler2DMS";
2399 break;
2401 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2402 sampler_type = "sampler2DMSArray";
2403 break;
2405 default:
2406 sampler_type = "unsupported_sampler";
2407 FIXME("Unhandled resource type %#x.\n", resource_type);
2408 break;
2411 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2412 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, version, entry->bind_idx);
2413 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2414 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2417 /* Declare images */
2418 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2420 const char *image_type_prefix, *image_type, *read_format;
2422 if (!reg_maps->uav_resource_info[i].type)
2423 continue;
2425 switch (reg_maps->uav_resource_info[i].data_type)
2427 case WINED3D_DATA_FLOAT:
2428 case WINED3D_DATA_UNORM:
2429 case WINED3D_DATA_SNORM:
2430 image_type_prefix = "";
2431 read_format = "r32f";
2432 break;
2434 case WINED3D_DATA_INT:
2435 image_type_prefix = "i";
2436 read_format = "r32i";
2437 break;
2439 case WINED3D_DATA_UINT:
2440 image_type_prefix = "u";
2441 read_format = "r32ui";
2442 break;
2444 default:
2445 image_type_prefix = "";
2446 read_format = "";
2447 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2448 break;
2451 switch (reg_maps->uav_resource_info[i].type)
2453 case WINED3D_SHADER_RESOURCE_BUFFER:
2454 image_type = "imageBuffer";
2455 break;
2457 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2458 image_type = "image1D";
2459 break;
2461 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2462 image_type = "image2D";
2463 break;
2465 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2466 image_type = "image3D";
2467 break;
2469 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2470 image_type = "image1DArray";
2471 break;
2473 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2474 image_type = "image2DArray";
2475 break;
2477 default:
2478 image_type = "unsupported_image";
2479 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2480 break;
2483 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2484 shader_addline(buffer, "layout(binding = %u)\n", i);
2485 if (reg_maps->uav_read_mask & (1u << i))
2486 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2487 read_format, image_type_prefix, image_type, prefix, i);
2488 else
2489 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2490 image_type_prefix, image_type, prefix, i);
2492 if (reg_maps->uav_counter_mask & (1u << i))
2493 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2494 i, prefix, i);
2497 /* Declare address variables */
2498 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2500 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2503 /* Declare output register temporaries */
2504 if (shader->limits->packed_output)
2505 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2507 /* Declare temporary variables */
2508 if (reg_maps->temporary_count)
2510 for (i = 0; i < reg_maps->temporary_count; ++i)
2511 shader_addline(buffer, "vec4 R%u;\n", i);
2513 else if (version->major < 4)
2515 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2517 if (map & 1)
2518 shader_addline(buffer, "vec4 R%u;\n", i);
2522 /* Declare indexable temporary variables */
2523 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2525 if (idx_temp_reg->component_count != 4)
2526 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2527 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2530 /* Declare loop registers aLx */
2531 if (version->major < 4)
2533 for (i = 0; i < reg_maps->loop_depth; ++i)
2535 shader_addline(buffer, "int aL%u;\n", i);
2536 shader_addline(buffer, "int tmpInt%u;\n", i);
2540 /* Temporary variables for matrix operations */
2541 shader_addline(buffer, "vec4 tmp0;\n");
2542 shader_addline(buffer, "vec4 tmp1;\n");
2543 if (gl_info->supported[ARB_GPU_SHADER5])
2544 shader_addline(buffer, "precise vec4 tmp_precise;\n");
2545 else
2546 shader_addline(buffer, "/* precise */ vec4 tmp_precise;\n");
2548 if (!shader->load_local_constsF)
2550 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2552 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2553 shader_glsl_append_imm_vec(buffer, (const float *)lconst->value, ARRAY_SIZE(lconst->value), gl_info);
2554 shader_addline(buffer, ";\n");
2559 /* Prototypes */
2560 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
2561 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2562 enum wined3d_data_type data_type);
2564 /** Used for opcode modifiers - They multiply the result by the specified amount */
2565 static const char * const shift_glsl_tab[] = {
2566 "", /* 0 (none) */
2567 "2.0 * ", /* 1 (x2) */
2568 "4.0 * ", /* 2 (x4) */
2569 "8.0 * ", /* 3 (x8) */
2570 "16.0 * ", /* 4 (x16) */
2571 "32.0 * ", /* 5 (x32) */
2572 "", /* 6 (x64) */
2573 "", /* 7 (x128) */
2574 "", /* 8 (d256) */
2575 "", /* 9 (d128) */
2576 "", /* 10 (d64) */
2577 "", /* 11 (d32) */
2578 "0.0625 * ", /* 12 (d16) */
2579 "0.125 * ", /* 13 (d8) */
2580 "0.25 * ", /* 14 (d4) */
2581 "0.5 * " /* 15 (d2) */
2584 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2585 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2586 const char *in_reg, const char *in_regswizzle, char *out_str)
2588 switch (src_modifier)
2590 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2591 case WINED3DSPSM_DW:
2592 case WINED3DSPSM_NONE:
2593 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2594 break;
2595 case WINED3DSPSM_NEG:
2596 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2597 break;
2598 case WINED3DSPSM_NOT:
2599 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2600 break;
2601 case WINED3DSPSM_BIAS:
2602 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2603 break;
2604 case WINED3DSPSM_BIASNEG:
2605 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2606 break;
2607 case WINED3DSPSM_SIGN:
2608 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2609 break;
2610 case WINED3DSPSM_SIGNNEG:
2611 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2612 break;
2613 case WINED3DSPSM_COMP:
2614 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2615 break;
2616 case WINED3DSPSM_X2:
2617 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2618 break;
2619 case WINED3DSPSM_X2NEG:
2620 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2621 break;
2622 case WINED3DSPSM_ABS:
2623 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2624 break;
2625 case WINED3DSPSM_ABSNEG:
2626 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2627 break;
2628 default:
2629 FIXME("Unhandled modifier %u\n", src_modifier);
2630 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2634 static void shader_glsl_fixup_scalar_register_variable(struct wined3d_string_buffer *register_name,
2635 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2637 /* The ARB_shading_language_420pack extension allows swizzle operations on
2638 * scalars. */
2639 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2640 string_buffer_sprintf(register_name, "%s", glsl_variable);
2641 else
2642 string_buffer_sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2645 /** Writes the GLSL variable name that corresponds to the register that the
2646 * DX opcode parameter is trying to access */
2647 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2648 enum wined3d_data_type data_type, struct wined3d_string_buffer *register_name,
2649 BOOL *is_swizzled, const struct wined3d_shader_context *ctx)
2651 /* oPos, oFog and oPts in D3D */
2652 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2654 const struct wined3d_shader *shader = ctx->shader;
2655 const struct wined3d_shader_reg_maps *reg_maps = ctx->reg_maps;
2656 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2657 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2658 const char *prefix = shader_glsl_get_prefix(version->type);
2659 const struct wined3d_gl_info *gl_info = priv->gl_info;
2660 struct glsl_src_param rel_param0, rel_param1;
2662 if (reg->idx[0].offset != ~0u && reg->idx[0].rel_addr)
2663 shader_glsl_add_src_param_ext(ctx, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0,
2664 &rel_param0, reg->idx[0].rel_addr->reg.data_type);
2665 if (reg->idx[1].offset != ~0u && reg->idx[1].rel_addr)
2666 shader_glsl_add_src_param_ext(ctx, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0,
2667 &rel_param1, reg->idx[1].rel_addr->reg.data_type);
2668 if (is_swizzled)
2669 *is_swizzled = FALSE;
2671 switch (reg->type)
2673 case WINED3DSPR_TEMP:
2674 string_buffer_sprintf(register_name, "R%u", reg->idx[0].offset);
2675 break;
2677 case WINED3DSPR_INPUT:
2678 case WINED3DSPR_INCONTROLPOINT:
2679 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2681 if (reg->idx[0].rel_addr)
2682 FIXME("VS3 input registers relative addressing.\n");
2683 if (is_swizzled && priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2684 *is_swizzled = TRUE;
2685 if (reg->idx[0].rel_addr)
2687 string_buffer_sprintf(register_name, "%s_in[%s + %u]",
2688 prefix, rel_param0.param_str, reg->idx[0].offset);
2690 else
2692 string_buffer_sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2694 break;
2697 if (version->type == WINED3D_SHADER_TYPE_HULL
2698 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2699 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2701 if (reg->idx[0].rel_addr)
2703 if (reg->idx[1].rel_addr)
2704 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2705 rel_param0.param_str, reg->idx[0].offset,
2706 rel_param1.param_str, reg->idx[1].offset);
2707 else
2708 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2709 rel_param0.param_str, reg->idx[0].offset,
2710 reg->idx[1].offset);
2712 else if (reg->idx[1].rel_addr)
2713 string_buffer_sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2714 rel_param1.param_str, reg->idx[1].offset);
2715 else
2716 string_buffer_sprintf(register_name, "shader_in[%u].reg[%u]",
2717 reg->idx[0].offset, reg->idx[1].offset);
2718 break;
2721 /* pixel shaders >= 3.0 */
2722 if (version->major >= 3)
2724 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2725 unsigned int in_count = vec4_varyings(version->major, gl_info);
2727 if (reg->idx[0].rel_addr)
2729 /* Removing a + 0 would be an obvious optimization, but
2730 * OS X doesn't see the NOP operation there. */
2731 if (idx)
2733 if (needs_legacy_glsl_syntax(gl_info)
2734 && shader->u.ps.declared_in_count > in_count)
2736 string_buffer_sprintf(register_name,
2737 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2738 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2739 prefix, rel_param0.param_str, idx);
2741 else
2743 string_buffer_sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2746 else
2748 if (needs_legacy_glsl_syntax(gl_info)
2749 && shader->u.ps.declared_in_count > in_count)
2751 string_buffer_sprintf(register_name,
2752 "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2753 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2754 prefix, rel_param0.param_str);
2756 else
2758 string_buffer_sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2762 else
2764 if (idx == in_count)
2765 string_buffer_sprintf(register_name, "gl_Color");
2766 else if (idx == in_count + 1)
2767 string_buffer_sprintf(register_name, "gl_SecondaryColor");
2768 else
2769 string_buffer_sprintf(register_name, "%s_in[%u]", prefix, idx);
2772 else
2774 if (!reg->idx[0].offset)
2775 string_buffer_sprintf(register_name, "ffp_varying_diffuse");
2776 else
2777 string_buffer_sprintf(register_name, "ffp_varying_specular");
2778 break;
2780 break;
2782 case WINED3DSPR_CONST:
2784 /* Relative addressing */
2785 if (reg->idx[0].rel_addr)
2787 if (wined3d_settings.check_float_constants)
2788 string_buffer_sprintf(register_name,
2789 "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2790 rel_param0.param_str, reg->idx[0].offset,
2791 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2792 prefix, rel_param0.param_str, reg->idx[0].offset);
2793 else if (reg->idx[0].offset)
2794 string_buffer_sprintf(register_name, "%s_c[%s + %u]",
2795 prefix, rel_param0.param_str, reg->idx[0].offset);
2796 else
2797 string_buffer_sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2799 else
2801 if (shader_constant_is_local(shader, reg->idx[0].offset))
2802 string_buffer_sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2803 else
2804 string_buffer_sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2807 break;
2809 case WINED3DSPR_CONSTINT:
2810 string_buffer_sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2811 break;
2813 case WINED3DSPR_CONSTBOOL:
2814 string_buffer_sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2815 break;
2817 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2818 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2819 string_buffer_sprintf(register_name, "T%u", reg->idx[0].offset);
2820 else
2821 string_buffer_sprintf(register_name, "A%u", reg->idx[0].offset);
2822 break;
2824 case WINED3DSPR_LOOP:
2825 string_buffer_sprintf(register_name, "aL%u", ctx->state->current_loop_reg - 1);
2826 break;
2828 case WINED3DSPR_SAMPLER:
2829 string_buffer_sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2830 break;
2832 case WINED3DSPR_COLOROUT:
2833 if (reg->idx[0].offset >= gl_info->limits.buffers)
2834 WARN("Write to render target %u, only %d supported.\n",
2835 reg->idx[0].offset, gl_info->limits.buffers);
2837 string_buffer_sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2838 break;
2840 case WINED3DSPR_RASTOUT:
2841 string_buffer_sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2842 break;
2844 case WINED3DSPR_DEPTHOUT:
2845 case WINED3DSPR_DEPTHOUTGE:
2846 case WINED3DSPR_DEPTHOUTLE:
2847 string_buffer_sprintf(register_name, "gl_FragDepth");
2848 break;
2850 case WINED3DSPR_ATTROUT:
2851 if (!reg->idx[0].offset)
2852 string_buffer_sprintf(register_name, "%s_out[8]", prefix);
2853 else
2854 string_buffer_sprintf(register_name, "%s_out[9]", prefix);
2855 break;
2857 case WINED3DSPR_TEXCRDOUT:
2858 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2859 if (reg->idx[0].rel_addr)
2860 string_buffer_sprintf(register_name, "%s_out[%s + %u]",
2861 prefix, rel_param0.param_str, reg->idx[0].offset);
2862 else
2863 string_buffer_sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2864 break;
2866 case WINED3DSPR_MISCTYPE:
2867 if (!reg->idx[0].offset)
2869 /* vPos */
2870 string_buffer_sprintf(register_name, "vpos");
2872 else if (reg->idx[0].offset == 1)
2874 /* Note that gl_FrontFacing is a bool, while vFace is
2875 * a float for which the sign determines front/back */
2876 string_buffer_sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2878 else
2880 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2881 string_buffer_sprintf(register_name, "unrecognized_register");
2883 break;
2885 case WINED3DSPR_IMMCONST:
2886 switch (reg->immconst_type)
2888 case WINED3D_IMMCONST_SCALAR:
2889 switch (data_type)
2891 case WINED3D_DATA_UNORM:
2892 case WINED3D_DATA_SNORM:
2893 case WINED3D_DATA_FLOAT:
2894 string_buffer_clear(register_name);
2895 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 1, gl_info);
2896 break;
2897 case WINED3D_DATA_INT:
2898 string_buffer_sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2899 break;
2900 case WINED3D_DATA_RESOURCE:
2901 case WINED3D_DATA_SAMPLER:
2902 case WINED3D_DATA_UINT:
2903 string_buffer_sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2904 break;
2905 default:
2906 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2907 break;
2909 break;
2911 case WINED3D_IMMCONST_VEC4:
2912 switch (data_type)
2914 case WINED3D_DATA_UNORM:
2915 case WINED3D_DATA_SNORM:
2916 case WINED3D_DATA_FLOAT:
2917 string_buffer_clear(register_name);
2918 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 4, gl_info);
2919 break;
2920 case WINED3D_DATA_INT:
2921 string_buffer_sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2922 reg->u.immconst_data[0], reg->u.immconst_data[1],
2923 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2924 break;
2925 case WINED3D_DATA_RESOURCE:
2926 case WINED3D_DATA_SAMPLER:
2927 case WINED3D_DATA_UINT:
2928 string_buffer_sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2929 reg->u.immconst_data[0], reg->u.immconst_data[1],
2930 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2931 break;
2932 default:
2933 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2934 break;
2936 break;
2938 default:
2939 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2940 string_buffer_sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2942 break;
2944 case WINED3DSPR_CONSTBUFFER:
2945 if (reg->idx[1].rel_addr)
2946 string_buffer_sprintf(register_name, "%s_cb%u[%s + %u]",
2947 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2948 else
2949 string_buffer_sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2950 break;
2952 case WINED3DSPR_IMMCONSTBUFFER:
2953 if (reg->idx[0].rel_addr)
2954 string_buffer_sprintf(register_name, "%s_icb[%s + %u]",
2955 prefix, rel_param0.param_str, reg->idx[0].offset);
2956 else
2957 string_buffer_sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2958 break;
2960 case WINED3DSPR_PRIMID:
2961 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2962 string_buffer_sprintf(register_name, "gl_PrimitiveIDIn");
2963 else
2964 string_buffer_sprintf(register_name, "gl_PrimitiveID");
2965 break;
2967 case WINED3DSPR_IDXTEMP:
2968 if (reg->idx[1].rel_addr)
2969 string_buffer_sprintf(register_name, "X%u[%s + %u]",
2970 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2971 else
2972 string_buffer_sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2973 break;
2975 case WINED3DSPR_LOCALTHREADINDEX:
2976 shader_glsl_fixup_scalar_register_variable(register_name,
2977 "int(gl_LocalInvocationIndex)", gl_info);
2978 break;
2980 case WINED3DSPR_GSINSTID:
2981 case WINED3DSPR_OUTPOINTID:
2982 shader_glsl_fixup_scalar_register_variable(register_name,
2983 "gl_InvocationID", gl_info);
2984 break;
2986 case WINED3DSPR_THREADID:
2987 string_buffer_sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2988 break;
2990 case WINED3DSPR_THREADGROUPID:
2991 string_buffer_sprintf(register_name, "ivec3(gl_WorkGroupID)");
2992 break;
2994 case WINED3DSPR_LOCALTHREADID:
2995 string_buffer_sprintf(register_name, "ivec3(gl_LocalInvocationID)");
2996 break;
2998 case WINED3DSPR_FORKINSTID:
2999 case WINED3DSPR_JOININSTID:
3000 shader_glsl_fixup_scalar_register_variable(register_name,
3001 "phase_instance_id", gl_info);
3002 break;
3004 case WINED3DSPR_TESSCOORD:
3005 string_buffer_sprintf(register_name, "gl_TessCoord");
3006 break;
3008 case WINED3DSPR_OUTCONTROLPOINT:
3009 if (reg->idx[0].rel_addr)
3011 if (reg->idx[1].rel_addr)
3012 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3013 rel_param0.param_str, reg->idx[0].offset,
3014 rel_param1.param_str, reg->idx[1].offset);
3015 else
3016 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3017 rel_param0.param_str, reg->idx[0].offset,
3018 reg->idx[1].offset);
3020 else if (reg->idx[1].rel_addr)
3022 string_buffer_sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3023 reg->idx[0].offset, rel_param1.param_str,
3024 reg->idx[1].offset);
3026 else
3028 string_buffer_sprintf(register_name, "shader_out[%u].reg[%u]",
3029 reg->idx[0].offset, reg->idx[1].offset);
3031 break;
3033 case WINED3DSPR_PATCHCONST:
3034 if (version->type == WINED3D_SHADER_TYPE_HULL)
3035 string_buffer_sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3036 else
3037 string_buffer_sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3038 break;
3040 case WINED3DSPR_COVERAGE:
3041 string_buffer_sprintf(register_name, "gl_SampleMaskIn[0]");
3042 break;
3044 case WINED3DSPR_SAMPLEMASK:
3045 string_buffer_sprintf(register_name, "sample_mask");
3046 break;
3048 default:
3049 FIXME("Unhandled register type %#x.\n", reg->type);
3050 string_buffer_sprintf(register_name, "unrecognised_register");
3051 break;
3055 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3057 *str++ = '.';
3058 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3059 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3060 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3061 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3062 *str = '\0';
3065 /* Get the GLSL write mask for the destination register */
3066 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3068 DWORD mask = param->write_mask;
3070 if (shader_is_scalar(&param->reg))
3072 mask = WINED3DSP_WRITEMASK_0;
3073 *write_mask = '\0';
3075 else
3077 shader_glsl_write_mask_to_str(mask, write_mask);
3080 return mask;
3083 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3085 unsigned int size = 0;
3087 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3088 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3089 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3090 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3092 return size;
3095 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3096 unsigned int component_idx)
3098 /* swizzle bits fields: wwzzyyxx */
3099 return (swizzle >> (2 * component_idx)) & 0x3;
3102 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3104 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3105 * but addressed as "rgba". To fix this we need to swap the register's x
3106 * and z components. */
3107 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3108 unsigned int i;
3110 *str++ = '.';
3111 for (i = 0; i < 4; ++i)
3113 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3114 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3116 *str = '\0';
3119 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3120 BOOL fixup, DWORD mask, char *swizzle_str)
3122 if (shader_is_scalar(&param->reg))
3123 *swizzle_str = '\0';
3124 else
3125 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3128 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3129 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type, unsigned int size)
3131 if (dst_data_type == WINED3D_DATA_UNORM || dst_data_type == WINED3D_DATA_SNORM)
3132 dst_data_type = WINED3D_DATA_FLOAT;
3133 if (src_data_type == WINED3D_DATA_UNORM || src_data_type == WINED3D_DATA_SNORM)
3134 src_data_type = WINED3D_DATA_FLOAT;
3136 if (dst_data_type == src_data_type)
3138 string_buffer_sprintf(dst_param, "%s", src_param);
3139 return;
3142 if (src_data_type == WINED3D_DATA_FLOAT)
3144 switch (dst_data_type)
3146 case WINED3D_DATA_INT:
3147 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3148 return;
3149 case WINED3D_DATA_RESOURCE:
3150 case WINED3D_DATA_SAMPLER:
3151 case WINED3D_DATA_UINT:
3152 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3153 return;
3154 default:
3155 break;
3159 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3161 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3162 return;
3165 if (src_data_type == WINED3D_DATA_INT)
3167 switch (dst_data_type)
3169 case WINED3D_DATA_FLOAT:
3170 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3171 return;
3172 case WINED3D_DATA_UINT:
3173 if (size == 1)
3174 string_buffer_sprintf(dst_param, "uint(%s)", src_param);
3175 else
3176 string_buffer_sprintf(dst_param, "uvec%u(%s)", size, src_param);
3177 return;
3178 default:
3179 break;
3183 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3184 string_buffer_sprintf(dst_param, "%s", src_param);
3187 /* From a given parameter token, generate the corresponding GLSL string.
3188 * Also, return the actual register name and swizzle in case the
3189 * caller needs this information as well. */
3190 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
3191 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3192 enum wined3d_data_type data_type)
3194 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3195 struct wined3d_string_buffer *param_str = string_buffer_get(priv->string_buffers);
3196 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3197 enum wined3d_data_type param_data_type;
3198 BOOL is_color = FALSE;
3199 char swizzle_str[6];
3200 unsigned int size;
3202 glsl_src->param_str[0] = '\0';
3203 swizzle_str[0] = '\0';
3205 shader_glsl_get_register_name(&wined3d_src->reg, data_type, reg_name, &is_color, ctx);
3206 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3208 switch (wined3d_src->reg.type)
3210 case WINED3DSPR_IMMCONST:
3211 param_data_type = data_type;
3212 size = wined3d_src->reg.immconst_type == WINED3D_IMMCONST_SCALAR ? 1 : 4;
3213 break;
3214 case WINED3DSPR_FORKINSTID:
3215 case WINED3DSPR_GSINSTID:
3216 case WINED3DSPR_JOININSTID:
3217 case WINED3DSPR_LOCALTHREADINDEX:
3218 case WINED3DSPR_OUTPOINTID:
3219 case WINED3DSPR_PRIMID:
3220 param_data_type = WINED3D_DATA_INT;
3221 size = 1;
3222 break;
3223 case WINED3DSPR_LOCALTHREADID:
3224 case WINED3DSPR_THREADGROUPID:
3225 case WINED3DSPR_THREADID:
3226 param_data_type = WINED3D_DATA_INT;
3227 size = 3;
3228 break;
3229 default:
3230 param_data_type = WINED3D_DATA_FLOAT;
3231 size = 4;
3232 break;
3235 shader_glsl_sprintf_cast(param_str, reg_name->buffer, data_type, param_data_type, size);
3236 shader_glsl_gen_modifier(wined3d_src->modifiers, param_str->buffer, swizzle_str, glsl_src->param_str);
3238 string_buffer_release(priv->string_buffers, reg_name);
3239 string_buffer_release(priv->string_buffers, param_str);
3242 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3243 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3245 shader_glsl_add_src_param_ext(ins->ctx, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3248 /* From a given parameter token, generate the corresponding GLSL string.
3249 * Also, return the actual register name and swizzle in case the
3250 * caller needs this information as well. */
3251 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3252 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3254 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3255 struct wined3d_string_buffer *reg_name;
3256 size_t len;
3258 glsl_dst->mask_str[0] = '\0';
3260 reg_name = string_buffer_get(priv->string_buffers);
3261 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type, reg_name, NULL, ins->ctx);
3262 len = min(reg_name->content_size, ARRAY_SIZE(glsl_dst->reg_name) - 1);
3263 memcpy(glsl_dst->reg_name, reg_name->buffer, len);
3264 glsl_dst->reg_name[len] = '\0';
3265 string_buffer_release(priv->string_buffers, reg_name);
3267 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3270 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3271 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3272 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3273 enum wined3d_data_type data_type)
3275 struct glsl_dst_param glsl_dst;
3276 DWORD mask;
3278 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3280 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3281 sprintf(glsl_dst.reg_name, "tmp_precise");
3283 switch (data_type)
3285 case WINED3D_DATA_FLOAT:
3286 case WINED3D_DATA_UNORM:
3287 case WINED3D_DATA_SNORM:
3288 shader_addline(buffer, "%s%s = %s(",
3289 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3290 break;
3291 case WINED3D_DATA_INT:
3292 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3293 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3294 break;
3295 case WINED3D_DATA_RESOURCE:
3296 case WINED3D_DATA_SAMPLER:
3297 case WINED3D_DATA_UINT:
3298 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3299 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3300 break;
3301 default:
3302 FIXME("Unhandled data type %#x.\n", data_type);
3303 shader_addline(buffer, "%s%s = %s(",
3304 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3305 break;
3309 return mask;
3312 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3313 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3315 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3318 /** Process GLSL instruction modifiers */
3319 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3321 struct glsl_dst_param dst_param;
3322 DWORD modifiers;
3324 if (!ins->dst_count) return;
3326 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3328 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3329 shader_addline(ins->ctx->buffer, "%s%s = tmp_precise%s;\n",
3330 dst_param.reg_name, dst_param.mask_str, dst_param.mask_str);
3333 modifiers = ins->dst[0].modifiers;
3334 if (!modifiers) return;
3336 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3338 if (modifiers & WINED3DSPDM_SATURATE)
3340 /* _SAT means to clamp the value of the register to between 0 and 1 */
3341 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3342 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3345 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3347 FIXME("_centroid modifier not handled\n");
3350 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3352 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3356 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3358 switch (op)
3360 case WINED3D_SHADER_REL_OP_GT: return ">";
3361 case WINED3D_SHADER_REL_OP_EQ: return "==";
3362 case WINED3D_SHADER_REL_OP_GE: return ">=";
3363 case WINED3D_SHADER_REL_OP_LT: return "<";
3364 case WINED3D_SHADER_REL_OP_NE: return "!=";
3365 case WINED3D_SHADER_REL_OP_LE: return "<=";
3366 default:
3367 FIXME("Unrecognized operator %#x.\n", op);
3368 return "(\?\?)";
3372 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3374 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3377 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3378 unsigned int *coord_size, unsigned int *deriv_size)
3380 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3381 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3383 *coord_size = resource_type_info[resource_type].coord_size;
3384 *deriv_size = *coord_size;
3385 if (is_array)
3386 --(*deriv_size);
3389 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3390 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3392 enum wined3d_shader_resource_type resource_type;
3393 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3394 const struct wined3d_gl_info *gl_info = priv->gl_info;
3395 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3396 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3397 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3398 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3399 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3400 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3401 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3402 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3403 const char *base = "texture", *type_part = "", *suffix = "";
3404 unsigned int coord_size, deriv_size;
3406 resource_type = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3407 ? pixelshader_get_resource_type(ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
3408 : ctx->reg_maps->resource_info[resource_idx].type;
3410 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3412 if (resource_type >= ARRAY_SIZE(resource_type_info))
3414 ERR("Unexpected resource type %#x.\n", resource_type);
3415 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3418 /* Note that there's no such thing as a projected cube texture. */
3419 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3420 projected = FALSE;
3422 if (needs_legacy_glsl_syntax(gl_info))
3424 if (shadow)
3425 base = "shadow";
3427 type_part = resource_type_info[resource_type].type_part;
3428 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3429 type_part = "2DRect";
3430 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3431 FIXME("Unhandled resource type %#x.\n", resource_type);
3433 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3435 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3436 suffix = "ARB";
3437 else
3438 FIXME("Unsupported grad function.\n");
3442 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3444 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3445 if (flags & ~texel_fetch_flags)
3446 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3448 base = "texelFetch";
3449 type_part = "";
3452 sample_function->name = string_buffer_get(priv->string_buffers);
3453 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3454 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3456 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3457 if (shadow)
3458 ++coord_size;
3459 sample_function->offset_size = offset ? deriv_size : 0;
3460 sample_function->coord_mask = (1u << coord_size) - 1;
3461 sample_function->deriv_mask = (1u << deriv_size) - 1;
3462 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3465 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3466 struct glsl_sample_function *sample_function)
3468 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3470 string_buffer_release(priv->string_buffers, sample_function->name);
3473 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3474 BOOL sign_fixup, enum fixup_channel_source channel_source)
3476 switch(channel_source)
3478 case CHANNEL_SOURCE_ZERO:
3479 strcat(arguments, "0.0");
3480 break;
3482 case CHANNEL_SOURCE_ONE:
3483 strcat(arguments, "1.0");
3484 break;
3486 case CHANNEL_SOURCE_X:
3487 strcat(arguments, reg_name);
3488 strcat(arguments, ".x");
3489 break;
3491 case CHANNEL_SOURCE_Y:
3492 strcat(arguments, reg_name);
3493 strcat(arguments, ".y");
3494 break;
3496 case CHANNEL_SOURCE_Z:
3497 strcat(arguments, reg_name);
3498 strcat(arguments, ".z");
3499 break;
3501 case CHANNEL_SOURCE_W:
3502 strcat(arguments, reg_name);
3503 strcat(arguments, ".w");
3504 break;
3506 default:
3507 FIXME("Unhandled channel source %#x\n", channel_source);
3508 strcat(arguments, "undefined");
3509 break;
3512 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3515 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3516 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3518 unsigned int mask_size, remaining;
3519 DWORD fixup_mask = 0;
3520 char arguments[256];
3521 char mask_str[6];
3523 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3524 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3525 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3526 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3527 if (!(mask &= fixup_mask))
3528 return;
3530 if (is_complex_fixup(fixup))
3532 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3533 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3534 return;
3537 shader_glsl_write_mask_to_str(mask, mask_str);
3538 mask_size = shader_glsl_get_write_mask_size(mask);
3540 arguments[0] = '\0';
3541 remaining = mask_size;
3542 if (mask & WINED3DSP_WRITEMASK_0)
3544 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3545 if (--remaining) strcat(arguments, ", ");
3547 if (mask & WINED3DSP_WRITEMASK_1)
3549 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3550 if (--remaining) strcat(arguments, ", ");
3552 if (mask & WINED3DSP_WRITEMASK_2)
3554 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3555 if (--remaining) strcat(arguments, ", ");
3557 if (mask & WINED3DSP_WRITEMASK_3)
3559 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3560 if (--remaining) strcat(arguments, ", ");
3563 if (mask_size > 1)
3564 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3565 else
3566 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3569 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3571 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3572 struct wined3d_string_buffer *reg_name;
3574 reg_name = string_buffer_get(priv->string_buffers);
3575 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, NULL, ins->ctx);
3576 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name->buffer, ins->dst[0].write_mask, fixup);
3577 string_buffer_release(priv->string_buffers, reg_name);
3580 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3581 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3582 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3583 const char *coord_reg_fmt, ...)
3585 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3586 char dst_swizzle[6];
3587 struct color_fixup_desc fixup;
3588 BOOL np2_fixup = FALSE;
3589 va_list args;
3590 int ret;
3592 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3594 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3595 * We actually rely on it for vertex shaders and SM4+. */
3596 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3598 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3599 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3601 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3602 np2_fixup = TRUE;
3604 else
3606 fixup = COLOR_FIXUP_IDENTITY;
3609 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3611 if (sample_function->output_single_component)
3612 shader_addline(ins->ctx->buffer, "vec4(");
3614 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3615 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3617 for (;;)
3619 va_start(args, coord_reg_fmt);
3620 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3621 va_end(args);
3622 if (!ret)
3623 break;
3624 if (!string_buffer_resize(ins->ctx->buffer, ret))
3625 break;
3628 if (np2_fixup)
3630 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3631 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3633 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3635 case 1:
3636 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3637 idx >> 1, (idx % 2) ? "z" : "x");
3638 break;
3639 case 2:
3640 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3641 idx >> 1, (idx % 2) ? "zw" : "xy");
3642 break;
3643 case 3:
3644 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3645 idx >> 1, (idx % 2) ? "zw" : "xy");
3646 break;
3647 case 4:
3648 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3649 idx >> 1, (idx % 2) ? "zw" : "xy");
3650 break;
3653 if (dx && dy)
3654 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3655 else if (bias)
3656 shader_addline(ins->ctx->buffer, ", %s", bias);
3657 if (sample_function->offset_size)
3659 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3660 shader_addline(ins->ctx->buffer, ", ");
3661 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3663 shader_addline(ins->ctx->buffer, ")");
3665 if (sample_function->output_single_component)
3666 shader_addline(ins->ctx->buffer, ")");
3668 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3670 if (!is_identity_fixup(fixup))
3671 shader_glsl_color_correction(ins, fixup);
3674 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3676 /* Write the final position.
3678 * OpenGL coordinates specify the center of the pixel while D3D coords
3679 * specify the corner. The offsets are stored in z and w in
3680 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3681 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3682 * a MAD. */
3683 if (use_viewport_index)
3685 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3686 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3688 else
3690 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3691 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3694 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3695 * in utils.c
3697 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3698 * shaders are run before the homogeneous divide, so we have to take the w
3699 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3700 * z = z * 2 - w. */
3701 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3704 /*****************************************************************************
3705 * Begin processing individual instruction opcodes
3706 ****************************************************************************/
3708 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3710 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3711 struct glsl_src_param src0_param;
3712 struct glsl_src_param src1_param;
3713 DWORD write_mask;
3714 const char *op;
3716 /* Determine the GLSL operator to use based on the opcode */
3717 switch (ins->handler_idx)
3719 case WINED3DSIH_ADD: op = "+"; break;
3720 case WINED3DSIH_AND: op = "&"; break;
3721 case WINED3DSIH_DIV: op = "/"; break;
3722 case WINED3DSIH_IADD: op = "+"; break;
3723 case WINED3DSIH_ISHL: op = "<<"; break;
3724 case WINED3DSIH_ISHR: op = ">>"; break;
3725 case WINED3DSIH_MUL: op = "*"; break;
3726 case WINED3DSIH_OR: op = "|"; break;
3727 case WINED3DSIH_SUB: op = "-"; break;
3728 case WINED3DSIH_USHR: op = ">>"; break;
3729 case WINED3DSIH_XOR: op = "^"; break;
3730 default:
3731 op = "<unhandled operator>";
3732 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3733 break;
3736 write_mask = shader_glsl_append_dst(buffer, ins);
3737 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3738 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3739 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3742 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3744 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3745 struct glsl_src_param src0_param;
3746 struct glsl_src_param src1_param;
3747 unsigned int mask_size;
3748 DWORD write_mask;
3749 const char *op;
3751 write_mask = shader_glsl_append_dst(buffer, ins);
3752 mask_size = shader_glsl_get_write_mask_size(write_mask);
3753 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3754 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3756 if (mask_size > 1)
3758 switch (ins->handler_idx)
3760 case WINED3DSIH_EQ: op = "equal"; break;
3761 case WINED3DSIH_IEQ: op = "equal"; break;
3762 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3763 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3764 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3765 case WINED3DSIH_LT: op = "lessThan"; break;
3766 case WINED3DSIH_ILT: op = "lessThan"; break;
3767 case WINED3DSIH_ULT: op = "lessThan"; break;
3768 case WINED3DSIH_NE: op = "notEqual"; break;
3769 case WINED3DSIH_INE: op = "notEqual"; break;
3770 default:
3771 op = "<unhandled operator>";
3772 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3773 break;
3776 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3777 mask_size, op, src0_param.param_str, src1_param.param_str);
3779 else
3781 switch (ins->handler_idx)
3783 case WINED3DSIH_EQ: op = "=="; break;
3784 case WINED3DSIH_IEQ: op = "=="; break;
3785 case WINED3DSIH_GE: op = ">="; break;
3786 case WINED3DSIH_IGE: op = ">="; break;
3787 case WINED3DSIH_UGE: op = ">="; break;
3788 case WINED3DSIH_LT: op = "<"; break;
3789 case WINED3DSIH_ILT: op = "<"; break;
3790 case WINED3DSIH_ULT: op = "<"; break;
3791 case WINED3DSIH_NE: op = "!="; break;
3792 case WINED3DSIH_INE: op = "!="; break;
3793 default:
3794 op = "<unhandled operator>";
3795 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3796 break;
3799 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3800 src0_param.param_str, op, src1_param.param_str);
3804 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3806 struct glsl_src_param src_param;
3807 DWORD write_mask;
3808 const char *op;
3810 switch (ins->handler_idx)
3812 case WINED3DSIH_INEG: op = "-"; break;
3813 case WINED3DSIH_NOT: op = "~"; break;
3814 default:
3815 op = "<unhandled operator>";
3816 ERR("Unhandled opcode %s.\n",
3817 debug_d3dshaderinstructionhandler(ins->handler_idx));
3818 break;
3821 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3822 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3823 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3826 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3828 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3829 struct glsl_src_param src0_param;
3830 struct glsl_src_param src1_param;
3831 DWORD write_mask;
3833 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3834 * If not, we can emulate it. */
3835 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3836 FIXME("64-bit integer multiplies not implemented.\n");
3838 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3840 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3841 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3842 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3844 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3845 src0_param.param_str, src1_param.param_str);
3849 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3851 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3852 struct glsl_src_param src0_param, src1_param;
3853 DWORD write_mask;
3855 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3857 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3859 char dst_mask[6];
3861 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3862 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3863 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3864 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3865 dst_mask, src0_param.param_str, src1_param.param_str);
3867 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3868 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3869 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3870 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3872 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3873 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3875 else
3877 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3878 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3879 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3880 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3883 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3885 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3886 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3887 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3888 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3892 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3893 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3895 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3896 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3897 const struct wined3d_gl_info *gl_info = priv->gl_info;
3898 struct glsl_src_param src0_param;
3899 DWORD write_mask;
3901 write_mask = shader_glsl_append_dst(buffer, ins);
3902 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3904 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3905 * shader versions WINED3DSIO_MOVA is used for this. */
3906 if (ins->ctx->reg_maps->shader_version.major == 1
3907 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3908 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3910 /* This is a simple floor() */
3911 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3912 if (mask_size > 1) {
3913 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3914 } else {
3915 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3918 else if (ins->handler_idx == WINED3DSIH_MOVA)
3920 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3922 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3924 if (mask_size > 1)
3925 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3926 else
3927 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3929 else
3931 if (mask_size > 1)
3932 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3933 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3934 else
3935 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3936 src0_param.param_str, src0_param.param_str);
3939 else
3941 shader_addline(buffer, "%s);\n", src0_param.param_str);
3945 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3946 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3948 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3949 struct glsl_src_param src0_param;
3950 struct glsl_src_param src1_param;
3951 DWORD dst_write_mask, src_write_mask;
3952 unsigned int dst_size;
3954 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3955 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3957 /* dp4 works on vec4, dp3 on vec3, etc. */
3958 if (ins->handler_idx == WINED3DSIH_DP4)
3959 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3960 else if (ins->handler_idx == WINED3DSIH_DP3)
3961 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3962 else
3963 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3965 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3966 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3968 if (dst_size > 1) {
3969 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3970 } else {
3971 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3975 /* Note that this instruction has some restrictions. The destination write mask
3976 * can't contain the w component, and the source swizzles have to be .xyzw */
3977 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3979 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3980 struct glsl_src_param src0_param;
3981 struct glsl_src_param src1_param;
3982 char dst_mask[6];
3984 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3985 shader_glsl_append_dst(ins->ctx->buffer, ins);
3986 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3987 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3988 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3991 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3993 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3995 if (!stream)
3996 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3997 else
3998 FIXME("Unhandled primitive stream %u.\n", stream);
4001 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4002 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4003 * GLSL uses the value as-is. */
4004 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4006 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4007 struct glsl_src_param src0_param;
4008 struct glsl_src_param src1_param;
4009 DWORD dst_write_mask;
4010 unsigned int dst_size;
4012 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4013 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4015 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4016 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4018 if (dst_size > 1)
4020 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4021 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4023 else
4025 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4026 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4030 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4031 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4033 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4034 struct glsl_src_param src_param;
4035 const char *instruction;
4036 DWORD write_mask;
4037 unsigned i;
4039 /* Determine the GLSL function to use based on the opcode */
4040 /* TODO: Possibly make this a table for faster lookups */
4041 switch (ins->handler_idx)
4043 case WINED3DSIH_ABS: instruction = "abs"; break;
4044 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4045 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4046 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4047 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4048 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4049 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4050 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4051 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4052 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4053 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4054 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4055 case WINED3DSIH_FRC: instruction = "fract"; break;
4056 case WINED3DSIH_IMAX: instruction = "max"; break;
4057 case WINED3DSIH_IMIN: instruction = "min"; break;
4058 case WINED3DSIH_MAX: instruction = "max"; break;
4059 case WINED3DSIH_MIN: instruction = "min"; break;
4060 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4061 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4062 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4063 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4064 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4065 case WINED3DSIH_UMAX: instruction = "max"; break;
4066 case WINED3DSIH_UMIN: instruction = "min"; break;
4067 default: instruction = "";
4068 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4069 break;
4072 write_mask = shader_glsl_append_dst(buffer, ins);
4074 /* In D3D bits are numbered from the most significant bit. */
4075 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4076 shader_addline(buffer, "31 - ");
4077 shader_addline(buffer, "%s(", instruction);
4079 if (ins->src_count)
4081 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4082 shader_addline(buffer, "%s", src_param.param_str);
4083 for (i = 1; i < ins->src_count; ++i)
4085 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4086 shader_addline(buffer, ", %s", src_param.param_str);
4090 shader_addline(buffer, "));\n");
4093 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4095 struct wined3d_shader_dst_param dst;
4096 struct glsl_src_param src;
4097 DWORD write_mask;
4098 const char *fmt;
4099 unsigned int i;
4101 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4102 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4104 dst = ins->dst[0];
4105 for (i = 0; i < 4; ++i)
4107 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4108 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4109 &dst, dst.reg.data_type)))
4110 continue;
4112 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4113 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4117 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4119 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4120 struct wined3d_shader_dst_param dst;
4121 struct glsl_src_param src[4];
4122 const char *instruction;
4123 BOOL tmp_dst = FALSE;
4124 char mask_char[6];
4125 unsigned int i, j;
4126 DWORD write_mask;
4128 switch (ins->handler_idx)
4130 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4131 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4132 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4133 default:
4134 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4135 return;
4138 for (i = 0; i < ins->src_count; ++i)
4140 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4141 && ins->dst[0].reg.type == ins->src[i].reg.type)
4142 tmp_dst = TRUE;
4145 dst = ins->dst[0];
4146 for (i = 0; i < 4; ++i)
4148 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4149 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4150 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4151 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4152 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4153 continue;
4155 for (j = 0; j < ins->src_count; ++j)
4156 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4157 shader_addline(buffer, "%s(", instruction);
4158 for (j = 0; j < ins->src_count - 2; ++j)
4159 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4160 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4163 if (tmp_dst)
4165 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4166 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4167 shader_addline(buffer, "tmp0%s);\n", mask_char);
4171 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4173 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4175 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4176 struct glsl_src_param src_param;
4177 unsigned int mask_size;
4178 DWORD write_mask;
4179 char dst_mask[6];
4181 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4182 mask_size = shader_glsl_get_write_mask_size(write_mask);
4183 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4185 if (mask_size > 3)
4186 shader_addline(buffer, "tmp0.x = dot(vec3(%s), vec3(%s));\n",
4187 src_param.param_str, src_param.param_str);
4188 else
4189 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4190 src_param.param_str, src_param.param_str);
4191 shader_glsl_append_dst(buffer, ins);
4193 shader_addline(buffer, "tmp0.x == 0.0 ? %s : (%s * inversesqrt(tmp0.x)));\n",
4194 src_param.param_str, src_param.param_str);
4197 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4199 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4200 ins->ctx->reg_maps->shader_version.minor);
4201 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4202 struct glsl_src_param src0_param;
4203 const char *prefix, *suffix;
4204 unsigned int dst_size;
4205 DWORD dst_write_mask;
4207 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4208 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4210 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4211 dst_write_mask = WINED3DSP_WRITEMASK_3;
4213 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4215 switch (ins->handler_idx)
4217 case WINED3DSIH_EXP:
4218 case WINED3DSIH_EXPP:
4219 prefix = "exp2(";
4220 suffix = ")";
4221 break;
4223 case WINED3DSIH_LOG:
4224 case WINED3DSIH_LOGP:
4225 prefix = "log2(abs(";
4226 suffix = "))";
4227 break;
4229 case WINED3DSIH_RCP:
4230 prefix = "1.0 / ";
4231 suffix = "";
4232 break;
4234 case WINED3DSIH_RSQ:
4235 prefix = "inversesqrt(abs(";
4236 suffix = "))";
4237 break;
4239 default:
4240 prefix = "";
4241 suffix = "";
4242 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4243 break;
4246 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4247 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4248 else
4249 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4252 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4253 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4254 * dst.x = 2^(floor(src))
4255 * dst.y = src - floor(src)
4256 * dst.z = 2^src (partial precision is allowed, but optional)
4257 * dst.w = 1.0;
4258 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4259 * dst = 2^src; (partial precision is allowed, but optional)
4261 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4263 if (ins->ctx->reg_maps->shader_version.major < 2)
4265 struct glsl_src_param src_param;
4266 char dst_mask[6];
4268 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4270 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4271 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4272 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4273 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4275 shader_glsl_append_dst(ins->ctx->buffer, ins);
4276 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4277 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4278 return;
4281 shader_glsl_scalar_op(ins);
4284 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4285 const char *vector_constructor, const char *scalar_constructor)
4287 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4288 struct glsl_src_param src_param;
4289 unsigned int mask_size;
4290 DWORD write_mask;
4292 write_mask = shader_glsl_append_dst(buffer, ins);
4293 mask_size = shader_glsl_get_write_mask_size(write_mask);
4294 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4296 if (mask_size > 1)
4297 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4298 else
4299 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4302 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4304 shader_glsl_cast(ins, "ivec", "int");
4307 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4309 shader_glsl_cast(ins, "uvec", "uint");
4312 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4314 shader_glsl_cast(ins, "vec", "float");
4317 /** Process signed comparison opcodes in GLSL. */
4318 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4320 struct glsl_src_param src0_param;
4321 struct glsl_src_param src1_param;
4322 DWORD write_mask;
4323 unsigned int mask_size;
4325 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4326 mask_size = shader_glsl_get_write_mask_size(write_mask);
4327 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4328 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4330 if (mask_size > 1) {
4331 const char *compare;
4333 switch(ins->handler_idx)
4335 case WINED3DSIH_SLT: compare = "lessThan"; break;
4336 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4337 default: compare = "";
4338 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4341 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4342 src0_param.param_str, src1_param.param_str);
4343 } else {
4344 switch(ins->handler_idx)
4346 case WINED3DSIH_SLT:
4347 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4348 * to return 0.0 but step returns 1.0 because step is not < x
4349 * An alternative is a bvec compare padded with an unused second component.
4350 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4351 * issue. Playing with not() is not possible either because not() does not accept
4352 * a scalar.
4354 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4355 src0_param.param_str, src1_param.param_str);
4356 break;
4357 case WINED3DSIH_SGE:
4358 /* Here we can use the step() function and safe a conditional */
4359 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4360 break;
4361 default:
4362 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4368 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4370 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4371 struct wined3d_shader_dst_param dst[2];
4372 struct glsl_src_param src[3];
4373 unsigned int i, j, k;
4374 char mask_char[6];
4375 DWORD write_mask;
4376 BOOL tmp_dst[2];
4378 for (i = 0; i < ins->dst_count; ++i)
4380 tmp_dst[i] = FALSE;
4381 for (j = 0; j < ins->src_count; ++j)
4383 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4384 && ins->dst[i].reg.type == ins->src[j].reg.type)
4385 tmp_dst[i] = TRUE;
4389 dst[0] = ins->dst[0];
4390 dst[1] = ins->dst[1];
4391 for (i = 0; i < 4; ++i)
4393 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4395 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4396 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4397 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4398 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4399 continue;
4401 for (k = 0; k < ARRAY_SIZE(src); ++k)
4402 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4404 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4405 src[0].param_str, src[1].param_str, src[2].param_str);
4409 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4411 if (tmp_dst[i])
4413 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4414 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4415 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4420 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4422 const char *condition_prefix, *condition_suffix;
4423 struct wined3d_shader_dst_param dst;
4424 struct glsl_src_param src0_param;
4425 struct glsl_src_param src1_param;
4426 struct glsl_src_param src2_param;
4427 BOOL temp_destination = FALSE;
4428 DWORD cmp_channel = 0;
4429 unsigned int i, j;
4430 char mask_char[6];
4431 DWORD write_mask;
4433 switch (ins->handler_idx)
4435 case WINED3DSIH_CMP:
4436 condition_prefix = "";
4437 condition_suffix = " >= 0.0";
4438 break;
4440 case WINED3DSIH_CND:
4441 condition_prefix = "";
4442 condition_suffix = " > 0.5";
4443 break;
4445 case WINED3DSIH_MOVC:
4446 condition_prefix = "bool(";
4447 condition_suffix = ")";
4448 break;
4450 default:
4451 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4452 condition_prefix = "<unhandled prefix>";
4453 condition_suffix = "<unhandled suffix>";
4454 break;
4457 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4459 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4460 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4461 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4462 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4464 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4465 condition_prefix, src0_param.param_str, condition_suffix,
4466 src1_param.param_str, src2_param.param_str);
4467 return;
4470 dst = ins->dst[0];
4472 /* Splitting the instruction up in multiple lines imposes a problem:
4473 * The first lines may overwrite source parameters of the following lines.
4474 * Deal with that by using a temporary destination register if needed. */
4475 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4476 && ins->src[0].reg.type == dst.reg.type)
4477 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4478 && ins->src[1].reg.type == dst.reg.type)
4479 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4480 && ins->src[2].reg.type == dst.reg.type))
4481 temp_destination = TRUE;
4483 /* Cycle through all source0 channels. */
4484 for (i = 0; i < 4; ++i)
4486 write_mask = 0;
4487 /* Find the destination channels which use the current source0 channel. */
4488 for (j = 0; j < 4; ++j)
4490 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4492 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4493 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4496 dst.write_mask = ins->dst[0].write_mask & write_mask;
4498 if (temp_destination)
4500 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4501 continue;
4502 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4504 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4505 continue;
4507 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4508 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4509 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4511 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4512 condition_prefix, src0_param.param_str, condition_suffix,
4513 src1_param.param_str, src2_param.param_str);
4516 if (temp_destination)
4518 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4519 shader_glsl_append_dst(ins->ctx->buffer, ins);
4520 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4524 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4525 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4526 * the compare is done per component of src0. */
4527 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4529 struct glsl_src_param src0_param;
4530 struct glsl_src_param src1_param;
4531 struct glsl_src_param src2_param;
4532 DWORD write_mask;
4533 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4534 ins->ctx->reg_maps->shader_version.minor);
4536 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4538 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4539 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4540 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4541 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4543 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4544 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4545 else
4546 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4547 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4548 return;
4551 shader_glsl_conditional_move(ins);
4554 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4555 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4557 struct glsl_src_param src0_param;
4558 struct glsl_src_param src1_param;
4559 struct glsl_src_param src2_param;
4560 DWORD write_mask;
4562 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4563 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4564 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4565 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4566 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4567 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4570 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4571 Vertex shaders to GLSL codes */
4572 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4574 int i;
4575 int nComponents = 0;
4576 struct wined3d_shader_dst_param tmp_dst = {{0}};
4577 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4578 struct wined3d_shader_instruction tmp_ins;
4580 memset(&tmp_ins, 0, sizeof(tmp_ins));
4582 /* Set constants for the temporary argument */
4583 tmp_ins.ctx = ins->ctx;
4584 tmp_ins.dst_count = 1;
4585 tmp_ins.dst = &tmp_dst;
4586 tmp_ins.src_count = 2;
4587 tmp_ins.src = tmp_src;
4589 switch(ins->handler_idx)
4591 case WINED3DSIH_M4x4:
4592 nComponents = 4;
4593 tmp_ins.handler_idx = WINED3DSIH_DP4;
4594 break;
4595 case WINED3DSIH_M4x3:
4596 nComponents = 3;
4597 tmp_ins.handler_idx = WINED3DSIH_DP4;
4598 break;
4599 case WINED3DSIH_M3x4:
4600 nComponents = 4;
4601 tmp_ins.handler_idx = WINED3DSIH_DP3;
4602 break;
4603 case WINED3DSIH_M3x3:
4604 nComponents = 3;
4605 tmp_ins.handler_idx = WINED3DSIH_DP3;
4606 break;
4607 case WINED3DSIH_M3x2:
4608 nComponents = 2;
4609 tmp_ins.handler_idx = WINED3DSIH_DP3;
4610 break;
4611 default:
4612 break;
4615 tmp_dst = ins->dst[0];
4616 tmp_src[0] = ins->src[0];
4617 tmp_src[1] = ins->src[1];
4618 for (i = 0; i < nComponents; ++i)
4620 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4621 shader_glsl_dot(&tmp_ins);
4622 ++tmp_src[1].reg.idx[0].offset;
4627 The LRP instruction performs a component-wise linear interpolation
4628 between the second and third operands using the first operand as the
4629 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4630 This is equivalent to mix(src2, src1, src0);
4632 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4634 struct glsl_src_param src0_param;
4635 struct glsl_src_param src1_param;
4636 struct glsl_src_param src2_param;
4637 DWORD write_mask;
4639 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4641 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4642 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4643 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4645 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4646 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4649 /** Process the WINED3DSIO_LIT instruction in GLSL:
4650 * dst.x = dst.w = 1.0
4651 * dst.y = (src0.x > 0) ? src0.x
4652 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4653 * where src.w is clamped at +- 128
4655 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4657 struct glsl_src_param src0_param;
4658 struct glsl_src_param src1_param;
4659 struct glsl_src_param src3_param;
4660 char dst_mask[6];
4662 shader_glsl_append_dst(ins->ctx->buffer, ins);
4663 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4665 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4666 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4667 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4669 /* The sdk specifies the instruction like this
4670 * dst.x = 1.0;
4671 * if(src.x > 0.0) dst.y = src.x
4672 * else dst.y = 0.0.
4673 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4674 * else dst.z = 0.0;
4675 * dst.w = 1.0;
4676 * (where power = src.w clamped between -128 and 128)
4678 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4679 * dst.x = 1.0 ... No further explanation needed
4680 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4681 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4682 * dst.w = 1.0. ... Nothing fancy.
4684 * So we still have one conditional in there. So do this:
4685 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4687 * 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),
4688 * 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.
4689 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4691 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4692 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4693 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4695 shader_addline(ins->ctx->buffer,
4696 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4697 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4698 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4699 src0_param.param_str, src3_param.param_str, dst_mask);
4702 /** Process the WINED3DSIO_DST instruction in GLSL:
4703 * dst.x = 1.0
4704 * dst.y = src0.x * src0.y
4705 * dst.z = src0.z
4706 * dst.w = src1.w
4708 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4710 struct glsl_src_param src0y_param;
4711 struct glsl_src_param src0z_param;
4712 struct glsl_src_param src1y_param;
4713 struct glsl_src_param src1w_param;
4714 char dst_mask[6];
4716 shader_glsl_append_dst(ins->ctx->buffer, ins);
4717 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4719 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4720 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4721 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4722 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4724 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4725 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4728 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4729 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4730 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4732 * dst.x = cos(src0.?)
4733 * dst.y = sin(src0.?)
4734 * dst.z = dst.z
4735 * dst.w = dst.w
4737 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4739 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4740 struct glsl_src_param src0_param;
4741 DWORD write_mask;
4743 if (ins->ctx->reg_maps->shader_version.major < 4)
4745 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4747 write_mask = shader_glsl_append_dst(buffer, ins);
4748 switch (write_mask)
4750 case WINED3DSP_WRITEMASK_0:
4751 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4752 break;
4754 case WINED3DSP_WRITEMASK_1:
4755 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4756 break;
4758 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4759 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4760 src0_param.param_str, src0_param.param_str);
4761 break;
4763 default:
4764 ERR("Write mask should be .x, .y or .xy\n");
4765 break;
4768 return;
4771 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4774 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4776 char dst_mask[6];
4778 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4779 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4780 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4782 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4783 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4784 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4786 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4787 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4789 else
4791 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4792 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4793 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4796 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4798 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4799 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4800 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4804 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4805 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4806 * generate invalid code
4808 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4810 struct glsl_src_param src0_param;
4811 DWORD write_mask;
4813 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4814 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4816 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4819 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4820 * Start a for() loop where src1.y is the initial value of aL,
4821 * increment aL by src1.z for a total of src1.x iterations.
4822 * Need to use a temporary variable for this operation.
4824 /* FIXME: I don't think nested loops will work correctly this way. */
4825 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4827 struct wined3d_shader_parser_state *state = ins->ctx->state;
4828 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4829 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4830 const struct wined3d_shader *shader = ins->ctx->shader;
4831 const struct wined3d_shader_lconst *constant;
4832 struct wined3d_string_buffer *reg_name;
4833 const DWORD *control_values = NULL;
4835 if (ins->ctx->reg_maps->shader_version.major < 4)
4837 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4838 * class hardware doesn't support real varying indexing, but Microsoft
4839 * designed this feature for Shader model 2.x+. If the loop control is
4840 * known at compile time, the GLSL compiler can unroll the loop, and
4841 * replace indirect addressing with direct addressing. */
4842 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4844 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4846 if (constant->idx == ins->src[1].reg.idx[0].offset)
4848 control_values = constant->value;
4849 break;
4854 if (control_values)
4856 struct wined3d_shader_loop_control loop_control;
4857 loop_control.count = control_values[0];
4858 loop_control.start = control_values[1];
4859 loop_control.step = (int)control_values[2];
4861 if (loop_control.step > 0)
4863 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4864 state->current_loop_depth, loop_control.start,
4865 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4866 state->current_loop_depth, loop_control.step);
4868 else if (loop_control.step < 0)
4870 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4871 state->current_loop_depth, loop_control.start,
4872 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4873 state->current_loop_depth, loop_control.step);
4875 else
4877 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4878 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4879 state->current_loop_depth, loop_control.count,
4880 state->current_loop_depth);
4883 else
4885 reg_name = string_buffer_get(priv->string_buffers);
4886 shader_glsl_get_register_name(&ins->src[1].reg, ins->src[1].reg.data_type, reg_name, NULL, ins->ctx);
4888 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4889 state->current_loop_depth, state->current_loop_reg, reg_name->buffer,
4890 state->current_loop_depth, reg_name->buffer,
4891 state->current_loop_depth, state->current_loop_reg, reg_name->buffer);
4893 string_buffer_release(priv->string_buffers, reg_name);
4897 ++state->current_loop_reg;
4899 else
4901 shader_addline(buffer, "for (;;)\n{\n");
4904 ++state->current_loop_depth;
4907 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4909 struct wined3d_shader_parser_state *state = ins->ctx->state;
4911 shader_addline(ins->ctx->buffer, "}\n");
4913 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4915 --state->current_loop_depth;
4916 --state->current_loop_reg;
4919 if (ins->handler_idx == WINED3DSIH_ENDREP)
4921 --state->current_loop_depth;
4925 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4927 struct wined3d_shader_parser_state *state = ins->ctx->state;
4928 const struct wined3d_shader *shader = ins->ctx->shader;
4929 const struct wined3d_shader_lconst *constant;
4930 struct glsl_src_param src0_param;
4931 const DWORD *control_values = NULL;
4933 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4934 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4936 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4938 if (constant->idx == ins->src[0].reg.idx[0].offset)
4940 control_values = constant->value;
4941 break;
4946 if (control_values)
4948 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4949 state->current_loop_depth, state->current_loop_depth,
4950 control_values[0], state->current_loop_depth);
4952 else
4954 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4955 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4956 state->current_loop_depth, state->current_loop_depth,
4957 src0_param.param_str, state->current_loop_depth);
4960 ++state->current_loop_depth;
4963 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4965 struct glsl_src_param src0_param;
4967 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4968 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4971 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4973 struct glsl_src_param src0_param;
4975 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4976 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4979 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4981 shader_addline(ins->ctx->buffer, "default:\n");
4984 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
4986 struct glsl_src_param src_param;
4987 const char *condition;
4989 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
4990 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4991 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
4994 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4996 shader_glsl_generate_condition(ins);
4997 shader_addline(ins->ctx->buffer, "{\n");
5000 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5002 struct glsl_src_param src0_param;
5003 struct glsl_src_param src1_param;
5005 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5006 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5008 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5009 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5012 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5014 shader_addline(ins->ctx->buffer, "} else {\n");
5017 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5019 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5020 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5021 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5023 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5024 if (!priv->gl_info->supported[ARB_CLIP_CONTROL])
5025 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5027 if (!stream)
5028 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5029 else
5030 FIXME("Unhandled primitive stream %u.\n", stream);
5033 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5035 shader_addline(ins->ctx->buffer, "break;\n");
5038 /* FIXME: According to MSDN the compare is done per component. */
5039 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5041 struct glsl_src_param src0_param;
5042 struct glsl_src_param src1_param;
5044 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5045 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5047 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5048 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5051 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5053 const char *op;
5055 switch (ins->handler_idx)
5057 case WINED3DSIH_BREAKP:
5058 op = "break;";
5059 break;
5060 case WINED3DSIH_CONTINUEP:
5061 op = "continue;";
5062 break;
5063 case WINED3DSIH_RETP:
5064 op = "return;";
5065 break;
5066 default:
5067 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5068 return;
5071 shader_glsl_generate_condition(ins);
5072 if (ins->handler_idx == WINED3DSIH_RETP)
5074 shader_addline(ins->ctx->buffer, "{\n");
5075 shader_glsl_generate_shader_epilogue(ins->ctx);
5077 shader_addline(ins->ctx->buffer, " %s\n", op);
5078 if (ins->handler_idx == WINED3DSIH_RETP)
5079 shader_addline(ins->ctx->buffer, "}\n");
5082 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5084 shader_addline(ins->ctx->buffer, "continue;\n");
5087 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5089 shader_addline(ins->ctx->buffer, "}\n");
5090 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5092 /* Subroutines appear at the end of the shader. */
5093 ins->ctx->state->in_subroutine = TRUE;
5096 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5098 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5101 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5103 struct glsl_src_param src1_param;
5105 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5106 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5107 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5110 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5112 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5114 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5116 shader_glsl_generate_shader_epilogue(ins->ctx);
5117 shader_addline(ins->ctx->buffer, "return;\n");
5121 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5123 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5124 ins->ctx->reg_maps->shader_version.minor);
5125 struct glsl_sample_function sample_function;
5126 DWORD sample_flags = 0;
5127 DWORD resource_idx;
5128 DWORD mask = 0, swizzle;
5129 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5130 enum wined3d_shader_resource_type resource_type;
5132 /* 1.0-1.4: Use destination register as sampler source.
5133 * 2.0+: Use provided sampler source. */
5134 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5135 resource_idx = ins->dst[0].reg.idx[0].offset;
5136 else
5137 resource_idx = ins->src[1].reg.idx[0].offset;
5139 resource_type = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
5140 ? pixelshader_get_resource_type(ins->ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
5141 : ins->ctx->reg_maps->resource_info[resource_idx].type;
5143 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5145 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5146 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5148 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5149 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5151 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5152 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5154 case WINED3D_TTFF_COUNT1:
5155 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5156 break;
5157 case WINED3D_TTFF_COUNT2:
5158 mask = WINED3DSP_WRITEMASK_1;
5159 break;
5160 case WINED3D_TTFF_COUNT3:
5161 mask = WINED3DSP_WRITEMASK_2;
5162 break;
5163 case WINED3D_TTFF_COUNT4:
5164 case WINED3D_TTFF_DISABLE:
5165 mask = WINED3DSP_WRITEMASK_3;
5166 break;
5170 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5172 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5174 if (src_mod == WINED3DSPSM_DZ) {
5175 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5176 mask = WINED3DSP_WRITEMASK_2;
5177 } else if (src_mod == WINED3DSPSM_DW) {
5178 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5179 mask = WINED3DSP_WRITEMASK_3;
5182 else
5184 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5185 && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5187 /* ps 2.0 texldp instruction always divides by the fourth component. */
5188 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5189 mask = WINED3DSP_WRITEMASK_3;
5193 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5194 mask |= sample_function.coord_mask;
5195 sample_function.coord_mask = mask;
5197 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5198 else swizzle = ins->src[1].swizzle;
5200 /* 1.0-1.3: Use destination register as coordinate source.
5201 1.4+: Use provided coordinate source register. */
5202 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5204 char coord_mask[6];
5205 shader_glsl_write_mask_to_str(mask, coord_mask);
5206 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5207 "T%u%s", resource_idx, coord_mask);
5209 else
5211 struct glsl_src_param coord_param;
5212 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5213 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5215 struct glsl_src_param bias;
5216 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5217 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5218 NULL, "%s", coord_param.param_str);
5219 } else {
5220 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5221 "%s", coord_param.param_str);
5224 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5227 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5229 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5230 const struct wined3d_gl_info *gl_info = priv->gl_info;
5231 struct glsl_src_param coord_param, dx_param, dy_param;
5232 struct glsl_sample_function sample_function;
5233 DWORD sampler_idx;
5234 DWORD swizzle = ins->src[1].swizzle;
5236 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5238 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5239 shader_glsl_tex(ins);
5240 return;
5243 sampler_idx = ins->src[1].reg.idx[0].offset;
5245 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5246 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5247 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5248 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5250 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5251 NULL, NULL, "%s", coord_param.param_str);
5252 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5255 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5257 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5258 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5259 const struct wined3d_gl_info *gl_info = priv->gl_info;
5260 struct glsl_src_param coord_param, lod_param;
5261 struct glsl_sample_function sample_function;
5262 DWORD swizzle = ins->src[1].swizzle;
5263 DWORD sampler_idx;
5265 sampler_idx = ins->src[1].reg.idx[0].offset;
5267 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5268 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5270 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5272 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5273 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5275 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5276 * However, the NVIDIA drivers allow them in fragment shaders as well,
5277 * even without the appropriate extension. */
5278 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5280 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5281 "%s", coord_param.param_str);
5282 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5285 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5286 unsigned int resource_idx, unsigned int sampler_idx)
5288 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5289 unsigned int i;
5291 for (i = 0; i < sampler_map->count; ++i)
5293 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5294 return entries[i].bind_idx;
5297 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5299 return ~0u;
5302 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5304 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5305 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5306 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5307 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5308 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5309 struct glsl_src_param structure_idx, offset, data, data2;
5310 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5311 enum wined3d_shader_resource_type resource_type;
5312 struct wined3d_string_buffer *address;
5313 enum wined3d_data_type data_type;
5314 unsigned int resource_idx, stride;
5315 const char *op, *resource;
5316 DWORD coord_mask;
5317 BOOL is_tgsm;
5319 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5320 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5321 if (is_tgsm)
5323 if (resource_idx >= reg_maps->tgsm_count)
5325 ERR("Invalid TGSM index %u.\n", resource_idx);
5326 return;
5328 resource = "g";
5329 data_type = WINED3D_DATA_UINT;
5330 coord_mask = 1;
5331 stride = reg_maps->tgsm[resource_idx].stride;
5333 else
5335 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5337 ERR("Invalid UAV index %u.\n", resource_idx);
5338 return;
5340 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5341 if (resource_type >= ARRAY_SIZE(resource_type_info))
5343 ERR("Unexpected resource type %#x.\n", resource_type);
5344 return;
5346 resource = "image";
5347 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5348 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5349 stride = reg_maps->uav_resource_info[resource_idx].stride;
5352 switch (ins->handler_idx)
5354 case WINED3DSIH_ATOMIC_AND:
5355 case WINED3DSIH_IMM_ATOMIC_AND:
5356 if (is_tgsm)
5357 op = "atomicAnd";
5358 else
5359 op = "imageAtomicAnd";
5360 break;
5361 case WINED3DSIH_ATOMIC_CMP_STORE:
5362 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5363 if (is_tgsm)
5364 op = "atomicCompSwap";
5365 else
5366 op = "imageAtomicCompSwap";
5367 break;
5368 case WINED3DSIH_ATOMIC_IADD:
5369 case WINED3DSIH_IMM_ATOMIC_IADD:
5370 if (is_tgsm)
5371 op = "atomicAdd";
5372 else
5373 op = "imageAtomicAdd";
5374 break;
5375 case WINED3DSIH_ATOMIC_IMAX:
5376 case WINED3DSIH_IMM_ATOMIC_IMAX:
5377 if (is_tgsm)
5378 op = "atomicMax";
5379 else
5380 op = "imageAtomicMax";
5381 if (data_type != WINED3D_DATA_INT)
5383 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5384 return;
5386 break;
5387 case WINED3DSIH_ATOMIC_IMIN:
5388 case WINED3DSIH_IMM_ATOMIC_IMIN:
5389 if (is_tgsm)
5390 op = "atomicMin";
5391 else
5392 op = "imageAtomicMin";
5393 if (data_type != WINED3D_DATA_INT)
5395 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5396 return;
5398 break;
5399 case WINED3DSIH_ATOMIC_OR:
5400 case WINED3DSIH_IMM_ATOMIC_OR:
5401 if (is_tgsm)
5402 op = "atomicOr";
5403 else
5404 op = "imageAtomicOr";
5405 break;
5406 case WINED3DSIH_ATOMIC_UMAX:
5407 case WINED3DSIH_IMM_ATOMIC_UMAX:
5408 if (is_tgsm)
5409 op = "atomicMax";
5410 else
5411 op = "imageAtomicMax";
5412 if (data_type != WINED3D_DATA_UINT)
5414 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5415 return;
5417 break;
5418 case WINED3DSIH_ATOMIC_UMIN:
5419 case WINED3DSIH_IMM_ATOMIC_UMIN:
5420 if (is_tgsm)
5421 op = "atomicMin";
5422 else
5423 op = "imageAtomicMin";
5424 if (data_type != WINED3D_DATA_UINT)
5426 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5427 return;
5429 break;
5430 case WINED3DSIH_ATOMIC_XOR:
5431 case WINED3DSIH_IMM_ATOMIC_XOR:
5432 if (is_tgsm)
5433 op = "atomicXor";
5434 else
5435 op = "imageAtomicXor";
5436 break;
5437 case WINED3DSIH_IMM_ATOMIC_EXCH:
5438 if (is_tgsm)
5439 op = "atomicExchange";
5440 else
5441 op = "imageAtomicExchange";
5442 break;
5443 default:
5444 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5445 return;
5448 address = string_buffer_get(priv->string_buffers);
5449 if (stride)
5451 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5452 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5453 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5455 else
5457 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5458 string_buffer_sprintf(address, "%s", offset.param_str);
5459 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5460 shader_addline(address, "/ 4");
5463 if (is_imm_instruction)
5464 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5466 if (is_tgsm)
5467 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5468 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5469 else
5470 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5471 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5473 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5474 shader_addline(buffer, "%s", data.param_str);
5475 if (ins->src_count >= 3)
5477 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5478 shader_addline(buffer, ", %s", data2.param_str);
5481 if (is_imm_instruction)
5482 shader_addline(buffer, ")");
5483 shader_addline(buffer, ");\n");
5485 string_buffer_release(priv->string_buffers, address);
5488 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5490 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5491 const char *op;
5493 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5494 op = "atomicCounterIncrement";
5495 else
5496 op = "atomicCounterDecrement";
5498 shader_glsl_append_dst(ins->ctx->buffer, ins);
5499 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5502 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5504 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5505 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5506 enum wined3d_shader_resource_type resource_type;
5507 struct glsl_src_param image_coord_param;
5508 enum wined3d_data_type data_type;
5509 DWORD coord_mask, write_mask;
5510 unsigned int uav_idx;
5511 char dst_swizzle[6];
5513 uav_idx = ins->src[1].reg.idx[0].offset;
5514 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5516 ERR("Invalid UAV index %u.\n", uav_idx);
5517 return;
5519 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5520 if (resource_type >= ARRAY_SIZE(resource_type_info))
5522 ERR("Unexpected resource type %#x.\n", resource_type);
5523 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5525 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5526 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5528 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5529 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5531 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5532 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5533 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5536 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5538 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5539 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5540 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5541 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5542 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5543 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5544 struct glsl_src_param structure_idx, offset;
5545 struct wined3d_string_buffer *address;
5546 struct wined3d_shader_dst_param dst;
5547 const char *function, *resource;
5549 resource_idx = src->reg.idx[0].offset;
5550 if (src->reg.type == WINED3DSPR_RESOURCE)
5552 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5554 ERR("Invalid resource index %u.\n", resource_idx);
5555 return;
5557 stride = reg_maps->resource_info[resource_idx].stride;
5558 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5559 function = "texelFetch";
5560 resource = "sampler";
5562 else if (src->reg.type == WINED3DSPR_UAV)
5564 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5566 ERR("Invalid UAV index %u.\n", resource_idx);
5567 return;
5569 stride = reg_maps->uav_resource_info[resource_idx].stride;
5570 bind_idx = resource_idx;
5571 function = "imageLoad";
5572 resource = "image";
5574 else
5576 if (resource_idx >= reg_maps->tgsm_count)
5578 ERR("Invalid TGSM index %u.\n", resource_idx);
5579 return;
5581 stride = reg_maps->tgsm[resource_idx].stride;
5582 bind_idx = resource_idx;
5583 function = NULL;
5584 resource = "g";
5587 address = string_buffer_get(priv->string_buffers);
5588 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5590 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5591 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5593 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5594 shader_addline(address, "%s / 4", offset.param_str);
5596 dst = ins->dst[0];
5597 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5599 /* The instruction is split into multiple lines. The first lines may
5600 * overwrite source parameters of the following lines. */
5601 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5602 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5605 for (i = 0; i < 4; ++i)
5607 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5608 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5609 continue;
5611 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5612 if (function)
5613 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5614 function, prefix, resource, bind_idx, address->buffer, swizzle);
5615 else
5616 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5617 prefix, resource, bind_idx, address->buffer, swizzle);
5620 string_buffer_release(priv->string_buffers, address);
5623 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5625 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5626 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5627 struct glsl_src_param image_coord_param, image_data_param;
5628 enum wined3d_shader_resource_type resource_type;
5629 enum wined3d_data_type data_type;
5630 unsigned int uav_idx;
5631 DWORD coord_mask;
5633 uav_idx = ins->dst[0].reg.idx[0].offset;
5634 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5636 ERR("Invalid UAV index %u.\n", uav_idx);
5637 return;
5639 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5640 if (resource_type >= ARRAY_SIZE(resource_type_info))
5642 ERR("Unexpected resource type %#x.\n", resource_type);
5643 return;
5645 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5646 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5648 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5649 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5650 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5651 shader_glsl_get_prefix(version->type), uav_idx,
5652 image_coord_param.param_str, image_data_param.param_str);
5655 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5657 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5658 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5659 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5660 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5661 struct glsl_src_param structure_idx, offset, data;
5662 unsigned int i, resource_idx, stride, src_idx = 0;
5663 struct wined3d_string_buffer *address;
5664 DWORD write_mask;
5665 BOOL is_tgsm;
5667 resource_idx = ins->dst[0].reg.idx[0].offset;
5668 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5669 if (is_tgsm)
5671 if (resource_idx >= reg_maps->tgsm_count)
5673 ERR("Invalid TGSM index %u.\n", resource_idx);
5674 return;
5676 stride = reg_maps->tgsm[resource_idx].stride;
5678 else
5680 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5682 ERR("Invalid UAV index %u.\n", resource_idx);
5683 return;
5685 stride = reg_maps->uav_resource_info[resource_idx].stride;
5688 address = string_buffer_get(priv->string_buffers);
5689 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5691 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5692 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5694 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5695 shader_addline(address, "%s / 4", offset.param_str);
5697 for (i = 0; i < 4; ++i)
5699 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5700 continue;
5702 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5704 if (is_tgsm)
5705 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5706 prefix, resource_idx, address->buffer, i, data.param_str);
5707 else
5708 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5709 prefix, resource_idx, address->buffer, i, data.param_str);
5712 string_buffer_release(priv->string_buffers, address);
5715 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5717 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5718 unsigned int sync_flags = ins->flags;
5720 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5722 shader_addline(buffer, "barrier();\n");
5723 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5726 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5728 shader_addline(buffer, "memoryBarrierShared();\n");
5729 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5732 if (sync_flags & WINED3DSSF_GLOBAL_UAV)
5734 shader_addline(buffer, "memoryBarrier();\n");
5735 sync_flags &= ~WINED3DSSF_GLOBAL_UAV;
5738 if (sync_flags)
5739 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5742 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5743 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5745 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5746 unsigned int idx = reg->idx[0].offset;
5748 if (reg->type == WINED3DSPR_RESOURCE)
5750 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5752 ERR("Invalid resource index %u.\n", idx);
5753 return NULL;
5755 return &reg_maps->resource_info[idx];
5758 if (reg->type == WINED3DSPR_UAV)
5760 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5762 ERR("Invalid UAV index %u.\n", idx);
5763 return NULL;
5765 return &reg_maps->uav_resource_info[idx];
5768 FIXME("Unhandled register type %#x.\n", reg->type);
5769 return NULL;
5772 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5774 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5775 const struct wined3d_shader_resource_info *resource_info;
5776 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5777 unsigned int resource_idx;
5778 char dst_swizzle[6];
5779 DWORD write_mask;
5781 write_mask = shader_glsl_append_dst(buffer, ins);
5782 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5784 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5785 return;
5786 resource_idx = ins->src[0].reg.idx[0].offset;
5788 shader_addline(buffer, "ivec2(");
5789 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5791 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5792 resource_idx, WINED3D_SAMPLER_DEFAULT);
5793 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5795 else
5797 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5799 if (resource_info->stride)
5800 shader_addline(buffer, " / %u", resource_info->stride);
5801 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5802 shader_addline(buffer, " * 4");
5803 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5806 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5808 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5809 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5812 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5814 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5817 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5819 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5820 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5821 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5822 const struct wined3d_gl_info *gl_info = priv->gl_info;
5823 enum wined3d_shader_resource_type resource_type;
5824 enum wined3d_shader_register_type reg_type;
5825 unsigned int resource_idx, bind_idx, i;
5826 enum wined3d_data_type dst_data_type;
5827 struct glsl_src_param lod_param;
5828 BOOL supports_mipmaps;
5829 char dst_swizzle[6];
5830 DWORD write_mask;
5832 dst_data_type = ins->dst[0].reg.data_type;
5833 if (ins->flags == WINED3DSI_RESINFO_UINT)
5834 dst_data_type = WINED3D_DATA_UINT;
5835 else if (ins->flags)
5836 FIXME("Unhandled flags %#x.\n", ins->flags);
5838 reg_type = ins->src[1].reg.type;
5839 resource_idx = ins->src[1].reg.idx[0].offset;
5840 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5841 if (reg_type == WINED3DSPR_RESOURCE)
5843 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5844 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5845 resource_idx, WINED3D_SAMPLER_DEFAULT);
5847 else
5849 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5850 bind_idx = resource_idx;
5853 if (resource_type >= ARRAY_SIZE(resource_type_info))
5855 ERR("Unexpected resource type %#x.\n", resource_type);
5856 return;
5859 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], dst_data_type);
5860 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5862 if (dst_data_type == WINED3D_DATA_UINT)
5863 shader_addline(buffer, "uvec4(");
5864 else
5865 shader_addline(buffer, "vec4(");
5867 if (reg_type == WINED3DSPR_RESOURCE)
5869 shader_addline(buffer, "textureSize(%s_sampler%u",
5870 shader_glsl_get_prefix(version->type), bind_idx);
5872 else
5874 shader_addline(buffer, "imageSize(%s_image%u",
5875 shader_glsl_get_prefix(version->type), bind_idx);
5878 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5879 if (supports_mipmaps)
5880 shader_addline(buffer, ", %s", lod_param.param_str);
5881 shader_addline(buffer, "), ");
5883 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5884 shader_addline(buffer, "0, ");
5886 if (supports_mipmaps)
5888 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5890 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5891 shader_glsl_get_prefix(version->type), bind_idx);
5893 else
5895 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
5896 shader_addline(buffer, "1");
5899 else
5901 shader_addline(buffer, "1");
5904 shader_addline(buffer, ")%s);\n", dst_swizzle);
5907 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
5909 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5910 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5911 const struct wined3d_gl_info *gl_info = priv->gl_info;
5912 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5913 const struct wined3d_shader_dst_param *dst = ins->dst;
5914 const struct wined3d_shader_src_param *src = ins->src;
5915 enum wined3d_shader_resource_type resource_type;
5916 enum wined3d_data_type dst_data_type;
5917 unsigned int resource_idx, bind_idx;
5918 char dst_swizzle[6];
5919 DWORD write_mask;
5921 dst_data_type = dst->reg.data_type;
5922 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
5923 dst_data_type = WINED3D_DATA_UINT;
5924 else if (ins->flags)
5925 FIXME("Unhandled flags %#x.\n", ins->flags);
5927 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, dst_data_type);
5928 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
5930 if (dst_data_type == WINED3D_DATA_UINT)
5931 shader_addline(buffer, "uvec4(");
5932 else
5933 shader_addline(buffer, "vec4(");
5935 if (src->reg.type == WINED3DSPR_RASTERIZER)
5937 if (gl_info->supported[ARB_SAMPLE_SHADING])
5939 shader_addline(buffer, "gl_NumSamples");
5941 else
5943 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
5944 shader_addline(buffer, "1");
5947 else
5949 resource_idx = src->reg.idx[0].offset;
5950 resource_type = reg_maps->resource_info[resource_idx].type;
5951 if (resource_type >= ARRAY_SIZE(resource_type_info))
5953 ERR("Unexpected resource type %#x.\n", resource_type);
5954 return;
5956 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5958 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
5960 shader_addline(buffer, "textureSamples(%s_sampler%u)",
5961 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
5963 else
5965 FIXME("textureSamples() is not supported.\n");
5966 shader_addline(buffer, "1");
5970 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
5973 static void shader_glsl_interpolate(const struct wined3d_shader_instruction *ins)
5975 const struct wined3d_shader_src_param *input = &ins->src[0];
5976 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5977 struct glsl_src_param sample_param;
5978 char dst_swizzle[6];
5979 DWORD write_mask;
5981 write_mask = shader_glsl_append_dst(buffer, ins);
5982 shader_glsl_get_swizzle(input, FALSE, write_mask, dst_swizzle);
5984 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &sample_param);
5985 shader_addline(buffer, "interpolateAtSample(shader_in.reg%u, %s)%s);\n",
5986 input->reg.idx[0].offset, sample_param.param_str, dst_swizzle);
5989 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5991 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5992 struct glsl_src_param coord_param, lod_param, sample_param;
5993 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5994 struct glsl_sample_function sample_function;
5995 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5996 BOOL has_lod_param;
5998 if (wined3d_shader_instruction_has_texel_offset(ins))
5999 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6001 resource_idx = ins->src[1].reg.idx[0].offset;
6002 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6004 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6006 ERR("Invalid resource index %u.\n", resource_idx);
6007 return;
6009 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6011 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6012 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6013 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6014 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6015 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6017 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6018 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6019 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6021 else
6023 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6024 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6025 "%s", coord_param.param_str);
6027 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6030 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6032 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6033 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6034 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6035 struct glsl_sample_function sample_function;
6036 DWORD flags = 0;
6038 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6039 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6040 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6041 flags |= WINED3D_GLSL_SAMPLE_LOD;
6042 if (wined3d_shader_instruction_has_texel_offset(ins))
6043 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6045 resource_idx = ins->src[1].reg.idx[0].offset;
6046 sampler_idx = ins->src[2].reg.idx[0].offset;
6048 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6049 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6051 switch (ins->handler_idx)
6053 case WINED3DSIH_SAMPLE:
6054 break;
6055 case WINED3DSIH_SAMPLE_B:
6056 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6057 lod_param_str = lod_param.param_str;
6058 break;
6059 case WINED3DSIH_SAMPLE_GRAD:
6060 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6061 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6062 dx_param_str = dx_param.param_str;
6063 dy_param_str = dy_param.param_str;
6064 break;
6065 case WINED3DSIH_SAMPLE_LOD:
6066 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6067 lod_param_str = lod_param.param_str;
6068 break;
6069 default:
6070 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6071 break;
6074 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6075 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6076 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6077 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6080 /* Unless EXT_texture_shadow_lod is available, GLSL doesn't provide a function
6081 * to sample from level zero with depth comparison for array textures and cube
6082 * textures. We use textureGrad*() to implement sample_c_lz in that case. */
6083 static void shader_glsl_gen_sample_c_lz_emulation(const struct wined3d_shader_instruction *ins,
6084 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6085 unsigned int coord_size, const char *coord_param, const char *ref_param)
6087 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6088 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6089 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6090 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6091 char dst_swizzle[6];
6093 WARN("Emitting textureGrad() for sample_c_lz.\n");
6095 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6096 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6097 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6098 sample_function->offset_size ? "Offset" : "",
6099 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6100 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6101 if (sample_function->offset_size)
6103 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6104 shader_addline(buffer, ", ");
6105 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6107 shader_addline(buffer, "))%s);\n", dst_swizzle);
6110 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6112 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6113 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6114 const struct wined3d_shader_resource_info *resource_info;
6115 const struct wined3d_gl_info *gl_info = priv->gl_info;
6116 struct glsl_src_param coord_param, compare_param;
6117 struct glsl_sample_function sample_function;
6118 const char *lod_param = NULL;
6119 unsigned int coord_size;
6120 DWORD flags = 0;
6122 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6124 lod_param = "0";
6125 flags |= WINED3D_GLSL_SAMPLE_LOD;
6128 if (wined3d_shader_instruction_has_texel_offset(ins))
6129 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6131 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6132 return;
6133 resource_idx = ins->src[1].reg.idx[0].offset;
6134 sampler_idx = ins->src[2].reg.idx[0].offset;
6136 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6137 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6138 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6139 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6140 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6141 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6142 && !gl_info->supported[EXT_TEXTURE_SHADOW_LOD]
6143 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6144 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6146 shader_glsl_gen_sample_c_lz_emulation(ins, sampler_bind_idx, &sample_function,
6147 coord_size, coord_param.param_str, compare_param.param_str);
6149 else
6151 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6152 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6153 coord_size, coord_param.param_str, compare_param.param_str);
6155 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6158 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6160 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6161 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6162 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6163 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6164 struct glsl_src_param coord_param, compare_param, offset_param;
6165 const struct wined3d_gl_info *gl_info = priv->gl_info;
6166 const struct wined3d_shader_resource_info *resource_info;
6167 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6168 unsigned int coord_size, offset_size;
6169 char dst_swizzle[6];
6170 BOOL has_offset;
6172 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6174 FIXME("OpenGL implementation does not support textureGather.\n");
6175 return;
6178 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6179 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6180 || wined3d_shader_instruction_has_texel_offset(ins);
6182 resource_param_idx =
6183 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6184 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6185 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6186 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6187 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6189 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6190 return;
6192 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6194 ERR("Unexpected resource type %#x.\n", resource_info->type);
6195 return;
6197 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6199 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6200 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6202 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6204 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6205 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6206 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6208 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6209 shader_addline(buffer, ", %s", compare_param.param_str);
6211 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6213 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6214 shader_addline(buffer, ", %s", offset_param.param_str);
6216 else if (has_offset)
6218 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6219 shader_addline(buffer, ", ");
6220 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6222 if (component_idx)
6223 shader_addline(buffer, ", %u", component_idx);
6225 shader_addline(buffer, ")%s);\n", dst_swizzle);
6228 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6230 /* FIXME: Make this work for more than just 2D textures */
6231 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6232 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6234 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6236 char dst_mask[6];
6238 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6239 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6240 ins->dst[0].reg.idx[0].offset, dst_mask);
6242 else
6244 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6245 DWORD reg = ins->src[0].reg.idx[0].offset;
6246 char dst_swizzle[6];
6248 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6250 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6252 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6253 struct glsl_src_param div_param;
6254 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6256 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6258 if (mask_size > 1)
6259 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6260 else
6261 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6263 else
6265 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6270 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6271 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6272 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6273 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6275 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6276 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6277 struct glsl_sample_function sample_function;
6278 struct glsl_src_param src0_param;
6279 UINT mask_size;
6281 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6283 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6284 * scalar, and projected sampling would require 4.
6286 * It is a dependent read - not valid with conditional NP2 textures
6288 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6289 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6291 switch(mask_size)
6293 case 1:
6294 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6295 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6296 break;
6298 case 2:
6299 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6300 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6301 break;
6303 case 3:
6304 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6305 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6306 break;
6308 default:
6309 FIXME("Unexpected mask size %u\n", mask_size);
6310 break;
6312 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6315 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6316 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6317 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6319 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6320 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6321 struct glsl_src_param src0_param;
6322 DWORD dst_mask;
6323 unsigned int mask_size;
6325 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6326 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6327 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6329 if (mask_size > 1) {
6330 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6331 } else {
6332 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6336 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6337 * Calculate the depth as dst.x / dst.y */
6338 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6340 struct glsl_dst_param dst_param;
6342 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6344 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6345 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6346 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6347 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6348 * >= 1.0 or < 0.0
6350 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6351 dst_param.reg_name, dst_param.reg_name);
6354 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6355 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6356 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6357 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6359 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6361 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6362 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6363 struct glsl_src_param src0_param;
6365 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6367 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6368 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6371 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6372 * Calculate the 1st of a 2-row matrix multiplication. */
6373 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6375 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6376 DWORD reg = ins->dst[0].reg.idx[0].offset;
6377 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6378 struct glsl_src_param src0_param;
6380 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6381 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6384 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6385 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6386 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6388 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6389 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6390 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6391 DWORD reg = ins->dst[0].reg.idx[0].offset;
6392 struct glsl_src_param src0_param;
6394 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6395 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6396 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6399 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6401 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6402 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6403 struct glsl_sample_function sample_function;
6404 DWORD reg = ins->dst[0].reg.idx[0].offset;
6405 struct glsl_src_param src0_param;
6407 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6408 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6410 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6412 /* Sample the texture using the calculated coordinates */
6413 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6414 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6417 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6418 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6419 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6421 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6422 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6423 struct glsl_sample_function sample_function;
6424 DWORD reg = ins->dst[0].reg.idx[0].offset;
6425 struct glsl_src_param src0_param;
6427 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6428 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6430 /* Dependent read, not valid with conditional NP2 */
6431 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6433 /* Sample the texture using the calculated coordinates */
6434 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6435 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6437 tex_mx->current_row = 0;
6440 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6441 * Perform the 3rd row of a 3x3 matrix multiply */
6442 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6444 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6445 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6446 DWORD reg = ins->dst[0].reg.idx[0].offset;
6447 struct glsl_src_param src0_param;
6448 char dst_mask[6];
6450 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6452 shader_glsl_append_dst(ins->ctx->buffer, ins);
6453 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6454 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6456 tex_mx->current_row = 0;
6459 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6460 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6461 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6463 struct glsl_src_param src0_param;
6464 struct glsl_src_param src1_param;
6465 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6466 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6467 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6468 struct glsl_sample_function sample_function;
6469 DWORD reg = ins->dst[0].reg.idx[0].offset;
6470 char coord_mask[6];
6472 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6473 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6475 /* Perform the last matrix multiply operation */
6476 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6477 /* Reflection calculation */
6478 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6480 /* Dependent read, not valid with conditional NP2 */
6481 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6482 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6484 /* Sample the texture */
6485 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6486 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6487 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6489 tex_mx->current_row = 0;
6492 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6493 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6494 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6496 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6497 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6498 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6499 struct glsl_sample_function sample_function;
6500 DWORD reg = ins->dst[0].reg.idx[0].offset;
6501 struct glsl_src_param src0_param;
6502 char coord_mask[6];
6504 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6506 /* Perform the last matrix multiply operation */
6507 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6509 /* Construct the eye-ray vector from w coordinates */
6510 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6511 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6512 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6514 /* Dependent read, not valid with conditional NP2 */
6515 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6516 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6518 /* Sample the texture using the calculated coordinates */
6519 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6520 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6521 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6523 tex_mx->current_row = 0;
6526 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6527 * Apply a fake bump map transform.
6528 * texbem is pshader <= 1.3 only, this saves a few version checks
6530 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6532 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6533 struct glsl_sample_function sample_function;
6534 struct glsl_src_param coord_param;
6535 DWORD sampler_idx;
6536 DWORD mask;
6537 DWORD flags;
6538 char coord_mask[6];
6540 sampler_idx = ins->dst[0].reg.idx[0].offset;
6541 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6542 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6544 /* Dependent read, not valid with conditional NP2 */
6545 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6546 mask = sample_function.coord_mask;
6548 shader_glsl_write_mask_to_str(mask, coord_mask);
6550 /* With projected textures, texbem only divides the static texture coord,
6551 * not the displacement, so we can't let GL handle this. */
6552 if (flags & WINED3D_PSARGS_PROJECTED)
6554 DWORD div_mask=0;
6555 char coord_div_mask[3];
6556 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6558 case WINED3D_TTFF_COUNT1:
6559 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6560 break;
6561 case WINED3D_TTFF_COUNT2:
6562 div_mask = WINED3DSP_WRITEMASK_1;
6563 break;
6564 case WINED3D_TTFF_COUNT3:
6565 div_mask = WINED3DSP_WRITEMASK_2;
6566 break;
6567 case WINED3D_TTFF_COUNT4:
6568 case WINED3D_TTFF_DISABLE:
6569 div_mask = WINED3DSP_WRITEMASK_3;
6570 break;
6572 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6573 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6576 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6578 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6579 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6580 coord_param.param_str, coord_mask);
6582 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6584 struct glsl_src_param luminance_param;
6585 struct glsl_dst_param dst_param;
6587 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6588 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6590 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6591 dst_param.reg_name, dst_param.mask_str,
6592 luminance_param.param_str, sampler_idx, sampler_idx);
6594 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6597 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6599 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6600 struct glsl_src_param src0_param, src1_param;
6602 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6603 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6605 shader_glsl_append_dst(ins->ctx->buffer, ins);
6606 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6607 src0_param.param_str, sampler_idx, src1_param.param_str);
6610 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6611 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6612 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6614 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6615 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6616 struct glsl_sample_function sample_function;
6617 struct wined3d_string_buffer *reg_name;
6619 reg_name = string_buffer_get(priv->string_buffers);
6620 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6622 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6623 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6624 "%s.wx", reg_name->buffer);
6625 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6627 string_buffer_release(priv->string_buffers, reg_name);
6630 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6631 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6632 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6634 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6635 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6636 struct glsl_sample_function sample_function;
6637 struct wined3d_string_buffer *reg_name;
6639 reg_name = string_buffer_get(priv->string_buffers);
6640 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6642 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6643 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6644 "%s.yz", reg_name->buffer);
6645 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6647 string_buffer_release(priv->string_buffers, reg_name);
6650 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6651 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6652 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6654 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6655 struct glsl_sample_function sample_function;
6656 struct glsl_src_param src0_param;
6658 /* Dependent read, not valid with conditional NP2 */
6659 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6660 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6662 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6663 "%s", src0_param.param_str);
6664 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6667 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6668 * If any of the first 3 components are < 0, discard this pixel */
6669 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6671 if (ins->ctx->reg_maps->shader_version.major >= 4)
6673 shader_glsl_generate_condition(ins);
6674 shader_addline(ins->ctx->buffer, " discard;\n");
6676 else
6678 struct glsl_dst_param dst_param;
6680 /* The argument is a destination parameter, and no writemasks are allowed */
6681 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6683 /* 2.0 shaders compare all 4 components in texkill. */
6684 if (ins->ctx->reg_maps->shader_version.major >= 2)
6685 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6686 /* 1.x shaders only compare the first 3 components, probably due to
6687 * the nature of the texkill instruction as a tex* instruction, and
6688 * phase, which kills all .w components. Even if all 4 components are
6689 * defined, only the first 3 are used. */
6690 else
6691 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6695 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6696 * dst = dot2(src0, src1) + src2 */
6697 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6699 struct glsl_src_param src0_param;
6700 struct glsl_src_param src1_param;
6701 struct glsl_src_param src2_param;
6702 DWORD write_mask;
6703 unsigned int mask_size;
6705 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6706 mask_size = shader_glsl_get_write_mask_size(write_mask);
6708 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6709 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6710 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6712 if (mask_size > 1) {
6713 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6714 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6715 } else {
6716 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6717 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6721 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6722 const struct wined3d_shader_signature *input_signature,
6723 const struct wined3d_shader_reg_maps *reg_maps,
6724 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6726 unsigned int i;
6728 for (i = 0; i < input_signature->element_count; ++i)
6730 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6731 const char *semantic_name;
6732 UINT semantic_idx;
6733 char reg_mask[6];
6735 /* Unused */
6736 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6737 continue;
6739 semantic_name = input->semantic_name;
6740 semantic_idx = input->semantic_idx;
6741 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6743 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6745 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6747 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6748 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6750 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6752 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6754 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6756 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6757 input->register_idx, reg_mask);
6759 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6761 if (gl_info->supported[ARB_SAMPLE_SHADING])
6762 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6763 input->register_idx, reg_mask);
6764 else
6765 FIXME("ARB_sample_shading is not supported.\n");
6767 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6769 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6770 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6771 input->register_idx, reg_mask);
6772 else
6773 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6775 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6777 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6778 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6779 input->register_idx, reg_mask);
6780 else
6781 FIXME("ARB_viewport_array is not supported.\n");
6783 else
6785 if (input->sysval_semantic)
6786 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6787 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6788 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6789 shader_glsl_shader_input_name(gl_info),
6790 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6793 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6795 if (args->pointsprite)
6796 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6797 shader->u.ps.input_reg_map[input->register_idx]);
6798 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6799 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6800 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6801 needs_legacy_glsl_syntax(gl_info)
6802 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6803 else
6804 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6805 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6807 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6809 if (!semantic_idx)
6810 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6811 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6812 else if (semantic_idx == 1)
6813 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6814 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6815 else
6816 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6817 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6819 else
6821 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6822 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6827 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6829 struct glsl_program_key key;
6831 key.vs_id = entry->vs.id;
6832 key.hs_id = entry->hs.id;
6833 key.ds_id = entry->ds.id;
6834 key.gs_id = entry->gs.id;
6835 key.ps_id = entry->ps.id;
6836 key.cs_id = entry->cs.id;
6838 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6840 ERR("Failed to insert program entry.\n");
6844 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6845 const struct glsl_program_key *key)
6847 struct wine_rb_entry *entry;
6849 entry = wine_rb_get(&priv->program_lookup, key);
6850 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6853 /* Context activation is done by the caller. */
6854 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6855 struct glsl_shader_prog_link *entry)
6857 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6859 GL_EXTCALL(glDeleteProgram(entry->id));
6860 if (entry->vs.id)
6861 list_remove(&entry->vs.shader_entry);
6862 if (entry->hs.id)
6863 list_remove(&entry->hs.shader_entry);
6864 if (entry->ds.id)
6865 list_remove(&entry->ds.shader_entry);
6866 if (entry->gs.id)
6867 list_remove(&entry->gs.shader_entry);
6868 if (entry->ps.id)
6869 list_remove(&entry->ps.shader_entry);
6870 if (entry->cs.id)
6871 list_remove(&entry->cs.shader_entry);
6872 heap_free(entry);
6875 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6876 const struct wined3d_gl_info *gl_info, const DWORD *map,
6877 const struct wined3d_shader_signature *input_signature,
6878 const struct wined3d_shader_reg_maps *reg_maps_in,
6879 const struct wined3d_shader_signature *output_signature,
6880 const struct wined3d_shader_reg_maps *reg_maps_out)
6882 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6883 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6884 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6885 unsigned int in_count = vec4_varyings(3, gl_info);
6886 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6887 DWORD in_idx, *set = NULL;
6888 unsigned int i, j;
6889 char reg_mask[6];
6891 set = heap_calloc(max_varyings, sizeof(*set));
6893 for (i = 0; i < input_signature->element_count; ++i)
6895 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6897 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6898 continue;
6900 in_idx = map[input->register_idx];
6901 /* Declared, but not read register */
6902 if (in_idx == ~0u)
6903 continue;
6904 if (in_idx >= max_varyings)
6906 FIXME("More input varyings declared than supported, expect issues.\n");
6907 continue;
6910 if (in_idx == in_count)
6911 string_buffer_sprintf(destination, "gl_FrontColor");
6912 else if (in_idx == in_count + 1)
6913 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6914 else
6915 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6917 if (!set[in_idx])
6918 set[in_idx] = ~0u;
6920 for (j = 0; j < output_signature->element_count; ++j)
6922 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6923 DWORD mask;
6925 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6926 || input->semantic_idx != output->semantic_idx
6927 || strcmp(input->semantic_name, output->semantic_name)
6928 || !(mask = input->mask & output->mask))
6929 continue;
6931 if (set[in_idx] == ~0u)
6932 set[in_idx] = 0;
6933 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6934 shader_glsl_write_mask_to_str(mask, reg_mask);
6936 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6937 destination->buffer, reg_mask, output->register_idx, reg_mask);
6941 for (i = 0; i < max_varyings; ++i)
6943 unsigned int size;
6945 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6946 continue;
6948 if (set[i] == ~0u)
6949 set[i] = 0;
6951 size = 0;
6952 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6953 reg_mask[size++] = 'x';
6954 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6955 reg_mask[size++] = 'y';
6956 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6957 reg_mask[size++] = 'z';
6958 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6959 reg_mask[size++] = 'w';
6960 reg_mask[size] = '\0';
6962 if (i == in_count)
6963 string_buffer_sprintf(destination, "gl_FrontColor");
6964 else if (i == in_count + 1)
6965 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6966 else
6967 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6969 if (size == 1)
6970 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6971 else
6972 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6975 heap_free(set);
6976 string_buffer_release(&priv->string_buffers, destination);
6979 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6980 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6981 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6982 BOOL rasterizer_setup)
6984 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6985 char reg_mask[6];
6986 unsigned int i;
6988 for (i = 0; i < output_signature->element_count; ++i)
6990 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6992 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6993 continue;
6995 if (output->stream_idx)
6996 continue;
6998 if (output->register_idx >= input_count)
6999 continue;
7001 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7003 shader_addline(buffer,
7004 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7005 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7009 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7010 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7012 unsigned int i, clip_or_cull_index;
7013 const char *name;
7014 char reg_mask[6];
7016 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7017 /* Assign consecutive indices starting from 0. */
7018 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7019 for (i = 0; i < 4; ++i)
7021 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7022 continue;
7024 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7025 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7026 name, clip_or_cull_index, element->register_idx, reg_mask);
7027 ++clip_or_cull_index;
7031 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7032 const struct wined3d_gl_info *gl_info, const DWORD *map,
7033 const struct wined3d_shader_signature *input_signature,
7034 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7035 const struct wined3d_shader_signature *output_signature,
7036 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7038 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7039 const char *semantic_name;
7040 unsigned int semantic_idx;
7041 char reg_mask[6];
7042 unsigned int i;
7044 /* First, sort out position and point size system values. */
7045 for (i = 0; i < output_signature->element_count; ++i)
7047 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7049 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7050 continue;
7052 if (output->stream_idx)
7053 continue;
7055 semantic_name = output->semantic_name;
7056 semantic_idx = output->semantic_idx;
7057 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7059 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7061 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7062 reg_mask, output->register_idx, reg_mask);
7064 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7066 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7067 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7069 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7071 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7072 output->register_idx, reg_mask);
7074 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7076 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7077 output->register_idx, reg_mask);
7079 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7081 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7083 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7085 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7087 else if (output->sysval_semantic)
7089 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7093 /* Then, setup the pixel shader input. */
7094 if (reg_maps_out->shader_version.major < 4)
7095 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7096 output_signature, reg_maps_out);
7097 else
7098 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7101 /* Context activation is done by the caller. */
7102 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7103 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7104 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7106 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7107 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7108 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7109 const char *semantic_name;
7110 UINT semantic_idx;
7111 char reg_mask[6];
7112 unsigned int i;
7113 GLuint ret;
7115 string_buffer_clear(buffer);
7117 shader_glsl_add_version_declaration(buffer, gl_info);
7119 if (per_vertex_point_size)
7121 shader_addline(buffer, "uniform struct\n{\n");
7122 shader_addline(buffer, " float size_min;\n");
7123 shader_addline(buffer, " float size_max;\n");
7124 shader_addline(buffer, "} ffp_point;\n");
7127 if (ps_major < 3)
7129 DWORD colors_written_mask[2] = {0};
7130 DWORD texcoords_written_mask[WINED3D_MAX_TEXTURES] = {0};
7132 if (!legacy_syntax)
7134 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7135 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7136 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7137 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7140 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7142 for (i = 0; i < vs->output_signature.element_count; ++i)
7144 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7145 DWORD write_mask;
7147 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7148 continue;
7150 semantic_name = output->semantic_name;
7151 semantic_idx = output->semantic_idx;
7152 write_mask = output->mask;
7153 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7155 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7157 if (legacy_syntax)
7158 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7159 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7160 else
7161 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7162 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7164 colors_written_mask[semantic_idx] = write_mask;
7166 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7168 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7169 reg_mask, output->register_idx, reg_mask);
7171 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7173 if (semantic_idx < WINED3D_MAX_TEXTURES)
7175 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7176 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7177 semantic_idx, reg_mask, output->register_idx, reg_mask);
7178 texcoords_written_mask[semantic_idx] = write_mask;
7181 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7183 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7184 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7186 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7188 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7189 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7190 output->register_idx, reg_mask[1]);
7194 for (i = 0; i < 2; ++i)
7196 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7198 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7199 if (!i)
7200 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7201 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7202 reg_mask, reg_mask);
7203 else
7204 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7205 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7206 reg_mask, reg_mask);
7209 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
7211 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7212 continue;
7214 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7216 if (!shader_glsl_full_ffp_varyings(gl_info) && !texcoords_written_mask[i])
7217 continue;
7219 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7220 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7221 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7225 else
7227 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7229 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7230 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7231 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7232 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7235 shader_addline(buffer, "}\n");
7237 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7238 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7239 shader_glsl_compile(gl_info, ret, buffer->buffer);
7241 return ret;
7244 static void shader_glsl_generate_stream_output_setup(struct wined3d_string_buffer *buffer,
7245 const struct wined3d_shader *shader)
7247 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
7248 unsigned int i, register_idx, component_idx;
7250 shader_addline(buffer, "out shader_in_out\n{\n");
7251 for (i = 0; i < so_desc->element_count; ++i)
7253 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7255 if (e->stream_idx)
7257 FIXME("Unhandled stream %u.\n", e->stream_idx);
7258 continue;
7260 if (!e->semantic_name)
7261 continue;
7262 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7263 continue;
7265 if (component_idx || e->component_count != 4)
7267 if (e->component_count == 1)
7268 shader_addline(buffer, "float");
7269 else
7270 shader_addline(buffer, "vec%u", e->component_count);
7272 shader_addline(buffer, " reg%u_%u_%u;\n",
7273 register_idx, component_idx, component_idx + e->component_count - 1);
7275 else
7277 shader_addline(buffer, "vec4 reg%u;\n", register_idx);
7280 shader_addline(buffer, "} shader_out;\n");
7282 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7283 shader->limits->packed_output);
7284 for (i = 0; i < so_desc->element_count; ++i)
7286 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7288 if (e->stream_idx)
7290 FIXME("Unhandled stream %u.\n", e->stream_idx);
7291 continue;
7293 if (!e->semantic_name)
7294 continue;
7295 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7296 continue;
7298 if (component_idx || e->component_count != 4)
7300 DWORD write_mask;
7301 char str_mask[6];
7303 write_mask = ((1u << e->component_count) - 1) << component_idx;
7304 shader_glsl_write_mask_to_str(write_mask, str_mask);
7305 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7306 register_idx, component_idx, component_idx + e->component_count - 1,
7307 register_idx, str_mask);
7309 else
7311 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n", register_idx, register_idx);
7314 shader_addline(buffer, "}\n");
7317 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7318 const struct wined3d_shader *shader, unsigned int input_count,
7319 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7321 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7322 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7324 if (rasterizer_setup)
7325 input_count = min(vec4_varyings(4, gl_info), input_count);
7327 if (input_count)
7328 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7330 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7331 prefix, shader->limits->packed_output);
7333 if (rasterizer_setup)
7334 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7335 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7336 else
7337 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7338 &shader->reg_maps, "shader_out", rasterizer_setup);
7340 shader_addline(buffer, "}\n");
7343 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7344 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7345 const char *reg_mask)
7347 if (!constant->sysval_semantic)
7349 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7350 return;
7353 switch (constant->sysval_semantic)
7355 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7356 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7357 case WINED3D_SV_TESS_FACTOR_LINEDET:
7358 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7359 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7360 break;
7361 case WINED3D_SV_TESS_FACTOR_QUADINT:
7362 case WINED3D_SV_TESS_FACTOR_TRIINT:
7363 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7364 break;
7365 default:
7366 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7367 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7371 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7372 const struct wined3d_shader_signature *signature, BOOL input_setup)
7374 unsigned int i, register_count, user_constant_index, user_constant_count;
7376 register_count = user_constant_count = 0;
7377 for (i = 0; i < signature->element_count; ++i)
7379 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7380 register_count = max(constant->register_idx + 1, register_count);
7381 if (!constant->sysval_semantic)
7382 ++user_constant_count;
7385 if (user_constant_count)
7386 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7387 input_setup ? "in" : "out", user_constant_count);
7388 if (input_setup)
7389 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7391 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7392 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7394 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7395 char reg_mask[6];
7397 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7399 if (input_setup)
7400 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7401 else
7402 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7404 shader_addline(buffer, " = ");
7406 if (input_setup)
7407 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7408 else
7409 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7411 shader_addline(buffer, ";\n");
7413 shader_addline(buffer, "}\n");
7416 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7417 const struct wined3d_gl_info *gl_info)
7419 const char *output = get_fragment_output(gl_info);
7421 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7422 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7423 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7424 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7425 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7426 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7429 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7430 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7432 const char *output = get_fragment_output(gl_info);
7434 switch (mode)
7436 case WINED3D_FFP_PS_FOG_OFF:
7437 return;
7439 case WINED3D_FFP_PS_FOG_LINEAR:
7440 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7441 break;
7443 case WINED3D_FFP_PS_FOG_EXP:
7444 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7445 break;
7447 case WINED3D_FFP_PS_FOG_EXP2:
7448 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7449 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7450 break;
7452 default:
7453 ERR("Invalid fog mode %#x.\n", mode);
7454 return;
7457 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7458 output, output);
7461 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7462 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7464 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7465 * flipping all the operators here, just negate the comparison below. */
7466 static const char * const comparison_operator[] =
7468 "", /* WINED3D_CMP_NEVER */
7469 "<", /* WINED3D_CMP_LESS */
7470 "==", /* WINED3D_CMP_EQUAL */
7471 "<=", /* WINED3D_CMP_LESSEQUAL */
7472 ">", /* WINED3D_CMP_GREATER */
7473 "!=", /* WINED3D_CMP_NOTEQUAL */
7474 ">=", /* WINED3D_CMP_GREATEREQUAL */
7475 "" /* WINED3D_CMP_ALWAYS */
7478 if (alpha_func == WINED3D_CMP_ALWAYS)
7479 return;
7481 if (alpha_func != WINED3D_CMP_NEVER)
7482 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7483 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7484 shader_addline(buffer, " discard;\n");
7487 static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *buffer,
7488 const char *colour, const char *colour_key_low, const char *colour_key_high)
7490 shader_addline(buffer, "if (all(greaterThanEqual(%s, %s)) && all(lessThan(%s, %s)))\n",
7491 colour, colour_key_low, colour, colour_key_high);
7492 shader_addline(buffer, " discard;\n");
7495 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7496 const struct wined3d_gl_info *gl_info)
7498 if (gl_info->supported[ARB_CULL_DISTANCE])
7499 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7500 if (gl_info->supported[ARB_GPU_SHADER5])
7501 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7502 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7503 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7504 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7505 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7506 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7507 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7508 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7509 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7510 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7511 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7512 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7513 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7514 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7515 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7516 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7517 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7518 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7519 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7520 if (gl_info->supported[ARB_TEXTURE_GATHER])
7521 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7522 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7523 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7524 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7525 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7526 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7527 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7528 if (gl_info->supported[EXT_GPU_SHADER4])
7529 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7530 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7531 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7532 if (gl_info->supported[EXT_TEXTURE_SHADOW_LOD])
7533 shader_addline(buffer, "#extension GL_EXT_texture_shadow_lod : enable\n");
7536 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7537 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7538 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7540 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7541 struct wined3d_string_buffer *src, *assignment;
7542 enum wined3d_data_type dst_data_type;
7543 const char *swizzle;
7544 unsigned int i;
7546 if (output_signature->element_count)
7548 src = string_buffer_get(string_buffers);
7549 assignment = string_buffer_get(string_buffers);
7550 for (i = 0; i < output_signature->element_count; ++i)
7552 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7554 /* register_idx is set to ~0u for non-color outputs. */
7555 if (output->register_idx == ~0u)
7556 continue;
7557 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7559 FIXME("Unhandled component type %#x.\n", output->component_type);
7560 continue;
7562 dst_data_type = component_type_info[output->component_type].data_type;
7563 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7564 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7565 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT, 4);
7566 swizzle = args->rt_alpha_swizzle & (1u << output->semantic_idx) ? ".argb" : "";
7567 shader_addline(buffer, "%s%s;\n", assignment->buffer, swizzle);
7569 string_buffer_release(string_buffers, src);
7570 string_buffer_release(string_buffers, assignment);
7572 else
7574 DWORD mask = shader->reg_maps.rt_mask;
7576 while (mask)
7578 i = wined3d_bit_scan(&mask);
7579 swizzle = args->rt_alpha_swizzle & (1u << i) ? ".argb" : "";
7580 shader_addline(buffer, "color_out%u = ps_out[%u]%s;\n", i, i, swizzle);
7585 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7586 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7587 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7589 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7591 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7592 if (reg_maps->shader_version.major < 2)
7593 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7595 if (args->srgb_correction)
7596 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7598 /* SM < 3 does not replace the fog stage. */
7599 if (reg_maps->shader_version.major < 3)
7600 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7602 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7604 if (reg_maps->sample_mask)
7605 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7607 if (!use_legacy_fragment_output(gl_info))
7608 shader_glsl_generate_color_output(buffer, gl_info, shader, args, string_buffers);
7611 /* Context activation is done by the caller. */
7612 static GLuint shader_glsl_generate_fragment_shader(const struct wined3d_context_gl *context_gl,
7613 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7614 const struct wined3d_shader *shader, const struct ps_compile_args *args,
7615 struct ps_np2fixup_info *np2fixup_info)
7617 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7618 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7619 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7620 const char *prefix = shader_glsl_get_prefix(version->type);
7621 BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7622 unsigned int i, extra_constants_needed = 0;
7623 struct shader_glsl_ctx_priv priv_ctx;
7624 GLuint shader_id;
7625 DWORD map;
7627 memset(&priv_ctx, 0, sizeof(priv_ctx));
7628 priv_ctx.gl_info = gl_info;
7629 priv_ctx.cur_ps_args = args;
7630 priv_ctx.cur_np2fixup_info = np2fixup_info;
7631 priv_ctx.string_buffers = string_buffers;
7633 shader_glsl_add_version_declaration(buffer, gl_info);
7635 shader_glsl_enable_extensions(buffer, gl_info);
7636 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7637 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7638 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7639 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7640 if (shader_glsl_use_explicit_attrib_location(gl_info))
7641 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7642 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7643 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7644 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7645 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7646 if (gl_info->supported[ARB_SAMPLE_SHADING])
7647 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7648 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7649 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7650 /* The spec says that it doesn't have to be explicitly enabled, but the
7651 * nvidia drivers write a warning if we don't do so. */
7652 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7653 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7655 /* Base Declarations */
7656 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7658 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7660 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7661 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7662 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7663 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7666 /* Declare uniforms for NP2 texcoord fixup:
7667 * This is NOT done inside the loop that declares the texture samplers
7668 * since the NP2 fixup code is currently only used for the GeforceFX
7669 * series and when forcing the ARB_npot extension off. Modern cards just
7670 * skip the code anyway, so put it inside a separate loop. */
7671 if (args->np2_fixup)
7673 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7674 unsigned int cur = 0;
7676 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7677 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7678 * samplerNP2Fixup stores texture dimensions and is updated through
7679 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7681 for (i = 0; i < shader->limits->sampler; ++i)
7683 enum wined3d_shader_resource_type resource_type;
7685 resource_type = pixelshader_get_resource_type(reg_maps, i, args->tex_types);
7687 if (!resource_type || !(args->np2_fixup & (1u << i)))
7688 continue;
7690 if (resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7692 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7693 continue;
7696 fixup->idx[i] = cur++;
7699 fixup->num_consts = (cur + 1) >> 1;
7700 fixup->active = args->np2_fixup;
7701 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7704 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7706 shader_addline(buffer, "uniform struct\n{\n");
7707 shader_addline(buffer, " vec4 color;\n");
7708 shader_addline(buffer, " float density;\n");
7709 shader_addline(buffer, " float end;\n");
7710 shader_addline(buffer, " float scale;\n");
7711 shader_addline(buffer, "} ffp_fog;\n");
7713 if (legacy_syntax)
7715 if (glsl_is_color_reg_read(shader, 0))
7716 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7717 if (glsl_is_color_reg_read(shader, 1))
7718 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7719 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7720 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7722 else
7724 if (glsl_is_color_reg_read(shader, 0))
7725 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7726 if (glsl_is_color_reg_read(shader, 1))
7727 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7728 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7729 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7730 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7734 if (version->major >= 3)
7736 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7738 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7739 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7740 shader->u.ps.interpolation_mode, version->major >= 4);
7741 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7744 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7746 if (!(map & 1))
7747 continue;
7749 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7751 if (reg_maps->luminanceparams & (1u << i))
7753 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7754 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7755 extra_constants_needed++;
7758 extra_constants_needed++;
7761 if (args->srgb_correction)
7763 shader_addline(buffer, "const vec4 srgb_const0 = ");
7764 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
7765 shader_addline(buffer, ";\n");
7766 shader_addline(buffer, "const vec4 srgb_const1 = ");
7767 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
7768 shader_addline(buffer, ";\n");
7770 if (reg_maps->vpos || reg_maps->usesdsy)
7772 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7774 ++extra_constants_needed;
7775 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7777 if (reg_maps->vpos)
7779 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7781 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7782 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7783 args->render_offscreen ? "" : "origin_upper_left, ");
7784 else if (!args->render_offscreen)
7785 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7787 shader_addline(buffer, "vec4 vpos;\n");
7791 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7792 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7794 if (!use_legacy_fragment_output(gl_info))
7796 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7798 if (args->dual_source_blend)
7799 shader_addline(buffer, "vec4 ps_out[2];\n");
7800 else
7801 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7802 if (output_signature->element_count)
7804 for (i = 0; i < output_signature->element_count; ++i)
7806 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7808 if (output->register_idx == ~0u)
7809 continue;
7810 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7812 FIXME("Unhandled component type %#x.\n", output->component_type);
7813 continue;
7815 if (shader_glsl_use_explicit_attrib_location(gl_info))
7817 if (args->dual_source_blend)
7818 shader_addline(buffer, "layout(location = 0, index = %u) ", output->semantic_idx);
7819 else
7820 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7822 shader_addline(buffer, "out %s4 color_out%u;\n",
7823 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7826 else
7828 DWORD mask = reg_maps->rt_mask;
7830 while (mask)
7832 i = wined3d_bit_scan(&mask);
7833 if (shader_glsl_use_explicit_attrib_location(gl_info))
7835 if (args->dual_source_blend)
7836 shader_addline(buffer, "layout(location = 0, index = %u) ", i);
7837 else
7838 shader_addline(buffer, "layout(location = %u) ", i);
7840 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7845 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7846 FIXME("Insufficient uniforms to run this shader.\n");
7848 if (shader->u.ps.force_early_depth_stencil)
7849 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7851 shader_addline(buffer, "void main()\n{\n");
7853 if (reg_maps->sample_mask)
7854 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7856 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7857 * add approximately 0.5. This causes off-by-one problems as spotted by
7858 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7859 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7860 * causes precision troubles when we just subtract 0.5.
7862 * To deal with that, just floor() the position. This will eliminate the
7863 * fraction on all cards.
7865 * TODO: Test how this behaves with multisampling.
7867 * An advantage of floor is that it works even if the driver doesn't add
7868 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7869 * to return in gl_FragCoord, even though coordinates specify the pixel
7870 * centers instead of the pixel corners. This code will behave correctly
7871 * on drivers that returns integer values. */
7872 if (reg_maps->vpos)
7874 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7875 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7876 else if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7877 shader_addline(buffer,
7878 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7879 else
7880 shader_addline(buffer,
7881 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7884 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7886 unsigned int i;
7887 WORD map = reg_maps->texcoord;
7889 if (legacy_syntax)
7891 if (glsl_is_color_reg_read(shader, 0))
7892 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7893 if (glsl_is_color_reg_read(shader, 1))
7894 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7897 for (i = 0; map; map >>= 1, ++i)
7899 if (map & 1)
7901 if (args->pointsprite)
7902 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7903 else if (args->texcoords_initialized & (1u << i))
7904 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7905 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7906 else
7907 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7908 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7912 if (legacy_syntax)
7913 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7916 /* Pack 3.0 inputs */
7917 if (reg_maps->shader_version.major >= 3)
7918 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7919 reg_maps->shader_version.major >= 4);
7921 /* Base Shader Body */
7922 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7923 return 0;
7925 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7926 if (reg_maps->shader_version.major < 4)
7927 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
7929 shader_addline(buffer, "}\n");
7931 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7932 TRACE("Compiling shader object %u.\n", shader_id);
7933 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7935 return shader_id;
7938 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7939 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7940 const struct vs_compile_args *args)
7942 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7943 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7944 unsigned int i;
7946 /* Unpack outputs. */
7947 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7949 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7950 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7951 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7952 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7954 if (reg_maps->shader_version.major < 3)
7956 if (args->fog_src == VS_FOG_Z)
7957 shader_addline(buffer, "%s = gl_Position.z;\n",
7958 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7959 else if (!reg_maps->fog)
7960 shader_addline(buffer, "%s = 0.0;\n",
7961 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7964 /* We always store the clipplanes without y inversion. */
7965 if (args->clip_enabled)
7967 if (legacy_syntax)
7968 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7969 else
7970 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7971 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7974 if (args->point_size && !args->per_vertex_point_size)
7975 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7977 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7978 shader_glsl_fixup_position(buffer, FALSE);
7981 /* Context activation is done by the caller. */
7982 static GLuint shader_glsl_generate_vertex_shader(const struct wined3d_context_gl *context_gl,
7983 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7985 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7986 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7987 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7988 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7989 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7990 struct shader_glsl_ctx_priv priv_ctx;
7991 GLuint shader_id;
7992 unsigned int i;
7994 memset(&priv_ctx, 0, sizeof(priv_ctx));
7995 priv_ctx.gl_info = gl_info;
7996 priv_ctx.cur_vs_args = args;
7997 priv_ctx.string_buffers = string_buffers;
7999 shader_glsl_add_version_declaration(buffer, gl_info);
8001 shader_glsl_enable_extensions(buffer, gl_info);
8002 if (gl_info->supported[ARB_DRAW_INSTANCED])
8003 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8004 if (shader_glsl_use_explicit_attrib_location(gl_info))
8005 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8006 if (gl_info->supported[ARB_SHADER_VIEWPORT_LAYER_ARRAY])
8007 shader_addline(buffer, "#extension GL_ARB_shader_viewport_layer_array : enable\n");
8009 /* Base Declarations */
8010 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8012 for (i = 0; i < shader->input_signature.element_count; ++i)
8013 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8015 if (args->point_size && !args->per_vertex_point_size)
8017 shader_addline(buffer, "uniform struct\n{\n");
8018 shader_addline(buffer, " float size;\n");
8019 shader_addline(buffer, " float size_min;\n");
8020 shader_addline(buffer, " float size_max;\n");
8021 shader_addline(buffer, "} ffp_point;\n");
8024 if (!needs_legacy_glsl_syntax(gl_info))
8026 if (args->clip_enabled)
8027 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8029 if (version->major < 3)
8031 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8032 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8033 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
8034 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8038 if (version->major < 4)
8039 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8041 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8042 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8044 if (reg_maps->shader_version.major >= 4)
8045 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8046 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8048 shader_addline(buffer, "void main()\n{\n");
8050 if (reg_maps->input_rel_addressing)
8052 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8053 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8054 for (i = 0; i < shader->input_signature.element_count; ++i)
8056 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8057 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8061 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8062 return 0;
8064 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8065 if (reg_maps->shader_version.major < 4)
8066 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8068 shader_addline(buffer, "}\n");
8070 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8071 TRACE("Compiling shader object %u.\n", shader_id);
8072 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8074 return shader_id;
8077 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8078 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8080 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8081 char reg_mask[6];
8082 unsigned int i;
8084 for (i = 0; i < output_signature->element_count; ++i)
8086 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8088 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8089 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8090 output->register_idx, reg_mask, output->register_idx, reg_mask);
8094 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8095 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8096 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8097 const char *phase_name, unsigned phase_idx)
8099 unsigned int i;
8100 HRESULT hr;
8102 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8103 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8104 for (i = 0; i < phase->temporary_count; ++i)
8105 shader_addline(buffer, "vec4 R%u;\n", i);
8106 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8107 shader_addline(buffer, "}\n");
8108 return hr;
8111 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8112 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8114 if (phase->instance_count)
8116 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8117 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8118 shader_addline(buffer, "}\n");
8120 else
8122 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8126 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context_gl *context_gl,
8127 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8129 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8130 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8131 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8132 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8133 const struct wined3d_hull_shader *hs = &shader->u.hs;
8134 const struct wined3d_shader_phase *phase;
8135 struct shader_glsl_ctx_priv priv_ctx;
8136 GLuint shader_id;
8137 unsigned int i;
8139 memset(&priv_ctx, 0, sizeof(priv_ctx));
8140 priv_ctx.gl_info = gl_info;
8141 priv_ctx.string_buffers = string_buffers;
8143 shader_glsl_add_version_declaration(buffer, gl_info);
8145 shader_glsl_enable_extensions(buffer, gl_info);
8146 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8148 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8150 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8152 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8153 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8155 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8157 if (hs->phases.control_point)
8159 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8160 shader->limits->packed_output);
8161 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8162 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8163 shader_addline(buffer, "}\n");
8166 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8167 if ((phase = hs->phases.control_point))
8169 for (i = 0; i < phase->temporary_count; ++i)
8170 shader_addline(buffer, "vec4 R%u;\n", i);
8171 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8172 return 0;
8173 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8175 else
8177 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8179 shader_addline(buffer, "}\n");
8181 for (i = 0; i < hs->phases.fork_count; ++i)
8183 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8184 &hs->phases.fork[i], "fork", i)))
8185 return 0;
8188 for (i = 0; i < hs->phases.join_count; ++i)
8190 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8191 &hs->phases.join[i], "join", i)))
8192 return 0;
8195 shader_addline(buffer, "void main()\n{\n");
8196 shader_addline(buffer, "hs_control_point_phase();\n");
8197 if (reg_maps->vocp)
8198 shader_addline(buffer, "barrier();\n");
8199 for (i = 0; i < hs->phases.fork_count; ++i)
8200 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8201 for (i = 0; i < hs->phases.join_count; ++i)
8202 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8203 shader_addline(buffer, "setup_patch_constant_output();\n");
8204 shader_addline(buffer, "}\n");
8206 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8207 TRACE("Compiling shader object %u.\n", shader_id);
8208 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8210 return shader_id;
8213 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8214 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8215 const struct ds_compile_args *args)
8217 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8219 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8220 shader_glsl_fixup_position(buffer, FALSE);
8223 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context_gl *context_gl,
8224 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8226 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8227 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8228 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8229 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8230 struct shader_glsl_ctx_priv priv_ctx;
8231 GLuint shader_id;
8233 memset(&priv_ctx, 0, sizeof(priv_ctx));
8234 priv_ctx.gl_info = gl_info;
8235 priv_ctx.cur_ds_args = args;
8236 priv_ctx.string_buffers = string_buffers;
8238 shader_glsl_add_version_declaration(buffer, gl_info);
8240 shader_glsl_enable_extensions(buffer, gl_info);
8241 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8243 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8245 shader_addline(buffer, "layout(");
8246 switch (shader->u.ds.tessellator_domain)
8248 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8249 shader_addline(buffer, "isolines");
8250 break;
8251 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8252 shader_addline(buffer, "quads");
8253 break;
8254 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8255 shader_addline(buffer, "triangles");
8256 break;
8258 switch (args->tessellator_output_primitive)
8260 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8261 if (args->render_offscreen)
8262 shader_addline(buffer, ", ccw");
8263 else
8264 shader_addline(buffer, ", cw");
8265 break;
8266 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8267 if (args->render_offscreen)
8268 shader_addline(buffer, ", cw");
8269 else
8270 shader_addline(buffer, ", ccw");
8271 break;
8272 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8273 shader_addline(buffer, ", point_mode");
8274 break;
8275 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8276 break;
8278 switch (args->tessellator_partitioning)
8280 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8281 shader_addline(buffer, ", fractional_odd_spacing");
8282 break;
8283 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8284 shader_addline(buffer, ", fractional_even_spacing");
8285 break;
8286 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8287 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8288 shader_addline(buffer, ", equal_spacing");
8289 break;
8291 shader_addline(buffer, ") in;\n");
8293 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8295 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8296 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8298 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8299 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8300 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8302 shader_addline(buffer, "void main()\n{\n");
8303 shader_addline(buffer, "setup_patch_constant_input();\n");
8305 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8306 return 0;
8308 shader_addline(buffer, "}\n");
8310 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8311 TRACE("Compiling shader object %u.\n", shader_id);
8312 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8314 return shader_id;
8317 /* Context activation is done by the caller. */
8318 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context_gl *context_gl,
8319 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8321 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8322 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8323 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8324 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8325 const struct wined3d_shader_signature_element *output;
8326 enum wined3d_primitive_type primitive_type;
8327 struct shader_glsl_ctx_priv priv_ctx;
8328 unsigned int max_vertices;
8329 unsigned int i, j;
8330 GLuint shader_id;
8332 memset(&priv_ctx, 0, sizeof(priv_ctx));
8333 priv_ctx.gl_info = gl_info;
8334 priv_ctx.string_buffers = string_buffers;
8336 shader_glsl_add_version_declaration(buffer, gl_info);
8338 shader_glsl_enable_extensions(buffer, gl_info);
8340 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8342 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8343 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8344 if (shader->u.gs.instance_count > 1)
8345 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8346 shader_addline(buffer, ") in;\n");
8348 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8349 if (!(max_vertices = shader->u.gs.vertices_out))
8351 switch (args->primitive_type)
8353 case WINED3D_PT_POINTLIST:
8354 max_vertices = 1;
8355 break;
8356 case WINED3D_PT_LINELIST:
8357 max_vertices = 2;
8358 break;
8359 case WINED3D_PT_TRIANGLELIST:
8360 max_vertices = 3;
8361 break;
8362 default:
8363 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8364 break;
8367 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8368 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8369 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8371 if (!gl_info->supported[ARB_CLIP_CONTROL])
8373 shader_addline(buffer, "uniform vec4 pos_fixup");
8374 if (reg_maps->viewport_array)
8375 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8376 shader_addline(buffer, ";\n");
8379 if (is_rasterization_disabled(shader))
8381 shader_glsl_generate_stream_output_setup(buffer, shader);
8383 else
8385 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8386 gl_info, TRUE, args->interpolation_mode);
8389 shader_addline(buffer, "void main()\n{\n");
8390 if (shader->function)
8392 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8393 return 0;
8395 else
8397 for (i = 0; i < max_vertices; ++i)
8399 for (j = 0; j < shader->output_signature.element_count; ++j)
8401 output = &shader->output_signature.elements[j];
8402 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8403 output->register_idx, i, output->register_idx);
8405 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8406 if (!gl_info->supported[ARB_CLIP_CONTROL])
8407 shader_glsl_fixup_position(buffer, FALSE);
8408 shader_addline(buffer, "EmitVertex();\n");
8411 shader_addline(buffer, "}\n");
8413 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8414 TRACE("Compiling shader object %u.\n", shader_id);
8415 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8417 return shader_id;
8420 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8422 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8423 const struct wined3d_gl_info *gl_info = priv->gl_info;
8424 struct wined3d_string_buffer *buffer = ctx->buffer;
8425 const struct wined3d_shader *shader = ctx->shader;
8427 switch (shader->reg_maps.shader_version.type)
8429 case WINED3D_SHADER_TYPE_PIXEL:
8430 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8431 break;
8432 case WINED3D_SHADER_TYPE_VERTEX:
8433 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8434 break;
8435 case WINED3D_SHADER_TYPE_DOMAIN:
8436 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8437 break;
8438 case WINED3D_SHADER_TYPE_GEOMETRY:
8439 case WINED3D_SHADER_TYPE_COMPUTE:
8440 break;
8441 default:
8442 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8443 break;
8447 /* Context activation is done by the caller. */
8448 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context_gl *context_gl,
8449 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8450 const struct wined3d_shader *shader)
8452 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8453 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8454 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8455 struct shader_glsl_ctx_priv priv_ctx;
8456 GLuint shader_id;
8457 unsigned int i;
8459 memset(&priv_ctx, 0, sizeof(priv_ctx));
8460 priv_ctx.gl_info = gl_info;
8461 priv_ctx.string_buffers = string_buffers;
8463 shader_glsl_add_version_declaration(buffer, gl_info);
8465 shader_glsl_enable_extensions(buffer, gl_info);
8466 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8468 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8470 for (i = 0; i < reg_maps->tgsm_count; ++i)
8472 if (reg_maps->tgsm[i].size)
8473 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8476 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8477 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8479 shader_addline(buffer, "void main()\n{\n");
8480 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8481 shader_addline(buffer, "}\n");
8483 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8484 TRACE("Compiling shader object %u.\n", shader_id);
8485 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8487 return shader_id;
8490 static GLuint find_glsl_fragment_shader(const struct wined3d_context_gl *context_gl,
8491 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8492 struct wined3d_shader *shader,
8493 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8495 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8496 struct glsl_shader_private *shader_data;
8497 struct ps_np2fixup_info *np2fixup;
8498 UINT i;
8499 DWORD new_size;
8500 GLuint ret;
8502 if (!shader->backend_data)
8504 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8506 ERR("Failed to allocate backend data.\n");
8507 return 0;
8510 shader_data = shader->backend_data;
8511 gl_shaders = shader_data->gl_shaders.ps;
8513 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8514 * so a linear search is more performant than a hashmap or a binary search
8515 * (cache coherency etc)
8517 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8519 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8521 if (args->np2_fixup)
8522 *np2fixup_info = &gl_shaders[i].np2fixup;
8523 return gl_shaders[i].id;
8527 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8528 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8530 if (shader_data->num_gl_shaders)
8532 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8533 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8535 else
8537 new_array = heap_alloc(sizeof(*gl_shaders));
8538 new_size = 1;
8541 if(!new_array) {
8542 ERR("Out of memory\n");
8543 return 0;
8545 shader_data->gl_shaders.ps = new_array;
8546 shader_data->shader_array_size = new_size;
8547 gl_shaders = new_array;
8550 gl_shaders[shader_data->num_gl_shaders].args = *args;
8552 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8553 memset(np2fixup, 0, sizeof(*np2fixup));
8554 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8556 string_buffer_clear(buffer);
8557 ret = shader_glsl_generate_fragment_shader(context_gl, buffer, string_buffers, shader, args, np2fixup);
8558 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8560 return ret;
8563 static BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8564 const DWORD use_map)
8566 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8567 return FALSE;
8568 if ((stored->clip_enabled) != new->clip_enabled)
8569 return FALSE;
8570 if (stored->point_size != new->point_size)
8571 return FALSE;
8572 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8573 return FALSE;
8574 if (stored->flatshading != new->flatshading)
8575 return FALSE;
8576 if (stored->next_shader_type != new->next_shader_type)
8577 return FALSE;
8578 if (stored->next_shader_input_count != new->next_shader_input_count)
8579 return FALSE;
8580 if (stored->fog_src != new->fog_src)
8581 return FALSE;
8582 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8585 static GLuint find_glsl_vertex_shader(const struct wined3d_context_gl *context_gl,
8586 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct vs_compile_args *args)
8588 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8589 uint32_t use_map = context_gl->c.stream_info.use_map;
8590 struct glsl_shader_private *shader_data;
8591 unsigned int i, new_size;
8592 GLuint ret;
8594 if (!shader->backend_data)
8596 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8598 ERR("Failed to allocate backend data.\n");
8599 return 0;
8602 shader_data = shader->backend_data;
8603 gl_shaders = shader_data->gl_shaders.vs;
8605 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8606 * so a linear search is more performant than a hashmap or a binary search
8607 * (cache coherency etc)
8609 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8611 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8612 return gl_shaders[i].id;
8615 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8617 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8619 if (shader_data->num_gl_shaders)
8621 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8622 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8624 else
8626 new_array = heap_alloc(sizeof(*gl_shaders));
8627 new_size = 1;
8630 if(!new_array) {
8631 ERR("Out of memory\n");
8632 return 0;
8634 shader_data->gl_shaders.vs = new_array;
8635 shader_data->shader_array_size = new_size;
8636 gl_shaders = new_array;
8639 gl_shaders[shader_data->num_gl_shaders].args = *args;
8641 string_buffer_clear(&priv->shader_buffer);
8642 ret = shader_glsl_generate_vertex_shader(context_gl, priv, shader, args);
8643 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8645 return ret;
8648 static GLuint find_glsl_hull_shader(const struct wined3d_context_gl *context_gl,
8649 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8651 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8652 struct glsl_shader_private *shader_data;
8653 unsigned int new_size;
8654 GLuint ret;
8656 if (!shader->backend_data)
8658 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8660 ERR("Failed to allocate backend data.\n");
8661 return 0;
8664 shader_data = shader->backend_data;
8665 gl_shaders = shader_data->gl_shaders.hs;
8667 if (shader_data->num_gl_shaders > 0)
8669 assert(shader_data->num_gl_shaders == 1);
8670 return gl_shaders[0].id;
8673 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8675 assert(!shader_data->gl_shaders.hs);
8676 new_size = 1;
8677 if (!(new_array = heap_alloc(sizeof(*new_array))))
8679 ERR("Failed to allocate GL shaders array.\n");
8680 return 0;
8682 shader_data->gl_shaders.hs = new_array;
8683 shader_data->shader_array_size = new_size;
8684 gl_shaders = new_array;
8686 string_buffer_clear(&priv->shader_buffer);
8687 ret = shader_glsl_generate_hull_shader(context_gl, priv, shader);
8688 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8690 return ret;
8693 static GLuint find_glsl_domain_shader(const struct wined3d_context_gl *context_gl,
8694 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8696 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8697 struct glsl_shader_private *shader_data;
8698 unsigned int i, new_size;
8699 GLuint ret;
8701 if (!shader->backend_data)
8703 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8705 ERR("Failed to allocate backend data.\n");
8706 return 0;
8709 shader_data = shader->backend_data;
8710 gl_shaders = shader_data->gl_shaders.ds;
8712 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8714 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8715 return gl_shaders[i].id;
8718 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8720 if (shader_data->num_gl_shaders)
8722 new_size = shader_data->shader_array_size + 1;
8723 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8725 else
8727 new_array = heap_alloc(sizeof(*new_array));
8728 new_size = 1;
8731 if (!new_array)
8733 ERR("Failed to allocate GL shaders array.\n");
8734 return 0;
8736 shader_data->gl_shaders.ds = new_array;
8737 shader_data->shader_array_size = new_size;
8738 gl_shaders = new_array;
8740 string_buffer_clear(&priv->shader_buffer);
8741 ret = shader_glsl_generate_domain_shader(context_gl, priv, shader, args);
8742 gl_shaders[shader_data->num_gl_shaders].args = *args;
8743 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8745 return ret;
8748 static GLuint find_glsl_geometry_shader(const struct wined3d_context_gl *context_gl,
8749 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8751 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8752 struct glsl_shader_private *shader_data;
8753 unsigned int i, new_size;
8754 GLuint ret;
8756 if (!shader->backend_data)
8758 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8760 ERR("Failed to allocate backend data.\n");
8761 return 0;
8764 shader_data = shader->backend_data;
8765 gl_shaders = shader_data->gl_shaders.gs;
8767 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8769 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8770 return gl_shaders[i].id;
8773 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8775 if (shader_data->num_gl_shaders)
8777 new_size = shader_data->shader_array_size + 1;
8778 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8780 else
8782 new_array = heap_alloc(sizeof(*new_array));
8783 new_size = 1;
8786 if (!new_array)
8788 ERR("Failed to allocate GL shaders array.\n");
8789 return 0;
8791 shader_data->gl_shaders.gs = new_array;
8792 shader_data->shader_array_size = new_size;
8793 gl_shaders = new_array;
8795 string_buffer_clear(&priv->shader_buffer);
8796 ret = shader_glsl_generate_geometry_shader(context_gl, priv, shader, args);
8797 gl_shaders[shader_data->num_gl_shaders].args = *args;
8798 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8800 return ret;
8803 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8805 switch (mcs)
8807 case WINED3D_MCS_MATERIAL:
8808 return material;
8809 case WINED3D_MCS_COLOR1:
8810 return "ffp_attrib_diffuse";
8811 case WINED3D_MCS_COLOR2:
8812 return "ffp_attrib_specular";
8813 default:
8814 ERR("Invalid material color source %#x.\n", mcs);
8815 return "<invalid>";
8819 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8820 const struct wined3d_ffp_vs_settings *settings, unsigned int idx, BOOL legacy_lighting)
8822 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8823 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8824 if (settings->localviewer)
8825 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8826 else
8827 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8828 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0%s) specular +="
8829 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n",
8830 legacy_lighting ? " && ffp_material.shininess > 0.0" : "", idx);
8833 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8834 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8836 const char *diffuse, *specular, *emissive, *ambient;
8837 unsigned int i, idx;
8839 if (!settings->lighting)
8841 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8842 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8843 return;
8846 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8847 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8848 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8849 shader_addline(buffer, "vec3 dir, dst;\n");
8850 shader_addline(buffer, "float att, t;\n");
8852 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8853 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8854 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8855 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8857 idx = 0;
8858 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8860 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8861 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8862 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8863 shader_addline(buffer, "dst.x = 1.0;\n");
8864 if (legacy_lighting)
8866 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8867 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8868 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8870 else
8872 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8874 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8875 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8876 if (!legacy_lighting)
8877 shader_addline(buffer, "att = 1.0 / att;\n");
8878 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8879 if (!settings->normal)
8881 shader_addline(buffer, "}\n");
8882 continue;
8884 shader_addline(buffer, "dir = normalize(dir);\n");
8885 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8886 shader_addline(buffer, "}\n");
8889 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8891 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8892 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8893 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8894 shader_addline(buffer, "dst.x = 1.0;\n");
8895 if (legacy_lighting)
8897 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8898 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8899 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8901 else
8903 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8905 shader_addline(buffer, "dir = normalize(dir);\n");
8906 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8907 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8908 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8909 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8910 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8911 idx, idx, idx, idx);
8912 if (legacy_lighting)
8913 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8914 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8915 idx, idx, idx);
8916 else
8917 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8918 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8919 idx, idx, idx);
8920 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8921 if (!settings->normal)
8923 shader_addline(buffer, "}\n");
8924 continue;
8926 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8927 shader_addline(buffer, "}\n");
8930 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8932 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8933 if (!settings->normal)
8934 continue;
8935 shader_addline(buffer, "att = 1.0;\n");
8936 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8937 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8940 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8942 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8943 if (!settings->normal)
8944 continue;
8945 shader_addline(buffer, "att = 1.0;\n");
8946 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8947 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8950 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8951 ambient, diffuse, emissive);
8952 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8953 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8956 /* Context activation is done by the caller. */
8957 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8958 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8960 static const struct attrib_info
8962 const char type[6];
8963 const char name[24];
8965 attrib_info[] =
8967 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8968 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8969 /* TODO: Indexed vertex blending */
8970 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8971 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8972 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8973 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8974 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8976 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8977 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8978 BOOL output_legacy_fogcoord = legacy_syntax;
8979 BOOL legacy_lighting = priv->legacy_lighting;
8980 GLuint shader_obj;
8981 unsigned int i;
8983 string_buffer_clear(buffer);
8985 shader_glsl_add_version_declaration(buffer, gl_info);
8987 if (shader_glsl_use_explicit_attrib_location(gl_info))
8988 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8990 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8992 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8994 if (shader_glsl_use_explicit_attrib_location(gl_info))
8995 shader_addline(buffer, "layout(location = %u) ", i);
8996 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8998 shader_addline(buffer, "\n");
9000 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9001 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9002 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9003 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", WINED3D_MAX_TEXTURES);
9005 shader_addline(buffer, "uniform struct\n{\n");
9006 shader_addline(buffer, " vec4 emissive;\n");
9007 shader_addline(buffer, " vec4 ambient;\n");
9008 shader_addline(buffer, " vec4 diffuse;\n");
9009 shader_addline(buffer, " vec4 specular;\n");
9010 shader_addline(buffer, " float shininess;\n");
9011 shader_addline(buffer, "} ffp_material;\n");
9013 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9014 shader_addline(buffer, "uniform struct\n{\n");
9015 shader_addline(buffer, " vec4 diffuse;\n");
9016 shader_addline(buffer, " vec4 specular;\n");
9017 shader_addline(buffer, " vec4 ambient;\n");
9018 shader_addline(buffer, " vec4 position;\n");
9019 shader_addline(buffer, " vec3 direction;\n");
9020 shader_addline(buffer, " float range;\n");
9021 shader_addline(buffer, " float falloff;\n");
9022 shader_addline(buffer, " float c_att;\n");
9023 shader_addline(buffer, " float l_att;\n");
9024 shader_addline(buffer, " float q_att;\n");
9025 shader_addline(buffer, " float cos_htheta;\n");
9026 shader_addline(buffer, " float cos_hphi;\n");
9027 shader_addline(buffer, "} ffp_light[%u];\n", WINED3D_MAX_ACTIVE_LIGHTS);
9029 if (settings->point_size)
9031 shader_addline(buffer, "uniform struct\n{\n");
9032 shader_addline(buffer, " float size;\n");
9033 shader_addline(buffer, " float size_min;\n");
9034 shader_addline(buffer, " float size_max;\n");
9035 shader_addline(buffer, " float c_att;\n");
9036 shader_addline(buffer, " float l_att;\n");
9037 shader_addline(buffer, " float q_att;\n");
9038 shader_addline(buffer, "} ffp_point;\n");
9041 if (legacy_syntax)
9043 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9044 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9045 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9046 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9048 else
9050 if (settings->clipping)
9051 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9053 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9054 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9055 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9056 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9059 shader_addline(buffer, "\nvoid main()\n{\n");
9060 shader_addline(buffer, "float m;\n");
9061 shader_addline(buffer, "vec3 r;\n");
9063 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9065 if (attrib_info[i].name[0])
9066 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9067 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9069 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9071 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9072 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9073 && settings->texcoords & (1u << i))
9074 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9077 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9079 if (settings->transformed)
9081 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9082 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9083 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9085 else
9087 for (i = 0; i < settings->vertexblends; ++i)
9088 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9090 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9091 for (i = 0; i < settings->vertexblends + 1; ++i)
9092 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9094 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9095 if (settings->clipping)
9097 if (legacy_syntax)
9098 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9099 else
9100 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9101 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9103 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9106 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9107 if (settings->normal)
9109 if (!settings->vertexblends)
9111 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9113 else
9115 for (i = 0; i < settings->vertexblends + 1; ++i)
9116 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9119 if (settings->normalize)
9120 shader_addline(buffer, "normal = normalize(normal);\n");
9123 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9124 if (legacy_syntax)
9126 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9127 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9129 else
9131 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9132 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9135 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9137 BOOL output_legacy_texcoord = legacy_syntax;
9139 switch (settings->texgen[i] & 0xffff0000)
9141 case WINED3DTSS_TCI_PASSTHRU:
9142 if (settings->texcoords & (1u << i))
9143 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9144 i, i, i);
9145 else if (shader_glsl_full_ffp_varyings(gl_info))
9146 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9147 else
9148 output_legacy_texcoord = FALSE;
9149 break;
9151 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9152 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9153 break;
9155 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9156 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9157 break;
9159 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9160 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9161 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9162 break;
9164 case WINED3DTSS_TCI_SPHEREMAP:
9165 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9166 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9167 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9168 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9169 break;
9171 default:
9172 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9173 break;
9175 if (output_legacy_texcoord)
9176 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9179 switch (settings->fog_mode)
9181 case WINED3D_FFP_VS_FOG_OFF:
9182 output_legacy_fogcoord = FALSE;
9183 break;
9185 case WINED3D_FFP_VS_FOG_FOGCOORD:
9186 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9187 break;
9189 case WINED3D_FFP_VS_FOG_RANGE:
9190 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9191 break;
9193 case WINED3D_FFP_VS_FOG_DEPTH:
9194 if (settings->ortho_fog)
9196 if (gl_info->supported[ARB_CLIP_CONTROL])
9197 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9198 else
9199 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9200 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9202 else if (settings->transformed)
9204 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9206 else
9208 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9210 break;
9212 default:
9213 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9214 break;
9216 if (output_legacy_fogcoord)
9217 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9219 if (settings->point_size)
9221 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9222 " + ffp_point.l_att * length(ec_pos.xyz)"
9223 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9224 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9225 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9228 shader_addline(buffer, "}\n");
9230 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9231 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9233 return shader_obj;
9236 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9237 DWORD argnum, unsigned int stage, DWORD arg)
9239 const char *ret;
9241 if (arg == ARG_UNUSED)
9242 return "<unused arg>";
9244 switch (arg & WINED3DTA_SELECTMASK)
9246 case WINED3DTA_DIFFUSE:
9247 ret = "ffp_varying_diffuse";
9248 break;
9250 case WINED3DTA_CURRENT:
9251 ret = "ret";
9252 break;
9254 case WINED3DTA_TEXTURE:
9255 switch (stage)
9257 case 0: ret = "tex0"; break;
9258 case 1: ret = "tex1"; break;
9259 case 2: ret = "tex2"; break;
9260 case 3: ret = "tex3"; break;
9261 case 4: ret = "tex4"; break;
9262 case 5: ret = "tex5"; break;
9263 case 6: ret = "tex6"; break;
9264 case 7: ret = "tex7"; break;
9265 default:
9266 ret = "<invalid texture>";
9267 break;
9269 break;
9271 case WINED3DTA_TFACTOR:
9272 ret = "tex_factor";
9273 break;
9275 case WINED3DTA_SPECULAR:
9276 ret = "ffp_varying_specular";
9277 break;
9279 case WINED3DTA_TEMP:
9280 ret = "temp_reg";
9281 break;
9283 case WINED3DTA_CONSTANT:
9284 switch (stage)
9286 case 0: ret = "tss_const0"; break;
9287 case 1: ret = "tss_const1"; break;
9288 case 2: ret = "tss_const2"; break;
9289 case 3: ret = "tss_const3"; break;
9290 case 4: ret = "tss_const4"; break;
9291 case 5: ret = "tss_const5"; break;
9292 case 6: ret = "tss_const6"; break;
9293 case 7: ret = "tss_const7"; break;
9294 default:
9295 ret = "<invalid constant>";
9296 break;
9298 break;
9300 default:
9301 return "<unhandled arg>";
9304 if (arg & WINED3DTA_COMPLEMENT)
9306 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9307 if (argnum == 0)
9308 ret = "arg0";
9309 else if (argnum == 1)
9310 ret = "arg1";
9311 else if (argnum == 2)
9312 ret = "arg2";
9315 if (arg & WINED3DTA_ALPHAREPLICATE)
9317 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9318 if (argnum == 0)
9319 ret = "arg0";
9320 else if (argnum == 1)
9321 ret = "arg1";
9322 else if (argnum == 2)
9323 ret = "arg2";
9326 return ret;
9329 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9330 BOOL alpha, BOOL tmp_dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9332 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9334 if (color && alpha)
9335 dstmask = "";
9336 else if (color)
9337 dstmask = ".xyz";
9338 else
9339 dstmask = ".w";
9341 dstreg = tmp_dst ? "temp_reg" : "ret";
9343 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9344 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9345 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9347 switch (op)
9349 case WINED3D_TOP_DISABLE:
9350 break;
9352 case WINED3D_TOP_SELECT_ARG1:
9353 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9354 break;
9356 case WINED3D_TOP_SELECT_ARG2:
9357 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9358 break;
9360 case WINED3D_TOP_MODULATE:
9361 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9362 break;
9364 case WINED3D_TOP_MODULATE_4X:
9365 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9366 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9367 break;
9369 case WINED3D_TOP_MODULATE_2X:
9370 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9371 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9372 break;
9374 case WINED3D_TOP_ADD:
9375 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9376 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9377 break;
9379 case WINED3D_TOP_ADD_SIGNED:
9380 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9381 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9382 break;
9384 case WINED3D_TOP_ADD_SIGNED_2X:
9385 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9386 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9387 break;
9389 case WINED3D_TOP_SUBTRACT:
9390 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9391 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9392 break;
9394 case WINED3D_TOP_ADD_SMOOTH:
9395 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9396 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9397 break;
9399 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9400 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9401 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9402 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9403 break;
9405 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9406 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9407 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9408 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9409 break;
9411 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9412 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9413 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9414 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9415 break;
9417 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9418 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9419 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9420 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9421 break;
9423 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9424 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9425 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9426 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9427 break;
9429 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9430 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9431 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9432 break;
9434 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9435 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9436 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9437 break;
9439 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9440 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9441 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9442 break;
9443 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9444 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9445 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9446 break;
9448 case WINED3D_TOP_BUMPENVMAP:
9449 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9450 /* These are handled in the first pass, nothing to do. */
9451 break;
9453 case WINED3D_TOP_DOTPRODUCT3:
9454 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9455 dstreg, dstmask, arg1, arg2, dstmask);
9456 break;
9458 case WINED3D_TOP_MULTIPLY_ADD:
9459 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9460 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9461 break;
9463 case WINED3D_TOP_LERP:
9464 /* MSDN isn't quite right here. */
9465 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9466 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9467 break;
9469 default:
9470 FIXME("Unhandled operation %#x.\n", op);
9471 break;
9475 /* Context activation is done by the caller. */
9476 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9477 const struct ffp_frag_settings *settings, const struct wined3d_context_gl *context_gl)
9479 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9480 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9481 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9482 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9483 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
9484 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9485 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9486 UINT lowest_disabled_stage;
9487 GLuint shader_id;
9488 DWORD arg0, arg1, arg2;
9489 unsigned int stage;
9491 string_buffer_clear(buffer);
9493 /* Find out which textures are read */
9494 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9496 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9497 break;
9499 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9500 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9501 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9503 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9504 || (stage == 0 && settings->color_key_enabled))
9505 tex_map |= 1u << stage;
9506 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9507 tfactor_used = TRUE;
9508 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9509 tempreg_used = TRUE;
9510 if (settings->op[stage].tmp_dst)
9511 tempreg_used = TRUE;
9512 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9513 tss_const_map |= 1u << stage;
9515 switch (settings->op[stage].cop)
9517 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9518 lum_map |= 1u << stage;
9519 /* fall through */
9520 case WINED3D_TOP_BUMPENVMAP:
9521 bump_map |= 1u << stage;
9522 /* fall through */
9523 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9524 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9525 tex_map |= 1u << stage;
9526 break;
9528 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9529 tfactor_used = TRUE;
9530 break;
9532 default:
9533 break;
9536 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9537 continue;
9539 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9540 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9541 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9543 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9544 tex_map |= 1u << stage;
9545 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9546 tfactor_used = TRUE;
9547 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9548 tempreg_used = TRUE;
9549 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9550 tss_const_map |= 1u << stage;
9552 lowest_disabled_stage = stage;
9554 shader_glsl_add_version_declaration(buffer, gl_info);
9556 if (shader_glsl_use_explicit_attrib_location(gl_info))
9557 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9558 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9559 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9560 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9561 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9563 if (!use_legacy_fragment_output(gl_info))
9565 shader_addline(buffer, "vec4 ps_out[1];\n");
9566 if (shader_glsl_use_explicit_attrib_location(gl_info))
9567 shader_addline(buffer, "layout(location = 0) ");
9568 shader_addline(buffer, "out vec4 color_out0;\n");
9571 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9572 shader_addline(buffer, "vec4 ret;\n");
9573 if (tempreg_used || settings->sRGB_write)
9574 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9575 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9577 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9579 const char *sampler_type;
9581 if (tss_const_map & (1u << stage))
9582 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9584 if (!(tex_map & (1u << stage)))
9585 continue;
9587 switch (settings->op[stage].tex_type)
9589 case WINED3D_GL_RES_TYPE_TEX_1D:
9590 sampler_type = "1D";
9591 break;
9592 case WINED3D_GL_RES_TYPE_TEX_2D:
9593 sampler_type = "2D";
9594 break;
9595 case WINED3D_GL_RES_TYPE_TEX_3D:
9596 sampler_type = "3D";
9597 break;
9598 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9599 sampler_type = "Cube";
9600 break;
9601 case WINED3D_GL_RES_TYPE_TEX_RECT:
9602 sampler_type = "2DRect";
9603 break;
9604 default:
9605 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9606 sampler_type = NULL;
9607 break;
9609 if (sampler_type)
9611 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9612 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
9613 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9616 shader_addline(buffer, "vec4 tex%u;\n", stage);
9618 if (!(bump_map & (1u << stage)))
9619 continue;
9620 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9622 if (!(lum_map & (1u << stage)))
9623 continue;
9624 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9625 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9627 if (tfactor_used)
9628 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9629 if (settings->color_key_enabled)
9630 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9631 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9633 if (settings->sRGB_write)
9635 shader_addline(buffer, "const vec4 srgb_const0 = ");
9636 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
9637 shader_addline(buffer, ";\n");
9638 shader_addline(buffer, "const vec4 srgb_const1 = ");
9639 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
9640 shader_addline(buffer, ";\n");
9643 shader_addline(buffer, "uniform struct\n{\n");
9644 shader_addline(buffer, " vec4 color;\n");
9645 shader_addline(buffer, " float density;\n");
9646 shader_addline(buffer, " float end;\n");
9647 shader_addline(buffer, " float scale;\n");
9648 shader_addline(buffer, "} ffp_fog;\n");
9650 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9651 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9653 if (legacy_syntax)
9655 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9656 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9657 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9658 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9659 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9661 else
9663 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9664 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9665 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9666 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9667 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9670 shader_addline(buffer, "void main()\n{\n");
9672 if (legacy_syntax)
9674 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9675 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9678 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9680 if (tex_map & (1u << stage))
9682 if (settings->pointsprite)
9683 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9684 else if (settings->texcoords_initialized & (1u << stage))
9685 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9686 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9687 else
9688 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9692 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9693 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9695 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9696 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9698 /* Generate texture sampling instructions */
9699 for (stage = 0; stage < WINED3D_MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9701 const char *texture_function, *coord_mask;
9702 BOOL proj;
9704 if (!(tex_map & (1u << stage)))
9705 continue;
9707 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9709 proj = FALSE;
9711 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9712 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9714 proj = TRUE;
9716 else
9718 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9719 proj = TRUE;
9722 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9723 proj = FALSE;
9725 switch (settings->op[stage].tex_type)
9727 case WINED3D_GL_RES_TYPE_TEX_1D:
9728 texture_function = "texture1D";
9729 coord_mask = "x";
9730 break;
9731 case WINED3D_GL_RES_TYPE_TEX_2D:
9732 texture_function = "texture2D";
9733 coord_mask = "xy";
9734 break;
9735 case WINED3D_GL_RES_TYPE_TEX_3D:
9736 texture_function = "texture3D";
9737 coord_mask = "xyz";
9738 break;
9739 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9740 texture_function = "textureCube";
9741 coord_mask = "xyz";
9742 break;
9743 case WINED3D_GL_RES_TYPE_TEX_RECT:
9744 texture_function = "texture2DRect";
9745 coord_mask = "xy";
9746 break;
9747 default:
9748 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9749 texture_function = "";
9750 coord_mask = "xyzw";
9751 proj = FALSE;
9752 break;
9754 if (!legacy_syntax)
9755 texture_function = "texture";
9757 if (stage > 0
9758 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9759 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9761 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9763 /* With projective textures, texbem only divides the static
9764 * texture coordinate, not the displacement, so multiply the
9765 * displacement with the dividing parameter before passing it to
9766 * TXP. */
9767 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9769 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9771 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9772 stage, stage);
9773 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9775 else
9777 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9778 stage, stage);
9779 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9782 else
9784 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9787 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ret.%s%s);\n",
9788 stage, texture_function, proj ? "Proj" : "", stage, coord_mask, proj ? "w" : "");
9790 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9791 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9792 stage, stage - 1, stage - 1, stage - 1);
9794 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9796 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9797 stage, texture_function, proj ? "Proj" : "", stage, stage);
9799 else
9801 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].%s%s);\n",
9802 stage, texture_function, proj ? "Proj" : "", stage, stage, coord_mask, proj ? "w" : "");
9805 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9806 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9807 settings->op[stage].color_fixup);
9810 if (settings->color_key_enabled)
9811 shader_glsl_generate_colour_key_test(buffer, "tex0", "color_key[0]", "color_key[1]");
9813 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9815 /* Generate the main shader */
9816 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9818 BOOL op_equal;
9820 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9821 break;
9823 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9824 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9825 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9826 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9827 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9828 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9829 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9830 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9831 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9832 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9833 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9834 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9835 else
9836 op_equal = settings->op[stage].aop == settings->op[stage].cop
9837 && settings->op[stage].carg0 == settings->op[stage].aarg0
9838 && settings->op[stage].carg1 == settings->op[stage].aarg1
9839 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9841 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9843 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9844 settings->op[stage].cop, settings->op[stage].carg0,
9845 settings->op[stage].carg1, settings->op[stage].carg2);
9847 else if (op_equal)
9849 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9850 settings->op[stage].cop, settings->op[stage].carg0,
9851 settings->op[stage].carg1, settings->op[stage].carg2);
9853 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9854 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9856 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9857 settings->op[stage].cop, settings->op[stage].carg0,
9858 settings->op[stage].carg1, settings->op[stage].carg2);
9859 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9860 settings->op[stage].aop, settings->op[stage].aarg0,
9861 settings->op[stage].aarg1, settings->op[stage].aarg2);
9865 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9866 get_fragment_output(gl_info));
9868 if (settings->sRGB_write)
9869 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9871 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9873 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9874 if (!use_legacy_fragment_output(gl_info))
9875 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9877 shader_addline(buffer, "}\n");
9879 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9880 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9882 string_buffer_release(&priv->string_buffers, tex_reg_name);
9883 return shader_id;
9886 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9887 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9889 struct glsl_ffp_vertex_shader *shader;
9890 const struct wine_rb_entry *entry;
9892 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9893 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9895 if (!(shader = heap_alloc(sizeof(*shader))))
9896 return NULL;
9898 shader->desc.settings = *settings;
9899 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9900 list_init(&shader->linked_programs);
9901 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9902 ERR("Failed to insert ffp vertex shader.\n");
9904 return shader;
9907 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9908 const struct ffp_frag_settings *args, const struct wined3d_context_gl *context_gl)
9910 struct glsl_ffp_fragment_shader *glsl_desc;
9911 const struct ffp_frag_desc *desc;
9913 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9914 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9916 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9917 return NULL;
9919 glsl_desc->entry.settings = *args;
9920 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context_gl);
9921 list_init(&glsl_desc->linked_programs);
9922 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9924 return glsl_desc;
9928 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9929 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9931 unsigned int i;
9932 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9934 for (i = 0; i < vs_c_count; ++i)
9936 string_buffer_sprintf(name, "vs_c[%u]", i);
9937 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9939 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9941 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9943 string_buffer_sprintf(name, "vs_i[%u]", i);
9944 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9947 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9949 string_buffer_sprintf(name, "vs_b[%u]", i);
9950 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9953 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9954 vs->base_vertex_id_location = GL_EXTCALL(glGetUniformLocation(program_id, "base_vertex_id"));
9956 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9958 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9959 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9961 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9962 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9963 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9965 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9966 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9968 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9969 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9970 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9971 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9972 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9973 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9974 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
9976 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9977 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9978 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9979 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9980 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9981 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9982 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9983 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9984 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9985 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9986 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9987 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9988 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9989 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9990 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9991 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9992 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9993 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9994 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9995 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9996 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9997 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9998 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9999 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10001 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10002 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10003 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10004 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10005 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10006 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10007 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10009 string_buffer_release(&priv->string_buffers, name);
10012 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10013 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10015 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10018 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10019 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10021 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10024 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10025 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10027 unsigned int i;
10028 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10030 for (i = 0; i < ps_c_count; ++i)
10032 string_buffer_sprintf(name, "ps_c[%u]", i);
10033 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10035 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10037 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10039 string_buffer_sprintf(name, "ps_i[%u]", i);
10040 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10043 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10045 string_buffer_sprintf(name, "ps_b[%u]", i);
10046 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10049 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10051 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10052 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10053 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10054 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10055 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10056 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10057 string_buffer_sprintf(name, "tss_const%u", i);
10058 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10061 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10062 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10064 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10065 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10066 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10067 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10069 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10071 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10072 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10073 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10075 string_buffer_release(&priv->string_buffers, name);
10078 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10079 const struct wined3d_context_gl *context_gl, struct wined3d_shader *shader)
10081 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10082 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10083 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10084 struct glsl_cs_compiled_shader *gl_shaders;
10085 struct glsl_shader_private *shader_data;
10086 struct glsl_shader_prog_link *entry;
10087 GLuint shader_id, program_id;
10089 if (!(entry = heap_alloc(sizeof(*entry))))
10091 ERR("Out of memory.\n");
10092 return E_OUTOFMEMORY;
10095 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10097 ERR("Failed to allocate backend data.\n");
10098 heap_free(entry);
10099 return E_OUTOFMEMORY;
10101 shader_data = shader->backend_data;
10103 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10105 ERR("Failed to allocate GL shader array.\n");
10106 heap_free(entry);
10107 heap_free(shader->backend_data);
10108 shader->backend_data = NULL;
10109 return E_OUTOFMEMORY;
10111 shader_data->shader_array_size = 1;
10112 gl_shaders = shader_data->gl_shaders.cs;
10114 TRACE("Compiling compute shader %p.\n", shader);
10116 string_buffer_clear(buffer);
10117 shader_id = shader_glsl_generate_compute_shader(context_gl, buffer, &priv->string_buffers, shader);
10118 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10120 program_id = GL_EXTCALL(glCreateProgram());
10121 TRACE("Created new GLSL shader program %u.\n", program_id);
10123 entry->id = program_id;
10124 entry->vs.id = 0;
10125 entry->hs.id = 0;
10126 entry->ds.id = 0;
10127 entry->gs.id = 0;
10128 entry->ps.id = 0;
10129 entry->cs.id = shader_id;
10130 entry->constant_version = 0;
10131 entry->shader_controlled_clip_distances = 0;
10132 entry->ps.np2_fixup_info = NULL;
10133 add_glsl_program_entry(priv, entry);
10135 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10136 GL_EXTCALL(glAttachShader(program_id, shader_id));
10137 checkGLcall("glAttachShader");
10139 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10141 TRACE("Linking GLSL shader program %u.\n", program_id);
10142 GL_EXTCALL(glLinkProgram(program_id));
10143 shader_glsl_validate_link(gl_info, program_id);
10145 GL_EXTCALL(glUseProgram(program_id));
10146 checkGLcall("glUseProgram");
10147 shader_glsl_load_program_resources(context_gl, priv, program_id, shader);
10148 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10150 entry->constant_update_mask = 0;
10152 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10153 checkGLcall("glUseProgram");
10154 return WINED3D_OK;
10157 static GLuint find_glsl_compute_shader(const struct wined3d_context_gl *context_gl,
10158 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10160 struct glsl_shader_private *shader_data;
10162 if (!shader->backend_data)
10164 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10165 if (FAILED(shader_glsl_compile_compute_shader(priv, context_gl, shader)))
10167 ERR("Failed to compile compute shader %p.\n", shader);
10168 return 0;
10171 shader_data = shader->backend_data;
10172 return shader_data->gl_shaders.cs[0].id;
10175 /* Context activation is done by the caller. */
10176 static void set_glsl_compute_shader_program(const struct wined3d_context_gl *context_gl,
10177 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10179 struct glsl_shader_prog_link *entry;
10180 struct wined3d_shader *shader;
10181 struct glsl_program_key key;
10182 GLuint cs_id;
10184 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10185 return;
10187 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10189 WARN("Compute shader is NULL.\n");
10190 ctx_data->glsl_program = NULL;
10191 return;
10194 cs_id = find_glsl_compute_shader(context_gl, priv, shader);
10195 memset(&key, 0, sizeof(key));
10196 key.cs_id = cs_id;
10197 if (!(entry = get_glsl_program_entry(priv, &key)))
10198 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10199 ctx_data->glsl_program = entry;
10202 /* Context activation is done by the caller. */
10203 static void set_glsl_shader_program(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
10204 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10206 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
10207 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10208 const struct wined3d_shader *pre_rasterization_shader;
10209 const struct ps_np2fixup_info *np2fixup_info = NULL;
10210 struct wined3d_shader *hshader, *dshader, *gshader;
10211 struct glsl_shader_prog_link *entry = NULL;
10212 struct wined3d_shader *vshader = NULL;
10213 struct wined3d_shader *pshader = NULL;
10214 GLuint reorder_shader_id = 0;
10215 struct glsl_program_key key;
10216 GLuint program_id;
10217 unsigned int i;
10218 GLuint vs_id = 0;
10219 GLuint hs_id = 0;
10220 GLuint ds_id = 0;
10221 GLuint gs_id = 0;
10222 GLuint ps_id = 0;
10223 struct list *ps_list, *vs_list;
10224 WORD attribs_map;
10225 struct wined3d_string_buffer *tmp_name;
10227 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10229 vs_id = ctx_data->glsl_program->vs.id;
10230 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10232 if (use_vs(state))
10233 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10235 else if (use_vs(state))
10237 struct vs_compile_args vs_compile_args;
10239 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10241 find_vs_compile_args(state, vshader, context_gl->c.stream_info.swizzle_map, &vs_compile_args, &context_gl->c);
10242 vs_id = find_glsl_vertex_shader(context_gl, priv, vshader, &vs_compile_args);
10243 vs_list = &vshader->linked_programs;
10245 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10247 struct glsl_ffp_vertex_shader *ffp_shader;
10248 struct wined3d_ffp_vs_settings settings;
10250 wined3d_ffp_get_vs_settings(&context_gl->c, state, &settings);
10251 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10252 vs_id = ffp_shader->id;
10253 vs_list = &ffp_shader->linked_programs;
10256 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10257 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10258 hs_id = ctx_data->glsl_program->hs.id;
10259 else if (hshader)
10260 hs_id = find_glsl_hull_shader(context_gl, priv, hshader);
10262 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10263 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10265 ds_id = ctx_data->glsl_program->ds.id;
10267 else if (dshader)
10269 struct ds_compile_args args;
10271 find_ds_compile_args(state, dshader, &args, &context_gl->c);
10272 ds_id = find_glsl_domain_shader(context_gl, priv, dshader, &args);
10275 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10276 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10278 gs_id = ctx_data->glsl_program->gs.id;
10280 else if (gshader)
10282 struct gs_compile_args args;
10284 find_gs_compile_args(state, gshader, &args, &context_gl->c);
10285 gs_id = find_glsl_geometry_shader(context_gl, priv, gshader, &args);
10288 /* A pixel shader is not used when rasterization is disabled. */
10289 if (is_rasterization_disabled(gshader))
10291 ps_id = 0;
10292 ps_list = NULL;
10294 else if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10296 ps_id = ctx_data->glsl_program->ps.id;
10297 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10299 if (use_ps(state))
10300 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10302 else if (use_ps(state))
10304 struct ps_compile_args ps_compile_args;
10305 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10306 find_ps_compile_args(state, pshader, context_gl->c.stream_info.position_transformed,
10307 &ps_compile_args, &context_gl->c);
10308 ps_id = find_glsl_fragment_shader(context_gl, &priv->shader_buffer, &priv->string_buffers,
10309 pshader, &ps_compile_args, &np2fixup_info);
10310 ps_list = &pshader->linked_programs;
10312 else if (priv->fragment_pipe == &glsl_fragment_pipe
10313 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10315 struct glsl_ffp_fragment_shader *ffp_shader;
10316 struct ffp_frag_settings settings;
10318 gen_ffp_frag_op(&context_gl->c, state, &settings, FALSE);
10319 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context_gl);
10320 ps_id = ffp_shader->id;
10321 ps_list = &ffp_shader->linked_programs;
10324 key.vs_id = vs_id;
10325 key.hs_id = hs_id;
10326 key.ds_id = ds_id;
10327 key.gs_id = gs_id;
10328 key.ps_id = ps_id;
10329 key.cs_id = 0;
10330 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10332 ctx_data->glsl_program = entry;
10333 return;
10336 /* If we get to this point, then no matching program exists, so we create one */
10337 program_id = GL_EXTCALL(glCreateProgram());
10338 TRACE("Created new GLSL shader program %u.\n", program_id);
10340 /* Create the entry */
10341 entry = heap_alloc(sizeof(*entry));
10342 entry->id = program_id;
10343 entry->vs.id = vs_id;
10344 entry->hs.id = hs_id;
10345 entry->ds.id = ds_id;
10346 entry->gs.id = gs_id;
10347 entry->ps.id = ps_id;
10348 entry->cs.id = 0;
10349 entry->constant_version = 0;
10350 entry->shader_controlled_clip_distances = 0;
10351 entry->ps.np2_fixup_info = np2fixup_info;
10352 /* Add the hash table entry */
10353 add_glsl_program_entry(priv, entry);
10355 /* Set the current program */
10356 ctx_data->glsl_program = entry;
10358 /* Attach GLSL vshader */
10359 if (vs_id)
10361 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10362 GL_EXTCALL(glAttachShader(program_id, vs_id));
10363 checkGLcall("glAttachShader");
10365 list_add_head(vs_list, &entry->vs.shader_entry);
10368 if (vshader)
10370 attribs_map = vshader->reg_maps.input_registers;
10371 if (vshader->reg_maps.shader_version.major < 4)
10373 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10374 state->primitive_type == WINED3D_PT_POINTLIST && vshader->reg_maps.point_size,
10375 d3d_info->emulated_flatshading
10376 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10377 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10378 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10379 checkGLcall("glAttachShader");
10380 /* Flag the reorder function for deletion, it will be freed
10381 * automatically when the program is destroyed. */
10382 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10385 else
10387 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10390 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10392 /* Bind vertex attributes to a corresponding index number to match
10393 * the same index numbers as ARB_vertex_programs (makes loading
10394 * vertex attributes simpler). With this method, we can use the
10395 * exact same code to load the attributes later for both ARB and
10396 * GLSL shaders.
10398 * We have to do this here because we need to know the Program ID
10399 * in order to make the bindings work, and it has to be done prior
10400 * to linking the GLSL program. */
10401 tmp_name = string_buffer_get(&priv->string_buffers);
10402 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10404 if (!(attribs_map & 1))
10405 continue;
10407 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10408 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10409 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10411 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10412 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10413 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10414 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10417 checkGLcall("glBindAttribLocation");
10419 if (!use_legacy_fragment_output(gl_info))
10421 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
10423 string_buffer_sprintf(tmp_name, "color_out%u", i);
10424 if (state->blend_state && state->blend_state->dual_source)
10425 GL_EXTCALL(glBindFragDataLocationIndexed(program_id, 0, i, tmp_name->buffer));
10426 else
10427 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10428 checkGLcall("glBindFragDataLocation");
10431 string_buffer_release(&priv->string_buffers, tmp_name);
10434 if (hshader)
10436 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10437 GL_EXTCALL(glAttachShader(program_id, hs_id));
10438 checkGLcall("glAttachShader");
10440 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10443 if (dshader)
10445 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10446 GL_EXTCALL(glAttachShader(program_id, ds_id));
10447 checkGLcall("glAttachShader");
10449 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10452 if (gshader)
10454 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10455 GL_EXTCALL(glAttachShader(program_id, gs_id));
10456 checkGLcall("glAttachShader");
10458 shader_glsl_init_transform_feedback(context_gl, priv, program_id, gshader);
10460 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10463 /* Attach GLSL pshader */
10464 if (ps_id)
10466 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10467 GL_EXTCALL(glAttachShader(program_id, ps_id));
10468 checkGLcall("glAttachShader");
10470 list_add_head(ps_list, &entry->ps.shader_entry);
10473 /* Link the program */
10474 TRACE("Linking GLSL shader program %u.\n", program_id);
10475 GL_EXTCALL(glLinkProgram(program_id));
10476 shader_glsl_validate_link(gl_info, program_id);
10478 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10479 vshader ? vshader->limits->constant_float : 0);
10480 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10481 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10482 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10483 pshader ? pshader->limits->constant_float : 0);
10484 checkGLcall("find glsl program uniform locations");
10486 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10487 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10489 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10490 entry->shader_controlled_clip_distances = 1;
10491 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10494 if (needs_legacy_glsl_syntax(gl_info))
10496 if (pshader && pshader->reg_maps.shader_version.major >= 3
10497 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10499 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10500 entry->vs.vertex_color_clamp = GL_FALSE;
10502 else
10504 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10507 else
10509 /* With core profile we never change vertex_color_clamp from
10510 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10511 * glClampColorARB(). */
10512 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10515 /* Set the shader to allow uniform loading on it */
10516 GL_EXTCALL(glUseProgram(program_id));
10517 checkGLcall("glUseProgram");
10519 entry->constant_update_mask = 0;
10520 if (vshader)
10522 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10523 if (vshader->reg_maps.integer_constants)
10524 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10525 if (vshader->reg_maps.boolean_constants)
10526 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10527 if (entry->vs.pos_fixup_location != -1)
10528 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10529 if (entry->vs.base_vertex_id_location != -1)
10530 entry->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
10532 shader_glsl_load_program_resources(context_gl, priv, program_id, vshader);
10534 else
10536 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10537 | WINED3D_SHADER_CONST_FFP_PROJ;
10539 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10541 if (entry->vs.modelview_matrix_location[i] != -1)
10543 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10544 break;
10548 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10550 if (entry->vs.texture_matrix_location[i] != -1)
10552 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10553 break;
10556 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10557 || entry->vs.material_specular_location != -1
10558 || entry->vs.material_emissive_location != -1
10559 || entry->vs.material_shininess_location != -1)
10560 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10561 if (entry->vs.light_ambient_location != -1)
10562 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10564 if (entry->vs.clip_planes_location != -1)
10565 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10566 if (entry->vs.pointsize_min_location != -1)
10567 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10569 if (hshader)
10570 shader_glsl_load_program_resources(context_gl, priv, program_id, hshader);
10572 if (dshader)
10574 if (entry->ds.pos_fixup_location != -1)
10575 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10577 shader_glsl_load_program_resources(context_gl, priv, program_id, dshader);
10580 if (gshader)
10582 if (entry->gs.pos_fixup_location != -1)
10583 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10585 shader_glsl_load_program_resources(context_gl, priv, program_id, gshader);
10588 if (ps_id)
10590 if (pshader)
10592 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10593 if (pshader->reg_maps.integer_constants)
10594 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10595 if (pshader->reg_maps.boolean_constants)
10596 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10597 if (entry->ps.ycorrection_location != -1)
10598 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10600 shader_glsl_load_program_resources(context_gl, priv, program_id, pshader);
10601 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10603 else
10605 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10607 shader_glsl_load_samplers(&context_gl->c, priv, program_id, NULL);
10610 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10612 if (entry->ps.bumpenv_mat_location[i] != -1)
10614 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10615 break;
10619 if (entry->ps.fog_color_location != -1)
10620 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10621 if (entry->ps.alpha_test_ref_location != -1)
10622 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10623 if (entry->ps.np2_fixup_location != -1)
10624 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10625 if (entry->ps.color_key_location != -1)
10626 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10630 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10632 struct wined3d_device *device = shader->device;
10633 struct wined3d_context *context;
10635 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10637 context = context_acquire(device, NULL, 0);
10638 shader_glsl_compile_compute_shader(shader_priv, wined3d_context_gl(context), shader);
10639 context_release(context);
10643 /* Context activation is done by the caller. */
10644 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10645 const struct wined3d_state *state)
10647 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10648 struct glsl_context_data *ctx_data = context->shader_backend_data;
10649 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10650 struct shader_glsl_priv *priv = shader_priv;
10651 struct glsl_shader_prog_link *glsl_program;
10652 GLenum current_vertex_color_clamp;
10653 GLuint program_id, prev_id;
10655 priv->vertex_pipe->vp_enable(context, !use_vs(state));
10656 priv->fragment_pipe->fp_enable(context, !use_ps(state));
10658 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10659 set_glsl_shader_program(context_gl, state, priv, ctx_data);
10660 glsl_program = ctx_data->glsl_program;
10662 if (glsl_program)
10664 program_id = glsl_program->id;
10665 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10666 if (glsl_program->shader_controlled_clip_distances)
10667 wined3d_context_gl_enable_clip_distances(context_gl, glsl_program->clip_distance_mask);
10669 else
10671 program_id = 0;
10672 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10675 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10677 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10678 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10680 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10681 checkGLcall("glClampColorARB");
10683 else
10685 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10689 TRACE("Using GLSL program %u.\n", program_id);
10691 if (prev_id != program_id)
10693 GL_EXTCALL(glUseProgram(program_id));
10694 checkGLcall("glUseProgram");
10696 if (glsl_program)
10697 context->constant_update_mask |= glsl_program->constant_update_mask;
10700 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10703 /* Context activation is done by the caller. */
10704 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10705 const struct wined3d_state *state)
10707 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10708 struct glsl_context_data *ctx_data = context->shader_backend_data;
10709 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10710 struct shader_glsl_priv *priv = shader_priv;
10711 GLuint program_id, prev_id;
10713 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10714 set_glsl_compute_shader_program(context_gl, state, priv, ctx_data);
10715 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10717 TRACE("Using GLSL program %u.\n", program_id);
10719 if (prev_id != program_id)
10721 GL_EXTCALL(glUseProgram(program_id));
10722 checkGLcall("glUseProgram");
10725 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10726 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10727 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10728 | (1u << WINED3D_SHADER_TYPE_HULL)
10729 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10732 /* "context" is not necessarily the currently active context. */
10733 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10735 struct glsl_context_data *ctx_data = context->shader_backend_data;
10737 ctx_data->glsl_program = NULL;
10738 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10739 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10740 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10741 | (1u << WINED3D_SHADER_TYPE_HULL)
10742 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10743 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10746 /* Context activation is done by the caller. */
10747 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10749 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10750 struct glsl_context_data *ctx_data = context->shader_backend_data;
10751 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10752 struct shader_glsl_priv *priv = shader_priv;
10754 shader_glsl_invalidate_current_program(context);
10755 GL_EXTCALL(glUseProgram(0));
10756 checkGLcall("glUseProgram");
10758 priv->vertex_pipe->vp_enable(context, FALSE);
10759 priv->fragment_pipe->fp_enable(context, FALSE);
10761 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10763 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10764 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10765 checkGLcall("glClampColorARB");
10769 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10770 const struct glsl_shader_prog_link *program)
10772 const struct glsl_context_data *ctx_data;
10773 struct wined3d_context *context;
10774 unsigned int i;
10776 for (i = 0; i < device->context_count; ++i)
10778 context = device->contexts[i];
10779 ctx_data = context->shader_backend_data;
10781 if (ctx_data->glsl_program == program)
10782 shader_glsl_invalidate_current_program(context);
10786 static void shader_glsl_destroy(struct wined3d_shader *shader)
10788 struct glsl_shader_private *shader_data = shader->backend_data;
10789 struct wined3d_device *device = shader->device;
10790 struct shader_glsl_priv *priv = device->shader_priv;
10791 const struct wined3d_gl_info *gl_info;
10792 const struct list *linked_programs;
10793 struct wined3d_context *context;
10795 if (!shader_data || !shader_data->num_gl_shaders)
10797 heap_free(shader_data);
10798 shader->backend_data = NULL;
10799 return;
10802 context = context_acquire(device, NULL, 0);
10803 gl_info = wined3d_context_gl(context)->gl_info;
10805 TRACE("Deleting linked programs.\n");
10806 linked_programs = &shader->linked_programs;
10807 if (linked_programs->next)
10809 struct glsl_shader_prog_link *entry, *entry2;
10810 UINT i;
10812 switch (shader->reg_maps.shader_version.type)
10814 case WINED3D_SHADER_TYPE_PIXEL:
10816 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10818 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10820 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10821 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10822 checkGLcall("glDeleteShader");
10824 heap_free(shader_data->gl_shaders.ps);
10826 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10827 struct glsl_shader_prog_link, ps.shader_entry)
10829 shader_glsl_invalidate_contexts_program(device, entry);
10830 delete_glsl_program_entry(priv, gl_info, entry);
10833 break;
10836 case WINED3D_SHADER_TYPE_VERTEX:
10838 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10840 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10842 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10843 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10844 checkGLcall("glDeleteShader");
10846 heap_free(shader_data->gl_shaders.vs);
10848 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10849 struct glsl_shader_prog_link, vs.shader_entry)
10851 shader_glsl_invalidate_contexts_program(device, entry);
10852 delete_glsl_program_entry(priv, gl_info, entry);
10855 break;
10858 case WINED3D_SHADER_TYPE_HULL:
10860 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10862 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10864 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10865 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10866 checkGLcall("glDeleteShader");
10868 heap_free(shader_data->gl_shaders.hs);
10870 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10871 struct glsl_shader_prog_link, hs.shader_entry)
10873 shader_glsl_invalidate_contexts_program(device, entry);
10874 delete_glsl_program_entry(priv, gl_info, entry);
10877 break;
10880 case WINED3D_SHADER_TYPE_DOMAIN:
10882 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10884 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10886 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10887 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10888 checkGLcall("glDeleteShader");
10890 heap_free(shader_data->gl_shaders.ds);
10892 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10893 struct glsl_shader_prog_link, ds.shader_entry)
10895 shader_glsl_invalidate_contexts_program(device, entry);
10896 delete_glsl_program_entry(priv, gl_info, entry);
10899 break;
10902 case WINED3D_SHADER_TYPE_GEOMETRY:
10904 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10906 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10908 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10909 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10910 checkGLcall("glDeleteShader");
10912 heap_free(shader_data->gl_shaders.gs);
10914 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10915 struct glsl_shader_prog_link, gs.shader_entry)
10917 shader_glsl_invalidate_contexts_program(device, entry);
10918 delete_glsl_program_entry(priv, gl_info, entry);
10921 break;
10924 case WINED3D_SHADER_TYPE_COMPUTE:
10926 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10928 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10930 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10931 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10932 checkGLcall("glDeleteShader");
10934 heap_free(shader_data->gl_shaders.cs);
10936 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10937 struct glsl_shader_prog_link, cs.shader_entry)
10939 shader_glsl_invalidate_contexts_program(device, entry);
10940 delete_glsl_program_entry(priv, gl_info, entry);
10943 break;
10946 default:
10947 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10948 break;
10952 heap_free(shader->backend_data);
10953 shader->backend_data = NULL;
10955 context_release(context);
10958 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10960 const struct glsl_program_key *k = key;
10961 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10962 const struct glsl_shader_prog_link, program_lookup_entry);
10964 if (k->vs_id > prog->vs.id) return 1;
10965 else if (k->vs_id < prog->vs.id) return -1;
10967 if (k->gs_id > prog->gs.id) return 1;
10968 else if (k->gs_id < prog->gs.id) return -1;
10970 if (k->ps_id > prog->ps.id) return 1;
10971 else if (k->ps_id < prog->ps.id) return -1;
10973 if (k->hs_id > prog->hs.id) return 1;
10974 else if (k->hs_id < prog->hs.id) return -1;
10976 if (k->ds_id > prog->ds.id) return 1;
10977 else if (k->ds_id < prog->ds.id) return -1;
10979 if (k->cs_id > prog->cs.id) return 1;
10980 else if (k->cs_id < prog->cs.id) return -1;
10982 return 0;
10985 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10987 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10988 + constant_count * sizeof(*heap->contained)
10989 + constant_count * sizeof(*heap->positions);
10990 void *mem;
10992 if (!(mem = heap_alloc(size)))
10994 ERR("Failed to allocate memory\n");
10995 return FALSE;
10998 heap->entries = mem;
10999 heap->entries[1].version = 0;
11000 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11001 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11002 heap->positions = (unsigned int *)(heap->contained + constant_count);
11003 heap->size = 1;
11005 return TRUE;
11008 static void constant_heap_free(struct constant_heap *heap)
11010 heap_free(heap->entries);
11013 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11014 const struct wined3d_fragment_pipe_ops *fragment_pipe)
11016 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11017 struct fragment_caps fragment_caps;
11018 void *vertex_priv, *fragment_priv;
11019 struct shader_glsl_priv *priv;
11021 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11022 return E_OUTOFMEMORY;
11024 string_buffer_list_init(&priv->string_buffers);
11026 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11028 ERR("Failed to initialize vertex pipe.\n");
11029 heap_free(priv);
11030 return E_FAIL;
11033 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11035 ERR("Failed to initialize fragment pipe.\n");
11036 vertex_pipe->vp_free(device, NULL);
11037 heap_free(priv);
11038 return E_FAIL;
11041 if (!string_buffer_init(&priv->shader_buffer))
11043 ERR("Failed to initialize shader buffer.\n");
11044 goto fail;
11047 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11049 ERR("Failed to allocate memory.\n");
11050 goto fail;
11053 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11055 ERR("Failed to initialize vertex shader constant heap\n");
11056 goto fail;
11059 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11061 ERR("Failed to initialize pixel shader constant heap\n");
11062 goto fail;
11065 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11067 priv->next_constant_version = 1;
11068 priv->vertex_pipe = vertex_pipe;
11069 priv->fragment_pipe = fragment_pipe;
11070 fragment_pipe->get_caps(device->adapter, &fragment_caps);
11071 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11072 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11074 device->vertex_priv = vertex_priv;
11075 device->fragment_priv = fragment_priv;
11076 device->shader_priv = priv;
11078 return WINED3D_OK;
11080 fail:
11081 constant_heap_free(&priv->pconst_heap);
11082 constant_heap_free(&priv->vconst_heap);
11083 heap_free(priv->stack);
11084 string_buffer_free(&priv->shader_buffer);
11085 fragment_pipe->free_private(device, NULL);
11086 vertex_pipe->vp_free(device, NULL);
11087 heap_free(priv);
11088 return E_OUTOFMEMORY;
11091 /* Context activation is done by the caller. */
11092 static void shader_glsl_free(struct wined3d_device *device, struct wined3d_context *context)
11094 struct shader_glsl_priv *priv = device->shader_priv;
11096 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11097 constant_heap_free(&priv->pconst_heap);
11098 constant_heap_free(&priv->vconst_heap);
11099 heap_free(priv->stack);
11100 string_buffer_list_cleanup(&priv->string_buffers);
11101 string_buffer_free(&priv->shader_buffer);
11102 priv->fragment_pipe->free_private(device, context);
11103 priv->vertex_pipe->vp_free(device, context);
11105 heap_free(device->shader_priv);
11106 device->shader_priv = NULL;
11109 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11111 struct glsl_context_data *ctx_data;
11113 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11114 return FALSE;
11115 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11116 context->shader_backend_data = ctx_data;
11117 return TRUE;
11120 static void shader_glsl_free_context_data(struct wined3d_context *context)
11122 heap_free(context->shader_backend_data);
11125 static void shader_glsl_init_context_state(struct wined3d_context *context)
11127 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11128 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11130 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11131 checkGLcall("GL_PROGRAM_POINT_SIZE");
11134 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11136 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11137 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11138 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11140 if (shader_model_4
11141 && gl_info->supported[ARB_COMPUTE_SHADER]
11142 && gl_info->supported[ARB_CULL_DISTANCE]
11143 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11144 && gl_info->supported[ARB_GPU_SHADER5]
11145 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11146 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11147 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11148 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11149 && gl_info->supported[ARB_TESSELLATION_SHADER]
11150 && gl_info->supported[ARB_TEXTURE_GATHER]
11151 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11152 return 5;
11154 if (shader_model_4)
11155 return 4;
11157 /* Support for texldd and texldl instructions in pixel shaders is required
11158 * for SM3. */
11159 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11160 return 3;
11162 return 2;
11165 static void shader_glsl_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
11167 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11168 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11170 TRACE("Shader model %u.\n", shader_model);
11172 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11173 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11174 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11175 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11176 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11177 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11179 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11180 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11182 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11183 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11184 caps->varying_count = gl_info->limits.glsl_varyings;
11186 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11187 * Direct3D minimum requirement.
11189 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11190 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11192 * The problem is that the refrast clamps temporary results in the shader to
11193 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11194 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11195 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11196 * offer a way to query this.
11198 if (shader_model >= 4)
11199 caps->ps_1x_max_value = FLT_MAX;
11200 else
11201 caps->ps_1x_max_value = 1024.0f;
11203 /* Ideally we'd only set caps like sRGB writes here if supported by both
11204 * the shader backend and the fragment pipe, but we can get called before
11205 * shader_glsl_alloc(). */
11206 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11207 | WINED3D_SHADER_CAP_SRGB_WRITE;
11208 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
11209 caps->wined3d_caps |= WINED3D_SHADER_CAP_OUTPUT_INTERPOLATION;
11210 if (shader_glsl_full_ffp_varyings(gl_info))
11211 caps->wined3d_caps |= WINED3D_SHADER_CAP_FULL_FFP_VARYINGS;
11214 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11216 /* We support everything except YUV conversions. */
11217 return !is_complex_fixup(fixup);
11220 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11222 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11223 /* WINED3DSIH_ADD */ shader_glsl_binop,
11224 /* WINED3DSIH_AND */ shader_glsl_binop,
11225 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11226 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11227 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11228 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11229 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11230 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11231 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11232 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11233 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11234 /* WINED3DSIH_BEM */ shader_glsl_bem,
11235 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11236 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11237 /* WINED3DSIH_BREAK */ shader_glsl_break,
11238 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11239 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11240 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11241 /* WINED3DSIH_CALL */ shader_glsl_call,
11242 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11243 /* WINED3DSIH_CASE */ shader_glsl_case,
11244 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11245 /* WINED3DSIH_CND */ shader_glsl_cnd,
11246 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11247 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11248 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11249 /* WINED3DSIH_CRS */ shader_glsl_cross,
11250 /* WINED3DSIH_CUT */ shader_glsl_cut,
11251 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11252 /* WINED3DSIH_DCL */ shader_glsl_nop,
11253 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11254 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11255 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11256 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11257 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11258 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11259 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11260 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11261 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11262 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11263 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11264 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11265 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11266 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11267 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11268 /* WINED3DSIH_DCL_INPUT_PS_SGV */ shader_glsl_nop,
11269 /* WINED3DSIH_DCL_INPUT_PS_SIV */ shader_glsl_nop,
11270 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11271 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11272 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11273 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11274 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11275 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11276 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11277 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11278 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11279 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11280 /* WINED3DSIH_DCL_STREAM */ NULL,
11281 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11282 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11283 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11284 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11285 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11286 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11287 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11288 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11289 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11290 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11291 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11292 /* WINED3DSIH_DEF */ shader_glsl_nop,
11293 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11294 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11295 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11296 /* WINED3DSIH_DIV */ shader_glsl_binop,
11297 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11298 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11299 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11300 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11301 /* WINED3DSIH_DST */ shader_glsl_dst,
11302 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11303 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11304 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11305 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11306 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11307 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11308 /* WINED3DSIH_ELSE */ shader_glsl_else,
11309 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11310 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11311 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11312 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11313 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11314 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11315 /* WINED3DSIH_EQ */ shader_glsl_relop,
11316 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ shader_glsl_interpolate,
11317 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11318 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11319 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11320 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11321 /* WINED3DSIH_FCALL */ NULL,
11322 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11323 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11324 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11325 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11326 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11327 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11328 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11329 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11330 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11331 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11332 /* WINED3DSIH_GE */ shader_glsl_relop,
11333 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11334 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11335 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11336 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11337 /* WINED3DSIH_IADD */ shader_glsl_binop,
11338 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11339 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11340 /* WINED3DSIH_IF */ shader_glsl_if,
11341 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11342 /* WINED3DSIH_IGE */ shader_glsl_relop,
11343 /* WINED3DSIH_ILT */ shader_glsl_relop,
11344 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11345 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11346 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11347 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11348 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11349 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11350 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11351 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11352 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11353 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11354 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11355 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11356 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11357 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11358 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11359 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11360 /* WINED3DSIH_INE */ shader_glsl_relop,
11361 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11362 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11363 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11364 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11365 /* WINED3DSIH_LABEL */ shader_glsl_label,
11366 /* WINED3DSIH_LD */ shader_glsl_ld,
11367 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11368 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11369 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11370 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11371 /* WINED3DSIH_LIT */ shader_glsl_lit,
11372 /* WINED3DSIH_LOD */ NULL,
11373 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11374 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11375 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11376 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11377 /* WINED3DSIH_LT */ shader_glsl_relop,
11378 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11379 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11380 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11381 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11382 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11383 /* WINED3DSIH_MAD */ shader_glsl_mad,
11384 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11385 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11386 /* WINED3DSIH_MOV */ shader_glsl_mov,
11387 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11388 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11389 /* WINED3DSIH_MUL */ shader_glsl_binop,
11390 /* WINED3DSIH_NE */ shader_glsl_relop,
11391 /* WINED3DSIH_NOP */ shader_glsl_nop,
11392 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11393 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11394 /* WINED3DSIH_OR */ shader_glsl_binop,
11395 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11396 /* WINED3DSIH_POW */ shader_glsl_pow,
11397 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11398 /* WINED3DSIH_REP */ shader_glsl_rep,
11399 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11400 /* WINED3DSIH_RET */ shader_glsl_ret,
11401 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11402 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11403 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11404 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11405 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11406 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11407 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11408 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11409 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11410 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11411 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11412 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11413 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11414 /* WINED3DSIH_SAMPLE_POS */ NULL,
11415 /* WINED3DSIH_SETP */ NULL,
11416 /* WINED3DSIH_SGE */ shader_glsl_compare,
11417 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11418 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11419 /* WINED3DSIH_SLT */ shader_glsl_compare,
11420 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11421 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11422 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11423 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11424 /* WINED3DSIH_SUB */ shader_glsl_binop,
11425 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11426 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11427 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11428 /* WINED3DSIH_TEX */ shader_glsl_tex,
11429 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11430 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11431 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11432 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11433 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11434 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11435 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11436 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11437 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11438 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11439 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11440 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11441 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11442 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11443 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11444 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11445 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11446 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11447 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11448 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11449 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11450 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11451 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11452 /* WINED3DSIH_UGE */ shader_glsl_relop,
11453 /* WINED3DSIH_ULT */ shader_glsl_relop,
11454 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11455 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11456 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11457 /* WINED3DSIH_USHR */ shader_glsl_binop,
11458 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11459 /* WINED3DSIH_XOR */ shader_glsl_binop,
11462 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11463 SHADER_HANDLER hw_fct;
11465 /* Select handler */
11466 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11468 /* Unhandled opcode */
11469 if (!hw_fct)
11471 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11472 return;
11474 hw_fct(ins);
11476 shader_glsl_add_instruction_modifiers(ins);
11479 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11481 struct shader_glsl_priv *priv = shader_priv;
11483 return priv->ffp_proj_control;
11486 const struct wined3d_shader_backend_ops glsl_shader_backend =
11488 shader_glsl_handle_instruction,
11489 shader_glsl_precompile,
11490 shader_glsl_select,
11491 shader_glsl_select_compute,
11492 shader_glsl_disable,
11493 shader_glsl_update_float_vertex_constants,
11494 shader_glsl_update_float_pixel_constants,
11495 shader_glsl_load_constants,
11496 shader_glsl_destroy,
11497 shader_glsl_alloc,
11498 shader_glsl_free,
11499 shader_glsl_allocate_context_data,
11500 shader_glsl_free_context_data,
11501 shader_glsl_init_context_state,
11502 shader_glsl_get_caps,
11503 shader_glsl_color_fixup_supported,
11504 shader_glsl_has_ffp_proj_control,
11507 static void glsl_vertex_pipe_vp_enable(const struct wined3d_context *context, BOOL enable) {}
11509 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_adapter *adapter, struct wined3d_vertex_caps *caps)
11511 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11513 caps->xyzrhw = TRUE;
11514 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11515 caps->ffp_generic_attributes = TRUE;
11516 caps->max_active_lights = WINED3D_MAX_ACTIVE_LIGHTS;
11517 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11518 caps->max_vertex_blend_matrix_index = 0;
11519 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11520 | WINED3DVTXPCAPS_MATERIALSOURCE7
11521 | WINED3DVTXPCAPS_VERTEXFOG
11522 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11523 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11524 | WINED3DVTXPCAPS_LOCALVIEWER
11525 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11526 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11527 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11528 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11531 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11533 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11534 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11535 return 0;
11538 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11540 struct shader_glsl_priv *priv;
11542 if (shader_backend == &glsl_shader_backend)
11544 priv = shader_priv;
11545 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11546 return priv;
11549 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11551 return NULL;
11554 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *param)
11556 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11557 struct glsl_ffp_vertex_shader, desc.entry);
11558 struct glsl_shader_prog_link *program, *program2;
11559 struct glsl_ffp_destroy_ctx *ctx = param;
11560 const struct wined3d_gl_info *gl_info;
11562 gl_info = ctx->context_gl->gl_info;
11563 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11564 struct glsl_shader_prog_link, vs.shader_entry)
11566 delete_glsl_program_entry(ctx->priv, gl_info, program);
11568 GL_EXTCALL(glDeleteShader(shader->id));
11569 heap_free(shader);
11572 /* Context activation is done by the caller. */
11573 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device, struct wined3d_context *context)
11575 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11576 struct shader_glsl_priv *priv = device->vertex_priv;
11577 struct glsl_ffp_destroy_ctx ctx;
11579 ctx.priv = priv;
11580 ctx.context_gl = context_gl;
11581 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11584 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11585 const struct wined3d_state *state, DWORD state_id) {}
11587 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11588 const struct wined3d_state *state, DWORD state_id)
11590 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11593 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11594 const struct wined3d_state *state, DWORD state_id)
11596 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11597 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11598 BOOL specular = !!(context->stream_info.use_map & (1u << WINED3D_FFP_SPECULAR));
11599 BOOL diffuse = !!(context->stream_info.use_map & (1u << WINED3D_FFP_DIFFUSE));
11600 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11601 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11602 BOOL transformed = context->stream_info.position_transformed;
11603 BOOL wasrhw = context->last_was_rhw;
11604 unsigned int i;
11606 context->last_was_rhw = transformed;
11608 /* If the vertex declaration contains a transformed position attribute,
11609 * the draw uses the fixed function vertex pipeline regardless of any
11610 * vertex shader set by the application. */
11611 if (transformed != wasrhw
11612 || context->stream_info.swizzle_map != context->last_swizzle_map)
11613 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11615 context->last_swizzle_map = context->stream_info.swizzle_map;
11617 if (!use_vs(state))
11619 if (context->last_was_vshader)
11621 if (legacy_clip_planes)
11622 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11623 clipplane(context, state, STATE_CLIPPLANE(i));
11624 else
11625 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11628 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11630 /* Because of settings->texcoords, we have to regenerate the vertex
11631 * shader on a vdecl change if there aren't enough varyings to just
11632 * always output all the texture coordinates.
11634 * Likewise, we have to invalidate the shader when using per-vertex
11635 * colours and diffuse/specular attribute presence changes, or when
11636 * normal presence changes. */
11637 if (!shader_glsl_full_ffp_varyings(gl_info) || (state->render_states[WINED3D_RS_COLORVERTEX]
11638 && (diffuse != context->last_was_diffuse || specular != context->last_was_specular))
11639 || normal != context->last_was_normal)
11640 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11642 if (use_ps(state)
11643 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11644 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11645 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11647 else
11649 if (!context->last_was_vshader)
11651 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11652 if (legacy_clip_planes)
11653 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11654 clipplane(context, state, STATE_CLIPPLANE(i));
11655 else
11656 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11660 context->last_was_vshader = use_vs(state);
11661 context->last_was_diffuse = diffuse;
11662 context->last_was_specular = specular;
11663 context->last_was_normal = normal;
11666 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11667 const struct wined3d_state *state, DWORD state_id)
11669 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11670 /* Different vertex shaders potentially require a different vertex attributes setup. */
11671 if (!isStateDirty(context, STATE_VDECL))
11672 context_apply_state(context, state, STATE_VDECL);
11675 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11676 const struct wined3d_state *state, DWORD state_id)
11678 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11679 * winding) are defined in Hull Shaders, while in GLSL those are
11680 * specified in Tessellation Evaluation Shaders. */
11681 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11683 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11684 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11687 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11688 const struct wined3d_state *state, DWORD state_id)
11690 struct glsl_context_data *ctx_data = context->shader_backend_data;
11691 BOOL rasterization_disabled;
11693 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11694 if (ctx_data->rasterization_disabled != rasterization_disabled)
11695 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11696 ctx_data->rasterization_disabled = rasterization_disabled;
11698 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11699 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11700 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11701 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11702 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11705 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11706 const struct wined3d_state *state, DWORD state_id)
11708 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11709 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11710 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11711 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11712 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11713 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11714 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11717 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11718 const struct wined3d_state *state, DWORD state_id)
11720 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11723 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11724 const struct wined3d_state *state, DWORD state_id)
11726 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11729 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11731 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11732 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11733 unsigned int k;
11735 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11736 | WINED3D_SHADER_CONST_FFP_LIGHTS
11737 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11739 if (needs_legacy_glsl_syntax(gl_info))
11741 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11743 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11744 clipplane(context, state, STATE_CLIPPLANE(k));
11747 else
11749 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11753 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11754 const struct wined3d_state *state, DWORD state_id)
11756 /* Table fog behavior depends on the projection matrix. */
11757 if (state->render_states[WINED3D_RS_FOGENABLE]
11758 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11759 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11760 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11763 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11764 const struct wined3d_state *state, DWORD state_id)
11766 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11767 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11768 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11769 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11770 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11771 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11774 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11775 const struct wined3d_state *state, DWORD state_id)
11777 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11780 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11781 const struct wined3d_state *state, DWORD state_id)
11783 DWORD sampler = state_id - STATE_SAMPLER(0);
11784 const struct wined3d_texture *texture = state->textures[sampler];
11785 BOOL np2;
11787 if (!texture)
11788 return;
11790 if (sampler >= WINED3D_MAX_TEXTURES)
11791 return;
11793 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11794 || context->lastWasPow2Texture & (1u << sampler))
11796 if (np2)
11797 context->lastWasPow2Texture |= 1u << sampler;
11798 else
11799 context->lastWasPow2Texture &= ~(1u << sampler);
11801 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11805 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11806 const struct wined3d_state *state, DWORD state_id)
11808 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11811 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11812 const struct wined3d_state *state, DWORD state_id)
11814 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11817 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11818 const struct wined3d_state *state, DWORD state_id)
11820 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11823 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11824 const struct wined3d_state *state, DWORD state_id)
11826 if (!use_vs(state))
11827 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11830 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11831 const struct wined3d_state *state, DWORD state_id)
11833 static unsigned int once;
11835 if (state->primitive_type == WINED3D_PT_POINTLIST
11836 && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11837 FIXME("Non-point sprite points not supported in core profile.\n");
11840 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11841 const struct wined3d_state *state, DWORD state_id)
11843 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11846 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11847 const struct wined3d_state *state, DWORD state_id)
11849 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11850 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11851 UINT index = state_id - STATE_CLIPPLANE(0);
11853 if (index >= gl_info->limits.user_clip_distances)
11854 return;
11856 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11859 static const struct wined3d_state_entry_template glsl_vertex_pipe_vp_states[] =
11861 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11862 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11863 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11864 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11865 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11866 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11867 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11868 /* Clip planes */
11869 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11870 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11871 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11872 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11873 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11874 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11875 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11876 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11877 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11878 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11879 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11880 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11881 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11882 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11883 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11884 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11885 /* Lights */
11886 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11887 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11888 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11889 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11890 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11891 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11892 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11893 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11894 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11895 /* Viewport */
11896 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11897 /* Transform states */
11898 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11899 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11900 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11901 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11902 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11903 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11904 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11905 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11906 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11907 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11908 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11909 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11910 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11911 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11912 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11913 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11914 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11915 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11916 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11917 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11918 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11919 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11920 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11921 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11922 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11923 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11924 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11925 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11926 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11927 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11928 /* Fog */
11929 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11930 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11931 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11932 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11933 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11934 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11935 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11936 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11937 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11938 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11939 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11940 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11941 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11942 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11943 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11944 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11945 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11946 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11947 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11948 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11949 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11950 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11951 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11952 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11953 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11954 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11955 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11956 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11957 /* NP2 texture matrix fixups. They are not needed if
11958 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11959 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11960 * matrix. */
11961 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11962 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11963 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11964 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11965 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11966 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11967 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11968 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11969 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11970 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11971 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11972 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11973 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11974 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11975 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11976 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11977 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11978 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11979 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11980 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11981 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11982 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11983 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11984 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11985 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11986 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11987 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11988 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11991 /* TODO:
11992 * - Implement vertex tweening. */
11993 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11995 glsl_vertex_pipe_vp_enable,
11996 glsl_vertex_pipe_vp_get_caps,
11997 glsl_vertex_pipe_vp_get_emul_mask,
11998 glsl_vertex_pipe_vp_alloc,
11999 glsl_vertex_pipe_vp_free,
12000 glsl_vertex_pipe_vp_states,
12003 static void glsl_fragment_pipe_enable(const struct wined3d_context *context, BOOL enable)
12005 /* Nothing to do. */
12008 static void glsl_fragment_pipe_get_caps(const struct wined3d_adapter *adapter, struct fragment_caps *caps)
12010 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
12012 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
12013 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
12014 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
12015 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12016 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12017 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12018 | WINED3DTEXOPCAPS_SELECTARG1
12019 | WINED3DTEXOPCAPS_SELECTARG2
12020 | WINED3DTEXOPCAPS_MODULATE4X
12021 | WINED3DTEXOPCAPS_MODULATE2X
12022 | WINED3DTEXOPCAPS_MODULATE
12023 | WINED3DTEXOPCAPS_ADDSIGNED2X
12024 | WINED3DTEXOPCAPS_ADDSIGNED
12025 | WINED3DTEXOPCAPS_ADD
12026 | WINED3DTEXOPCAPS_SUBTRACT
12027 | WINED3DTEXOPCAPS_ADDSMOOTH
12028 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12029 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12030 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12031 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12032 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12033 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12034 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12035 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12036 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12037 | WINED3DTEXOPCAPS_DOTPRODUCT3
12038 | WINED3DTEXOPCAPS_MULTIPLYADD
12039 | WINED3DTEXOPCAPS_LERP
12040 | WINED3DTEXOPCAPS_BUMPENVMAP
12041 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12042 caps->MaxTextureBlendStages = WINED3D_MAX_TEXTURES;
12043 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], WINED3D_MAX_TEXTURES);
12046 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
12048 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
12049 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12050 return 0;
12053 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12055 struct shader_glsl_priv *priv;
12057 if (shader_backend == &glsl_shader_backend)
12059 priv = shader_priv;
12060 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12061 return priv;
12064 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12066 return NULL;
12069 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *param)
12071 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12072 struct glsl_ffp_fragment_shader, entry.entry);
12073 struct glsl_shader_prog_link *program, *program2;
12074 struct glsl_ffp_destroy_ctx *ctx = param;
12075 const struct wined3d_gl_info *gl_info;
12077 gl_info = ctx->context_gl->gl_info;
12078 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12079 struct glsl_shader_prog_link, ps.shader_entry)
12081 delete_glsl_program_entry(ctx->priv, gl_info, program);
12083 GL_EXTCALL(glDeleteShader(shader->id));
12084 heap_free(shader);
12087 /* Context activation is done by the caller. */
12088 static void glsl_fragment_pipe_free(struct wined3d_device *device, struct wined3d_context *context)
12090 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12091 struct shader_glsl_priv *priv = device->fragment_priv;
12092 struct glsl_ffp_destroy_ctx ctx;
12094 ctx.priv = priv;
12095 ctx.context_gl = context_gl;
12096 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12099 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12100 const struct wined3d_state *state, DWORD state_id)
12102 context->last_was_pshader = use_ps(state);
12104 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12107 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12108 const struct wined3d_state *state, DWORD state_id)
12110 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12113 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12114 const struct wined3d_state *state, DWORD state_id)
12116 BOOL use_vshader = use_vs(state);
12117 enum fogsource new_source;
12118 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12119 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12121 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12123 if (!state->render_states[WINED3D_RS_FOGENABLE])
12124 return;
12126 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12128 if (use_vshader)
12129 new_source = FOGSOURCE_VS;
12130 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12131 new_source = FOGSOURCE_COORD;
12132 else
12133 new_source = FOGSOURCE_FFP;
12135 else
12137 new_source = FOGSOURCE_FFP;
12140 if (new_source != context->fog_source || fogstart == fogend)
12142 context->fog_source = new_source;
12143 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12147 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12148 const struct wined3d_state *state, DWORD state_id)
12150 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12152 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12153 if (!shader_glsl_full_ffp_varyings(gl_info))
12154 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12156 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12157 glsl_fragment_pipe_fog(context, state, state_id);
12160 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12161 const struct wined3d_state *state, DWORD state_id)
12163 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12165 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12166 if (!shader_glsl_full_ffp_varyings(gl_info))
12167 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12170 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12171 const struct wined3d_state *state, DWORD state_id)
12173 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12176 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12177 const struct wined3d_state *state, DWORD state_id)
12179 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12182 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12183 const struct wined3d_state *state, DWORD state_id)
12185 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12186 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12187 float ref = wined3d_alpha_ref(state);
12189 if (func)
12191 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12192 checkGLcall("glAlphaFunc");
12196 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12197 const struct wined3d_state *state, DWORD state_id)
12199 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12202 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12203 const struct wined3d_state *state, DWORD state_id)
12205 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12207 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12209 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12210 checkGLcall("glEnable(GL_ALPHA_TEST)");
12212 else
12214 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12215 checkGLcall("glDisable(GL_ALPHA_TEST)");
12219 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12220 const struct wined3d_state *state, DWORD state_id)
12222 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12225 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12226 const struct wined3d_state *state, DWORD state_id)
12228 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12231 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12232 const struct wined3d_state *state, DWORD state_id)
12234 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12237 static const struct wined3d_state_entry_template glsl_fragment_pipe_state_template[] =
12239 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12240 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12241 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12242 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12243 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12244 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12245 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12246 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12247 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12248 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12249 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12250 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12251 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12252 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12253 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12254 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12255 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12256 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12257 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12258 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12259 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12260 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12261 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12262 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12263 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12264 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12265 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12266 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12267 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12268 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12269 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12270 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12271 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12272 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12273 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12274 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12275 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12276 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12277 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12278 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12279 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12280 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12281 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12282 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12283 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12284 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12285 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12286 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12287 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12288 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12289 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12290 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12291 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12292 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12293 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12294 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12295 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12296 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12297 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12298 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12299 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12300 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12301 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12302 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12303 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12304 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12305 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12306 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12307 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12308 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12309 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12310 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12311 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12312 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12313 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12314 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12315 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12316 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12317 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12318 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12319 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12320 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12321 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12322 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12323 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12324 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12325 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12326 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12327 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12328 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12329 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12330 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12331 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12332 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12333 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12334 {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 },
12335 {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 },
12336 {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 },
12337 {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 },
12338 {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 },
12339 {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 },
12340 {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 },
12341 {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 },
12342 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12343 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12344 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12345 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12346 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12347 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12348 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12349 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12350 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12351 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12352 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12353 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12354 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12357 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12359 return TRUE;
12362 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12366 const struct wined3d_fragment_pipe_ops glsl_fragment_pipe =
12368 glsl_fragment_pipe_enable,
12369 glsl_fragment_pipe_get_caps,
12370 glsl_fragment_pipe_get_emul_mask,
12371 glsl_fragment_pipe_alloc,
12372 glsl_fragment_pipe_free,
12373 glsl_fragment_pipe_alloc_context_data,
12374 glsl_fragment_pipe_free_context_data,
12375 shader_glsl_color_fixup_supported,
12376 glsl_fragment_pipe_state_template,
12379 struct glsl_blitter_args
12381 GLenum texture_type;
12382 struct color_fixup_desc fixup;
12383 unsigned short use_colour_key;
12386 struct glsl_blitter_program
12388 struct wine_rb_entry entry;
12389 struct glsl_blitter_args args;
12390 GLuint id;
12393 struct wined3d_glsl_blitter
12395 struct wined3d_blitter blitter;
12396 struct wined3d_string_buffer_list string_buffers;
12397 struct wine_rb_tree programs;
12398 GLuint palette_texture;
12401 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12403 const struct glsl_blitter_args *a = key;
12404 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12406 return memcmp(a, b, sizeof(*a));
12409 /* Context activation is done by the caller. */
12410 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12412 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12413 struct wined3d_context_gl *context_gl = ctx;
12414 const struct wined3d_gl_info *gl_info;
12416 gl_info = context_gl->gl_info;
12417 GL_EXTCALL(glDeleteProgram(program->id));
12418 checkGLcall("glDeleteProgram()");
12419 heap_free(program);
12422 /* Context activation is done by the caller. */
12423 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12425 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12426 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12427 struct wined3d_glsl_blitter *glsl_blitter;
12428 struct wined3d_blitter *next;
12430 if ((next = blitter->next))
12431 next->ops->blitter_destroy(next, context);
12433 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12435 if (glsl_blitter->palette_texture)
12436 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12438 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context_gl);
12439 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12441 heap_free(glsl_blitter);
12444 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12445 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12446 const char *output, const char *tex_type, const char *swizzle)
12448 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12449 shader_addline(buffer, "\nvoid main()\n{\n");
12450 /* The alpha-component contains the palette index. */
12451 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12452 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12453 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12454 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12455 * the middle. */
12456 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12457 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12458 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12459 if (args->use_colour_key)
12460 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12461 shader_addline(buffer, "}\n");
12464 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12465 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12466 const char *tex_type)
12468 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12469 char chroma, luminance;
12470 const char *tex;
12472 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12473 * macropixel, giving effectively 16 bits per pixel. The color consists of
12474 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12475 * luminance values, one for each single pixel it contains, and one U and
12476 * one V value shared between both pixels.
12478 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12479 * component contains the luminance and alpha the chroma. With UYVY it is
12480 * vice versa. Thus take the format into account when generating the read
12481 * swizzles
12483 * Reading the Y value is straightforward - just sample the texture. The
12484 * hardware takes care of filtering in the horizontal and vertical
12485 * direction.
12487 * Reading the U and V values is harder. We have to avoid filtering
12488 * horizontally, because that would mix the U and V values of one pixel or
12489 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12490 * get an unfiltered read, regardless of the filtering setting. Vertical
12491 * filtering works automatically though - the U and V values of two rows
12492 * are mixed nicely.
12494 * Apart of avoiding filtering issues, the code has to know which value it
12495 * just read, and where it can find the other one. To determine this, it
12496 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12497 * accordingly.
12499 * Handling horizontal filtering of U and V values requires reading a 2nd
12500 * pair of pixels, extracting U and V and mixing them. This is not
12501 * implemented yet.
12503 * An alternative implementation idea is to load the texture as A8R8G8B8
12504 * texture, with width / 2. This way one read gives all 3 values, finding
12505 * U and V is easy in an unfiltered situation. Finding the luminance on
12506 * the other hand requires finding out if it is an odd or even pixel. The
12507 * real drawback of this approach is filtering. This would have to be
12508 * emulated completely in the shader, reading up two 2 packed pixels in up
12509 * to 2 rows and interpolating both horizontally and vertically. Beyond
12510 * that it would require adjustments to the texture handling code to deal
12511 * with the width scaling. */
12513 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12515 chroma = 'x';
12516 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12518 else
12520 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12521 luminance = 'x';
12524 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12526 /* First we have to read the chroma values. This means we need at least
12527 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12528 * unmodified chroma, we have to rid ourselves of the filtering when we
12529 * sample the texture. */
12530 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12531 /* We must not allow filtering between pixel x and x+1, this would mix U
12532 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12533 * center is at 0.5, so add 0.5. */
12534 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12535 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12537 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12538 * and 0.75 for the even and odd pixels respectively. */
12539 /* Put the value into either of the chroma values. */
12540 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12541 shader_addline(buffer, " if (even)\n");
12542 shader_addline(buffer, " chroma.y = luminance;\n");
12543 shader_addline(buffer, " else\n");
12544 shader_addline(buffer, " chroma.x = luminance;\n");
12546 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12547 * current one. Otherwise, sample the left pixel. */
12548 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12549 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12551 /* Put the value into the other chroma. */
12552 shader_addline(buffer, " if (even)\n");
12553 shader_addline(buffer, " chroma.x = luminance;\n");
12554 shader_addline(buffer, " else\n");
12555 shader_addline(buffer, " chroma.y = luminance;\n");
12557 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12558 * the current one and lerp the two U and V values. */
12560 /* This gives the correctly filtered luminance value. */
12561 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12564 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12565 const struct wined3d_gl_info *gl_info, const char *tex_type)
12567 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12568 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12570 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12571 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12572 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12573 * have only half the pitch of the luminance plane, the packing into the
12574 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12575 * luminance data it looks approximately like this:
12577 * +----------------------------------+----
12578 * | |
12579 * | |
12580 * | |
12581 * | |
12582 * | | 2
12583 * | LUMINANCE | -
12584 * | | 3
12585 * | |
12586 * | |
12587 * | |
12588 * | |
12589 * +----------------+-----------------+----
12590 * | | |
12591 * | V even rows | V odd rows |
12592 * | | | 1
12593 * +----------------+------------------ -
12594 * | | | 3
12595 * | U even rows | U odd rows |
12596 * | | |
12597 * +----------------+-----------------+----
12598 * | | |
12599 * | 0.5 | 0.5 |
12601 * So it appears as if there are 4 chroma images, but in fact the odd rows
12602 * in the chroma images are in the same row as the even ones. So it is
12603 * kinda tricky to read. */
12605 /* First sample the chroma values. */
12606 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12607 /* The chroma planes have only half the width. */
12608 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12610 /* The first value is between 2/3 and 5/6 of the texture's height, so
12611 * scale+bias the coordinate. Also read the right side of the image when
12612 * reading odd lines.
12614 * Don't forget to clamp the y values in into the range, otherwise we'll
12615 * get filtering bleeding. */
12617 /* Read odd lines from the right side (add 0.5 to the x coordinate). */
12618 shader_addline(buffer, " if (fract(floor(texcoord.y * size.y) * 0.5 + 1.0 / 6.0) >= 0.5)\n");
12619 shader_addline(buffer, " texcoord.x += 0.5;\n");
12621 /* Clamp, keep the half pixel origin in mind. */
12622 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12623 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12625 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12627 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12628 * 6/6th No need to clamp because we're just reusing the already clamped
12629 * value from above. */
12630 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12631 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12633 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12634 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12635 * values from bleeding into the sampled luminance values due to
12636 * filtering. */
12637 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12638 /* Multiply the y coordinate by 2/3 and clamp it. */
12639 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12640 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12643 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12644 const struct wined3d_gl_info *gl_info, const char *tex_type)
12646 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12647 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12649 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12650 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12651 * effective bitdepth is 12 bits per pixel. If the whole texture is
12652 * interpreted as luminance data it looks approximately like this:
12654 * +----------------------------------+----
12655 * | |
12656 * | |
12657 * | |
12658 * | |
12659 * | | 2
12660 * | LUMINANCE | -
12661 * | | 3
12662 * | |
12663 * | |
12664 * | |
12665 * | |
12666 * +----------------------------------+----
12667 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12668 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12669 * | | 1
12670 * | | -
12671 * | | 3
12672 * | |
12673 * | |
12674 * +----------------------------------+---- */
12676 /* First sample the chroma values. */
12677 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12678 /* We only have half the number of chroma pixels. */
12679 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12680 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12682 /* We must not allow filtering horizontally, this would mix U and V.
12683 * Vertical filtering is ok. However, bear in mind that the pixel center
12684 * is at 0.5, so add 0.5. */
12686 /* Convert to non-normalised coordinates so we can find the individual
12687 * pixel. */
12688 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12689 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12690 * 0.5 to hit the center of the pixel. Then convert back to normalised
12691 * coordinates. */
12692 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12693 /* Clamp, keep the half pixel origin in mind. */
12694 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12696 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12697 /* Add 1.0 / size.x to sample the adjacent texel. */
12698 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12699 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12701 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12702 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12703 * values from bleeding into the sampled luminance values due to
12704 * filtering. */
12705 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12706 /* Multiply the y coordinate by 2/3 and clamp it. */
12707 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12708 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12711 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12712 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12713 const char *output, const char *tex_type, const char *swizzle)
12715 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12717 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12718 shader_addline(buffer, "float luminance;\n");
12719 shader_addline(buffer, "vec2 texcoord;\n");
12720 shader_addline(buffer, "vec2 chroma;\n");
12721 shader_addline(buffer, "uniform vec2 size;\n");
12723 shader_addline(buffer, "\nvoid main()\n{\n");
12725 switch (complex_fixup)
12727 case COMPLEX_FIXUP_UYVY:
12728 case COMPLEX_FIXUP_YUY2:
12729 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12730 break;
12732 case COMPLEX_FIXUP_YV12:
12733 gen_yv12_read(buffer, gl_info, tex_type);
12734 break;
12736 case COMPLEX_FIXUP_NV12:
12737 gen_nv12_read(buffer, gl_info, tex_type);
12738 break;
12740 case COMPLEX_FIXUP_YUV:
12741 /* With APPLE_rgb_422, things are much simpler. The only thing we
12742 * have to do here is Y'CbCr to RGB conversion. */
12743 shader_addline(buffer, " vec3 yuv = vec3(texture%s(sampler, out_texcoord.xy));\n",
12744 needs_legacy_glsl_syntax(gl_info) ? tex_type : "");
12745 shader_addline(buffer, " luminance = yuv.y;\n");
12746 shader_addline(buffer, " chroma = yuv.xz;\n");
12747 break;
12749 default:
12750 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12751 string_buffer_free(buffer);
12752 return;
12755 /* Calculate the final result. Formula is taken from
12756 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12757 * ranges from -0.5 to 0.5. */
12758 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12760 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12761 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12762 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12763 if (args->use_colour_key)
12764 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12766 shader_addline(buffer, "}\n");
12769 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12770 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12771 const char *output, const char *tex_type, const char *swizzle)
12773 shader_addline(buffer, "\nvoid main()\n{\n");
12774 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12775 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12776 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12777 if (args->use_colour_key)
12778 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12779 shader_addline(buffer, "}\n");
12782 /* Context activation is done by the caller. */
12783 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12784 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12786 static const struct
12788 GLenum texture_target;
12789 const char texture_type[7];
12790 const char texcoord_swizzle[4];
12792 texture_data[] =
12794 {GL_TEXTURE_2D, "2D", "xy"},
12795 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12796 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12798 static const char vshader_main[] =
12799 "\n"
12800 "void main()\n"
12801 "{\n"
12802 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12803 " out_texcoord = texcoord;\n"
12804 "}\n";
12805 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12806 struct wined3d_string_buffer *buffer, *output;
12807 GLuint program, vshader_id, fshader_id;
12808 const char *tex_type = NULL, *swizzle = NULL, *ptr;
12809 unsigned int i;
12810 GLint loc;
12812 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12814 if (args->texture_type == texture_data[i].texture_target)
12816 tex_type = texture_data[i].texture_type;
12817 swizzle = texture_data[i].texcoord_swizzle;
12818 break;
12821 if (i == ARRAY_SIZE(texture_data))
12823 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12824 return 0;
12827 program = GL_EXTCALL(glCreateProgram());
12829 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12831 buffer = string_buffer_get(&blitter->string_buffers);
12832 shader_glsl_add_version_declaration(buffer, gl_info);
12833 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12834 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12835 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12836 shader_addline(buffer, vshader_main);
12838 ptr = buffer->buffer;
12839 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12840 GL_EXTCALL(glAttachShader(program, vshader_id));
12841 GL_EXTCALL(glDeleteShader(vshader_id));
12843 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12845 string_buffer_clear(buffer);
12846 shader_glsl_add_version_declaration(buffer, gl_info);
12847 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12848 if (args->use_colour_key)
12850 shader_addline(buffer, "uniform struct\n{\n");
12851 shader_addline(buffer, " vec4 low, high;\n");
12852 shader_addline(buffer, "} colour_key;\n");
12854 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12855 /* TODO: Declare the out variable with the correct type (and put it in the
12856 * blitter args). */
12857 if (!use_legacy_fragment_output(gl_info))
12858 shader_addline(buffer, "out vec4 ps_out[1];\n");
12860 output = string_buffer_get(&blitter->string_buffers);
12861 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12863 switch (complex_fixup)
12865 case COMPLEX_FIXUP_P8:
12866 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12867 break;
12868 case COMPLEX_FIXUP_YUY2:
12869 case COMPLEX_FIXUP_UYVY:
12870 case COMPLEX_FIXUP_YV12:
12871 case COMPLEX_FIXUP_NV12:
12872 case COMPLEX_FIXUP_YUV:
12873 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12874 break;
12875 case COMPLEX_FIXUP_NONE:
12876 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12879 string_buffer_release(&blitter->string_buffers, output);
12881 ptr = buffer->buffer;
12882 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12883 string_buffer_release(&blitter->string_buffers, buffer);
12884 GL_EXTCALL(glAttachShader(program, fshader_id));
12885 GL_EXTCALL(glDeleteShader(fshader_id));
12887 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12888 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12890 if (!use_legacy_fragment_output(gl_info))
12891 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12893 GL_EXTCALL(glCompileShader(vshader_id));
12894 print_glsl_info_log(gl_info, vshader_id, FALSE);
12895 GL_EXTCALL(glCompileShader(fshader_id));
12896 print_glsl_info_log(gl_info, fshader_id, FALSE);
12897 GL_EXTCALL(glLinkProgram(program));
12898 shader_glsl_validate_link(gl_info, program);
12900 GL_EXTCALL(glUseProgram(program));
12901 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12902 GL_EXTCALL(glUniform1i(loc, 0));
12903 if (complex_fixup == COMPLEX_FIXUP_P8)
12905 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12906 GL_EXTCALL(glUniform1i(loc, 1));
12909 return program;
12912 /* Context activation is done by the caller. */
12913 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12914 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl)
12916 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12917 const struct wined3d_palette *palette;
12919 palette = texture_gl->t.swapchain ? texture_gl->t.swapchain->palette : NULL;
12921 if (!blitter->palette_texture)
12922 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12924 wined3d_context_gl_active_texture(context_gl, gl_info, 1);
12925 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12926 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12927 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12928 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12930 if (palette)
12932 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12933 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12935 else
12937 static const DWORD black;
12939 FIXME("P8 texture loaded without a palette.\n");
12940 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
12941 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
12944 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
12947 /* Context activation is done by the caller. */
12948 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
12949 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl, BOOL use_colour_key)
12951 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12952 struct glsl_blitter_program *program;
12953 struct glsl_blitter_args args;
12954 struct wine_rb_entry *entry;
12956 memset(&args, 0, sizeof(args));
12957 args.texture_type = texture_gl->target;
12958 args.fixup = texture_gl->t.resource.format->color_fixup;
12959 args.use_colour_key = use_colour_key;
12961 if ((entry = wine_rb_get(&blitter->programs, &args)))
12962 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12964 if (!(program = heap_alloc(sizeof(*program))))
12966 ERR("Failed to allocate blitter program memory.\n");
12967 return NULL;
12970 program->args = args;
12971 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
12973 WARN("Failed to generate blitter program.\n");
12974 heap_free(program);
12975 return NULL;
12978 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
12980 ERR("Failed to store blitter program.\n");
12981 GL_EXTCALL(glDeleteProgram(program->id));
12982 heap_free(program);
12983 return NULL;
12986 return program;
12989 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
12990 const struct wined3d_texture_gl *src_texture, DWORD src_location,
12991 const struct wined3d_texture_gl *dst_texture, DWORD dst_location)
12993 const struct wined3d_resource *src_resource = &src_texture->t.resource;
12994 const struct wined3d_resource *dst_resource = &dst_texture->t.resource;
12995 const struct wined3d_format *src_format = src_resource->format;
12996 const struct wined3d_format *dst_format = dst_resource->format;
12997 BOOL decompress;
12999 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13001 if (dst_format->depth_size || dst_format->stencil_size)
13002 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13003 else
13004 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13007 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY
13008 && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13010 TRACE("Unsupported blit_op %#x.\n", blit_op);
13011 return FALSE;
13014 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13015 return FALSE;
13017 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13018 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13020 TRACE("Multi-sample source textures not supported.\n");
13021 return FALSE;
13024 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13026 TRACE("Destination resource does not have GPU access.\n");
13027 return FALSE;
13030 /* We don't necessarily want to blit from resources without
13031 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13032 * compressed textures. */
13033 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
13034 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
13035 if (!decompress && !(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13037 TRACE("Source resource does not have GPU access.\n");
13038 return FALSE;
13041 if (!is_identity_fixup(dst_format->color_fixup)
13042 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13044 TRACE("Destination fixups are not supported.\n");
13045 return FALSE;
13048 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
13049 && !((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
13050 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
13052 TRACE("Destination texture is not FBO attachable.\n");
13053 return FALSE;
13056 TRACE("Returning supported.\n");
13057 return TRUE;
13060 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13061 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13062 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13063 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13064 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
13066 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
13067 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
13068 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
13069 struct wined3d_device *device = dst_texture->resource.device;
13070 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13071 struct wined3d_texture *staging_texture = NULL;
13072 struct wined3d_glsl_blitter *glsl_blitter;
13073 struct wined3d_color_key alpha_test_key;
13074 struct glsl_blitter_program *program;
13075 struct wined3d_blitter *next;
13076 unsigned int src_level;
13077 GLint location;
13078 RECT s, d;
13080 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
13081 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
13082 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13083 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13084 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
13086 if (!glsl_blitter_supported(op, context, src_texture_gl, src_location, dst_texture_gl, dst_location))
13088 if (!(next = blitter->next))
13090 ERR("No blitter to handle blit op %#x.\n", op);
13091 return dst_location;
13094 TRACE("Forwarding to blitter %p.\n", next);
13095 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13096 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
13099 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13101 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13103 struct wined3d_resource_desc desc;
13104 struct wined3d_box upload_box;
13105 HRESULT hr;
13107 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13109 src_level = src_sub_resource_idx % src_texture->level_count;
13110 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13111 desc.format = src_texture->resource.format->id;
13112 desc.multisample_type = src_texture->resource.multisample_type;
13113 desc.multisample_quality = src_texture->resource.multisample_quality;
13114 desc.usage = WINED3DUSAGE_PRIVATE;
13115 desc.bind_flags = 0;
13116 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13117 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13118 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13119 desc.depth = 1;
13120 desc.size = 0;
13122 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13123 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13125 ERR("Failed to create staging texture, hr %#x.\n", hr);
13126 return dst_location;
13129 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13130 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13131 src_texture, src_sub_resource_idx, &upload_box);
13133 src_texture = staging_texture;
13134 src_texture_gl = wined3d_texture_gl(src_texture);
13135 src_sub_resource_idx = 0;
13137 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13138 && (src_texture->sub_resources[src_sub_resource_idx].locations
13139 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13140 && !wined3d_resource_is_offscreen(&src_texture->resource))
13143 /* Without FBO blits transferring from the drawable to the texture is
13144 * expensive, because we have to flip the data in sysmem. Since we can
13145 * flip in the blitter, we don't actually need that flip anyway. So we
13146 * use the surface's texture as scratch texture, and flip the source
13147 * rectangle instead. */
13148 texture2d_load_fb_texture(src_texture_gl, src_sub_resource_idx, FALSE, context);
13150 s = *src_rect;
13151 src_level = src_sub_resource_idx % src_texture->level_count;
13152 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13153 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13154 src_rect = &s;
13156 else
13158 wined3d_texture_load(src_texture, context, FALSE);
13161 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
13162 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13163 else
13164 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13166 wined3d_context_gl_apply_blit_state(context_gl, device);
13168 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13170 d = *dst_rect;
13171 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
13172 dst_rect = &d;
13175 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
13177 GLenum buffer;
13179 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13181 TRACE("Destination texture %p is onscreen.\n", dst_texture);
13182 buffer = wined3d_texture_get_gl_buffer(dst_texture);
13184 else
13186 TRACE("Destination texture %p is offscreen.\n", dst_texture);
13187 buffer = GL_COLOR_ATTACHMENT0;
13189 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
13190 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
13191 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
13192 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
13193 context_invalidate_state(context, STATE_FRAMEBUFFER);
13196 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13198 const struct wined3d_format *f = src_texture->resource.format;
13200 alpha_test_key.color_space_low_value = 0;
13201 alpha_test_key.color_space_high_value = ~(((1u << f->alpha_size) - 1) << f->alpha_offset);
13202 colour_key = &alpha_test_key;
13204 else if (op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY)
13206 colour_key = NULL;
13209 if (!(program = glsl_blitter_get_program(glsl_blitter, context_gl, src_texture_gl, !!colour_key)))
13211 ERR("Failed to get blitter program.\n");
13212 return dst_location;
13214 GL_EXTCALL(glUseProgram(program->id));
13215 switch (get_complex_fixup(program->args.fixup))
13217 case COMPLEX_FIXUP_P8:
13218 glsl_blitter_upload_palette(glsl_blitter, context_gl, src_texture_gl);
13219 break;
13221 case COMPLEX_FIXUP_YUY2:
13222 case COMPLEX_FIXUP_UYVY:
13223 case COMPLEX_FIXUP_YV12:
13224 case COMPLEX_FIXUP_NV12:
13225 case COMPLEX_FIXUP_YUV:
13226 src_level = src_sub_resource_idx % src_texture->level_count;
13227 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13228 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13229 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13230 break;
13232 default:
13233 break;
13235 if (colour_key)
13237 struct wined3d_color float_key[2];
13239 wined3d_format_get_float_color_key(src_texture->resource.format, colour_key, float_key);
13240 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.low"));
13241 GL_EXTCALL(glUniform4fv(location, 1, &float_key[0].r));
13242 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.high"));
13243 GL_EXTCALL(glUniform4fv(location, 1, &float_key[1].r));
13245 wined3d_context_gl_draw_shaded_quad(context_gl, src_texture_gl, src_sub_resource_idx, src_rect, dst_rect, filter);
13246 GL_EXTCALL(glUseProgram(0));
13248 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13249 gl_info->gl_ops.gl.p_glFlush();
13251 if (staging_texture)
13252 wined3d_texture_decref(staging_texture);
13254 return dst_location;
13257 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13258 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13259 const RECT *draw_rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
13261 struct wined3d_blitter *next;
13263 if ((next = blitter->next))
13264 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13265 clear_rects, draw_rect, flags, color, depth, stencil);
13268 static const struct wined3d_blitter_ops glsl_blitter_ops =
13270 glsl_blitter_destroy,
13271 glsl_blitter_clear,
13272 glsl_blitter_blit,
13275 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13276 const struct wined3d_device *device)
13278 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
13279 struct wined3d_glsl_blitter *blitter;
13281 if (device->shader_backend != &glsl_shader_backend)
13282 return NULL;
13284 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13285 return NULL;
13287 if (!(blitter = heap_alloc(sizeof(*blitter))))
13289 ERR("Failed to allocate blitter.\n");
13290 return NULL;
13293 TRACE("Created blitter %p.\n", blitter);
13295 blitter->blitter.ops = &glsl_blitter_ops;
13296 blitter->blitter.next = *next;
13297 string_buffer_list_init(&blitter->string_buffers);
13298 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13299 blitter->palette_texture = 0;
13300 *next = &blitter->blitter;
13302 return *next;