winebus.sys: Add missing keyboard free_device callback.
[wine.git] / dlls / wined3d / glsl_shader.c
blob5d0e7b13bd4860eaba8cc37824b159c98784bb2d
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 static 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[2];\n");
2545 else
2546 shader_addline(buffer, "/* precise */ vec4 tmp_precise[2];\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 unsigned int dst_idx, 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[%u]", dst_idx);
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], 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 const struct wined3d_shader_dst_param *dst;
3322 struct glsl_dst_param dst_param;
3323 DWORD modifiers;
3324 unsigned int i;
3326 for (i = 0; i < ins->dst_count; ++i)
3328 if ((dst = &ins->dst[i])->reg.type == WINED3DSPR_NULL)
3329 continue;
3331 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3333 shader_glsl_add_dst_param(ins, dst, &dst_param);
3334 shader_addline(ins->ctx->buffer, "%s%s = tmp_precise[%u]%s;\n",
3335 dst_param.reg_name, dst_param.mask_str, i, dst_param.mask_str);
3338 if (!(modifiers = dst->modifiers))
3339 continue;
3341 shader_glsl_add_dst_param(ins, dst, &dst_param);
3343 if (modifiers & WINED3DSPDM_SATURATE)
3345 /* _SAT means to clamp the value of the register to between 0 and 1 */
3346 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3347 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3350 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3352 FIXME("_centroid modifier not handled\n");
3355 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3357 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3362 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3364 switch (op)
3366 case WINED3D_SHADER_REL_OP_GT: return ">";
3367 case WINED3D_SHADER_REL_OP_EQ: return "==";
3368 case WINED3D_SHADER_REL_OP_GE: return ">=";
3369 case WINED3D_SHADER_REL_OP_LT: return "<";
3370 case WINED3D_SHADER_REL_OP_NE: return "!=";
3371 case WINED3D_SHADER_REL_OP_LE: return "<=";
3372 default:
3373 FIXME("Unrecognized operator %#x.\n", op);
3374 return "(\?\?)";
3378 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3380 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3383 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3384 unsigned int *coord_size, unsigned int *deriv_size)
3386 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3387 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3389 *coord_size = resource_type_info[resource_type].coord_size;
3390 *deriv_size = *coord_size;
3391 if (is_array)
3392 --(*deriv_size);
3395 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3396 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3398 enum wined3d_shader_resource_type resource_type;
3399 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3400 const struct wined3d_gl_info *gl_info = priv->gl_info;
3401 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3402 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3403 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3404 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3405 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3406 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3407 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3408 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3409 const char *base = "texture", *type_part = "", *suffix = "";
3410 unsigned int coord_size, deriv_size;
3412 resource_type = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3413 ? pixelshader_get_resource_type(ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
3414 : ctx->reg_maps->resource_info[resource_idx].type;
3416 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3418 if (resource_type >= ARRAY_SIZE(resource_type_info))
3420 ERR("Unexpected resource type %#x.\n", resource_type);
3421 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3424 /* Note that there's no such thing as a projected cube texture. */
3425 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3426 projected = FALSE;
3428 if (needs_legacy_glsl_syntax(gl_info))
3430 if (shadow)
3431 base = "shadow";
3433 type_part = resource_type_info[resource_type].type_part;
3434 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3435 type_part = "2DRect";
3436 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3437 FIXME("Unhandled resource type %#x.\n", resource_type);
3439 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3441 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3442 suffix = "ARB";
3443 else
3444 FIXME("Unsupported grad function.\n");
3448 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3450 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3451 if (flags & ~texel_fetch_flags)
3452 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3454 base = "texelFetch";
3455 type_part = "";
3458 sample_function->name = string_buffer_get(priv->string_buffers);
3459 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3460 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3462 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3463 if (shadow)
3464 ++coord_size;
3465 sample_function->offset_size = offset ? deriv_size : 0;
3466 sample_function->coord_mask = (1u << coord_size) - 1;
3467 sample_function->deriv_mask = (1u << deriv_size) - 1;
3468 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3471 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3472 struct glsl_sample_function *sample_function)
3474 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3476 string_buffer_release(priv->string_buffers, sample_function->name);
3479 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3480 BOOL sign_fixup, enum fixup_channel_source channel_source)
3482 switch(channel_source)
3484 case CHANNEL_SOURCE_ZERO:
3485 strcat(arguments, "0.0");
3486 break;
3488 case CHANNEL_SOURCE_ONE:
3489 strcat(arguments, "1.0");
3490 break;
3492 case CHANNEL_SOURCE_X:
3493 strcat(arguments, reg_name);
3494 strcat(arguments, ".x");
3495 break;
3497 case CHANNEL_SOURCE_Y:
3498 strcat(arguments, reg_name);
3499 strcat(arguments, ".y");
3500 break;
3502 case CHANNEL_SOURCE_Z:
3503 strcat(arguments, reg_name);
3504 strcat(arguments, ".z");
3505 break;
3507 case CHANNEL_SOURCE_W:
3508 strcat(arguments, reg_name);
3509 strcat(arguments, ".w");
3510 break;
3512 default:
3513 FIXME("Unhandled channel source %#x\n", channel_source);
3514 strcat(arguments, "undefined");
3515 break;
3518 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3521 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3522 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3524 unsigned int mask_size, remaining;
3525 DWORD fixup_mask = 0;
3526 char arguments[256];
3527 char mask_str[6];
3529 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3530 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3531 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3532 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3533 if (!(mask &= fixup_mask))
3534 return;
3536 if (is_complex_fixup(fixup))
3538 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3539 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3540 return;
3543 shader_glsl_write_mask_to_str(mask, mask_str);
3544 mask_size = shader_glsl_get_write_mask_size(mask);
3546 arguments[0] = '\0';
3547 remaining = mask_size;
3548 if (mask & WINED3DSP_WRITEMASK_0)
3550 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3551 if (--remaining) strcat(arguments, ", ");
3553 if (mask & WINED3DSP_WRITEMASK_1)
3555 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3556 if (--remaining) strcat(arguments, ", ");
3558 if (mask & WINED3DSP_WRITEMASK_2)
3560 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3561 if (--remaining) strcat(arguments, ", ");
3563 if (mask & WINED3DSP_WRITEMASK_3)
3565 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3566 if (--remaining) strcat(arguments, ", ");
3569 if (mask_size > 1)
3570 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3571 else
3572 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3575 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3577 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3578 struct wined3d_string_buffer *reg_name;
3580 reg_name = string_buffer_get(priv->string_buffers);
3581 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, NULL, ins->ctx);
3582 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name->buffer, ins->dst[0].write_mask, fixup);
3583 string_buffer_release(priv->string_buffers, reg_name);
3586 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3587 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3588 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3589 const char *coord_reg_fmt, ...)
3591 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3592 char dst_swizzle[6];
3593 struct color_fixup_desc fixup;
3594 BOOL np2_fixup = FALSE;
3595 va_list args;
3596 int ret;
3598 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3600 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3601 * We actually rely on it for vertex shaders and SM4+. */
3602 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3604 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3605 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3607 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3608 np2_fixup = TRUE;
3610 else
3612 fixup = COLOR_FIXUP_IDENTITY;
3615 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, sample_function->data_type);
3617 if (sample_function->output_single_component)
3618 shader_addline(ins->ctx->buffer, "vec4(");
3620 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3621 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3623 for (;;)
3625 va_start(args, coord_reg_fmt);
3626 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3627 va_end(args);
3628 if (!ret)
3629 break;
3630 if (!string_buffer_resize(ins->ctx->buffer, ret))
3631 break;
3634 if (np2_fixup)
3636 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3637 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3639 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3641 case 1:
3642 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3643 idx >> 1, (idx % 2) ? "z" : "x");
3644 break;
3645 case 2:
3646 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3647 idx >> 1, (idx % 2) ? "zw" : "xy");
3648 break;
3649 case 3:
3650 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3651 idx >> 1, (idx % 2) ? "zw" : "xy");
3652 break;
3653 case 4:
3654 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3655 idx >> 1, (idx % 2) ? "zw" : "xy");
3656 break;
3659 if (dx && dy)
3660 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3661 else if (bias)
3662 shader_addline(ins->ctx->buffer, ", %s", bias);
3663 if (sample_function->offset_size)
3665 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3666 shader_addline(ins->ctx->buffer, ", ");
3667 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3669 shader_addline(ins->ctx->buffer, ")");
3671 if (sample_function->output_single_component)
3672 shader_addline(ins->ctx->buffer, ")");
3674 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3676 if (!is_identity_fixup(fixup))
3677 shader_glsl_color_correction(ins, fixup);
3680 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3682 /* Write the final position.
3684 * OpenGL coordinates specify the center of the pixel while D3D coords
3685 * specify the corner. The offsets are stored in z and w in
3686 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3687 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3688 * a MAD. */
3689 if (use_viewport_index)
3691 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3692 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3694 else
3696 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3697 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3700 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3701 * in utils.c
3703 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3704 * shaders are run before the homogeneous divide, so we have to take the w
3705 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3706 * z = z * 2 - w. */
3707 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3710 /*****************************************************************************
3711 * Begin processing individual instruction opcodes
3712 ****************************************************************************/
3714 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3716 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3717 struct glsl_src_param src0_param;
3718 struct glsl_src_param src1_param;
3719 DWORD write_mask;
3720 const char *op;
3722 /* Determine the GLSL operator to use based on the opcode */
3723 switch (ins->handler_idx)
3725 case WINED3DSIH_ADD: op = "+"; break;
3726 case WINED3DSIH_AND: op = "&"; break;
3727 case WINED3DSIH_DIV: op = "/"; break;
3728 case WINED3DSIH_IADD: op = "+"; break;
3729 case WINED3DSIH_ISHL: op = "<<"; break;
3730 case WINED3DSIH_ISHR: op = ">>"; break;
3731 case WINED3DSIH_MUL: op = "*"; break;
3732 case WINED3DSIH_OR: op = "|"; break;
3733 case WINED3DSIH_SUB: op = "-"; break;
3734 case WINED3DSIH_USHR: op = ">>"; break;
3735 case WINED3DSIH_XOR: op = "^"; break;
3736 default:
3737 op = "<unhandled operator>";
3738 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3739 break;
3742 write_mask = shader_glsl_append_dst(buffer, ins);
3743 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3744 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3745 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3748 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3750 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3751 struct glsl_src_param src0_param;
3752 struct glsl_src_param src1_param;
3753 unsigned int mask_size;
3754 DWORD write_mask;
3755 const char *op;
3757 write_mask = shader_glsl_append_dst(buffer, ins);
3758 mask_size = shader_glsl_get_write_mask_size(write_mask);
3759 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3760 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3762 if (mask_size > 1)
3764 switch (ins->handler_idx)
3766 case WINED3DSIH_EQ: op = "equal"; break;
3767 case WINED3DSIH_IEQ: op = "equal"; break;
3768 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3769 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3770 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3771 case WINED3DSIH_LT: op = "lessThan"; break;
3772 case WINED3DSIH_ILT: op = "lessThan"; break;
3773 case WINED3DSIH_ULT: op = "lessThan"; break;
3774 case WINED3DSIH_NE: op = "notEqual"; break;
3775 case WINED3DSIH_INE: op = "notEqual"; break;
3776 default:
3777 op = "<unhandled operator>";
3778 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3779 break;
3782 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3783 mask_size, op, src0_param.param_str, src1_param.param_str);
3785 else
3787 switch (ins->handler_idx)
3789 case WINED3DSIH_EQ: op = "=="; break;
3790 case WINED3DSIH_IEQ: op = "=="; break;
3791 case WINED3DSIH_GE: op = ">="; break;
3792 case WINED3DSIH_IGE: op = ">="; break;
3793 case WINED3DSIH_UGE: op = ">="; break;
3794 case WINED3DSIH_LT: op = "<"; break;
3795 case WINED3DSIH_ILT: op = "<"; break;
3796 case WINED3DSIH_ULT: op = "<"; break;
3797 case WINED3DSIH_NE: op = "!="; break;
3798 case WINED3DSIH_INE: op = "!="; break;
3799 default:
3800 op = "<unhandled operator>";
3801 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3802 break;
3805 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3806 src0_param.param_str, op, src1_param.param_str);
3810 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3812 struct glsl_src_param src_param;
3813 DWORD write_mask;
3814 const char *op;
3816 switch (ins->handler_idx)
3818 case WINED3DSIH_INEG: op = "-"; break;
3819 case WINED3DSIH_NOT: op = "~"; break;
3820 default:
3821 op = "<unhandled operator>";
3822 ERR("Unhandled opcode %s.\n",
3823 debug_d3dshaderinstructionhandler(ins->handler_idx));
3824 break;
3827 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3828 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3829 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3832 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3834 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3835 struct glsl_src_param src0_param;
3836 struct glsl_src_param src1_param;
3837 DWORD write_mask;
3839 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3840 * If not, we can emulate it. */
3841 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3842 FIXME("64-bit integer multiplies not implemented.\n");
3844 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3846 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3847 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3848 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3850 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3851 src0_param.param_str, src1_param.param_str);
3855 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3857 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3858 struct glsl_src_param src0_param, src1_param;
3859 DWORD write_mask;
3861 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3863 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3865 char dst_mask[6];
3867 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
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, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3871 dst_mask, src0_param.param_str, src1_param.param_str);
3873 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3874 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3875 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3876 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3878 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
3879 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3881 else
3883 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3884 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3885 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3886 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3889 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3891 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3892 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3893 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3894 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3898 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3899 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3901 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3902 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3903 const struct wined3d_gl_info *gl_info = priv->gl_info;
3904 struct glsl_src_param src0_param;
3905 DWORD write_mask;
3907 write_mask = shader_glsl_append_dst(buffer, ins);
3908 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3910 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3911 * shader versions WINED3DSIO_MOVA is used for this. */
3912 if (ins->ctx->reg_maps->shader_version.major == 1
3913 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3914 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3916 /* This is a simple floor() */
3917 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3918 if (mask_size > 1) {
3919 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3920 } else {
3921 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3924 else if (ins->handler_idx == WINED3DSIH_MOVA)
3926 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3928 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3930 if (mask_size > 1)
3931 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3932 else
3933 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3935 else
3937 if (mask_size > 1)
3938 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3939 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3940 else
3941 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3942 src0_param.param_str, src0_param.param_str);
3945 else
3947 shader_addline(buffer, "%s);\n", src0_param.param_str);
3951 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3952 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3954 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3955 struct glsl_src_param src0_param;
3956 struct glsl_src_param src1_param;
3957 DWORD dst_write_mask, src_write_mask;
3958 unsigned int dst_size;
3960 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3961 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3963 /* dp4 works on vec4, dp3 on vec3, etc. */
3964 if (ins->handler_idx == WINED3DSIH_DP4)
3965 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3966 else if (ins->handler_idx == WINED3DSIH_DP3)
3967 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3968 else
3969 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3971 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3972 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3974 if (dst_size > 1) {
3975 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3976 } else {
3977 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3981 /* Note that this instruction has some restrictions. The destination write mask
3982 * can't contain the w component, and the source swizzles have to be .xyzw */
3983 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3985 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3986 struct glsl_src_param src0_param;
3987 struct glsl_src_param src1_param;
3988 char dst_mask[6];
3990 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3991 shader_glsl_append_dst(ins->ctx->buffer, ins);
3992 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3993 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3994 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3997 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3999 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4001 if (!stream)
4002 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4003 else
4004 FIXME("Unhandled primitive stream %u.\n", stream);
4007 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4008 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4009 * GLSL uses the value as-is. */
4010 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4012 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4013 struct glsl_src_param src0_param;
4014 struct glsl_src_param src1_param;
4015 DWORD dst_write_mask;
4016 unsigned int dst_size;
4018 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4019 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4021 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4022 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4024 if (dst_size > 1)
4026 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4027 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4029 else
4031 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4032 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4036 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4037 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4039 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4040 bool y_correction = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4041 ? priv->cur_ps_args->y_correction : false;
4042 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4043 struct glsl_src_param src_param;
4044 const char *instruction;
4045 DWORD write_mask;
4046 unsigned i;
4048 /* Determine the GLSL function to use based on the opcode */
4049 /* TODO: Possibly make this a table for faster lookups */
4050 switch (ins->handler_idx)
4052 case WINED3DSIH_ABS: instruction = "abs"; break;
4053 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4054 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4055 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4056 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4057 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4058 case WINED3DSIH_DSY: instruction = y_correction ? "ycorrection.y * dFdy" : "dFdy"; break;
4059 case WINED3DSIH_DSY_COARSE: instruction = y_correction ? "ycorrection.y * dFdyCoarse" : "dFdyCoarse"; break;
4060 case WINED3DSIH_DSY_FINE: instruction = y_correction ? "ycorrection.y * dFdyFine" : "dFdyFine"; break;
4061 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4062 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4063 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4064 case WINED3DSIH_FRC: instruction = "fract"; break;
4065 case WINED3DSIH_IMAX: instruction = "max"; break;
4066 case WINED3DSIH_IMIN: instruction = "min"; break;
4067 case WINED3DSIH_MAX: instruction = "max"; break;
4068 case WINED3DSIH_MIN: instruction = "min"; break;
4069 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4070 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4071 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4072 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4073 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4074 case WINED3DSIH_UMAX: instruction = "max"; break;
4075 case WINED3DSIH_UMIN: instruction = "min"; break;
4076 default: instruction = "";
4077 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4078 break;
4081 write_mask = shader_glsl_append_dst(buffer, ins);
4083 /* In D3D bits are numbered from the most significant bit. */
4084 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4085 shader_addline(buffer, "31 - ");
4086 shader_addline(buffer, "%s(", instruction);
4088 if (ins->src_count)
4090 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4091 shader_addline(buffer, "%s", src_param.param_str);
4092 for (i = 1; i < ins->src_count; ++i)
4094 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4095 shader_addline(buffer, ", %s", src_param.param_str);
4099 shader_addline(buffer, "));\n");
4102 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4104 struct wined3d_shader_dst_param dst;
4105 struct glsl_src_param src;
4106 DWORD write_mask;
4107 const char *fmt;
4108 unsigned int i;
4110 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4111 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4113 dst = ins->dst[0];
4114 for (i = 0; i < 4; ++i)
4116 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4117 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4118 &dst, 0, dst.reg.data_type)))
4119 continue;
4121 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4122 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4126 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4128 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4129 struct wined3d_shader_dst_param dst;
4130 struct glsl_src_param src[4];
4131 const char *instruction;
4132 BOOL tmp_dst = FALSE;
4133 char mask_char[6];
4134 unsigned int i, j;
4135 DWORD write_mask;
4137 switch (ins->handler_idx)
4139 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4140 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4141 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4142 default:
4143 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4144 return;
4147 for (i = 0; i < ins->src_count; ++i)
4149 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4150 && ins->dst[0].reg.type == ins->src[i].reg.type)
4151 tmp_dst = TRUE;
4154 dst = ins->dst[0];
4155 for (i = 0; i < 4; ++i)
4157 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4158 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4159 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4160 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4161 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, 0, dst.reg.data_type)))
4162 continue;
4164 for (j = 0; j < ins->src_count; ++j)
4165 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4166 shader_addline(buffer, "%s(", instruction);
4167 for (j = 0; j < ins->src_count - 2; ++j)
4168 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4169 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4172 if (tmp_dst)
4174 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
4175 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4176 shader_addline(buffer, "tmp0%s);\n", mask_char);
4180 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4182 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4184 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4185 struct glsl_src_param src_param;
4186 unsigned int mask_size;
4187 DWORD write_mask;
4188 char dst_mask[6];
4190 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4191 mask_size = shader_glsl_get_write_mask_size(write_mask);
4192 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4194 if (mask_size > 3)
4195 shader_addline(buffer, "tmp0.x = dot(vec3(%s), vec3(%s));\n",
4196 src_param.param_str, src_param.param_str);
4197 else
4198 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4199 src_param.param_str, src_param.param_str);
4200 shader_glsl_append_dst(buffer, ins);
4202 shader_addline(buffer, "tmp0.x == 0.0 ? %s : (%s * inversesqrt(tmp0.x)));\n",
4203 src_param.param_str, src_param.param_str);
4206 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4208 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4209 ins->ctx->reg_maps->shader_version.minor);
4210 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4211 struct glsl_src_param src0_param;
4212 const char *prefix, *suffix;
4213 unsigned int dst_size;
4214 DWORD dst_write_mask;
4216 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4217 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4219 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4220 dst_write_mask = WINED3DSP_WRITEMASK_3;
4222 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4224 switch (ins->handler_idx)
4226 case WINED3DSIH_EXP:
4227 case WINED3DSIH_EXPP:
4228 prefix = "exp2(";
4229 suffix = ")";
4230 break;
4232 case WINED3DSIH_LOG:
4233 case WINED3DSIH_LOGP:
4234 prefix = "log2(abs(";
4235 suffix = "))";
4236 break;
4238 case WINED3DSIH_RCP:
4239 prefix = "1.0 / ";
4240 suffix = "";
4241 break;
4243 case WINED3DSIH_RSQ:
4244 prefix = "inversesqrt(abs(";
4245 suffix = "))";
4246 break;
4248 default:
4249 prefix = "";
4250 suffix = "";
4251 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4252 break;
4255 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4256 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4257 else
4258 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4261 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4262 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4263 * dst.x = 2^(floor(src))
4264 * dst.y = src - floor(src)
4265 * dst.z = 2^src (partial precision is allowed, but optional)
4266 * dst.w = 1.0;
4267 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4268 * dst = 2^src; (partial precision is allowed, but optional)
4270 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4272 if (ins->ctx->reg_maps->shader_version.major < 2)
4274 struct glsl_src_param src_param;
4275 char dst_mask[6];
4277 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4279 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4280 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4281 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4282 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4284 shader_glsl_append_dst(ins->ctx->buffer, ins);
4285 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4286 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4287 return;
4290 shader_glsl_scalar_op(ins);
4293 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4294 const char *vector_constructor, const char *scalar_constructor)
4296 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4297 struct glsl_src_param src_param;
4298 unsigned int mask_size;
4299 DWORD write_mask;
4301 write_mask = shader_glsl_append_dst(buffer, ins);
4302 mask_size = shader_glsl_get_write_mask_size(write_mask);
4303 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4305 if (mask_size > 1)
4306 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4307 else
4308 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4311 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4313 shader_glsl_cast(ins, "ivec", "int");
4316 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4318 shader_glsl_cast(ins, "uvec", "uint");
4321 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4323 shader_glsl_cast(ins, "vec", "float");
4326 /** Process signed comparison opcodes in GLSL. */
4327 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4329 struct glsl_src_param src0_param;
4330 struct glsl_src_param src1_param;
4331 DWORD write_mask;
4332 unsigned int mask_size;
4334 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4335 mask_size = shader_glsl_get_write_mask_size(write_mask);
4336 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4337 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4339 if (mask_size > 1) {
4340 const char *compare;
4342 switch(ins->handler_idx)
4344 case WINED3DSIH_SLT: compare = "lessThan"; break;
4345 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4346 default: compare = "";
4347 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4350 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4351 src0_param.param_str, src1_param.param_str);
4352 } else {
4353 switch(ins->handler_idx)
4355 case WINED3DSIH_SLT:
4356 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4357 * to return 0.0 but step returns 1.0 because step is not < x
4358 * An alternative is a bvec compare padded with an unused second component.
4359 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4360 * issue. Playing with not() is not possible either because not() does not accept
4361 * a scalar.
4363 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4364 src0_param.param_str, src1_param.param_str);
4365 break;
4366 case WINED3DSIH_SGE:
4367 /* Here we can use the step() function and safe a conditional */
4368 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4369 break;
4370 default:
4371 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4377 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4379 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4380 struct wined3d_shader_dst_param dst[2];
4381 struct glsl_src_param src[3];
4382 unsigned int i, j, k;
4383 char mask_char[6];
4384 DWORD write_mask;
4385 BOOL tmp_dst[2];
4387 for (i = 0; i < ins->dst_count; ++i)
4389 tmp_dst[i] = FALSE;
4390 for (j = 0; j < ins->src_count; ++j)
4392 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4393 && ins->dst[i].reg.type == ins->src[j].reg.type)
4394 tmp_dst[i] = TRUE;
4398 dst[0] = ins->dst[0];
4399 dst[1] = ins->dst[1];
4400 for (i = 0; i < 4; ++i)
4402 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4404 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4405 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4406 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4407 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], j, dst[j].reg.data_type)))
4408 continue;
4410 for (k = 0; k < ARRAY_SIZE(src); ++k)
4411 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4413 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4414 src[0].param_str, src[1].param_str, src[2].param_str);
4418 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4420 if (tmp_dst[i])
4422 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4423 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], i, ins->dst[i].reg.data_type);
4424 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4429 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4431 const char *condition_prefix, *condition_suffix;
4432 struct wined3d_shader_dst_param dst;
4433 struct glsl_src_param src0_param;
4434 struct glsl_src_param src1_param;
4435 struct glsl_src_param src2_param;
4436 BOOL temp_destination = FALSE;
4437 DWORD cmp_channel = 0;
4438 unsigned int i, j;
4439 char mask_char[6];
4440 DWORD write_mask;
4442 switch (ins->handler_idx)
4444 case WINED3DSIH_CMP:
4445 condition_prefix = "";
4446 condition_suffix = " >= 0.0";
4447 break;
4449 case WINED3DSIH_CND:
4450 condition_prefix = "";
4451 condition_suffix = " > 0.5";
4452 break;
4454 case WINED3DSIH_MOVC:
4455 condition_prefix = "bool(";
4456 condition_suffix = ")";
4457 break;
4459 default:
4460 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4461 condition_prefix = "<unhandled prefix>";
4462 condition_suffix = "<unhandled suffix>";
4463 break;
4466 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4468 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4469 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4470 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4471 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4473 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4474 condition_prefix, src0_param.param_str, condition_suffix,
4475 src1_param.param_str, src2_param.param_str);
4476 return;
4479 dst = ins->dst[0];
4481 /* Splitting the instruction up in multiple lines imposes a problem:
4482 * The first lines may overwrite source parameters of the following lines.
4483 * Deal with that by using a temporary destination register if needed. */
4484 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4485 && ins->src[0].reg.type == dst.reg.type)
4486 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4487 && ins->src[1].reg.type == dst.reg.type)
4488 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4489 && ins->src[2].reg.type == dst.reg.type))
4490 temp_destination = TRUE;
4492 /* Cycle through all source0 channels. */
4493 for (i = 0; i < 4; ++i)
4495 write_mask = 0;
4496 /* Find the destination channels which use the current source0 channel. */
4497 for (j = 0; j < 4; ++j)
4499 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4501 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4502 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4505 dst.write_mask = ins->dst[0].write_mask & write_mask;
4507 if (temp_destination)
4509 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4510 continue;
4511 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4513 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type)))
4514 continue;
4516 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4517 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4518 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4520 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4521 condition_prefix, src0_param.param_str, condition_suffix,
4522 src1_param.param_str, src2_param.param_str);
4525 if (temp_destination)
4527 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4528 shader_glsl_append_dst(ins->ctx->buffer, ins);
4529 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4533 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4534 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4535 * the compare is done per component of src0. */
4536 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4538 struct glsl_src_param src0_param;
4539 struct glsl_src_param src1_param;
4540 struct glsl_src_param src2_param;
4541 DWORD write_mask;
4542 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4543 ins->ctx->reg_maps->shader_version.minor);
4545 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4547 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4548 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4549 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4550 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4552 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4553 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4554 else
4555 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4556 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4557 return;
4560 shader_glsl_conditional_move(ins);
4563 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4564 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4566 struct glsl_src_param src0_param;
4567 struct glsl_src_param src1_param;
4568 struct glsl_src_param src2_param;
4569 DWORD write_mask;
4571 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4572 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4573 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4574 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4575 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4576 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4579 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4580 Vertex shaders to GLSL codes */
4581 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4583 int i;
4584 int nComponents = 0;
4585 struct wined3d_shader_dst_param tmp_dst = {{0}};
4586 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4587 struct wined3d_shader_instruction tmp_ins;
4589 memset(&tmp_ins, 0, sizeof(tmp_ins));
4591 /* Set constants for the temporary argument */
4592 tmp_ins.ctx = ins->ctx;
4593 tmp_ins.dst_count = 1;
4594 tmp_ins.dst = &tmp_dst;
4595 tmp_ins.src_count = 2;
4596 tmp_ins.src = tmp_src;
4598 switch(ins->handler_idx)
4600 case WINED3DSIH_M4x4:
4601 nComponents = 4;
4602 tmp_ins.handler_idx = WINED3DSIH_DP4;
4603 break;
4604 case WINED3DSIH_M4x3:
4605 nComponents = 3;
4606 tmp_ins.handler_idx = WINED3DSIH_DP4;
4607 break;
4608 case WINED3DSIH_M3x4:
4609 nComponents = 4;
4610 tmp_ins.handler_idx = WINED3DSIH_DP3;
4611 break;
4612 case WINED3DSIH_M3x3:
4613 nComponents = 3;
4614 tmp_ins.handler_idx = WINED3DSIH_DP3;
4615 break;
4616 case WINED3DSIH_M3x2:
4617 nComponents = 2;
4618 tmp_ins.handler_idx = WINED3DSIH_DP3;
4619 break;
4620 default:
4621 break;
4624 tmp_dst = ins->dst[0];
4625 tmp_src[0] = ins->src[0];
4626 tmp_src[1] = ins->src[1];
4627 for (i = 0; i < nComponents; ++i)
4629 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4630 shader_glsl_dot(&tmp_ins);
4631 ++tmp_src[1].reg.idx[0].offset;
4636 The LRP instruction performs a component-wise linear interpolation
4637 between the second and third operands using the first operand as the
4638 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4639 This is equivalent to mix(src2, src1, src0);
4641 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4643 struct glsl_src_param src0_param;
4644 struct glsl_src_param src1_param;
4645 struct glsl_src_param src2_param;
4646 DWORD write_mask;
4648 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4650 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4651 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4652 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4654 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4655 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4658 /** Process the WINED3DSIO_LIT instruction in GLSL:
4659 * dst.x = dst.w = 1.0
4660 * dst.y = (src0.x > 0) ? src0.x
4661 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4662 * where src.w is clamped at +- 128
4664 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4666 struct glsl_src_param src0_param;
4667 struct glsl_src_param src1_param;
4668 struct glsl_src_param src3_param;
4669 char dst_mask[6];
4671 shader_glsl_append_dst(ins->ctx->buffer, ins);
4672 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4674 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4675 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4676 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4678 /* The sdk specifies the instruction like this
4679 * dst.x = 1.0;
4680 * if(src.x > 0.0) dst.y = src.x
4681 * else dst.y = 0.0.
4682 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4683 * else dst.z = 0.0;
4684 * dst.w = 1.0;
4685 * (where power = src.w clamped between -128 and 128)
4687 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4688 * dst.x = 1.0 ... No further explanation needed
4689 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4690 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4691 * dst.w = 1.0. ... Nothing fancy.
4693 * So we still have one conditional in there. So do this:
4694 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4696 * 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),
4697 * 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.
4698 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4700 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4701 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4702 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4704 shader_addline(ins->ctx->buffer,
4705 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4706 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4707 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4708 src0_param.param_str, src3_param.param_str, dst_mask);
4711 /** Process the WINED3DSIO_DST instruction in GLSL:
4712 * dst.x = 1.0
4713 * dst.y = src0.x * src0.y
4714 * dst.z = src0.z
4715 * dst.w = src1.w
4717 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4719 struct glsl_src_param src0y_param;
4720 struct glsl_src_param src0z_param;
4721 struct glsl_src_param src1y_param;
4722 struct glsl_src_param src1w_param;
4723 char dst_mask[6];
4725 shader_glsl_append_dst(ins->ctx->buffer, ins);
4726 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4728 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4729 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4730 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4731 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4733 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4734 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4737 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4738 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4739 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4741 * dst.x = cos(src0.?)
4742 * dst.y = sin(src0.?)
4743 * dst.z = dst.z
4744 * dst.w = dst.w
4746 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4748 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4749 struct glsl_src_param src0_param;
4750 DWORD write_mask;
4752 if (ins->ctx->reg_maps->shader_version.major < 4)
4754 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4756 write_mask = shader_glsl_append_dst(buffer, ins);
4757 switch (write_mask)
4759 case WINED3DSP_WRITEMASK_0:
4760 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4761 break;
4763 case WINED3DSP_WRITEMASK_1:
4764 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4765 break;
4767 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4768 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4769 src0_param.param_str, src0_param.param_str);
4770 break;
4772 default:
4773 ERR("Write mask should be .x, .y or .xy\n");
4774 break;
4777 return;
4780 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4783 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4785 char dst_mask[6];
4787 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4788 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4789 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4791 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4792 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4793 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4795 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4796 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4798 else
4800 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4801 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4802 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4805 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4807 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4808 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4809 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4813 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4814 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4815 * generate invalid code
4817 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4819 struct glsl_src_param src0_param;
4820 DWORD write_mask;
4822 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4823 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4825 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4828 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4829 * Start a for() loop where src1.y is the initial value of aL,
4830 * increment aL by src1.z for a total of src1.x iterations.
4831 * Need to use a temporary variable for this operation.
4833 /* FIXME: I don't think nested loops will work correctly this way. */
4834 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4836 struct wined3d_shader_parser_state *state = ins->ctx->state;
4837 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4838 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4839 const struct wined3d_shader *shader = ins->ctx->shader;
4840 const struct wined3d_shader_lconst *constant;
4841 struct wined3d_string_buffer *reg_name;
4842 const DWORD *control_values = NULL;
4844 if (ins->ctx->reg_maps->shader_version.major < 4)
4846 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4847 * class hardware doesn't support real varying indexing, but Microsoft
4848 * designed this feature for Shader model 2.x+. If the loop control is
4849 * known at compile time, the GLSL compiler can unroll the loop, and
4850 * replace indirect addressing with direct addressing. */
4851 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4853 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4855 if (constant->idx == ins->src[1].reg.idx[0].offset)
4857 control_values = constant->value;
4858 break;
4863 if (control_values)
4865 struct wined3d_shader_loop_control loop_control;
4866 loop_control.count = control_values[0];
4867 loop_control.start = control_values[1];
4868 loop_control.step = (int)control_values[2];
4870 if (loop_control.step > 0)
4872 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4873 state->current_loop_depth, loop_control.start,
4874 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4875 state->current_loop_depth, loop_control.step);
4877 else if (loop_control.step < 0)
4879 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4880 state->current_loop_depth, loop_control.start,
4881 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4882 state->current_loop_depth, loop_control.step);
4884 else
4886 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4887 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4888 state->current_loop_depth, loop_control.count,
4889 state->current_loop_depth);
4892 else
4894 reg_name = string_buffer_get(priv->string_buffers);
4895 shader_glsl_get_register_name(&ins->src[1].reg, ins->src[1].reg.data_type, reg_name, NULL, ins->ctx);
4897 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4898 state->current_loop_depth, state->current_loop_reg, reg_name->buffer,
4899 state->current_loop_depth, reg_name->buffer,
4900 state->current_loop_depth, state->current_loop_reg, reg_name->buffer);
4902 string_buffer_release(priv->string_buffers, reg_name);
4906 ++state->current_loop_reg;
4908 else
4910 shader_addline(buffer, "for (;;)\n{\n");
4913 ++state->current_loop_depth;
4916 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4918 struct wined3d_shader_parser_state *state = ins->ctx->state;
4920 shader_addline(ins->ctx->buffer, "}\n");
4922 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4924 --state->current_loop_depth;
4925 --state->current_loop_reg;
4928 if (ins->handler_idx == WINED3DSIH_ENDREP)
4930 --state->current_loop_depth;
4934 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4936 struct wined3d_shader_parser_state *state = ins->ctx->state;
4937 const struct wined3d_shader *shader = ins->ctx->shader;
4938 const struct wined3d_shader_lconst *constant;
4939 struct glsl_src_param src0_param;
4940 const DWORD *control_values = NULL;
4942 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4943 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4945 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4947 if (constant->idx == ins->src[0].reg.idx[0].offset)
4949 control_values = constant->value;
4950 break;
4955 if (control_values)
4957 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4958 state->current_loop_depth, state->current_loop_depth,
4959 control_values[0], state->current_loop_depth);
4961 else
4963 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4964 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4965 state->current_loop_depth, state->current_loop_depth,
4966 src0_param.param_str, state->current_loop_depth);
4969 ++state->current_loop_depth;
4972 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4974 struct glsl_src_param src0_param;
4976 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4977 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4980 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4982 struct glsl_src_param src0_param;
4984 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4985 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4988 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4990 shader_addline(ins->ctx->buffer, "default:\n");
4993 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
4995 struct glsl_src_param src_param;
4996 const char *condition;
4998 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
4999 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5000 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
5003 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5005 shader_glsl_generate_condition(ins);
5006 shader_addline(ins->ctx->buffer, "{\n");
5009 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5011 struct glsl_src_param src0_param;
5012 struct glsl_src_param src1_param;
5014 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5015 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5017 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5018 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5021 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5023 shader_addline(ins->ctx->buffer, "} else {\n");
5026 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5028 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5029 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5030 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5032 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5033 if (!priv->gl_info->supported[ARB_CLIP_CONTROL])
5034 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5036 if (!stream)
5037 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5038 else
5039 FIXME("Unhandled primitive stream %u.\n", stream);
5042 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5044 shader_addline(ins->ctx->buffer, "break;\n");
5047 /* FIXME: According to MSDN the compare is done per component. */
5048 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5050 struct glsl_src_param src0_param;
5051 struct glsl_src_param src1_param;
5053 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5054 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5056 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5057 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5060 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5062 const char *op;
5064 switch (ins->handler_idx)
5066 case WINED3DSIH_BREAKP:
5067 op = "break;";
5068 break;
5069 case WINED3DSIH_CONTINUEP:
5070 op = "continue;";
5071 break;
5072 case WINED3DSIH_RETP:
5073 op = "return;";
5074 break;
5075 default:
5076 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5077 return;
5080 shader_glsl_generate_condition(ins);
5081 if (ins->handler_idx == WINED3DSIH_RETP)
5083 shader_addline(ins->ctx->buffer, "{\n");
5084 shader_glsl_generate_shader_epilogue(ins->ctx);
5086 shader_addline(ins->ctx->buffer, " %s\n", op);
5087 if (ins->handler_idx == WINED3DSIH_RETP)
5088 shader_addline(ins->ctx->buffer, "}\n");
5091 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5093 shader_addline(ins->ctx->buffer, "continue;\n");
5096 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5098 shader_addline(ins->ctx->buffer, "}\n");
5099 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5101 /* Subroutines appear at the end of the shader. */
5102 ins->ctx->state->in_subroutine = TRUE;
5105 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5107 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5110 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5112 struct glsl_src_param src1_param;
5114 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5115 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5116 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5119 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5121 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5123 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5125 shader_glsl_generate_shader_epilogue(ins->ctx);
5126 shader_addline(ins->ctx->buffer, "return;\n");
5130 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5132 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5133 ins->ctx->reg_maps->shader_version.minor);
5134 struct glsl_sample_function sample_function;
5135 DWORD sample_flags = 0;
5136 DWORD resource_idx;
5137 DWORD mask = 0, swizzle;
5138 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5139 enum wined3d_shader_resource_type resource_type;
5141 /* 1.0-1.4: Use destination register as sampler source.
5142 * 2.0+: Use provided sampler source. */
5143 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5144 resource_idx = ins->dst[0].reg.idx[0].offset;
5145 else
5146 resource_idx = ins->src[1].reg.idx[0].offset;
5148 resource_type = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
5149 ? pixelshader_get_resource_type(ins->ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
5150 : ins->ctx->reg_maps->resource_info[resource_idx].type;
5152 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5154 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5155 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5157 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5158 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5160 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5161 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5163 case WINED3D_TTFF_COUNT1:
5164 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5165 break;
5166 case WINED3D_TTFF_COUNT2:
5167 mask = WINED3DSP_WRITEMASK_1;
5168 break;
5169 case WINED3D_TTFF_COUNT3:
5170 mask = WINED3DSP_WRITEMASK_2;
5171 break;
5172 case WINED3D_TTFF_COUNT4:
5173 case WINED3D_TTFF_DISABLE:
5174 mask = WINED3DSP_WRITEMASK_3;
5175 break;
5179 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5181 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5183 if (src_mod == WINED3DSPSM_DZ) {
5184 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5185 mask = WINED3DSP_WRITEMASK_2;
5186 } else if (src_mod == WINED3DSPSM_DW) {
5187 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5188 mask = WINED3DSP_WRITEMASK_3;
5191 else
5193 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5194 && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5196 /* ps 2.0 texldp instruction always divides by the fourth component. */
5197 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5198 mask = WINED3DSP_WRITEMASK_3;
5202 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5203 mask |= sample_function.coord_mask;
5204 sample_function.coord_mask = mask;
5206 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5207 else swizzle = ins->src[1].swizzle;
5209 /* 1.0-1.3: Use destination register as coordinate source.
5210 1.4+: Use provided coordinate source register. */
5211 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5213 char coord_mask[6];
5214 shader_glsl_write_mask_to_str(mask, coord_mask);
5215 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5216 "T%u%s", resource_idx, coord_mask);
5218 else
5220 struct glsl_src_param coord_param;
5221 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5222 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5224 struct glsl_src_param bias;
5225 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5226 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5227 NULL, "%s", coord_param.param_str);
5228 } else {
5229 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5230 "%s", coord_param.param_str);
5233 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5236 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5238 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5239 const struct wined3d_gl_info *gl_info = priv->gl_info;
5240 struct glsl_src_param coord_param, dx_param, dy_param;
5241 struct glsl_sample_function sample_function;
5242 DWORD sampler_idx;
5243 DWORD swizzle = ins->src[1].swizzle;
5245 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5247 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5248 shader_glsl_tex(ins);
5249 return;
5252 sampler_idx = ins->src[1].reg.idx[0].offset;
5254 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5255 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5256 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5257 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5259 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5260 NULL, NULL, "%s", coord_param.param_str);
5261 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5264 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5266 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5267 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5268 const struct wined3d_gl_info *gl_info = priv->gl_info;
5269 struct glsl_src_param coord_param, lod_param;
5270 struct glsl_sample_function sample_function;
5271 DWORD swizzle = ins->src[1].swizzle;
5272 DWORD sampler_idx;
5274 sampler_idx = ins->src[1].reg.idx[0].offset;
5276 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5277 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5279 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5281 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5282 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5284 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5285 * However, the NVIDIA drivers allow them in fragment shaders as well,
5286 * even without the appropriate extension. */
5287 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5289 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5290 "%s", coord_param.param_str);
5291 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5294 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5295 unsigned int resource_idx, unsigned int sampler_idx)
5297 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5298 unsigned int i;
5300 for (i = 0; i < sampler_map->count; ++i)
5302 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5303 return entries[i].bind_idx;
5306 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5308 return ~0u;
5311 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5313 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5314 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5315 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5316 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5317 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5318 struct glsl_src_param structure_idx, offset, data, data2;
5319 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5320 enum wined3d_shader_resource_type resource_type;
5321 struct wined3d_string_buffer *address;
5322 enum wined3d_data_type data_type;
5323 unsigned int resource_idx, stride;
5324 const char *op, *resource;
5325 DWORD coord_mask;
5326 BOOL is_tgsm;
5328 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5329 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5330 if (is_tgsm)
5332 if (resource_idx >= reg_maps->tgsm_count)
5334 ERR("Invalid TGSM index %u.\n", resource_idx);
5335 return;
5337 resource = "g";
5338 data_type = WINED3D_DATA_UINT;
5339 coord_mask = 1;
5340 stride = reg_maps->tgsm[resource_idx].stride;
5342 else
5344 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5346 ERR("Invalid UAV index %u.\n", resource_idx);
5347 return;
5349 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5350 if (resource_type >= ARRAY_SIZE(resource_type_info))
5352 ERR("Unexpected resource type %#x.\n", resource_type);
5353 return;
5355 resource = "image";
5356 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5357 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5358 stride = reg_maps->uav_resource_info[resource_idx].stride;
5361 switch (ins->handler_idx)
5363 case WINED3DSIH_ATOMIC_AND:
5364 case WINED3DSIH_IMM_ATOMIC_AND:
5365 if (is_tgsm)
5366 op = "atomicAnd";
5367 else
5368 op = "imageAtomicAnd";
5369 break;
5370 case WINED3DSIH_ATOMIC_CMP_STORE:
5371 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5372 if (is_tgsm)
5373 op = "atomicCompSwap";
5374 else
5375 op = "imageAtomicCompSwap";
5376 break;
5377 case WINED3DSIH_ATOMIC_IADD:
5378 case WINED3DSIH_IMM_ATOMIC_IADD:
5379 if (is_tgsm)
5380 op = "atomicAdd";
5381 else
5382 op = "imageAtomicAdd";
5383 break;
5384 case WINED3DSIH_ATOMIC_IMAX:
5385 case WINED3DSIH_IMM_ATOMIC_IMAX:
5386 if (is_tgsm)
5387 op = "atomicMax";
5388 else
5389 op = "imageAtomicMax";
5390 if (data_type != WINED3D_DATA_INT)
5392 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5393 return;
5395 break;
5396 case WINED3DSIH_ATOMIC_IMIN:
5397 case WINED3DSIH_IMM_ATOMIC_IMIN:
5398 if (is_tgsm)
5399 op = "atomicMin";
5400 else
5401 op = "imageAtomicMin";
5402 if (data_type != WINED3D_DATA_INT)
5404 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5405 return;
5407 break;
5408 case WINED3DSIH_ATOMIC_OR:
5409 case WINED3DSIH_IMM_ATOMIC_OR:
5410 if (is_tgsm)
5411 op = "atomicOr";
5412 else
5413 op = "imageAtomicOr";
5414 break;
5415 case WINED3DSIH_ATOMIC_UMAX:
5416 case WINED3DSIH_IMM_ATOMIC_UMAX:
5417 if (is_tgsm)
5418 op = "atomicMax";
5419 else
5420 op = "imageAtomicMax";
5421 if (data_type != WINED3D_DATA_UINT)
5423 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5424 return;
5426 break;
5427 case WINED3DSIH_ATOMIC_UMIN:
5428 case WINED3DSIH_IMM_ATOMIC_UMIN:
5429 if (is_tgsm)
5430 op = "atomicMin";
5431 else
5432 op = "imageAtomicMin";
5433 if (data_type != WINED3D_DATA_UINT)
5435 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5436 return;
5438 break;
5439 case WINED3DSIH_ATOMIC_XOR:
5440 case WINED3DSIH_IMM_ATOMIC_XOR:
5441 if (is_tgsm)
5442 op = "atomicXor";
5443 else
5444 op = "imageAtomicXor";
5445 break;
5446 case WINED3DSIH_IMM_ATOMIC_EXCH:
5447 if (is_tgsm)
5448 op = "atomicExchange";
5449 else
5450 op = "imageAtomicExchange";
5451 break;
5452 default:
5453 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5454 return;
5457 address = string_buffer_get(priv->string_buffers);
5458 if (stride)
5460 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5461 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5462 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5464 else
5466 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5467 string_buffer_sprintf(address, "%s", offset.param_str);
5468 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5469 shader_addline(address, "/ 4");
5472 if (is_imm_instruction)
5473 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5475 if (is_tgsm)
5476 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5477 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5478 else
5479 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5480 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5482 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5483 shader_addline(buffer, "%s", data.param_str);
5484 if (ins->src_count >= 3)
5486 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5487 shader_addline(buffer, ", %s", data2.param_str);
5490 if (is_imm_instruction)
5491 shader_addline(buffer, ")");
5492 shader_addline(buffer, ");\n");
5494 string_buffer_release(priv->string_buffers, address);
5497 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5499 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5500 const char *op;
5502 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5503 op = "atomicCounterIncrement";
5504 else
5505 op = "atomicCounterDecrement";
5507 shader_glsl_append_dst(ins->ctx->buffer, ins);
5508 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5511 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5513 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5514 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5515 enum wined3d_shader_resource_type resource_type;
5516 struct glsl_src_param image_coord_param;
5517 enum wined3d_data_type data_type;
5518 DWORD coord_mask, write_mask;
5519 unsigned int uav_idx;
5520 char dst_swizzle[6];
5522 uav_idx = ins->src[1].reg.idx[0].offset;
5523 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5525 ERR("Invalid UAV index %u.\n", uav_idx);
5526 return;
5528 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5529 if (resource_type >= ARRAY_SIZE(resource_type_info))
5531 ERR("Unexpected resource type %#x.\n", resource_type);
5532 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5534 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5535 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5537 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5538 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5540 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5541 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5542 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5545 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5547 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5548 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5549 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5550 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5551 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5552 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5553 struct glsl_src_param structure_idx, offset;
5554 struct wined3d_string_buffer *address;
5555 struct wined3d_shader_dst_param dst;
5556 const char *function, *resource;
5558 resource_idx = src->reg.idx[0].offset;
5559 if (src->reg.type == WINED3DSPR_RESOURCE)
5561 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5563 ERR("Invalid resource index %u.\n", resource_idx);
5564 return;
5566 stride = reg_maps->resource_info[resource_idx].stride;
5567 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5568 function = "texelFetch";
5569 resource = "sampler";
5571 else if (src->reg.type == WINED3DSPR_UAV)
5573 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5575 ERR("Invalid UAV index %u.\n", resource_idx);
5576 return;
5578 stride = reg_maps->uav_resource_info[resource_idx].stride;
5579 bind_idx = resource_idx;
5580 function = "imageLoad";
5581 resource = "image";
5583 else
5585 if (resource_idx >= reg_maps->tgsm_count)
5587 ERR("Invalid TGSM index %u.\n", resource_idx);
5588 return;
5590 stride = reg_maps->tgsm[resource_idx].stride;
5591 bind_idx = resource_idx;
5592 function = NULL;
5593 resource = "g";
5596 address = string_buffer_get(priv->string_buffers);
5597 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5599 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5600 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5602 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5603 shader_addline(address, "%s / 4", offset.param_str);
5605 dst = ins->dst[0];
5606 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5608 /* The instruction is split into multiple lines. The first lines may
5609 * overwrite source parameters of the following lines. */
5610 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5611 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5614 for (i = 0; i < 4; ++i)
5616 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5617 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type))
5618 continue;
5620 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5621 if (function)
5622 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5623 function, prefix, resource, bind_idx, address->buffer, swizzle);
5624 else
5625 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5626 prefix, resource, bind_idx, address->buffer, swizzle);
5629 string_buffer_release(priv->string_buffers, address);
5632 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5634 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5635 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5636 struct glsl_src_param image_coord_param, image_data_param;
5637 enum wined3d_shader_resource_type resource_type;
5638 enum wined3d_data_type data_type;
5639 unsigned int uav_idx;
5640 DWORD coord_mask;
5642 uav_idx = ins->dst[0].reg.idx[0].offset;
5643 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5645 ERR("Invalid UAV index %u.\n", uav_idx);
5646 return;
5648 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5649 if (resource_type >= ARRAY_SIZE(resource_type_info))
5651 ERR("Unexpected resource type %#x.\n", resource_type);
5652 return;
5654 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5655 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5657 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5658 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5659 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5660 shader_glsl_get_prefix(version->type), uav_idx,
5661 image_coord_param.param_str, image_data_param.param_str);
5664 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5666 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5667 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5668 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5669 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5670 struct glsl_src_param structure_idx, offset, data;
5671 unsigned int i, resource_idx, stride, src_idx = 0;
5672 struct wined3d_string_buffer *address;
5673 DWORD write_mask;
5674 BOOL is_tgsm;
5676 resource_idx = ins->dst[0].reg.idx[0].offset;
5677 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5678 if (is_tgsm)
5680 if (resource_idx >= reg_maps->tgsm_count)
5682 ERR("Invalid TGSM index %u.\n", resource_idx);
5683 return;
5685 stride = reg_maps->tgsm[resource_idx].stride;
5687 else
5689 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5691 ERR("Invalid UAV index %u.\n", resource_idx);
5692 return;
5694 stride = reg_maps->uav_resource_info[resource_idx].stride;
5697 address = string_buffer_get(priv->string_buffers);
5698 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5700 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5701 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5703 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5704 shader_addline(address, "%s / 4", offset.param_str);
5706 for (i = 0; i < 4; ++i)
5708 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5709 continue;
5711 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5713 if (is_tgsm)
5714 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5715 prefix, resource_idx, address->buffer, i, data.param_str);
5716 else
5717 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5718 prefix, resource_idx, address->buffer, i, data.param_str);
5721 string_buffer_release(priv->string_buffers, address);
5724 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5726 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5727 unsigned int sync_flags = ins->flags;
5729 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5731 shader_addline(buffer, "barrier();\n");
5732 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5735 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5737 shader_addline(buffer, "memoryBarrierShared();\n");
5738 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5741 if (sync_flags & WINED3DSSF_GLOBAL_UAV)
5743 shader_addline(buffer, "memoryBarrier();\n");
5744 sync_flags &= ~WINED3DSSF_GLOBAL_UAV;
5747 if (sync_flags)
5748 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5751 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5752 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5754 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5755 unsigned int idx = reg->idx[0].offset;
5757 if (reg->type == WINED3DSPR_RESOURCE)
5759 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5761 ERR("Invalid resource index %u.\n", idx);
5762 return NULL;
5764 return &reg_maps->resource_info[idx];
5767 if (reg->type == WINED3DSPR_UAV)
5769 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5771 ERR("Invalid UAV index %u.\n", idx);
5772 return NULL;
5774 return &reg_maps->uav_resource_info[idx];
5777 FIXME("Unhandled register type %#x.\n", reg->type);
5778 return NULL;
5781 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5783 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5784 const struct wined3d_shader_resource_info *resource_info;
5785 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5786 unsigned int resource_idx;
5787 char dst_swizzle[6];
5788 DWORD write_mask;
5790 write_mask = shader_glsl_append_dst(buffer, ins);
5791 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5793 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5794 return;
5795 resource_idx = ins->src[0].reg.idx[0].offset;
5797 shader_addline(buffer, "ivec2(");
5798 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5800 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5801 resource_idx, WINED3D_SAMPLER_DEFAULT);
5802 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5804 else
5806 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5808 if (resource_info->stride)
5809 shader_addline(buffer, " / %u", resource_info->stride);
5810 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5811 shader_addline(buffer, " * 4");
5812 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5815 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5817 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5818 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5821 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5823 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5826 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5828 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5829 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5830 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5831 const struct wined3d_gl_info *gl_info = priv->gl_info;
5832 enum wined3d_shader_resource_type resource_type;
5833 enum wined3d_shader_register_type reg_type;
5834 unsigned int resource_idx, bind_idx, i;
5835 enum wined3d_data_type dst_data_type;
5836 struct glsl_src_param lod_param;
5837 BOOL supports_mipmaps;
5838 char dst_swizzle[6];
5839 DWORD write_mask;
5841 dst_data_type = ins->dst[0].reg.data_type;
5842 if (ins->flags == WINED3DSI_RESINFO_UINT)
5843 dst_data_type = WINED3D_DATA_UINT;
5844 else if (ins->flags)
5845 FIXME("Unhandled flags %#x.\n", ins->flags);
5847 reg_type = ins->src[1].reg.type;
5848 resource_idx = ins->src[1].reg.idx[0].offset;
5849 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5850 if (reg_type == WINED3DSPR_RESOURCE)
5852 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5853 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5854 resource_idx, WINED3D_SAMPLER_DEFAULT);
5856 else
5858 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5859 bind_idx = resource_idx;
5862 if (resource_type >= ARRAY_SIZE(resource_type_info))
5864 ERR("Unexpected resource type %#x.\n", resource_type);
5865 return;
5868 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, dst_data_type);
5869 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5871 if (dst_data_type == WINED3D_DATA_UINT)
5872 shader_addline(buffer, "uvec4(");
5873 else
5874 shader_addline(buffer, "vec4(");
5876 if (reg_type == WINED3DSPR_RESOURCE)
5878 shader_addline(buffer, "textureSize(%s_sampler%u",
5879 shader_glsl_get_prefix(version->type), bind_idx);
5881 else
5883 shader_addline(buffer, "imageSize(%s_image%u",
5884 shader_glsl_get_prefix(version->type), bind_idx);
5887 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5888 if (supports_mipmaps)
5889 shader_addline(buffer, ", %s", lod_param.param_str);
5890 shader_addline(buffer, "), ");
5892 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5893 shader_addline(buffer, "0, ");
5895 if (supports_mipmaps)
5897 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5899 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5900 shader_glsl_get_prefix(version->type), bind_idx);
5902 else
5904 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
5905 shader_addline(buffer, "1");
5908 else
5910 shader_addline(buffer, "1");
5913 shader_addline(buffer, ")%s);\n", dst_swizzle);
5916 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
5918 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5919 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5920 const struct wined3d_gl_info *gl_info = priv->gl_info;
5921 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5922 const struct wined3d_shader_dst_param *dst = ins->dst;
5923 const struct wined3d_shader_src_param *src = ins->src;
5924 enum wined3d_shader_resource_type resource_type;
5925 enum wined3d_data_type dst_data_type;
5926 unsigned int resource_idx, bind_idx;
5927 char dst_swizzle[6];
5928 DWORD write_mask;
5930 dst_data_type = dst->reg.data_type;
5931 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
5932 dst_data_type = WINED3D_DATA_UINT;
5933 else if (ins->flags)
5934 FIXME("Unhandled flags %#x.\n", ins->flags);
5936 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, 0, dst_data_type);
5937 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
5939 if (dst_data_type == WINED3D_DATA_UINT)
5940 shader_addline(buffer, "uvec4(");
5941 else
5942 shader_addline(buffer, "vec4(");
5944 if (src->reg.type == WINED3DSPR_RASTERIZER)
5946 if (gl_info->supported[ARB_SAMPLE_SHADING])
5948 shader_addline(buffer, "gl_NumSamples");
5950 else
5952 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
5953 shader_addline(buffer, "1");
5956 else
5958 resource_idx = src->reg.idx[0].offset;
5959 resource_type = reg_maps->resource_info[resource_idx].type;
5960 if (resource_type >= ARRAY_SIZE(resource_type_info))
5962 ERR("Unexpected resource type %#x.\n", resource_type);
5963 return;
5965 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5967 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
5969 shader_addline(buffer, "textureSamples(%s_sampler%u)",
5970 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
5972 else
5974 FIXME("textureSamples() is not supported.\n");
5975 shader_addline(buffer, "1");
5979 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
5982 static void shader_glsl_interpolate(const struct wined3d_shader_instruction *ins)
5984 const struct wined3d_shader_src_param *input = &ins->src[0];
5985 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5986 struct glsl_src_param sample_param;
5987 char dst_swizzle[6];
5988 DWORD write_mask;
5990 write_mask = shader_glsl_append_dst(buffer, ins);
5991 shader_glsl_get_swizzle(input, FALSE, write_mask, dst_swizzle);
5993 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &sample_param);
5994 shader_addline(buffer, "interpolateAtSample(shader_in.reg%u, %s)%s);\n",
5995 input->reg.idx[0].offset, sample_param.param_str, dst_swizzle);
5998 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6000 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6001 struct glsl_src_param coord_param, lod_param, sample_param;
6002 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6003 struct glsl_sample_function sample_function;
6004 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6005 BOOL has_lod_param;
6007 if (wined3d_shader_instruction_has_texel_offset(ins))
6008 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6010 resource_idx = ins->src[1].reg.idx[0].offset;
6011 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6013 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6015 ERR("Invalid resource index %u.\n", resource_idx);
6016 return;
6018 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6020 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6021 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6022 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6023 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6024 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6026 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6027 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6028 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6030 else
6032 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6033 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6034 "%s", coord_param.param_str);
6036 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6039 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6041 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6042 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6043 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6044 struct glsl_sample_function sample_function;
6045 DWORD flags = 0;
6047 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6048 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6049 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6050 flags |= WINED3D_GLSL_SAMPLE_LOD;
6051 if (wined3d_shader_instruction_has_texel_offset(ins))
6052 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6054 resource_idx = ins->src[1].reg.idx[0].offset;
6055 sampler_idx = ins->src[2].reg.idx[0].offset;
6057 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6058 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6060 switch (ins->handler_idx)
6062 case WINED3DSIH_SAMPLE:
6063 break;
6064 case WINED3DSIH_SAMPLE_B:
6065 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6066 lod_param_str = lod_param.param_str;
6067 break;
6068 case WINED3DSIH_SAMPLE_GRAD:
6069 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6070 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6071 dx_param_str = dx_param.param_str;
6072 dy_param_str = dy_param.param_str;
6073 break;
6074 case WINED3DSIH_SAMPLE_LOD:
6075 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6076 lod_param_str = lod_param.param_str;
6077 break;
6078 default:
6079 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6080 break;
6083 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6084 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6085 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6086 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6089 /* Unless EXT_texture_shadow_lod is available, GLSL doesn't provide a function
6090 * to sample from level zero with depth comparison for array textures and cube
6091 * textures. We use textureGrad*() to implement sample_c_lz in that case. */
6092 static void shader_glsl_gen_sample_c_lz_emulation(const struct wined3d_shader_instruction *ins,
6093 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6094 unsigned int coord_size, const char *coord_param, const char *ref_param)
6096 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6097 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6098 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6099 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6100 char dst_swizzle[6];
6102 WARN("Emitting textureGrad() for sample_c_lz.\n");
6104 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6105 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, sample_function->data_type);
6106 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6107 sample_function->offset_size ? "Offset" : "",
6108 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6109 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6110 if (sample_function->offset_size)
6112 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6113 shader_addline(buffer, ", ");
6114 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6116 shader_addline(buffer, "))%s);\n", dst_swizzle);
6119 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6121 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6122 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6123 const struct wined3d_shader_resource_info *resource_info;
6124 const struct wined3d_gl_info *gl_info = priv->gl_info;
6125 struct glsl_src_param coord_param, compare_param;
6126 struct glsl_sample_function sample_function;
6127 const char *lod_param = NULL;
6128 unsigned int coord_size;
6129 DWORD flags = 0;
6131 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6133 lod_param = "0";
6134 flags |= WINED3D_GLSL_SAMPLE_LOD;
6137 if (wined3d_shader_instruction_has_texel_offset(ins))
6138 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6140 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6141 return;
6142 resource_idx = ins->src[1].reg.idx[0].offset;
6143 sampler_idx = ins->src[2].reg.idx[0].offset;
6145 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6146 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6147 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6148 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6149 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6150 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6151 && !gl_info->supported[EXT_TEXTURE_SHADOW_LOD]
6152 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6153 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6155 shader_glsl_gen_sample_c_lz_emulation(ins, sampler_bind_idx, &sample_function,
6156 coord_size, coord_param.param_str, compare_param.param_str);
6158 else
6160 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6161 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6162 coord_size, coord_param.param_str, compare_param.param_str);
6164 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6167 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6169 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6170 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6171 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6172 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6173 struct glsl_src_param coord_param, compare_param, offset_param;
6174 const struct wined3d_gl_info *gl_info = priv->gl_info;
6175 const struct wined3d_shader_resource_info *resource_info;
6176 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6177 unsigned int coord_size, offset_size;
6178 char dst_swizzle[6];
6179 BOOL has_offset;
6181 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6183 FIXME("OpenGL implementation does not support textureGather.\n");
6184 return;
6187 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6188 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6189 || wined3d_shader_instruction_has_texel_offset(ins);
6191 resource_param_idx =
6192 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6193 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6194 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6195 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6196 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6198 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6199 return;
6201 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6203 ERR("Unexpected resource type %#x.\n", resource_info->type);
6204 return;
6206 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6208 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6209 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, resource_info->data_type);
6211 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6213 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6214 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6215 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6217 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6218 shader_addline(buffer, ", %s", compare_param.param_str);
6220 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6222 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6223 shader_addline(buffer, ", %s", offset_param.param_str);
6225 else if (has_offset)
6227 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6228 shader_addline(buffer, ", ");
6229 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6231 if (component_idx)
6232 shader_addline(buffer, ", %u", component_idx);
6234 shader_addline(buffer, ")%s);\n", dst_swizzle);
6237 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6239 /* FIXME: Make this work for more than just 2D textures */
6240 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6241 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6243 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6245 char dst_mask[6];
6247 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6248 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6249 ins->dst[0].reg.idx[0].offset, dst_mask);
6251 else
6253 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6254 DWORD reg = ins->src[0].reg.idx[0].offset;
6255 char dst_swizzle[6];
6257 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6259 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6261 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6262 struct glsl_src_param div_param;
6263 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6265 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6267 if (mask_size > 1)
6268 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6269 else
6270 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6272 else
6274 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6279 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6280 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6281 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6282 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6284 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6285 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6286 struct glsl_sample_function sample_function;
6287 struct glsl_src_param src0_param;
6288 UINT mask_size;
6290 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6292 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6293 * scalar, and projected sampling would require 4.
6295 * It is a dependent read - not valid with conditional NP2 textures
6297 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6298 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6300 switch(mask_size)
6302 case 1:
6303 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6304 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6305 break;
6307 case 2:
6308 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6309 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6310 break;
6312 case 3:
6313 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6314 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6315 break;
6317 default:
6318 FIXME("Unexpected mask size %u\n", mask_size);
6319 break;
6321 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6324 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6325 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6326 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6328 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6329 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6330 struct glsl_src_param src0_param;
6331 DWORD dst_mask;
6332 unsigned int mask_size;
6334 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6335 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6336 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6338 if (mask_size > 1) {
6339 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6340 } else {
6341 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6345 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6346 * Calculate the depth as dst.x / dst.y */
6347 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6349 struct glsl_dst_param dst_param;
6351 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6353 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6354 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6355 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6356 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6357 * >= 1.0 or < 0.0
6359 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6360 dst_param.reg_name, dst_param.reg_name);
6363 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6364 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6365 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6366 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6368 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6370 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6371 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6372 struct glsl_src_param src0_param;
6374 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6376 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6377 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6380 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6381 * Calculate the 1st of a 2-row matrix multiplication. */
6382 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6384 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6385 DWORD reg = ins->dst[0].reg.idx[0].offset;
6386 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6387 struct glsl_src_param src0_param;
6389 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6390 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6393 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6394 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6395 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6397 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6398 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6399 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6400 DWORD reg = ins->dst[0].reg.idx[0].offset;
6401 struct glsl_src_param src0_param;
6403 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6404 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6405 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6408 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6410 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6411 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6412 struct glsl_sample_function sample_function;
6413 DWORD reg = ins->dst[0].reg.idx[0].offset;
6414 struct glsl_src_param src0_param;
6416 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6417 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6419 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6421 /* Sample the texture using the calculated coordinates */
6422 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6423 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6426 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6427 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6428 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6430 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6431 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6432 struct glsl_sample_function sample_function;
6433 DWORD reg = ins->dst[0].reg.idx[0].offset;
6434 struct glsl_src_param src0_param;
6436 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6437 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6439 /* Dependent read, not valid with conditional NP2 */
6440 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6442 /* Sample the texture using the calculated coordinates */
6443 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6444 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6446 tex_mx->current_row = 0;
6449 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6450 * Perform the 3rd row of a 3x3 matrix multiply */
6451 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6453 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6454 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6455 DWORD reg = ins->dst[0].reg.idx[0].offset;
6456 struct glsl_src_param src0_param;
6457 char dst_mask[6];
6459 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6461 shader_glsl_append_dst(ins->ctx->buffer, ins);
6462 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6463 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6465 tex_mx->current_row = 0;
6468 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6469 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6470 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6472 struct glsl_src_param src0_param;
6473 struct glsl_src_param src1_param;
6474 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6475 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6476 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6477 struct glsl_sample_function sample_function;
6478 DWORD reg = ins->dst[0].reg.idx[0].offset;
6479 char coord_mask[6];
6481 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6482 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6484 /* Perform the last matrix multiply operation */
6485 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6486 /* Reflection calculation */
6487 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6489 /* Dependent read, not valid with conditional NP2 */
6490 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6491 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6493 /* Sample the texture */
6494 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6495 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6496 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6498 tex_mx->current_row = 0;
6501 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6502 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6503 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6505 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6506 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6507 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6508 struct glsl_sample_function sample_function;
6509 DWORD reg = ins->dst[0].reg.idx[0].offset;
6510 struct glsl_src_param src0_param;
6511 char coord_mask[6];
6513 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6515 /* Perform the last matrix multiply operation */
6516 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6518 /* Construct the eye-ray vector from w coordinates */
6519 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6520 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6521 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6523 /* Dependent read, not valid with conditional NP2 */
6524 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6525 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6527 /* Sample the texture using the calculated coordinates */
6528 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6529 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6530 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6532 tex_mx->current_row = 0;
6535 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6536 * Apply a fake bump map transform.
6537 * texbem is pshader <= 1.3 only, this saves a few version checks
6539 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6541 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6542 struct glsl_sample_function sample_function;
6543 struct glsl_src_param coord_param;
6544 DWORD sampler_idx;
6545 DWORD mask;
6546 DWORD flags;
6547 char coord_mask[6];
6549 sampler_idx = ins->dst[0].reg.idx[0].offset;
6550 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6551 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6553 /* Dependent read, not valid with conditional NP2 */
6554 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6555 mask = sample_function.coord_mask;
6557 shader_glsl_write_mask_to_str(mask, coord_mask);
6559 /* With projected textures, texbem only divides the static texture coord,
6560 * not the displacement, so we can't let GL handle this. */
6561 if (flags & WINED3D_PSARGS_PROJECTED)
6563 DWORD div_mask=0;
6564 char coord_div_mask[3];
6565 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6567 case WINED3D_TTFF_COUNT1:
6568 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6569 break;
6570 case WINED3D_TTFF_COUNT2:
6571 div_mask = WINED3DSP_WRITEMASK_1;
6572 break;
6573 case WINED3D_TTFF_COUNT3:
6574 div_mask = WINED3DSP_WRITEMASK_2;
6575 break;
6576 case WINED3D_TTFF_COUNT4:
6577 case WINED3D_TTFF_DISABLE:
6578 div_mask = WINED3DSP_WRITEMASK_3;
6579 break;
6581 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6582 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6585 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6587 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6588 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6589 coord_param.param_str, coord_mask);
6591 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6593 struct glsl_src_param luminance_param;
6594 struct glsl_dst_param dst_param;
6596 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6597 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6599 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6600 dst_param.reg_name, dst_param.mask_str,
6601 luminance_param.param_str, sampler_idx, sampler_idx);
6603 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6606 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6608 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6609 struct glsl_src_param src0_param, src1_param;
6611 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6612 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6614 shader_glsl_append_dst(ins->ctx->buffer, ins);
6615 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6616 src0_param.param_str, sampler_idx, src1_param.param_str);
6619 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6620 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6621 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6623 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6624 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6625 struct glsl_sample_function sample_function;
6626 struct wined3d_string_buffer *reg_name;
6628 reg_name = string_buffer_get(priv->string_buffers);
6629 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6631 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6632 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6633 "%s.wx", reg_name->buffer);
6634 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6636 string_buffer_release(priv->string_buffers, reg_name);
6639 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6640 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6641 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6643 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6644 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6645 struct glsl_sample_function sample_function;
6646 struct wined3d_string_buffer *reg_name;
6648 reg_name = string_buffer_get(priv->string_buffers);
6649 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6651 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6652 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6653 "%s.yz", reg_name->buffer);
6654 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6656 string_buffer_release(priv->string_buffers, reg_name);
6659 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6660 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6661 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6663 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6664 struct glsl_sample_function sample_function;
6665 struct glsl_src_param src0_param;
6667 /* Dependent read, not valid with conditional NP2 */
6668 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6669 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6671 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6672 "%s", src0_param.param_str);
6673 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6676 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6677 * If any of the first 3 components are < 0, discard this pixel */
6678 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6680 if (ins->ctx->reg_maps->shader_version.major >= 4)
6682 shader_glsl_generate_condition(ins);
6683 shader_addline(ins->ctx->buffer, " discard;\n");
6685 else
6687 struct glsl_dst_param dst_param;
6689 /* The argument is a destination parameter, and no writemasks are allowed */
6690 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6692 /* 2.0 shaders compare all 4 components in texkill. */
6693 if (ins->ctx->reg_maps->shader_version.major >= 2)
6694 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6695 /* 1.x shaders only compare the first 3 components, probably due to
6696 * the nature of the texkill instruction as a tex* instruction, and
6697 * phase, which kills all .w components. Even if all 4 components are
6698 * defined, only the first 3 are used. */
6699 else
6700 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6704 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6705 * dst = dot2(src0, src1) + src2 */
6706 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6708 struct glsl_src_param src0_param;
6709 struct glsl_src_param src1_param;
6710 struct glsl_src_param src2_param;
6711 DWORD write_mask;
6712 unsigned int mask_size;
6714 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6715 mask_size = shader_glsl_get_write_mask_size(write_mask);
6717 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6718 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6719 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6721 if (mask_size > 1) {
6722 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6723 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6724 } else {
6725 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6726 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6730 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6731 const struct wined3d_shader_signature *input_signature,
6732 const struct wined3d_shader_reg_maps *reg_maps,
6733 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6735 unsigned int i;
6737 for (i = 0; i < input_signature->element_count; ++i)
6739 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6740 const char *semantic_name;
6741 UINT semantic_idx;
6742 char reg_mask[6];
6744 /* Unused */
6745 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6746 continue;
6748 semantic_name = input->semantic_name;
6749 semantic_idx = input->semantic_idx;
6750 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6752 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6754 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6756 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6757 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6759 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6761 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6763 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6765 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6766 input->register_idx, reg_mask);
6768 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6770 if (gl_info->supported[ARB_SAMPLE_SHADING])
6771 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6772 input->register_idx, reg_mask);
6773 else
6774 FIXME("ARB_sample_shading is not supported.\n");
6776 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6778 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6779 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6780 input->register_idx, reg_mask);
6781 else
6782 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6784 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6786 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6787 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6788 input->register_idx, reg_mask);
6789 else
6790 FIXME("ARB_viewport_array is not supported.\n");
6792 else
6794 if (input->sysval_semantic)
6795 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6796 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6797 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6798 shader_glsl_shader_input_name(gl_info),
6799 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6802 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6804 if (args->pointsprite)
6805 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6806 shader->u.ps.input_reg_map[input->register_idx]);
6807 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6808 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6809 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6810 needs_legacy_glsl_syntax(gl_info)
6811 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6812 else
6813 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6814 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6816 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6818 if (!semantic_idx)
6819 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6820 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6821 else if (semantic_idx == 1)
6822 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6823 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6824 else
6825 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6826 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6828 else
6830 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6831 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6836 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6838 struct glsl_program_key key;
6840 key.vs_id = entry->vs.id;
6841 key.hs_id = entry->hs.id;
6842 key.ds_id = entry->ds.id;
6843 key.gs_id = entry->gs.id;
6844 key.ps_id = entry->ps.id;
6845 key.cs_id = entry->cs.id;
6847 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6849 ERR("Failed to insert program entry.\n");
6853 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6854 const struct glsl_program_key *key)
6856 struct wine_rb_entry *entry;
6858 entry = wine_rb_get(&priv->program_lookup, key);
6859 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6862 /* Context activation is done by the caller. */
6863 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6864 struct glsl_shader_prog_link *entry)
6866 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6868 GL_EXTCALL(glDeleteProgram(entry->id));
6869 if (entry->vs.id)
6870 list_remove(&entry->vs.shader_entry);
6871 if (entry->hs.id)
6872 list_remove(&entry->hs.shader_entry);
6873 if (entry->ds.id)
6874 list_remove(&entry->ds.shader_entry);
6875 if (entry->gs.id)
6876 list_remove(&entry->gs.shader_entry);
6877 if (entry->ps.id)
6878 list_remove(&entry->ps.shader_entry);
6879 if (entry->cs.id)
6880 list_remove(&entry->cs.shader_entry);
6881 heap_free(entry);
6884 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6885 const struct wined3d_gl_info *gl_info, const DWORD *map,
6886 const struct wined3d_shader_signature *input_signature,
6887 const struct wined3d_shader_reg_maps *reg_maps_in,
6888 const struct wined3d_shader_signature *output_signature,
6889 const struct wined3d_shader_reg_maps *reg_maps_out)
6891 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6892 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6893 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6894 unsigned int in_count = vec4_varyings(3, gl_info);
6895 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6896 DWORD in_idx, *set = NULL;
6897 unsigned int i, j;
6898 char reg_mask[6];
6900 set = heap_calloc(max_varyings, sizeof(*set));
6902 for (i = 0; i < input_signature->element_count; ++i)
6904 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6906 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6907 continue;
6909 in_idx = map[input->register_idx];
6910 /* Declared, but not read register */
6911 if (in_idx == ~0u)
6912 continue;
6913 if (in_idx >= max_varyings)
6915 FIXME("More input varyings declared than supported, expect issues.\n");
6916 continue;
6919 if (in_idx == in_count)
6920 string_buffer_sprintf(destination, "gl_FrontColor");
6921 else if (in_idx == in_count + 1)
6922 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6923 else
6924 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6926 if (!set[in_idx])
6927 set[in_idx] = ~0u;
6929 for (j = 0; j < output_signature->element_count; ++j)
6931 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6932 DWORD mask;
6934 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6935 || input->semantic_idx != output->semantic_idx
6936 || strcmp(input->semantic_name, output->semantic_name)
6937 || !(mask = input->mask & output->mask))
6938 continue;
6940 if (set[in_idx] == ~0u)
6941 set[in_idx] = 0;
6942 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6943 shader_glsl_write_mask_to_str(mask, reg_mask);
6945 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6946 destination->buffer, reg_mask, output->register_idx, reg_mask);
6950 for (i = 0; i < max_varyings; ++i)
6952 unsigned int size;
6954 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6955 continue;
6957 if (set[i] == ~0u)
6958 set[i] = 0;
6960 size = 0;
6961 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6962 reg_mask[size++] = 'x';
6963 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6964 reg_mask[size++] = 'y';
6965 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6966 reg_mask[size++] = 'z';
6967 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6968 reg_mask[size++] = 'w';
6969 reg_mask[size] = '\0';
6971 if (i == in_count)
6972 string_buffer_sprintf(destination, "gl_FrontColor");
6973 else if (i == in_count + 1)
6974 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6975 else
6976 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6978 if (size == 1)
6979 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6980 else
6981 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6984 heap_free(set);
6985 string_buffer_release(&priv->string_buffers, destination);
6988 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6989 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6990 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6991 BOOL rasterizer_setup)
6993 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6994 char reg_mask[6];
6995 unsigned int i;
6997 for (i = 0; i < output_signature->element_count; ++i)
6999 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7001 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7002 continue;
7004 if (output->stream_idx)
7005 continue;
7007 if (output->register_idx >= input_count)
7008 continue;
7010 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7012 shader_addline(buffer,
7013 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7014 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7018 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7019 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7021 unsigned int i, clip_or_cull_index;
7022 const char *name;
7023 char reg_mask[6];
7025 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7026 /* Assign consecutive indices starting from 0. */
7027 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7028 for (i = 0; i < 4; ++i)
7030 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7031 continue;
7033 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7034 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7035 name, clip_or_cull_index, element->register_idx, reg_mask);
7036 ++clip_or_cull_index;
7040 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7041 const struct wined3d_gl_info *gl_info, const DWORD *map,
7042 const struct wined3d_shader_signature *input_signature,
7043 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7044 const struct wined3d_shader_signature *output_signature,
7045 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7047 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7048 const char *semantic_name;
7049 unsigned int semantic_idx;
7050 char reg_mask[6];
7051 unsigned int i;
7053 /* First, sort out position and point size system values. */
7054 for (i = 0; i < output_signature->element_count; ++i)
7056 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7058 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7059 continue;
7061 if (output->stream_idx)
7062 continue;
7064 semantic_name = output->semantic_name;
7065 semantic_idx = output->semantic_idx;
7066 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7068 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7070 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7071 reg_mask, output->register_idx, reg_mask);
7073 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7075 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7076 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7078 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7080 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7081 output->register_idx, reg_mask);
7083 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7085 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7086 output->register_idx, reg_mask);
7088 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7090 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7092 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7094 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7096 else if (output->sysval_semantic)
7098 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7102 /* Then, setup the pixel shader input. */
7103 if (reg_maps_out->shader_version.major < 4)
7104 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7105 output_signature, reg_maps_out);
7106 else
7107 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7110 /* Context activation is done by the caller. */
7111 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7112 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7113 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7115 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7116 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7117 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7118 const char *semantic_name;
7119 UINT semantic_idx;
7120 char reg_mask[6];
7121 unsigned int i;
7122 GLuint ret;
7124 string_buffer_clear(buffer);
7126 shader_glsl_add_version_declaration(buffer, gl_info);
7128 if (per_vertex_point_size)
7130 shader_addline(buffer, "uniform struct\n{\n");
7131 shader_addline(buffer, " float size_min;\n");
7132 shader_addline(buffer, " float size_max;\n");
7133 shader_addline(buffer, "} ffp_point;\n");
7136 if (ps_major < 3)
7138 DWORD colors_written_mask[2] = {0};
7139 DWORD texcoords_written_mask[WINED3D_MAX_TEXTURES] = {0};
7141 if (!legacy_syntax)
7143 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7144 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7145 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7146 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7149 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7151 for (i = 0; i < vs->output_signature.element_count; ++i)
7153 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7154 DWORD write_mask;
7156 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7157 continue;
7159 semantic_name = output->semantic_name;
7160 semantic_idx = output->semantic_idx;
7161 write_mask = output->mask;
7162 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7164 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7166 if (legacy_syntax)
7167 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7168 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7169 else
7170 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7171 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7173 colors_written_mask[semantic_idx] = write_mask;
7175 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7177 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7178 reg_mask, output->register_idx, reg_mask);
7180 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7182 if (semantic_idx < WINED3D_MAX_TEXTURES)
7184 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7185 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7186 semantic_idx, reg_mask, output->register_idx, reg_mask);
7187 texcoords_written_mask[semantic_idx] = write_mask;
7190 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7192 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7193 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7195 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7197 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7198 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7199 output->register_idx, reg_mask[1]);
7203 for (i = 0; i < 2; ++i)
7205 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7207 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7208 if (!i)
7209 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7210 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7211 reg_mask, reg_mask);
7212 else
7213 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7214 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7215 reg_mask, reg_mask);
7218 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
7220 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7221 continue;
7223 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7225 if (!shader_glsl_full_ffp_varyings(gl_info) && !texcoords_written_mask[i])
7226 continue;
7228 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7229 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7230 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7234 else
7236 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7238 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7239 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7240 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7241 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7244 shader_addline(buffer, "}\n");
7246 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7247 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7248 shader_glsl_compile(gl_info, ret, buffer->buffer);
7250 return ret;
7253 static void shader_glsl_generate_stream_output_setup(struct wined3d_string_buffer *buffer,
7254 const struct wined3d_shader *shader)
7256 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
7257 unsigned int i, register_idx, component_idx;
7259 shader_addline(buffer, "out shader_in_out\n{\n");
7260 for (i = 0; i < so_desc->element_count; ++i)
7262 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7264 if (e->stream_idx)
7266 FIXME("Unhandled stream %u.\n", e->stream_idx);
7267 continue;
7269 if (!e->semantic_name)
7270 continue;
7271 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7272 continue;
7274 if (component_idx || e->component_count != 4)
7276 if (e->component_count == 1)
7277 shader_addline(buffer, "float");
7278 else
7279 shader_addline(buffer, "vec%u", e->component_count);
7281 shader_addline(buffer, " reg%u_%u_%u;\n",
7282 register_idx, component_idx, component_idx + e->component_count - 1);
7284 else
7286 shader_addline(buffer, "vec4 reg%u;\n", register_idx);
7289 shader_addline(buffer, "} shader_out;\n");
7291 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7292 shader->limits->packed_output);
7293 for (i = 0; i < so_desc->element_count; ++i)
7295 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7297 if (e->stream_idx)
7299 FIXME("Unhandled stream %u.\n", e->stream_idx);
7300 continue;
7302 if (!e->semantic_name)
7303 continue;
7304 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7305 continue;
7307 if (component_idx || e->component_count != 4)
7309 DWORD write_mask;
7310 char str_mask[6];
7312 write_mask = ((1u << e->component_count) - 1) << component_idx;
7313 shader_glsl_write_mask_to_str(write_mask, str_mask);
7314 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7315 register_idx, component_idx, component_idx + e->component_count - 1,
7316 register_idx, str_mask);
7318 else
7320 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n", register_idx, register_idx);
7323 shader_addline(buffer, "}\n");
7326 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7327 const struct wined3d_shader *shader, unsigned int input_count,
7328 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7330 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7331 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7333 if (rasterizer_setup)
7334 input_count = min(vec4_varyings(4, gl_info), input_count);
7336 if (input_count)
7337 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7339 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7340 prefix, shader->limits->packed_output);
7342 if (rasterizer_setup)
7343 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7344 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7345 else
7346 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7347 &shader->reg_maps, "shader_out", rasterizer_setup);
7349 shader_addline(buffer, "}\n");
7352 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7353 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7354 const char *reg_mask)
7356 if (!constant->sysval_semantic)
7358 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7359 return;
7362 switch (constant->sysval_semantic)
7364 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7365 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7366 case WINED3D_SV_TESS_FACTOR_LINEDET:
7367 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7368 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7369 break;
7370 case WINED3D_SV_TESS_FACTOR_QUADINT:
7371 case WINED3D_SV_TESS_FACTOR_TRIINT:
7372 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7373 break;
7374 default:
7375 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7376 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7380 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7381 const struct wined3d_shader_signature *signature, BOOL input_setup)
7383 unsigned int i, register_count, user_constant_index, user_constant_count;
7385 register_count = user_constant_count = 0;
7386 for (i = 0; i < signature->element_count; ++i)
7388 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7389 register_count = max(constant->register_idx + 1, register_count);
7390 if (!constant->sysval_semantic)
7391 ++user_constant_count;
7394 if (user_constant_count)
7395 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7396 input_setup ? "in" : "out", user_constant_count);
7397 if (input_setup)
7398 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7400 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7401 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7403 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7404 char reg_mask[6];
7406 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7408 if (input_setup)
7409 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7410 else
7411 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7413 shader_addline(buffer, " = ");
7415 if (input_setup)
7416 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7417 else
7418 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7420 shader_addline(buffer, ";\n");
7422 shader_addline(buffer, "}\n");
7425 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7426 const struct wined3d_gl_info *gl_info)
7428 const char *output = get_fragment_output(gl_info);
7430 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7431 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7432 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7433 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7434 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7435 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7438 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7439 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7441 const char *output = get_fragment_output(gl_info);
7443 switch (mode)
7445 case WINED3D_FFP_PS_FOG_OFF:
7446 return;
7448 case WINED3D_FFP_PS_FOG_LINEAR:
7449 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7450 break;
7452 case WINED3D_FFP_PS_FOG_EXP:
7453 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7454 break;
7456 case WINED3D_FFP_PS_FOG_EXP2:
7457 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7458 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7459 break;
7461 default:
7462 ERR("Invalid fog mode %#x.\n", mode);
7463 return;
7466 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7467 output, output);
7470 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7471 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7473 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7474 * flipping all the operators here, just negate the comparison below. */
7475 static const char * const comparison_operator[] =
7477 "", /* WINED3D_CMP_NEVER */
7478 "<", /* WINED3D_CMP_LESS */
7479 "==", /* WINED3D_CMP_EQUAL */
7480 "<=", /* WINED3D_CMP_LESSEQUAL */
7481 ">", /* WINED3D_CMP_GREATER */
7482 "!=", /* WINED3D_CMP_NOTEQUAL */
7483 ">=", /* WINED3D_CMP_GREATEREQUAL */
7484 "" /* WINED3D_CMP_ALWAYS */
7487 if (alpha_func == WINED3D_CMP_ALWAYS)
7488 return;
7490 if (alpha_func != WINED3D_CMP_NEVER)
7491 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7492 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7493 shader_addline(buffer, " discard;\n");
7496 static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *buffer,
7497 const char *colour, const char *colour_key_low, const char *colour_key_high)
7499 shader_addline(buffer, "if (all(greaterThanEqual(%s, %s)) && all(lessThan(%s, %s)))\n",
7500 colour, colour_key_low, colour, colour_key_high);
7501 shader_addline(buffer, " discard;\n");
7504 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7505 const struct wined3d_gl_info *gl_info)
7507 if (gl_info->supported[ARB_CULL_DISTANCE])
7508 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7509 if (gl_info->supported[ARB_GPU_SHADER5])
7510 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7511 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7512 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7513 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7514 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7515 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7516 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7517 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7518 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7519 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7520 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7521 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7522 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7523 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7524 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7525 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7526 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7527 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7528 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7529 if (gl_info->supported[ARB_TEXTURE_GATHER])
7530 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7531 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7532 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7533 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7534 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7535 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7536 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7537 if (gl_info->supported[EXT_GPU_SHADER4])
7538 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7539 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7540 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7541 if (gl_info->supported[EXT_TEXTURE_SHADOW_LOD])
7542 shader_addline(buffer, "#extension GL_EXT_texture_shadow_lod : enable\n");
7545 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7546 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7547 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7549 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7550 struct wined3d_string_buffer *src, *assignment;
7551 enum wined3d_data_type dst_data_type;
7552 const char *swizzle;
7553 unsigned int i;
7555 if (output_signature->element_count)
7557 src = string_buffer_get(string_buffers);
7558 assignment = string_buffer_get(string_buffers);
7559 for (i = 0; i < output_signature->element_count; ++i)
7561 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7563 /* register_idx is set to ~0u for non-color outputs. */
7564 if (output->register_idx == ~0u)
7565 continue;
7566 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7568 FIXME("Unhandled component type %#x.\n", output->component_type);
7569 continue;
7571 dst_data_type = component_type_info[output->component_type].data_type;
7572 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7573 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7574 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT, 4);
7575 swizzle = args->rt_alpha_swizzle & (1u << output->semantic_idx) ? ".argb" : "";
7576 shader_addline(buffer, "%s%s;\n", assignment->buffer, swizzle);
7578 string_buffer_release(string_buffers, src);
7579 string_buffer_release(string_buffers, assignment);
7581 else
7583 DWORD mask = shader->reg_maps.rt_mask;
7585 while (mask)
7587 i = wined3d_bit_scan(&mask);
7588 swizzle = args->rt_alpha_swizzle & (1u << i) ? ".argb" : "";
7589 shader_addline(buffer, "color_out%u = ps_out[%u]%s;\n", i, i, swizzle);
7594 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7595 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7596 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7598 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7600 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7601 if (reg_maps->shader_version.major < 2)
7602 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7604 if (args->srgb_correction)
7605 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7607 /* SM < 3 does not replace the fog stage. */
7608 if (reg_maps->shader_version.major < 3)
7609 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7611 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7613 if (reg_maps->sample_mask)
7614 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7616 if (!use_legacy_fragment_output(gl_info))
7617 shader_glsl_generate_color_output(buffer, gl_info, shader, args, string_buffers);
7620 /* Context activation is done by the caller. */
7621 static GLuint shader_glsl_generate_fragment_shader(const struct wined3d_context_gl *context_gl,
7622 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7623 const struct wined3d_shader *shader, const struct ps_compile_args *args,
7624 struct ps_np2fixup_info *np2fixup_info)
7626 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7627 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7628 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7629 const char *prefix = shader_glsl_get_prefix(version->type);
7630 BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7631 unsigned int i, extra_constants_needed = 0;
7632 struct shader_glsl_ctx_priv priv_ctx;
7633 GLuint shader_id;
7634 DWORD map;
7636 memset(&priv_ctx, 0, sizeof(priv_ctx));
7637 priv_ctx.gl_info = gl_info;
7638 priv_ctx.cur_ps_args = args;
7639 priv_ctx.cur_np2fixup_info = np2fixup_info;
7640 priv_ctx.string_buffers = string_buffers;
7642 shader_glsl_add_version_declaration(buffer, gl_info);
7644 shader_glsl_enable_extensions(buffer, gl_info);
7645 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7646 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7647 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7648 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7649 if (shader_glsl_use_explicit_attrib_location(gl_info))
7650 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7651 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7652 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7653 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7654 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7655 if (gl_info->supported[ARB_SAMPLE_SHADING])
7656 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7657 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7658 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7659 /* The spec says that it doesn't have to be explicitly enabled, but the
7660 * nvidia drivers write a warning if we don't do so. */
7661 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7662 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7664 /* Base Declarations */
7665 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7667 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7669 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7670 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7671 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7672 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7675 /* Declare uniforms for NP2 texcoord fixup:
7676 * This is NOT done inside the loop that declares the texture samplers
7677 * since the NP2 fixup code is currently only used for the GeforceFX
7678 * series and when forcing the ARB_npot extension off. Modern cards just
7679 * skip the code anyway, so put it inside a separate loop. */
7680 if (args->np2_fixup)
7682 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7683 unsigned int cur = 0;
7685 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7686 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7687 * samplerNP2Fixup stores texture dimensions and is updated through
7688 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7690 for (i = 0; i < shader->limits->sampler; ++i)
7692 enum wined3d_shader_resource_type resource_type;
7694 resource_type = pixelshader_get_resource_type(reg_maps, i, args->tex_types);
7696 if (!resource_type || !(args->np2_fixup & (1u << i)))
7697 continue;
7699 if (resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7701 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7702 continue;
7705 fixup->idx[i] = cur++;
7708 fixup->num_consts = (cur + 1) >> 1;
7709 fixup->active = args->np2_fixup;
7710 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7713 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7715 shader_addline(buffer, "uniform struct\n{\n");
7716 shader_addline(buffer, " vec4 color;\n");
7717 shader_addline(buffer, " float density;\n");
7718 shader_addline(buffer, " float end;\n");
7719 shader_addline(buffer, " float scale;\n");
7720 shader_addline(buffer, "} ffp_fog;\n");
7722 if (legacy_syntax)
7724 if (glsl_is_color_reg_read(shader, 0))
7725 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7726 if (glsl_is_color_reg_read(shader, 1))
7727 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7728 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7729 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7731 else
7733 if (glsl_is_color_reg_read(shader, 0))
7734 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7735 if (glsl_is_color_reg_read(shader, 1))
7736 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7737 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7738 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7739 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7743 if (version->major >= 3)
7745 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7747 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7748 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7749 shader->u.ps.interpolation_mode, version->major >= 4);
7750 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7753 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7755 if (!(map & 1))
7756 continue;
7758 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7760 if (reg_maps->luminanceparams & (1u << i))
7762 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7763 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7764 extra_constants_needed++;
7767 extra_constants_needed++;
7770 if (args->srgb_correction)
7772 shader_addline(buffer, "const vec4 srgb_const0 = ");
7773 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
7774 shader_addline(buffer, ";\n");
7775 shader_addline(buffer, "const vec4 srgb_const1 = ");
7776 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
7777 shader_addline(buffer, ";\n");
7779 if ((reg_maps->usesdsy && wined3d_settings.offscreen_rendering_mode != ORM_FBO)
7780 || (reg_maps->vpos && !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]))
7782 ++extra_constants_needed;
7783 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7785 if (reg_maps->vpos)
7787 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7789 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7790 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7791 args->y_correction ? "origin_upper_left, " : "");
7792 else if (args->y_correction)
7793 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7795 shader_addline(buffer, "vec4 vpos;\n");
7798 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7799 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7801 if (!use_legacy_fragment_output(gl_info))
7803 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7805 if (args->dual_source_blend)
7806 shader_addline(buffer, "vec4 ps_out[2];\n");
7807 else
7808 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7809 if (output_signature->element_count)
7811 for (i = 0; i < output_signature->element_count; ++i)
7813 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7815 if (output->register_idx == ~0u)
7816 continue;
7817 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7819 FIXME("Unhandled component type %#x.\n", output->component_type);
7820 continue;
7822 if (shader_glsl_use_explicit_attrib_location(gl_info))
7824 if (args->dual_source_blend)
7825 shader_addline(buffer, "layout(location = 0, index = %u) ", output->semantic_idx);
7826 else
7827 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7829 shader_addline(buffer, "out %s4 color_out%u;\n",
7830 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7833 else
7835 DWORD mask = reg_maps->rt_mask;
7837 while (mask)
7839 i = wined3d_bit_scan(&mask);
7840 if (shader_glsl_use_explicit_attrib_location(gl_info))
7842 if (args->dual_source_blend)
7843 shader_addline(buffer, "layout(location = 0, index = %u) ", i);
7844 else
7845 shader_addline(buffer, "layout(location = %u) ", i);
7847 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7852 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7853 FIXME("Insufficient uniforms to run this shader.\n");
7855 if (shader->u.ps.force_early_depth_stencil)
7856 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7858 shader_addline(buffer, "void main()\n{\n");
7860 if (reg_maps->sample_mask)
7861 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7863 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7864 * add approximately 0.5. This causes off-by-one problems as spotted by
7865 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7866 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7867 * causes precision troubles when we just subtract 0.5.
7869 * To deal with that, just floor() the position. This will eliminate the
7870 * fraction on all cards.
7872 * TODO: Test how this behaves with multisampling.
7874 * An advantage of floor is that it works even if the driver doesn't add
7875 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7876 * to return in gl_FragCoord, even though coordinates specify the pixel
7877 * centers instead of the pixel corners. This code will behave correctly
7878 * on drivers that returns integer values. */
7879 if (reg_maps->vpos)
7881 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7882 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7883 else if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7884 shader_addline(buffer,
7885 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7886 else
7887 shader_addline(buffer,
7888 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7891 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7893 unsigned int i;
7894 WORD map = reg_maps->texcoord;
7896 if (legacy_syntax)
7898 if (glsl_is_color_reg_read(shader, 0))
7899 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7900 if (glsl_is_color_reg_read(shader, 1))
7901 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7904 for (i = 0; map; map >>= 1, ++i)
7906 if (map & 1)
7908 if (args->pointsprite)
7909 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7910 else if (args->texcoords_initialized & (1u << i))
7911 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7912 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7913 else
7914 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7915 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7919 if (legacy_syntax)
7920 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7923 /* Pack 3.0 inputs */
7924 if (reg_maps->shader_version.major >= 3)
7925 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7926 reg_maps->shader_version.major >= 4);
7928 /* Base Shader Body */
7929 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7930 return 0;
7932 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7933 if (reg_maps->shader_version.major < 4)
7934 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
7936 shader_addline(buffer, "}\n");
7938 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7939 TRACE("Compiling shader object %u.\n", shader_id);
7940 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7942 return shader_id;
7945 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7946 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7947 const struct vs_compile_args *args)
7949 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7950 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7951 unsigned int i;
7953 /* Unpack outputs. */
7954 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7956 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7957 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7958 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7959 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7961 if (reg_maps->shader_version.major < 3)
7963 if (args->fog_src == VS_FOG_Z)
7964 shader_addline(buffer, "%s = gl_Position.z;\n",
7965 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7966 else if (!reg_maps->fog)
7967 shader_addline(buffer, "%s = 0.0;\n",
7968 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7971 /* We always store the clipplanes without y inversion. */
7972 if (args->clip_enabled)
7974 if (legacy_syntax)
7975 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7976 else
7977 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7978 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7981 if (args->point_size && !args->per_vertex_point_size)
7982 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7984 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7985 shader_glsl_fixup_position(buffer, FALSE);
7988 /* Context activation is done by the caller. */
7989 static GLuint shader_glsl_generate_vertex_shader(const struct wined3d_context_gl *context_gl,
7990 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7992 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7993 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7994 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7995 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7996 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7997 struct shader_glsl_ctx_priv priv_ctx;
7998 GLuint shader_id;
7999 unsigned int i;
8001 memset(&priv_ctx, 0, sizeof(priv_ctx));
8002 priv_ctx.gl_info = gl_info;
8003 priv_ctx.cur_vs_args = args;
8004 priv_ctx.string_buffers = string_buffers;
8006 shader_glsl_add_version_declaration(buffer, gl_info);
8008 shader_glsl_enable_extensions(buffer, gl_info);
8009 if (gl_info->supported[ARB_DRAW_INSTANCED])
8010 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8011 if (shader_glsl_use_explicit_attrib_location(gl_info))
8012 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8013 if (gl_info->supported[ARB_SHADER_VIEWPORT_LAYER_ARRAY])
8014 shader_addline(buffer, "#extension GL_ARB_shader_viewport_layer_array : enable\n");
8016 /* Base Declarations */
8017 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8019 for (i = 0; i < shader->input_signature.element_count; ++i)
8020 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8022 if (args->point_size && !args->per_vertex_point_size)
8024 shader_addline(buffer, "uniform struct\n{\n");
8025 shader_addline(buffer, " float size;\n");
8026 shader_addline(buffer, " float size_min;\n");
8027 shader_addline(buffer, " float size_max;\n");
8028 shader_addline(buffer, "} ffp_point;\n");
8031 if (!needs_legacy_glsl_syntax(gl_info))
8033 if (args->clip_enabled)
8034 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8036 if (version->major < 3)
8038 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8039 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8040 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
8041 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8045 if (version->major < 4)
8046 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8048 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8049 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8051 if (reg_maps->shader_version.major >= 4)
8052 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8053 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8055 shader_addline(buffer, "void main()\n{\n");
8057 if (reg_maps->input_rel_addressing)
8059 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8060 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8061 for (i = 0; i < shader->input_signature.element_count; ++i)
8063 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8064 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8068 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8069 return 0;
8071 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8072 if (reg_maps->shader_version.major < 4)
8073 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8075 shader_addline(buffer, "}\n");
8077 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8078 TRACE("Compiling shader object %u.\n", shader_id);
8079 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8081 return shader_id;
8084 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8085 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8087 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8088 char reg_mask[6];
8089 unsigned int i;
8091 for (i = 0; i < output_signature->element_count; ++i)
8093 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8095 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8096 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8097 output->register_idx, reg_mask, output->register_idx, reg_mask);
8101 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8102 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8103 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8104 const char *phase_name, unsigned phase_idx)
8106 unsigned int i;
8107 HRESULT hr;
8109 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8110 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8111 for (i = 0; i < phase->temporary_count; ++i)
8112 shader_addline(buffer, "vec4 R%u;\n", i);
8113 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8114 shader_addline(buffer, "}\n");
8115 return hr;
8118 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8119 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8121 if (phase->instance_count)
8123 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8124 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8125 shader_addline(buffer, "}\n");
8127 else
8129 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8133 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context_gl *context_gl,
8134 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8136 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8137 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8138 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8139 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8140 const struct wined3d_hull_shader *hs = &shader->u.hs;
8141 const struct wined3d_shader_phase *phase;
8142 struct shader_glsl_ctx_priv priv_ctx;
8143 GLuint shader_id;
8144 unsigned int i;
8146 memset(&priv_ctx, 0, sizeof(priv_ctx));
8147 priv_ctx.gl_info = gl_info;
8148 priv_ctx.string_buffers = string_buffers;
8150 shader_glsl_add_version_declaration(buffer, gl_info);
8152 shader_glsl_enable_extensions(buffer, gl_info);
8153 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8155 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8157 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8159 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8160 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8162 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8164 if (hs->phases.control_point)
8166 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8167 shader->limits->packed_output);
8168 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8169 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8170 shader_addline(buffer, "}\n");
8173 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8174 if ((phase = hs->phases.control_point))
8176 for (i = 0; i < phase->temporary_count; ++i)
8177 shader_addline(buffer, "vec4 R%u;\n", i);
8178 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8179 return 0;
8180 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8182 else
8184 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8186 shader_addline(buffer, "}\n");
8188 for (i = 0; i < hs->phases.fork_count; ++i)
8190 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8191 &hs->phases.fork[i], "fork", i)))
8192 return 0;
8195 for (i = 0; i < hs->phases.join_count; ++i)
8197 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8198 &hs->phases.join[i], "join", i)))
8199 return 0;
8202 shader_addline(buffer, "void main()\n{\n");
8203 shader_addline(buffer, "hs_control_point_phase();\n");
8204 if (reg_maps->vocp)
8205 shader_addline(buffer, "barrier();\n");
8206 for (i = 0; i < hs->phases.fork_count; ++i)
8207 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8208 for (i = 0; i < hs->phases.join_count; ++i)
8209 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8210 shader_addline(buffer, "setup_patch_constant_output();\n");
8211 shader_addline(buffer, "}\n");
8213 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8214 TRACE("Compiling shader object %u.\n", shader_id);
8215 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8217 return shader_id;
8220 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8221 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8222 const struct ds_compile_args *args)
8224 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8226 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8227 shader_glsl_fixup_position(buffer, FALSE);
8230 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context_gl *context_gl,
8231 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8233 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8234 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8235 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8236 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8237 struct shader_glsl_ctx_priv priv_ctx;
8238 GLuint shader_id;
8240 memset(&priv_ctx, 0, sizeof(priv_ctx));
8241 priv_ctx.gl_info = gl_info;
8242 priv_ctx.cur_ds_args = args;
8243 priv_ctx.string_buffers = string_buffers;
8245 shader_glsl_add_version_declaration(buffer, gl_info);
8247 shader_glsl_enable_extensions(buffer, gl_info);
8248 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8250 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8252 shader_addline(buffer, "layout(");
8253 switch (shader->u.ds.tessellator_domain)
8255 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8256 shader_addline(buffer, "isolines");
8257 break;
8258 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8259 shader_addline(buffer, "quads");
8260 break;
8261 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8262 shader_addline(buffer, "triangles");
8263 break;
8265 switch (args->tessellator_output_primitive)
8267 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8268 if (args->render_offscreen)
8269 shader_addline(buffer, ", ccw");
8270 else
8271 shader_addline(buffer, ", cw");
8272 break;
8273 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8274 if (args->render_offscreen)
8275 shader_addline(buffer, ", cw");
8276 else
8277 shader_addline(buffer, ", ccw");
8278 break;
8279 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8280 shader_addline(buffer, ", point_mode");
8281 break;
8282 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8283 break;
8285 switch (args->tessellator_partitioning)
8287 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8288 shader_addline(buffer, ", fractional_odd_spacing");
8289 break;
8290 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8291 shader_addline(buffer, ", fractional_even_spacing");
8292 break;
8293 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8294 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8295 shader_addline(buffer, ", equal_spacing");
8296 break;
8298 shader_addline(buffer, ") in;\n");
8300 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8302 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8303 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8305 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8306 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8307 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8309 shader_addline(buffer, "void main()\n{\n");
8310 shader_addline(buffer, "setup_patch_constant_input();\n");
8312 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8313 return 0;
8315 shader_addline(buffer, "}\n");
8317 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8318 TRACE("Compiling shader object %u.\n", shader_id);
8319 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8321 return shader_id;
8324 /* Context activation is done by the caller. */
8325 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context_gl *context_gl,
8326 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8328 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8329 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8330 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8331 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8332 const struct wined3d_shader_signature_element *output;
8333 enum wined3d_primitive_type primitive_type;
8334 struct shader_glsl_ctx_priv priv_ctx;
8335 unsigned int max_vertices;
8336 unsigned int i, j;
8337 GLuint shader_id;
8339 memset(&priv_ctx, 0, sizeof(priv_ctx));
8340 priv_ctx.gl_info = gl_info;
8341 priv_ctx.string_buffers = string_buffers;
8343 shader_glsl_add_version_declaration(buffer, gl_info);
8345 shader_glsl_enable_extensions(buffer, gl_info);
8347 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8349 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8350 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8351 if (shader->u.gs.instance_count > 1)
8352 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8353 shader_addline(buffer, ") in;\n");
8355 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8356 if (!(max_vertices = shader->u.gs.vertices_out))
8358 switch (args->primitive_type)
8360 case WINED3D_PT_POINTLIST:
8361 max_vertices = 1;
8362 break;
8363 case WINED3D_PT_LINELIST:
8364 max_vertices = 2;
8365 break;
8366 case WINED3D_PT_TRIANGLELIST:
8367 max_vertices = 3;
8368 break;
8369 default:
8370 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8371 break;
8374 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8375 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8376 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8378 if (!gl_info->supported[ARB_CLIP_CONTROL])
8380 shader_addline(buffer, "uniform vec4 pos_fixup");
8381 if (reg_maps->viewport_array)
8382 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8383 shader_addline(buffer, ";\n");
8386 if (is_rasterization_disabled(shader))
8388 shader_glsl_generate_stream_output_setup(buffer, shader);
8390 else
8392 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8393 gl_info, TRUE, args->interpolation_mode);
8396 shader_addline(buffer, "void main()\n{\n");
8397 if (shader->function)
8399 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8400 return 0;
8402 else
8404 for (i = 0; i < max_vertices; ++i)
8406 for (j = 0; j < shader->output_signature.element_count; ++j)
8408 output = &shader->output_signature.elements[j];
8409 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8410 output->register_idx, i, output->register_idx);
8412 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8413 if (!gl_info->supported[ARB_CLIP_CONTROL])
8414 shader_glsl_fixup_position(buffer, FALSE);
8415 shader_addline(buffer, "EmitVertex();\n");
8418 shader_addline(buffer, "}\n");
8420 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8421 TRACE("Compiling shader object %u.\n", shader_id);
8422 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8424 return shader_id;
8427 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8429 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8430 const struct wined3d_gl_info *gl_info = priv->gl_info;
8431 struct wined3d_string_buffer *buffer = ctx->buffer;
8432 const struct wined3d_shader *shader = ctx->shader;
8434 switch (shader->reg_maps.shader_version.type)
8436 case WINED3D_SHADER_TYPE_PIXEL:
8437 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8438 break;
8439 case WINED3D_SHADER_TYPE_VERTEX:
8440 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8441 break;
8442 case WINED3D_SHADER_TYPE_DOMAIN:
8443 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8444 break;
8445 case WINED3D_SHADER_TYPE_GEOMETRY:
8446 case WINED3D_SHADER_TYPE_COMPUTE:
8447 break;
8448 default:
8449 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8450 break;
8454 /* Context activation is done by the caller. */
8455 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context_gl *context_gl,
8456 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8457 const struct wined3d_shader *shader)
8459 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8460 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8461 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8462 struct shader_glsl_ctx_priv priv_ctx;
8463 GLuint shader_id;
8464 unsigned int i;
8466 memset(&priv_ctx, 0, sizeof(priv_ctx));
8467 priv_ctx.gl_info = gl_info;
8468 priv_ctx.string_buffers = string_buffers;
8470 shader_glsl_add_version_declaration(buffer, gl_info);
8472 shader_glsl_enable_extensions(buffer, gl_info);
8473 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8475 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8477 for (i = 0; i < reg_maps->tgsm_count; ++i)
8479 if (reg_maps->tgsm[i].size)
8480 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8483 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8484 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8486 shader_addline(buffer, "void main()\n{\n");
8487 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8488 shader_addline(buffer, "}\n");
8490 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8491 TRACE("Compiling shader object %u.\n", shader_id);
8492 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8494 return shader_id;
8497 static GLuint find_glsl_fragment_shader(const struct wined3d_context_gl *context_gl,
8498 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8499 struct wined3d_shader *shader,
8500 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8502 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8503 struct glsl_shader_private *shader_data;
8504 struct ps_np2fixup_info *np2fixup;
8505 UINT i;
8506 DWORD new_size;
8507 GLuint ret;
8509 if (!shader->backend_data)
8511 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8513 ERR("Failed to allocate backend data.\n");
8514 return 0;
8517 shader_data = shader->backend_data;
8518 gl_shaders = shader_data->gl_shaders.ps;
8520 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8521 * so a linear search is more performant than a hashmap or a binary search
8522 * (cache coherency etc)
8524 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8526 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8528 if (args->np2_fixup)
8529 *np2fixup_info = &gl_shaders[i].np2fixup;
8530 return gl_shaders[i].id;
8534 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8535 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8537 if (shader_data->num_gl_shaders)
8539 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8540 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8542 else
8544 new_array = heap_alloc(sizeof(*gl_shaders));
8545 new_size = 1;
8548 if(!new_array) {
8549 ERR("Out of memory\n");
8550 return 0;
8552 shader_data->gl_shaders.ps = new_array;
8553 shader_data->shader_array_size = new_size;
8554 gl_shaders = new_array;
8557 gl_shaders[shader_data->num_gl_shaders].args = *args;
8559 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8560 memset(np2fixup, 0, sizeof(*np2fixup));
8561 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8563 string_buffer_clear(buffer);
8564 ret = shader_glsl_generate_fragment_shader(context_gl, buffer, string_buffers, shader, args, np2fixup);
8565 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8567 return ret;
8570 static BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8571 const DWORD use_map)
8573 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8574 return FALSE;
8575 if ((stored->clip_enabled) != new->clip_enabled)
8576 return FALSE;
8577 if (stored->point_size != new->point_size)
8578 return FALSE;
8579 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8580 return FALSE;
8581 if (stored->flatshading != new->flatshading)
8582 return FALSE;
8583 if (stored->next_shader_type != new->next_shader_type)
8584 return FALSE;
8585 if (stored->next_shader_input_count != new->next_shader_input_count)
8586 return FALSE;
8587 if (stored->fog_src != new->fog_src)
8588 return FALSE;
8589 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8592 static GLuint find_glsl_vertex_shader(const struct wined3d_context_gl *context_gl,
8593 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct vs_compile_args *args)
8595 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8596 uint32_t use_map = context_gl->c.stream_info.use_map;
8597 struct glsl_shader_private *shader_data;
8598 unsigned int i, new_size;
8599 GLuint ret;
8601 if (!shader->backend_data)
8603 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8605 ERR("Failed to allocate backend data.\n");
8606 return 0;
8609 shader_data = shader->backend_data;
8610 gl_shaders = shader_data->gl_shaders.vs;
8612 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8613 * so a linear search is more performant than a hashmap or a binary search
8614 * (cache coherency etc)
8616 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8618 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8619 return gl_shaders[i].id;
8622 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8624 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8626 if (shader_data->num_gl_shaders)
8628 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8629 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8631 else
8633 new_array = heap_alloc(sizeof(*gl_shaders));
8634 new_size = 1;
8637 if(!new_array) {
8638 ERR("Out of memory\n");
8639 return 0;
8641 shader_data->gl_shaders.vs = new_array;
8642 shader_data->shader_array_size = new_size;
8643 gl_shaders = new_array;
8646 gl_shaders[shader_data->num_gl_shaders].args = *args;
8648 string_buffer_clear(&priv->shader_buffer);
8649 ret = shader_glsl_generate_vertex_shader(context_gl, priv, shader, args);
8650 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8652 return ret;
8655 static GLuint find_glsl_hull_shader(const struct wined3d_context_gl *context_gl,
8656 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8658 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8659 struct glsl_shader_private *shader_data;
8660 unsigned int new_size;
8661 GLuint ret;
8663 if (!shader->backend_data)
8665 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8667 ERR("Failed to allocate backend data.\n");
8668 return 0;
8671 shader_data = shader->backend_data;
8672 gl_shaders = shader_data->gl_shaders.hs;
8674 if (shader_data->num_gl_shaders > 0)
8676 assert(shader_data->num_gl_shaders == 1);
8677 return gl_shaders[0].id;
8680 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8682 assert(!shader_data->gl_shaders.hs);
8683 new_size = 1;
8684 if (!(new_array = heap_alloc(sizeof(*new_array))))
8686 ERR("Failed to allocate GL shaders array.\n");
8687 return 0;
8689 shader_data->gl_shaders.hs = new_array;
8690 shader_data->shader_array_size = new_size;
8691 gl_shaders = new_array;
8693 string_buffer_clear(&priv->shader_buffer);
8694 ret = shader_glsl_generate_hull_shader(context_gl, priv, shader);
8695 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8697 return ret;
8700 static GLuint find_glsl_domain_shader(const struct wined3d_context_gl *context_gl,
8701 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8703 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8704 struct glsl_shader_private *shader_data;
8705 unsigned int i, new_size;
8706 GLuint ret;
8708 if (!shader->backend_data)
8710 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8712 ERR("Failed to allocate backend data.\n");
8713 return 0;
8716 shader_data = shader->backend_data;
8717 gl_shaders = shader_data->gl_shaders.ds;
8719 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8721 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8722 return gl_shaders[i].id;
8725 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8727 if (shader_data->num_gl_shaders)
8729 new_size = shader_data->shader_array_size + 1;
8730 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8732 else
8734 new_array = heap_alloc(sizeof(*new_array));
8735 new_size = 1;
8738 if (!new_array)
8740 ERR("Failed to allocate GL shaders array.\n");
8741 return 0;
8743 shader_data->gl_shaders.ds = new_array;
8744 shader_data->shader_array_size = new_size;
8745 gl_shaders = new_array;
8747 string_buffer_clear(&priv->shader_buffer);
8748 ret = shader_glsl_generate_domain_shader(context_gl, priv, shader, args);
8749 gl_shaders[shader_data->num_gl_shaders].args = *args;
8750 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8752 return ret;
8755 static GLuint find_glsl_geometry_shader(const struct wined3d_context_gl *context_gl,
8756 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8758 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8759 struct glsl_shader_private *shader_data;
8760 unsigned int i, new_size;
8761 GLuint ret;
8763 if (!shader->backend_data)
8765 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8767 ERR("Failed to allocate backend data.\n");
8768 return 0;
8771 shader_data = shader->backend_data;
8772 gl_shaders = shader_data->gl_shaders.gs;
8774 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8776 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8777 return gl_shaders[i].id;
8780 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8782 if (shader_data->num_gl_shaders)
8784 new_size = shader_data->shader_array_size + 1;
8785 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8787 else
8789 new_array = heap_alloc(sizeof(*new_array));
8790 new_size = 1;
8793 if (!new_array)
8795 ERR("Failed to allocate GL shaders array.\n");
8796 return 0;
8798 shader_data->gl_shaders.gs = new_array;
8799 shader_data->shader_array_size = new_size;
8800 gl_shaders = new_array;
8802 string_buffer_clear(&priv->shader_buffer);
8803 ret = shader_glsl_generate_geometry_shader(context_gl, priv, shader, args);
8804 gl_shaders[shader_data->num_gl_shaders].args = *args;
8805 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8807 return ret;
8810 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8812 switch (mcs)
8814 case WINED3D_MCS_MATERIAL:
8815 return material;
8816 case WINED3D_MCS_COLOR1:
8817 return "ffp_attrib_diffuse";
8818 case WINED3D_MCS_COLOR2:
8819 return "ffp_attrib_specular";
8820 default:
8821 ERR("Invalid material color source %#x.\n", mcs);
8822 return "<invalid>";
8826 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8827 const struct wined3d_ffp_vs_settings *settings, unsigned int idx, BOOL legacy_lighting)
8829 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8830 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8831 if (settings->localviewer)
8832 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8833 else
8834 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8835 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0%s) specular +="
8836 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n",
8837 legacy_lighting ? " && ffp_material.shininess > 0.0" : "", idx);
8840 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8841 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8843 const char *diffuse, *specular, *emissive, *ambient;
8844 unsigned int i, idx;
8846 if (!settings->lighting)
8848 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8849 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8850 return;
8853 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8854 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8855 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8856 shader_addline(buffer, "vec3 dir, dst;\n");
8857 shader_addline(buffer, "float att, t;\n");
8859 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8860 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8861 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8862 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8864 idx = 0;
8865 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8867 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8868 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8869 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8870 shader_addline(buffer, "dst.x = 1.0;\n");
8871 if (legacy_lighting)
8873 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8874 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8875 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8877 else
8879 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8881 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8882 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8883 if (!legacy_lighting)
8884 shader_addline(buffer, "att = 1.0 / att;\n");
8885 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8886 if (!settings->normal)
8888 shader_addline(buffer, "}\n");
8889 continue;
8891 shader_addline(buffer, "dir = normalize(dir);\n");
8892 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8893 shader_addline(buffer, "}\n");
8896 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8898 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8899 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8900 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8901 shader_addline(buffer, "dst.x = 1.0;\n");
8902 if (legacy_lighting)
8904 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8905 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8906 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8908 else
8910 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8912 shader_addline(buffer, "dir = normalize(dir);\n");
8913 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8914 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8915 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8916 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8917 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8918 idx, idx, idx, idx);
8919 if (legacy_lighting)
8920 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8921 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8922 idx, idx, idx);
8923 else
8924 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8925 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8926 idx, idx, idx);
8927 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8928 if (!settings->normal)
8930 shader_addline(buffer, "}\n");
8931 continue;
8933 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8934 shader_addline(buffer, "}\n");
8937 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8939 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8940 if (!settings->normal)
8941 continue;
8942 shader_addline(buffer, "att = 1.0;\n");
8943 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8944 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8947 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8949 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8950 if (!settings->normal)
8951 continue;
8952 shader_addline(buffer, "att = 1.0;\n");
8953 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8954 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8957 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8958 ambient, diffuse, emissive);
8959 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8960 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8963 /* Context activation is done by the caller. */
8964 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8965 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8967 static const struct attrib_info
8969 const char type[6];
8970 const char name[24];
8972 attrib_info[] =
8974 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8975 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8976 /* TODO: Indexed vertex blending */
8977 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8978 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8979 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8980 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8981 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8983 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8984 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8985 BOOL output_legacy_fogcoord = legacy_syntax;
8986 BOOL legacy_lighting = priv->legacy_lighting;
8987 GLuint shader_obj;
8988 unsigned int i;
8990 string_buffer_clear(buffer);
8992 shader_glsl_add_version_declaration(buffer, gl_info);
8994 if (shader_glsl_use_explicit_attrib_location(gl_info))
8995 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8997 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8999 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
9001 if (shader_glsl_use_explicit_attrib_location(gl_info))
9002 shader_addline(buffer, "layout(location = %u) ", i);
9003 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
9005 shader_addline(buffer, "\n");
9007 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9008 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9009 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9010 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", WINED3D_MAX_TEXTURES);
9012 shader_addline(buffer, "uniform struct\n{\n");
9013 shader_addline(buffer, " vec4 emissive;\n");
9014 shader_addline(buffer, " vec4 ambient;\n");
9015 shader_addline(buffer, " vec4 diffuse;\n");
9016 shader_addline(buffer, " vec4 specular;\n");
9017 shader_addline(buffer, " float shininess;\n");
9018 shader_addline(buffer, "} ffp_material;\n");
9020 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9021 shader_addline(buffer, "uniform struct\n{\n");
9022 shader_addline(buffer, " vec4 diffuse;\n");
9023 shader_addline(buffer, " vec4 specular;\n");
9024 shader_addline(buffer, " vec4 ambient;\n");
9025 shader_addline(buffer, " vec4 position;\n");
9026 shader_addline(buffer, " vec3 direction;\n");
9027 shader_addline(buffer, " float range;\n");
9028 shader_addline(buffer, " float falloff;\n");
9029 shader_addline(buffer, " float c_att;\n");
9030 shader_addline(buffer, " float l_att;\n");
9031 shader_addline(buffer, " float q_att;\n");
9032 shader_addline(buffer, " float cos_htheta;\n");
9033 shader_addline(buffer, " float cos_hphi;\n");
9034 shader_addline(buffer, "} ffp_light[%u];\n", WINED3D_MAX_ACTIVE_LIGHTS);
9036 if (settings->point_size)
9038 shader_addline(buffer, "uniform struct\n{\n");
9039 shader_addline(buffer, " float size;\n");
9040 shader_addline(buffer, " float size_min;\n");
9041 shader_addline(buffer, " float size_max;\n");
9042 shader_addline(buffer, " float c_att;\n");
9043 shader_addline(buffer, " float l_att;\n");
9044 shader_addline(buffer, " float q_att;\n");
9045 shader_addline(buffer, "} ffp_point;\n");
9048 if (legacy_syntax)
9050 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9051 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9052 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9053 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9055 else
9057 if (settings->clipping)
9058 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9060 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9061 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9062 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9063 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9066 shader_addline(buffer, "\nvoid main()\n{\n");
9067 shader_addline(buffer, "float m;\n");
9068 shader_addline(buffer, "vec3 r;\n");
9070 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9072 if (attrib_info[i].name[0])
9073 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9074 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9076 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9078 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9079 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9080 && settings->texcoords & (1u << i))
9081 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9084 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9086 if (settings->transformed)
9088 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9089 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9090 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9092 else
9094 for (i = 0; i < settings->vertexblends; ++i)
9095 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9097 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9098 for (i = 0; i < settings->vertexblends + 1; ++i)
9099 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9101 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9102 if (settings->clipping)
9104 if (legacy_syntax)
9105 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9106 else
9107 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9108 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9110 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9113 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9114 if (settings->normal)
9116 if (!settings->vertexblends)
9118 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9120 else
9122 for (i = 0; i < settings->vertexblends + 1; ++i)
9123 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9126 if (settings->normalize)
9127 shader_addline(buffer, "normal = normalize(normal);\n");
9130 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9131 if (legacy_syntax)
9133 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9134 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9136 else
9138 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9139 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9142 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9144 BOOL output_legacy_texcoord = legacy_syntax;
9146 switch (settings->texgen[i] & 0xffff0000)
9148 case WINED3DTSS_TCI_PASSTHRU:
9149 if (settings->texcoords & (1u << i))
9150 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9151 i, i, i);
9152 else if (shader_glsl_full_ffp_varyings(gl_info))
9153 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9154 else
9155 output_legacy_texcoord = FALSE;
9156 break;
9158 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9159 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9160 break;
9162 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9163 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9164 break;
9166 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9167 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9168 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9169 break;
9171 case WINED3DTSS_TCI_SPHEREMAP:
9172 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9173 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9174 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9175 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9176 break;
9178 default:
9179 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9180 break;
9182 if (output_legacy_texcoord)
9183 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9186 switch (settings->fog_mode)
9188 case WINED3D_FFP_VS_FOG_OFF:
9189 output_legacy_fogcoord = FALSE;
9190 break;
9192 case WINED3D_FFP_VS_FOG_FOGCOORD:
9193 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9194 break;
9196 case WINED3D_FFP_VS_FOG_RANGE:
9197 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9198 break;
9200 case WINED3D_FFP_VS_FOG_DEPTH:
9201 if (settings->ortho_fog)
9203 if (gl_info->supported[ARB_CLIP_CONTROL])
9204 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9205 else
9206 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9207 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9209 else if (settings->transformed)
9211 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9213 else
9215 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9217 break;
9219 default:
9220 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9221 break;
9223 if (output_legacy_fogcoord)
9224 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9226 if (settings->point_size)
9228 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9229 " + ffp_point.l_att * length(ec_pos.xyz)"
9230 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9231 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9232 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9235 shader_addline(buffer, "}\n");
9237 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9238 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9240 return shader_obj;
9243 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9244 DWORD argnum, unsigned int stage, DWORD arg)
9246 const char *ret;
9248 if (arg == ARG_UNUSED)
9249 return "<unused arg>";
9251 switch (arg & WINED3DTA_SELECTMASK)
9253 case WINED3DTA_DIFFUSE:
9254 ret = "ffp_varying_diffuse";
9255 break;
9257 case WINED3DTA_CURRENT:
9258 ret = "ret";
9259 break;
9261 case WINED3DTA_TEXTURE:
9262 switch (stage)
9264 case 0: ret = "tex0"; break;
9265 case 1: ret = "tex1"; break;
9266 case 2: ret = "tex2"; break;
9267 case 3: ret = "tex3"; break;
9268 case 4: ret = "tex4"; break;
9269 case 5: ret = "tex5"; break;
9270 case 6: ret = "tex6"; break;
9271 case 7: ret = "tex7"; break;
9272 default:
9273 ret = "<invalid texture>";
9274 break;
9276 break;
9278 case WINED3DTA_TFACTOR:
9279 ret = "tex_factor";
9280 break;
9282 case WINED3DTA_SPECULAR:
9283 ret = "ffp_varying_specular";
9284 break;
9286 case WINED3DTA_TEMP:
9287 ret = "temp_reg";
9288 break;
9290 case WINED3DTA_CONSTANT:
9291 switch (stage)
9293 case 0: ret = "tss_const0"; break;
9294 case 1: ret = "tss_const1"; break;
9295 case 2: ret = "tss_const2"; break;
9296 case 3: ret = "tss_const3"; break;
9297 case 4: ret = "tss_const4"; break;
9298 case 5: ret = "tss_const5"; break;
9299 case 6: ret = "tss_const6"; break;
9300 case 7: ret = "tss_const7"; break;
9301 default:
9302 ret = "<invalid constant>";
9303 break;
9305 break;
9307 default:
9308 return "<unhandled arg>";
9311 if (arg & WINED3DTA_COMPLEMENT)
9313 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9314 if (argnum == 0)
9315 ret = "arg0";
9316 else if (argnum == 1)
9317 ret = "arg1";
9318 else if (argnum == 2)
9319 ret = "arg2";
9322 if (arg & WINED3DTA_ALPHAREPLICATE)
9324 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9325 if (argnum == 0)
9326 ret = "arg0";
9327 else if (argnum == 1)
9328 ret = "arg1";
9329 else if (argnum == 2)
9330 ret = "arg2";
9333 return ret;
9336 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9337 BOOL alpha, BOOL tmp_dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9339 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9341 if (color && alpha)
9342 dstmask = "";
9343 else if (color)
9344 dstmask = ".xyz";
9345 else
9346 dstmask = ".w";
9348 dstreg = tmp_dst ? "temp_reg" : "ret";
9350 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9351 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9352 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9354 switch (op)
9356 case WINED3D_TOP_DISABLE:
9357 break;
9359 case WINED3D_TOP_SELECT_ARG1:
9360 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9361 break;
9363 case WINED3D_TOP_SELECT_ARG2:
9364 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9365 break;
9367 case WINED3D_TOP_MODULATE:
9368 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9369 break;
9371 case WINED3D_TOP_MODULATE_4X:
9372 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9373 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9374 break;
9376 case WINED3D_TOP_MODULATE_2X:
9377 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9378 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9379 break;
9381 case WINED3D_TOP_ADD:
9382 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9383 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9384 break;
9386 case WINED3D_TOP_ADD_SIGNED:
9387 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9388 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9389 break;
9391 case WINED3D_TOP_ADD_SIGNED_2X:
9392 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9393 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9394 break;
9396 case WINED3D_TOP_SUBTRACT:
9397 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9398 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9399 break;
9401 case WINED3D_TOP_ADD_SMOOTH:
9402 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9403 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9404 break;
9406 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9407 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9408 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9409 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9410 break;
9412 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9413 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9414 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9415 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9416 break;
9418 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9419 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9420 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9421 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9422 break;
9424 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9425 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9426 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9427 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9428 break;
9430 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9431 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9432 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9433 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9434 break;
9436 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9437 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9438 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9439 break;
9441 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9442 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9443 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9444 break;
9446 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9447 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9448 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9449 break;
9450 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9451 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9452 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9453 break;
9455 case WINED3D_TOP_BUMPENVMAP:
9456 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9457 /* These are handled in the first pass, nothing to do. */
9458 break;
9460 case WINED3D_TOP_DOTPRODUCT3:
9461 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9462 dstreg, dstmask, arg1, arg2, dstmask);
9463 break;
9465 case WINED3D_TOP_MULTIPLY_ADD:
9466 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9467 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9468 break;
9470 case WINED3D_TOP_LERP:
9471 /* MSDN isn't quite right here. */
9472 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9473 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9474 break;
9476 default:
9477 FIXME("Unhandled operation %#x.\n", op);
9478 break;
9482 /* Context activation is done by the caller. */
9483 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9484 const struct ffp_frag_settings *settings, const struct wined3d_context_gl *context_gl)
9486 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9487 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9488 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9489 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9490 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
9491 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9492 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9493 UINT lowest_disabled_stage;
9494 GLuint shader_id;
9495 DWORD arg0, arg1, arg2;
9496 unsigned int stage;
9498 string_buffer_clear(buffer);
9500 /* Find out which textures are read */
9501 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9503 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9504 break;
9506 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9507 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9508 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9510 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9511 || (stage == 0 && settings->color_key_enabled))
9512 tex_map |= 1u << stage;
9513 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9514 tfactor_used = TRUE;
9515 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9516 tempreg_used = TRUE;
9517 if (settings->op[stage].tmp_dst)
9518 tempreg_used = TRUE;
9519 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9520 tss_const_map |= 1u << stage;
9522 switch (settings->op[stage].cop)
9524 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9525 lum_map |= 1u << stage;
9526 /* fall through */
9527 case WINED3D_TOP_BUMPENVMAP:
9528 bump_map |= 1u << stage;
9529 /* fall through */
9530 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9531 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9532 tex_map |= 1u << stage;
9533 break;
9535 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9536 tfactor_used = TRUE;
9537 break;
9539 default:
9540 break;
9543 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9544 continue;
9546 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9547 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9548 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9550 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9551 tex_map |= 1u << stage;
9552 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9553 tfactor_used = TRUE;
9554 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9555 tempreg_used = TRUE;
9556 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9557 tss_const_map |= 1u << stage;
9559 lowest_disabled_stage = stage;
9561 shader_glsl_add_version_declaration(buffer, gl_info);
9563 if (shader_glsl_use_explicit_attrib_location(gl_info))
9564 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9565 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9566 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9567 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9568 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9570 if (!use_legacy_fragment_output(gl_info))
9572 shader_addline(buffer, "vec4 ps_out[1];\n");
9573 if (shader_glsl_use_explicit_attrib_location(gl_info))
9574 shader_addline(buffer, "layout(location = 0) ");
9575 shader_addline(buffer, "out vec4 color_out0;\n");
9578 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9579 shader_addline(buffer, "vec4 ret;\n");
9580 if (tempreg_used || settings->sRGB_write)
9581 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9582 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9584 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9586 const char *sampler_type;
9588 if (tss_const_map & (1u << stage))
9589 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9591 if (!(tex_map & (1u << stage)))
9592 continue;
9594 switch (settings->op[stage].tex_type)
9596 case WINED3D_GL_RES_TYPE_TEX_1D:
9597 sampler_type = "1D";
9598 break;
9599 case WINED3D_GL_RES_TYPE_TEX_2D:
9600 sampler_type = "2D";
9601 break;
9602 case WINED3D_GL_RES_TYPE_TEX_3D:
9603 sampler_type = "3D";
9604 break;
9605 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9606 sampler_type = "Cube";
9607 break;
9608 case WINED3D_GL_RES_TYPE_TEX_RECT:
9609 sampler_type = "2DRect";
9610 break;
9611 default:
9612 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9613 sampler_type = NULL;
9614 break;
9616 if (sampler_type)
9618 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9619 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
9620 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9623 shader_addline(buffer, "vec4 tex%u;\n", stage);
9625 if (!(bump_map & (1u << stage)))
9626 continue;
9627 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9629 if (!(lum_map & (1u << stage)))
9630 continue;
9631 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9632 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9634 if (tfactor_used)
9635 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9636 if (settings->color_key_enabled)
9637 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9638 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9640 if (settings->sRGB_write)
9642 shader_addline(buffer, "const vec4 srgb_const0 = ");
9643 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
9644 shader_addline(buffer, ";\n");
9645 shader_addline(buffer, "const vec4 srgb_const1 = ");
9646 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
9647 shader_addline(buffer, ";\n");
9650 shader_addline(buffer, "uniform struct\n{\n");
9651 shader_addline(buffer, " vec4 color;\n");
9652 shader_addline(buffer, " float density;\n");
9653 shader_addline(buffer, " float end;\n");
9654 shader_addline(buffer, " float scale;\n");
9655 shader_addline(buffer, "} ffp_fog;\n");
9657 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9658 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9660 if (legacy_syntax)
9662 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9663 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9664 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9665 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9666 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9668 else
9670 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9671 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9672 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9673 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9674 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9677 shader_addline(buffer, "void main()\n{\n");
9679 if (legacy_syntax)
9681 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9682 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9685 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9687 if (tex_map & (1u << stage))
9689 if (settings->pointsprite)
9690 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9691 else if (settings->texcoords_initialized & (1u << stage))
9692 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9693 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9694 else
9695 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9699 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9700 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9702 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9703 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9705 /* Generate texture sampling instructions */
9706 for (stage = 0; stage < WINED3D_MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9708 const char *texture_function, *coord_mask;
9709 BOOL proj;
9711 if (!(tex_map & (1u << stage)))
9712 continue;
9714 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9716 proj = FALSE;
9718 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9719 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9721 proj = TRUE;
9723 else
9725 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9726 proj = TRUE;
9729 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9730 proj = FALSE;
9732 switch (settings->op[stage].tex_type)
9734 case WINED3D_GL_RES_TYPE_TEX_1D:
9735 texture_function = "texture1D";
9736 coord_mask = "x";
9737 break;
9738 case WINED3D_GL_RES_TYPE_TEX_2D:
9739 texture_function = "texture2D";
9740 coord_mask = "xy";
9741 break;
9742 case WINED3D_GL_RES_TYPE_TEX_3D:
9743 texture_function = "texture3D";
9744 coord_mask = "xyz";
9745 break;
9746 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9747 texture_function = "textureCube";
9748 coord_mask = "xyz";
9749 break;
9750 case WINED3D_GL_RES_TYPE_TEX_RECT:
9751 texture_function = "texture2DRect";
9752 coord_mask = "xy";
9753 break;
9754 default:
9755 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9756 texture_function = "";
9757 coord_mask = "xyzw";
9758 proj = FALSE;
9759 break;
9761 if (!legacy_syntax)
9762 texture_function = "texture";
9764 if (stage > 0
9765 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9766 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9768 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9770 /* With projective textures, texbem only divides the static
9771 * texture coordinate, not the displacement, so multiply the
9772 * displacement with the dividing parameter before passing it to
9773 * TXP. */
9774 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9776 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9778 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9779 stage, stage);
9780 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9782 else
9784 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9785 stage, stage);
9786 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9789 else
9791 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9794 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ret.%s%s);\n",
9795 stage, texture_function, proj ? "Proj" : "", stage, coord_mask, proj ? "w" : "");
9797 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9798 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9799 stage, stage - 1, stage - 1, stage - 1);
9801 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9803 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9804 stage, texture_function, proj ? "Proj" : "", stage, stage);
9806 else
9808 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].%s%s);\n",
9809 stage, texture_function, proj ? "Proj" : "", stage, stage, coord_mask, proj ? "w" : "");
9812 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9813 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9814 settings->op[stage].color_fixup);
9817 if (settings->color_key_enabled)
9818 shader_glsl_generate_colour_key_test(buffer, "tex0", "color_key[0]", "color_key[1]");
9820 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9822 /* Generate the main shader */
9823 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9825 BOOL op_equal;
9827 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9828 break;
9830 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9831 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9832 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9833 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9834 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9835 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9836 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9837 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9838 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9839 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9840 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9841 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9842 else
9843 op_equal = settings->op[stage].aop == settings->op[stage].cop
9844 && settings->op[stage].carg0 == settings->op[stage].aarg0
9845 && settings->op[stage].carg1 == settings->op[stage].aarg1
9846 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9848 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9850 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9851 settings->op[stage].cop, settings->op[stage].carg0,
9852 settings->op[stage].carg1, settings->op[stage].carg2);
9854 else if (op_equal)
9856 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9857 settings->op[stage].cop, settings->op[stage].carg0,
9858 settings->op[stage].carg1, settings->op[stage].carg2);
9860 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9861 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9863 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9864 settings->op[stage].cop, settings->op[stage].carg0,
9865 settings->op[stage].carg1, settings->op[stage].carg2);
9866 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9867 settings->op[stage].aop, settings->op[stage].aarg0,
9868 settings->op[stage].aarg1, settings->op[stage].aarg2);
9872 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9873 get_fragment_output(gl_info));
9875 if (settings->sRGB_write)
9876 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9878 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9880 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9881 if (!use_legacy_fragment_output(gl_info))
9882 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9884 shader_addline(buffer, "}\n");
9886 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9887 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9889 string_buffer_release(&priv->string_buffers, tex_reg_name);
9890 return shader_id;
9893 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9894 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9896 struct glsl_ffp_vertex_shader *shader;
9897 const struct wine_rb_entry *entry;
9899 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9900 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9902 if (!(shader = heap_alloc(sizeof(*shader))))
9903 return NULL;
9905 shader->desc.settings = *settings;
9906 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9907 list_init(&shader->linked_programs);
9908 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9909 ERR("Failed to insert ffp vertex shader.\n");
9911 return shader;
9914 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9915 const struct ffp_frag_settings *args, const struct wined3d_context_gl *context_gl)
9917 struct glsl_ffp_fragment_shader *glsl_desc;
9918 const struct ffp_frag_desc *desc;
9920 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9921 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9923 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9924 return NULL;
9926 glsl_desc->entry.settings = *args;
9927 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context_gl);
9928 list_init(&glsl_desc->linked_programs);
9929 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9931 return glsl_desc;
9935 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9936 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9938 unsigned int i;
9939 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9941 for (i = 0; i < vs_c_count; ++i)
9943 string_buffer_sprintf(name, "vs_c[%u]", i);
9944 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9946 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9948 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9950 string_buffer_sprintf(name, "vs_i[%u]", i);
9951 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9954 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9956 string_buffer_sprintf(name, "vs_b[%u]", i);
9957 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9960 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9961 vs->base_vertex_id_location = GL_EXTCALL(glGetUniformLocation(program_id, "base_vertex_id"));
9963 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9965 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9966 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9968 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9969 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9970 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9972 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9973 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9975 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9976 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9977 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9978 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9979 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9980 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9981 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
9983 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9984 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9985 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9986 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9987 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9988 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9989 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9990 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9991 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9992 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9993 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9994 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9995 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9996 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9997 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9998 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9999 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
10000 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10001 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
10002 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10003 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
10004 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10005 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
10006 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10008 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10009 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10010 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10011 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10012 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10013 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10014 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10016 string_buffer_release(&priv->string_buffers, name);
10019 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10020 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10022 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10025 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10026 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10028 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10031 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10032 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10034 unsigned int i;
10035 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10037 for (i = 0; i < ps_c_count; ++i)
10039 string_buffer_sprintf(name, "ps_c[%u]", i);
10040 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10042 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10044 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10046 string_buffer_sprintf(name, "ps_i[%u]", i);
10047 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10050 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10052 string_buffer_sprintf(name, "ps_b[%u]", i);
10053 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10056 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10058 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10059 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10060 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10061 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10062 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10063 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10064 string_buffer_sprintf(name, "tss_const%u", i);
10065 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10068 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10069 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10071 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10072 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10073 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10074 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10076 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10078 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10079 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10080 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10082 string_buffer_release(&priv->string_buffers, name);
10085 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10086 const struct wined3d_context_gl *context_gl, struct wined3d_shader *shader)
10088 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10089 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10090 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10091 struct glsl_cs_compiled_shader *gl_shaders;
10092 struct glsl_shader_private *shader_data;
10093 struct glsl_shader_prog_link *entry;
10094 GLuint shader_id, program_id;
10096 if (!(entry = heap_alloc(sizeof(*entry))))
10098 ERR("Out of memory.\n");
10099 return E_OUTOFMEMORY;
10102 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10104 ERR("Failed to allocate backend data.\n");
10105 heap_free(entry);
10106 return E_OUTOFMEMORY;
10108 shader_data = shader->backend_data;
10110 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10112 ERR("Failed to allocate GL shader array.\n");
10113 heap_free(entry);
10114 heap_free(shader->backend_data);
10115 shader->backend_data = NULL;
10116 return E_OUTOFMEMORY;
10118 shader_data->shader_array_size = 1;
10119 gl_shaders = shader_data->gl_shaders.cs;
10121 TRACE("Compiling compute shader %p.\n", shader);
10123 string_buffer_clear(buffer);
10124 shader_id = shader_glsl_generate_compute_shader(context_gl, buffer, &priv->string_buffers, shader);
10125 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10127 program_id = GL_EXTCALL(glCreateProgram());
10128 TRACE("Created new GLSL shader program %u.\n", program_id);
10130 entry->id = program_id;
10131 entry->vs.id = 0;
10132 entry->hs.id = 0;
10133 entry->ds.id = 0;
10134 entry->gs.id = 0;
10135 entry->ps.id = 0;
10136 entry->cs.id = shader_id;
10137 entry->constant_version = 0;
10138 entry->shader_controlled_clip_distances = 0;
10139 entry->ps.np2_fixup_info = NULL;
10140 add_glsl_program_entry(priv, entry);
10142 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10143 GL_EXTCALL(glAttachShader(program_id, shader_id));
10144 checkGLcall("glAttachShader");
10146 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10148 TRACE("Linking GLSL shader program %u.\n", program_id);
10149 GL_EXTCALL(glLinkProgram(program_id));
10150 shader_glsl_validate_link(gl_info, program_id);
10152 GL_EXTCALL(glUseProgram(program_id));
10153 checkGLcall("glUseProgram");
10154 shader_glsl_load_program_resources(context_gl, priv, program_id, shader);
10155 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10157 entry->constant_update_mask = 0;
10159 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10160 checkGLcall("glUseProgram");
10161 return WINED3D_OK;
10164 static GLuint find_glsl_compute_shader(const struct wined3d_context_gl *context_gl,
10165 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10167 struct glsl_shader_private *shader_data;
10169 if (!shader->backend_data)
10171 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10172 if (FAILED(shader_glsl_compile_compute_shader(priv, context_gl, shader)))
10174 ERR("Failed to compile compute shader %p.\n", shader);
10175 return 0;
10178 shader_data = shader->backend_data;
10179 return shader_data->gl_shaders.cs[0].id;
10182 /* Context activation is done by the caller. */
10183 static void set_glsl_compute_shader_program(const struct wined3d_context_gl *context_gl,
10184 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10186 struct glsl_shader_prog_link *entry;
10187 struct wined3d_shader *shader;
10188 struct glsl_program_key key;
10189 GLuint cs_id;
10191 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10192 return;
10194 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10196 WARN("Compute shader is NULL.\n");
10197 ctx_data->glsl_program = NULL;
10198 return;
10201 cs_id = find_glsl_compute_shader(context_gl, priv, shader);
10202 memset(&key, 0, sizeof(key));
10203 key.cs_id = cs_id;
10204 if (!(entry = get_glsl_program_entry(priv, &key)))
10205 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10206 ctx_data->glsl_program = entry;
10209 /* Context activation is done by the caller. */
10210 static void set_glsl_shader_program(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
10211 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10213 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
10214 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10215 const struct wined3d_shader *pre_rasterization_shader;
10216 const struct ps_np2fixup_info *np2fixup_info = NULL;
10217 struct wined3d_shader *hshader, *dshader, *gshader;
10218 struct glsl_shader_prog_link *entry = NULL;
10219 struct wined3d_shader *vshader = NULL;
10220 struct wined3d_shader *pshader = NULL;
10221 GLuint reorder_shader_id = 0;
10222 struct glsl_program_key key;
10223 GLuint program_id;
10224 unsigned int i;
10225 GLuint vs_id = 0;
10226 GLuint hs_id = 0;
10227 GLuint ds_id = 0;
10228 GLuint gs_id = 0;
10229 GLuint ps_id = 0;
10230 struct list *ps_list, *vs_list;
10231 WORD attribs_map;
10232 struct wined3d_string_buffer *tmp_name;
10234 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10236 vs_id = ctx_data->glsl_program->vs.id;
10237 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10239 if (use_vs(state))
10240 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10242 else if (use_vs(state))
10244 struct vs_compile_args vs_compile_args;
10246 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10248 find_vs_compile_args(state, vshader, &vs_compile_args, &context_gl->c);
10249 vs_id = find_glsl_vertex_shader(context_gl, priv, vshader, &vs_compile_args);
10250 vs_list = &vshader->linked_programs;
10252 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10254 struct glsl_ffp_vertex_shader *ffp_shader;
10255 struct wined3d_ffp_vs_settings settings;
10257 wined3d_ffp_get_vs_settings(&context_gl->c, state, &settings);
10258 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10259 vs_id = ffp_shader->id;
10260 vs_list = &ffp_shader->linked_programs;
10263 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10264 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10265 hs_id = ctx_data->glsl_program->hs.id;
10266 else if (hshader)
10267 hs_id = find_glsl_hull_shader(context_gl, priv, hshader);
10269 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10270 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10272 ds_id = ctx_data->glsl_program->ds.id;
10274 else if (dshader)
10276 struct ds_compile_args args;
10278 find_ds_compile_args(state, dshader, &args, &context_gl->c);
10279 ds_id = find_glsl_domain_shader(context_gl, priv, dshader, &args);
10282 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10283 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10285 gs_id = ctx_data->glsl_program->gs.id;
10287 else if (gshader)
10289 struct gs_compile_args args;
10291 find_gs_compile_args(state, gshader, &args, &context_gl->c);
10292 gs_id = find_glsl_geometry_shader(context_gl, priv, gshader, &args);
10295 /* A pixel shader is not used when rasterization is disabled. */
10296 if (is_rasterization_disabled(gshader))
10298 ps_id = 0;
10299 ps_list = NULL;
10301 else if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10303 ps_id = ctx_data->glsl_program->ps.id;
10304 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10306 if (use_ps(state))
10307 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10309 else if (use_ps(state))
10311 struct ps_compile_args ps_compile_args;
10312 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10313 find_ps_compile_args(state, pshader, context_gl->c.stream_info.position_transformed,
10314 &ps_compile_args, &context_gl->c);
10315 ps_id = find_glsl_fragment_shader(context_gl, &priv->shader_buffer, &priv->string_buffers,
10316 pshader, &ps_compile_args, &np2fixup_info);
10317 ps_list = &pshader->linked_programs;
10319 else if (priv->fragment_pipe == &glsl_fragment_pipe
10320 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10322 struct glsl_ffp_fragment_shader *ffp_shader;
10323 struct ffp_frag_settings settings;
10325 wined3d_ffp_get_fs_settings(&context_gl->c, state, &settings, FALSE);
10326 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context_gl);
10327 ps_id = ffp_shader->id;
10328 ps_list = &ffp_shader->linked_programs;
10331 key.vs_id = vs_id;
10332 key.hs_id = hs_id;
10333 key.ds_id = ds_id;
10334 key.gs_id = gs_id;
10335 key.ps_id = ps_id;
10336 key.cs_id = 0;
10337 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10339 ctx_data->glsl_program = entry;
10340 return;
10343 /* If we get to this point, then no matching program exists, so we create one */
10344 program_id = GL_EXTCALL(glCreateProgram());
10345 TRACE("Created new GLSL shader program %u.\n", program_id);
10347 /* Create the entry */
10348 entry = heap_alloc(sizeof(*entry));
10349 entry->id = program_id;
10350 entry->vs.id = vs_id;
10351 entry->hs.id = hs_id;
10352 entry->ds.id = ds_id;
10353 entry->gs.id = gs_id;
10354 entry->ps.id = ps_id;
10355 entry->cs.id = 0;
10356 entry->constant_version = 0;
10357 entry->shader_controlled_clip_distances = 0;
10358 entry->ps.np2_fixup_info = np2fixup_info;
10359 /* Add the hash table entry */
10360 add_glsl_program_entry(priv, entry);
10362 /* Set the current program */
10363 ctx_data->glsl_program = entry;
10365 /* Attach GLSL vshader */
10366 if (vs_id)
10368 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10369 GL_EXTCALL(glAttachShader(program_id, vs_id));
10370 checkGLcall("glAttachShader");
10372 list_add_head(vs_list, &entry->vs.shader_entry);
10375 if (vshader)
10377 attribs_map = vshader->reg_maps.input_registers;
10378 if (vshader->reg_maps.shader_version.major < 4)
10380 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10381 state->primitive_type == WINED3D_PT_POINTLIST && vshader->reg_maps.point_size,
10382 d3d_info->emulated_flatshading
10383 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10384 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10385 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10386 checkGLcall("glAttachShader");
10387 /* Flag the reorder function for deletion, it will be freed
10388 * automatically when the program is destroyed. */
10389 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10392 else
10394 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10397 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10399 /* Bind vertex attributes to a corresponding index number to match
10400 * the same index numbers as ARB_vertex_programs (makes loading
10401 * vertex attributes simpler). With this method, we can use the
10402 * exact same code to load the attributes later for both ARB and
10403 * GLSL shaders.
10405 * We have to do this here because we need to know the Program ID
10406 * in order to make the bindings work, and it has to be done prior
10407 * to linking the GLSL program. */
10408 tmp_name = string_buffer_get(&priv->string_buffers);
10409 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10411 if (!(attribs_map & 1))
10412 continue;
10414 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10415 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10416 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10418 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10419 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10420 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10421 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10424 checkGLcall("glBindAttribLocation");
10426 if (!use_legacy_fragment_output(gl_info))
10428 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
10430 string_buffer_sprintf(tmp_name, "color_out%u", i);
10431 if (state->blend_state && state->blend_state->dual_source)
10432 GL_EXTCALL(glBindFragDataLocationIndexed(program_id, 0, i, tmp_name->buffer));
10433 else
10434 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10435 checkGLcall("glBindFragDataLocation");
10438 string_buffer_release(&priv->string_buffers, tmp_name);
10441 if (hshader)
10443 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10444 GL_EXTCALL(glAttachShader(program_id, hs_id));
10445 checkGLcall("glAttachShader");
10447 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10450 if (dshader)
10452 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10453 GL_EXTCALL(glAttachShader(program_id, ds_id));
10454 checkGLcall("glAttachShader");
10456 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10459 if (gshader)
10461 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10462 GL_EXTCALL(glAttachShader(program_id, gs_id));
10463 checkGLcall("glAttachShader");
10465 shader_glsl_init_transform_feedback(context_gl, priv, program_id, gshader);
10467 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10470 /* Attach GLSL pshader */
10471 if (ps_id)
10473 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10474 GL_EXTCALL(glAttachShader(program_id, ps_id));
10475 checkGLcall("glAttachShader");
10477 list_add_head(ps_list, &entry->ps.shader_entry);
10480 /* Link the program */
10481 TRACE("Linking GLSL shader program %u.\n", program_id);
10482 GL_EXTCALL(glLinkProgram(program_id));
10483 shader_glsl_validate_link(gl_info, program_id);
10485 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10486 vshader ? vshader->limits->constant_float : 0);
10487 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10488 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10489 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10490 pshader ? pshader->limits->constant_float : 0);
10491 checkGLcall("find glsl program uniform locations");
10493 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10494 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10496 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10497 entry->shader_controlled_clip_distances = 1;
10498 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10501 if (needs_legacy_glsl_syntax(gl_info))
10503 if (pshader && pshader->reg_maps.shader_version.major >= 3
10504 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10506 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10507 entry->vs.vertex_color_clamp = GL_FALSE;
10509 else
10511 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10514 else
10516 /* With core profile we never change vertex_color_clamp from
10517 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10518 * glClampColorARB(). */
10519 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10522 /* Set the shader to allow uniform loading on it */
10523 GL_EXTCALL(glUseProgram(program_id));
10524 checkGLcall("glUseProgram");
10526 entry->constant_update_mask = 0;
10527 if (vshader)
10529 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10530 if (vshader->reg_maps.integer_constants)
10531 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10532 if (vshader->reg_maps.boolean_constants)
10533 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10534 if (entry->vs.pos_fixup_location != -1)
10535 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10536 if (entry->vs.base_vertex_id_location != -1)
10537 entry->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
10539 shader_glsl_load_program_resources(context_gl, priv, program_id, vshader);
10541 else
10543 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10544 | WINED3D_SHADER_CONST_FFP_PROJ;
10546 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10548 if (entry->vs.modelview_matrix_location[i] != -1)
10550 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10551 break;
10555 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10557 if (entry->vs.texture_matrix_location[i] != -1)
10559 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10560 break;
10563 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10564 || entry->vs.material_specular_location != -1
10565 || entry->vs.material_emissive_location != -1
10566 || entry->vs.material_shininess_location != -1)
10567 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10568 if (entry->vs.light_ambient_location != -1)
10569 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10571 if (entry->vs.clip_planes_location != -1)
10572 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10573 if (entry->vs.pointsize_min_location != -1)
10574 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10576 if (hshader)
10577 shader_glsl_load_program_resources(context_gl, priv, program_id, hshader);
10579 if (dshader)
10581 if (entry->ds.pos_fixup_location != -1)
10582 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10584 shader_glsl_load_program_resources(context_gl, priv, program_id, dshader);
10587 if (gshader)
10589 if (entry->gs.pos_fixup_location != -1)
10590 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10592 shader_glsl_load_program_resources(context_gl, priv, program_id, gshader);
10595 if (ps_id)
10597 if (pshader)
10599 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10600 if (pshader->reg_maps.integer_constants)
10601 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10602 if (pshader->reg_maps.boolean_constants)
10603 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10604 if (entry->ps.ycorrection_location != -1)
10605 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10607 shader_glsl_load_program_resources(context_gl, priv, program_id, pshader);
10608 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10610 else
10612 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10614 shader_glsl_load_samplers(&context_gl->c, priv, program_id, NULL);
10617 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10619 if (entry->ps.bumpenv_mat_location[i] != -1)
10621 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10622 break;
10626 if (entry->ps.fog_color_location != -1)
10627 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10628 if (entry->ps.alpha_test_ref_location != -1)
10629 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10630 if (entry->ps.np2_fixup_location != -1)
10631 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10632 if (entry->ps.color_key_location != -1)
10633 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10637 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10639 struct wined3d_device *device = shader->device;
10640 struct wined3d_context *context;
10642 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10644 context = context_acquire(device, NULL, 0);
10645 shader_glsl_compile_compute_shader(shader_priv, wined3d_context_gl(context), shader);
10646 context_release(context);
10650 /* Context activation is done by the caller. */
10651 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10652 const struct wined3d_state *state)
10654 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10655 struct glsl_context_data *ctx_data = context->shader_backend_data;
10656 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10657 struct shader_glsl_priv *priv = shader_priv;
10658 struct glsl_shader_prog_link *glsl_program;
10659 GLenum current_vertex_color_clamp;
10660 GLuint program_id, prev_id;
10662 priv->vertex_pipe->vp_enable(context, !use_vs(state));
10663 priv->fragment_pipe->fp_enable(context, !use_ps(state));
10665 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10666 set_glsl_shader_program(context_gl, state, priv, ctx_data);
10667 glsl_program = ctx_data->glsl_program;
10669 if (glsl_program)
10671 program_id = glsl_program->id;
10672 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10673 if (glsl_program->shader_controlled_clip_distances)
10674 wined3d_context_gl_enable_clip_distances(context_gl, glsl_program->clip_distance_mask);
10676 else
10678 program_id = 0;
10679 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10682 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10684 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10685 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10687 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10688 checkGLcall("glClampColorARB");
10690 else
10692 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10696 TRACE("Using GLSL program %u.\n", program_id);
10698 if (prev_id != program_id)
10700 GL_EXTCALL(glUseProgram(program_id));
10701 checkGLcall("glUseProgram");
10703 if (glsl_program)
10704 context->constant_update_mask |= glsl_program->constant_update_mask;
10707 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10710 /* Context activation is done by the caller. */
10711 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10712 const struct wined3d_state *state)
10714 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10715 struct glsl_context_data *ctx_data = context->shader_backend_data;
10716 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10717 struct shader_glsl_priv *priv = shader_priv;
10718 GLuint program_id, prev_id;
10720 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10721 set_glsl_compute_shader_program(context_gl, state, priv, ctx_data);
10722 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10724 TRACE("Using GLSL program %u.\n", program_id);
10726 if (prev_id != program_id)
10728 GL_EXTCALL(glUseProgram(program_id));
10729 checkGLcall("glUseProgram");
10732 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10733 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10734 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10735 | (1u << WINED3D_SHADER_TYPE_HULL)
10736 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10739 /* "context" is not necessarily the currently active context. */
10740 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10742 struct glsl_context_data *ctx_data = context->shader_backend_data;
10744 ctx_data->glsl_program = NULL;
10745 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10746 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10747 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10748 | (1u << WINED3D_SHADER_TYPE_HULL)
10749 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10750 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10753 /* Context activation is done by the caller. */
10754 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10756 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10757 struct glsl_context_data *ctx_data = context->shader_backend_data;
10758 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10759 struct shader_glsl_priv *priv = shader_priv;
10761 shader_glsl_invalidate_current_program(context);
10762 GL_EXTCALL(glUseProgram(0));
10763 checkGLcall("glUseProgram");
10765 priv->vertex_pipe->vp_enable(context, FALSE);
10766 priv->fragment_pipe->fp_enable(context, FALSE);
10768 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10770 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10771 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10772 checkGLcall("glClampColorARB");
10776 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10777 const struct glsl_shader_prog_link *program)
10779 const struct glsl_context_data *ctx_data;
10780 struct wined3d_context *context;
10781 unsigned int i;
10783 for (i = 0; i < device->context_count; ++i)
10785 context = device->contexts[i];
10786 ctx_data = context->shader_backend_data;
10788 if (ctx_data->glsl_program == program)
10789 shader_glsl_invalidate_current_program(context);
10793 static void shader_glsl_destroy(struct wined3d_shader *shader)
10795 struct glsl_shader_private *shader_data = shader->backend_data;
10796 struct wined3d_device *device = shader->device;
10797 struct shader_glsl_priv *priv = device->shader_priv;
10798 const struct wined3d_gl_info *gl_info;
10799 const struct list *linked_programs;
10800 struct wined3d_context *context;
10802 if (!shader_data || !shader_data->num_gl_shaders)
10804 heap_free(shader_data);
10805 shader->backend_data = NULL;
10806 return;
10809 context = context_acquire(device, NULL, 0);
10810 gl_info = wined3d_context_gl(context)->gl_info;
10812 TRACE("Deleting linked programs.\n");
10813 linked_programs = &shader->linked_programs;
10814 if (linked_programs->next)
10816 struct glsl_shader_prog_link *entry, *entry2;
10817 UINT i;
10819 switch (shader->reg_maps.shader_version.type)
10821 case WINED3D_SHADER_TYPE_PIXEL:
10823 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10825 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10827 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10828 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10829 checkGLcall("glDeleteShader");
10831 heap_free(shader_data->gl_shaders.ps);
10833 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10834 struct glsl_shader_prog_link, ps.shader_entry)
10836 shader_glsl_invalidate_contexts_program(device, entry);
10837 delete_glsl_program_entry(priv, gl_info, entry);
10840 break;
10843 case WINED3D_SHADER_TYPE_VERTEX:
10845 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10847 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10849 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10850 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10851 checkGLcall("glDeleteShader");
10853 heap_free(shader_data->gl_shaders.vs);
10855 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10856 struct glsl_shader_prog_link, vs.shader_entry)
10858 shader_glsl_invalidate_contexts_program(device, entry);
10859 delete_glsl_program_entry(priv, gl_info, entry);
10862 break;
10865 case WINED3D_SHADER_TYPE_HULL:
10867 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10869 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10871 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10872 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10873 checkGLcall("glDeleteShader");
10875 heap_free(shader_data->gl_shaders.hs);
10877 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10878 struct glsl_shader_prog_link, hs.shader_entry)
10880 shader_glsl_invalidate_contexts_program(device, entry);
10881 delete_glsl_program_entry(priv, gl_info, entry);
10884 break;
10887 case WINED3D_SHADER_TYPE_DOMAIN:
10889 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10891 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10893 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10894 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10895 checkGLcall("glDeleteShader");
10897 heap_free(shader_data->gl_shaders.ds);
10899 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10900 struct glsl_shader_prog_link, ds.shader_entry)
10902 shader_glsl_invalidate_contexts_program(device, entry);
10903 delete_glsl_program_entry(priv, gl_info, entry);
10906 break;
10909 case WINED3D_SHADER_TYPE_GEOMETRY:
10911 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10913 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10915 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10916 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10917 checkGLcall("glDeleteShader");
10919 heap_free(shader_data->gl_shaders.gs);
10921 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10922 struct glsl_shader_prog_link, gs.shader_entry)
10924 shader_glsl_invalidate_contexts_program(device, entry);
10925 delete_glsl_program_entry(priv, gl_info, entry);
10928 break;
10931 case WINED3D_SHADER_TYPE_COMPUTE:
10933 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10935 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10937 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10938 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10939 checkGLcall("glDeleteShader");
10941 heap_free(shader_data->gl_shaders.cs);
10943 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10944 struct glsl_shader_prog_link, cs.shader_entry)
10946 shader_glsl_invalidate_contexts_program(device, entry);
10947 delete_glsl_program_entry(priv, gl_info, entry);
10950 break;
10953 default:
10954 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10955 break;
10959 heap_free(shader->backend_data);
10960 shader->backend_data = NULL;
10962 context_release(context);
10965 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10967 const struct glsl_program_key *k = key;
10968 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10969 const struct glsl_shader_prog_link, program_lookup_entry);
10971 if (k->vs_id > prog->vs.id) return 1;
10972 else if (k->vs_id < prog->vs.id) return -1;
10974 if (k->gs_id > prog->gs.id) return 1;
10975 else if (k->gs_id < prog->gs.id) return -1;
10977 if (k->ps_id > prog->ps.id) return 1;
10978 else if (k->ps_id < prog->ps.id) return -1;
10980 if (k->hs_id > prog->hs.id) return 1;
10981 else if (k->hs_id < prog->hs.id) return -1;
10983 if (k->ds_id > prog->ds.id) return 1;
10984 else if (k->ds_id < prog->ds.id) return -1;
10986 if (k->cs_id > prog->cs.id) return 1;
10987 else if (k->cs_id < prog->cs.id) return -1;
10989 return 0;
10992 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10994 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10995 + constant_count * sizeof(*heap->contained)
10996 + constant_count * sizeof(*heap->positions);
10997 void *mem;
10999 if (!(mem = heap_alloc(size)))
11001 ERR("Failed to allocate memory\n");
11002 return FALSE;
11005 heap->entries = mem;
11006 heap->entries[1].version = 0;
11007 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11008 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11009 heap->positions = (unsigned int *)(heap->contained + constant_count);
11010 heap->size = 1;
11012 return TRUE;
11015 static void constant_heap_free(struct constant_heap *heap)
11017 heap_free(heap->entries);
11020 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11021 const struct wined3d_fragment_pipe_ops *fragment_pipe)
11023 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11024 struct fragment_caps fragment_caps;
11025 void *vertex_priv, *fragment_priv;
11026 struct shader_glsl_priv *priv;
11028 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11029 return E_OUTOFMEMORY;
11031 string_buffer_list_init(&priv->string_buffers);
11033 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11035 ERR("Failed to initialize vertex pipe.\n");
11036 heap_free(priv);
11037 return E_FAIL;
11040 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11042 ERR("Failed to initialize fragment pipe.\n");
11043 vertex_pipe->vp_free(device, NULL);
11044 heap_free(priv);
11045 return E_FAIL;
11048 if (!string_buffer_init(&priv->shader_buffer))
11050 ERR("Failed to initialize shader buffer.\n");
11051 goto fail;
11054 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11056 ERR("Failed to allocate memory.\n");
11057 goto fail;
11060 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11062 ERR("Failed to initialize vertex shader constant heap\n");
11063 goto fail;
11066 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11068 ERR("Failed to initialize pixel shader constant heap\n");
11069 goto fail;
11072 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11074 priv->next_constant_version = 1;
11075 priv->vertex_pipe = vertex_pipe;
11076 priv->fragment_pipe = fragment_pipe;
11077 fragment_pipe->get_caps(device->adapter, &fragment_caps);
11078 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11079 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11081 device->vertex_priv = vertex_priv;
11082 device->fragment_priv = fragment_priv;
11083 device->shader_priv = priv;
11085 return WINED3D_OK;
11087 fail:
11088 constant_heap_free(&priv->pconst_heap);
11089 constant_heap_free(&priv->vconst_heap);
11090 heap_free(priv->stack);
11091 string_buffer_free(&priv->shader_buffer);
11092 fragment_pipe->free_private(device, NULL);
11093 vertex_pipe->vp_free(device, NULL);
11094 heap_free(priv);
11095 return E_OUTOFMEMORY;
11098 /* Context activation is done by the caller. */
11099 static void shader_glsl_free(struct wined3d_device *device, struct wined3d_context *context)
11101 struct shader_glsl_priv *priv = device->shader_priv;
11103 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11104 constant_heap_free(&priv->pconst_heap);
11105 constant_heap_free(&priv->vconst_heap);
11106 heap_free(priv->stack);
11107 string_buffer_list_cleanup(&priv->string_buffers);
11108 string_buffer_free(&priv->shader_buffer);
11109 priv->fragment_pipe->free_private(device, context);
11110 priv->vertex_pipe->vp_free(device, context);
11112 heap_free(device->shader_priv);
11113 device->shader_priv = NULL;
11116 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11118 struct glsl_context_data *ctx_data;
11120 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11121 return FALSE;
11122 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11123 context->shader_backend_data = ctx_data;
11124 return TRUE;
11127 static void shader_glsl_free_context_data(struct wined3d_context *context)
11129 heap_free(context->shader_backend_data);
11132 static void shader_glsl_init_context_state(struct wined3d_context *context)
11134 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11135 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11137 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11138 checkGLcall("GL_PROGRAM_POINT_SIZE");
11141 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11143 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11144 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11145 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11147 if (shader_model_4
11148 && gl_info->supported[ARB_COMPUTE_SHADER]
11149 && gl_info->supported[ARB_CULL_DISTANCE]
11150 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11151 && gl_info->supported[ARB_GPU_SHADER5]
11152 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11153 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11154 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11155 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11156 && gl_info->supported[ARB_TESSELLATION_SHADER]
11157 && gl_info->supported[ARB_TEXTURE_GATHER]
11158 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11159 return 5;
11161 if (shader_model_4)
11162 return 4;
11164 /* Support for texldd and texldl instructions in pixel shaders is required
11165 * for SM3. */
11166 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11167 return 3;
11169 return 2;
11172 static void shader_glsl_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
11174 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11175 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11177 TRACE("Shader model %u.\n", shader_model);
11179 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11180 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11181 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11182 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11183 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11184 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11186 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11187 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11189 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11190 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11191 caps->varying_count = gl_info->limits.glsl_varyings;
11193 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11194 * Direct3D minimum requirement.
11196 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11197 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11199 * The problem is that the refrast clamps temporary results in the shader to
11200 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11201 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11202 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11203 * offer a way to query this.
11205 if (shader_model >= 4)
11206 caps->ps_1x_max_value = FLT_MAX;
11207 else
11208 caps->ps_1x_max_value = 1024.0f;
11210 /* Ideally we'd only set caps like sRGB writes here if supported by both
11211 * the shader backend and the fragment pipe, but we can get called before
11212 * shader_glsl_alloc(). */
11213 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11214 | WINED3D_SHADER_CAP_SRGB_WRITE;
11215 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
11216 caps->wined3d_caps |= WINED3D_SHADER_CAP_OUTPUT_INTERPOLATION;
11217 if (shader_glsl_full_ffp_varyings(gl_info))
11218 caps->wined3d_caps |= WINED3D_SHADER_CAP_FULL_FFP_VARYINGS;
11221 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11223 /* We support everything except YUV conversions. */
11224 return !is_complex_fixup(fixup);
11227 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11229 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11230 /* WINED3DSIH_ADD */ shader_glsl_binop,
11231 /* WINED3DSIH_AND */ shader_glsl_binop,
11232 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11233 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11234 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11235 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11236 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11237 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11238 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11239 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11240 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11241 /* WINED3DSIH_BEM */ shader_glsl_bem,
11242 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11243 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11244 /* WINED3DSIH_BREAK */ shader_glsl_break,
11245 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11246 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11247 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11248 /* WINED3DSIH_CALL */ shader_glsl_call,
11249 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11250 /* WINED3DSIH_CASE */ shader_glsl_case,
11251 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11252 /* WINED3DSIH_CND */ shader_glsl_cnd,
11253 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11254 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11255 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11256 /* WINED3DSIH_CRS */ shader_glsl_cross,
11257 /* WINED3DSIH_CUT */ shader_glsl_cut,
11258 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11259 /* WINED3DSIH_DCL */ shader_glsl_nop,
11260 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11261 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11262 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11263 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11264 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11265 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11266 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11267 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11268 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11269 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11270 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11271 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11272 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11273 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11274 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11275 /* WINED3DSIH_DCL_INPUT_PS_SGV */ shader_glsl_nop,
11276 /* WINED3DSIH_DCL_INPUT_PS_SIV */ shader_glsl_nop,
11277 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11278 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11279 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11280 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11281 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11282 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11283 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11284 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11285 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11286 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11287 /* WINED3DSIH_DCL_STREAM */ NULL,
11288 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11289 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11290 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11291 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11292 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11293 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11294 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11295 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11296 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11297 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11298 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11299 /* WINED3DSIH_DEF */ shader_glsl_nop,
11300 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11301 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11302 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11303 /* WINED3DSIH_DIV */ shader_glsl_binop,
11304 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11305 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11306 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11307 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11308 /* WINED3DSIH_DST */ shader_glsl_dst,
11309 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11310 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11311 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11312 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11313 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11314 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11315 /* WINED3DSIH_ELSE */ shader_glsl_else,
11316 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11317 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11318 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11319 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11320 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11321 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11322 /* WINED3DSIH_EQ */ shader_glsl_relop,
11323 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ shader_glsl_interpolate,
11324 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11325 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11326 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11327 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11328 /* WINED3DSIH_FCALL */ NULL,
11329 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11330 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11331 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11332 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11333 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11334 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11335 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11336 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11337 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11338 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11339 /* WINED3DSIH_GE */ shader_glsl_relop,
11340 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11341 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11342 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11343 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11344 /* WINED3DSIH_IADD */ shader_glsl_binop,
11345 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11346 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11347 /* WINED3DSIH_IF */ shader_glsl_if,
11348 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11349 /* WINED3DSIH_IGE */ shader_glsl_relop,
11350 /* WINED3DSIH_ILT */ shader_glsl_relop,
11351 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11352 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11353 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11354 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11355 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11356 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11357 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11358 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11359 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11360 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11361 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11362 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11363 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11364 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11365 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11366 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11367 /* WINED3DSIH_INE */ shader_glsl_relop,
11368 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11369 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11370 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11371 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11372 /* WINED3DSIH_LABEL */ shader_glsl_label,
11373 /* WINED3DSIH_LD */ shader_glsl_ld,
11374 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11375 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11376 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11377 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11378 /* WINED3DSIH_LIT */ shader_glsl_lit,
11379 /* WINED3DSIH_LOD */ NULL,
11380 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11381 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11382 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11383 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11384 /* WINED3DSIH_LT */ shader_glsl_relop,
11385 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11386 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11387 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11388 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11389 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11390 /* WINED3DSIH_MAD */ shader_glsl_mad,
11391 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11392 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11393 /* WINED3DSIH_MOV */ shader_glsl_mov,
11394 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11395 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11396 /* WINED3DSIH_MUL */ shader_glsl_binop,
11397 /* WINED3DSIH_NE */ shader_glsl_relop,
11398 /* WINED3DSIH_NOP */ shader_glsl_nop,
11399 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11400 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11401 /* WINED3DSIH_OR */ shader_glsl_binop,
11402 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11403 /* WINED3DSIH_POW */ shader_glsl_pow,
11404 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11405 /* WINED3DSIH_REP */ shader_glsl_rep,
11406 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11407 /* WINED3DSIH_RET */ shader_glsl_ret,
11408 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11409 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11410 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11411 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11412 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11413 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11414 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11415 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11416 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11417 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11418 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11419 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11420 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11421 /* WINED3DSIH_SAMPLE_POS */ NULL,
11422 /* WINED3DSIH_SETP */ NULL,
11423 /* WINED3DSIH_SGE */ shader_glsl_compare,
11424 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11425 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11426 /* WINED3DSIH_SLT */ shader_glsl_compare,
11427 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11428 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11429 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11430 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11431 /* WINED3DSIH_SUB */ shader_glsl_binop,
11432 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11433 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11434 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11435 /* WINED3DSIH_TEX */ shader_glsl_tex,
11436 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11437 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11438 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11439 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11440 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11441 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11442 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11443 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11444 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11445 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11446 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11447 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11448 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11449 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11450 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11451 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11452 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11453 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11454 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11455 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11456 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11457 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11458 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11459 /* WINED3DSIH_UGE */ shader_glsl_relop,
11460 /* WINED3DSIH_ULT */ shader_glsl_relop,
11461 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11462 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11463 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11464 /* WINED3DSIH_USHR */ shader_glsl_binop,
11465 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11466 /* WINED3DSIH_XOR */ shader_glsl_binop,
11469 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11470 SHADER_HANDLER hw_fct;
11472 /* Select handler */
11473 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11475 /* Unhandled opcode */
11476 if (!hw_fct)
11478 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11479 return;
11481 hw_fct(ins);
11483 shader_glsl_add_instruction_modifiers(ins);
11486 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11488 struct shader_glsl_priv *priv = shader_priv;
11490 return priv->ffp_proj_control;
11493 const struct wined3d_shader_backend_ops glsl_shader_backend =
11495 shader_glsl_handle_instruction,
11496 shader_glsl_precompile,
11497 shader_glsl_select,
11498 shader_glsl_select_compute,
11499 shader_glsl_disable,
11500 shader_glsl_update_float_vertex_constants,
11501 shader_glsl_update_float_pixel_constants,
11502 shader_glsl_load_constants,
11503 shader_glsl_destroy,
11504 shader_glsl_alloc,
11505 shader_glsl_free,
11506 shader_glsl_allocate_context_data,
11507 shader_glsl_free_context_data,
11508 shader_glsl_init_context_state,
11509 shader_glsl_get_caps,
11510 shader_glsl_color_fixup_supported,
11511 shader_glsl_has_ffp_proj_control,
11514 static void glsl_vertex_pipe_vp_enable(const struct wined3d_context *context, BOOL enable) {}
11516 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_adapter *adapter, struct wined3d_vertex_caps *caps)
11518 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11520 caps->xyzrhw = TRUE;
11521 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11522 caps->ffp_generic_attributes = TRUE;
11523 caps->max_active_lights = WINED3D_MAX_ACTIVE_LIGHTS;
11524 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11525 caps->max_vertex_blend_matrix_index = 0;
11526 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11527 | WINED3DVTXPCAPS_MATERIALSOURCE7
11528 | WINED3DVTXPCAPS_VERTEXFOG
11529 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11530 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11531 | WINED3DVTXPCAPS_LOCALVIEWER
11532 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11533 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11534 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11535 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11538 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11540 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11541 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11542 return 0;
11545 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11547 struct shader_glsl_priv *priv;
11549 if (shader_backend == &glsl_shader_backend)
11551 priv = shader_priv;
11552 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11553 return priv;
11556 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11558 return NULL;
11561 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *param)
11563 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11564 struct glsl_ffp_vertex_shader, desc.entry);
11565 struct glsl_shader_prog_link *program, *program2;
11566 struct glsl_ffp_destroy_ctx *ctx = param;
11567 const struct wined3d_gl_info *gl_info;
11569 gl_info = ctx->context_gl->gl_info;
11570 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11571 struct glsl_shader_prog_link, vs.shader_entry)
11573 delete_glsl_program_entry(ctx->priv, gl_info, program);
11575 GL_EXTCALL(glDeleteShader(shader->id));
11576 heap_free(shader);
11579 /* Context activation is done by the caller. */
11580 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device, struct wined3d_context *context)
11582 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11583 struct shader_glsl_priv *priv = device->vertex_priv;
11584 struct glsl_ffp_destroy_ctx ctx;
11586 ctx.priv = priv;
11587 ctx.context_gl = context_gl;
11588 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11591 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11592 const struct wined3d_state *state, DWORD state_id) {}
11594 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11595 const struct wined3d_state *state, DWORD state_id)
11597 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11600 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11601 const struct wined3d_state *state, DWORD state_id)
11603 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11604 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11605 BOOL specular = !!(context->stream_info.use_map & (1u << WINED3D_FFP_SPECULAR));
11606 BOOL diffuse = !!(context->stream_info.use_map & (1u << WINED3D_FFP_DIFFUSE));
11607 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11608 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11609 BOOL transformed = context->stream_info.position_transformed;
11610 BOOL wasrhw = context->last_was_rhw;
11611 unsigned int i;
11613 context->last_was_rhw = transformed;
11615 /* If the vertex declaration contains a transformed position attribute,
11616 * the draw uses the fixed function vertex pipeline regardless of any
11617 * vertex shader set by the application. */
11618 if (transformed != wasrhw
11619 || context->stream_info.swizzle_map != context->last_swizzle_map)
11620 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11622 context->last_swizzle_map = context->stream_info.swizzle_map;
11624 if (!use_vs(state))
11626 if (context->last_was_vshader)
11628 if (legacy_clip_planes)
11629 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11630 clipplane(context, state, STATE_CLIPPLANE(i));
11631 else
11632 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11635 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11637 /* Because of settings->texcoords, we have to regenerate the vertex
11638 * shader on a vdecl change if there aren't enough varyings to just
11639 * always output all the texture coordinates.
11641 * Likewise, we have to invalidate the shader when using per-vertex
11642 * colours and diffuse/specular attribute presence changes, or when
11643 * normal presence changes. */
11644 if (!shader_glsl_full_ffp_varyings(gl_info) || (state->render_states[WINED3D_RS_COLORVERTEX]
11645 && (diffuse != context->last_was_diffuse || specular != context->last_was_specular))
11646 || normal != context->last_was_normal)
11647 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11649 if (use_ps(state)
11650 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11651 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11652 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11654 else
11656 if (!context->last_was_vshader)
11658 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11659 if (legacy_clip_planes)
11660 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11661 clipplane(context, state, STATE_CLIPPLANE(i));
11662 else
11663 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11667 context->last_was_vshader = use_vs(state);
11668 context->last_was_diffuse = diffuse;
11669 context->last_was_specular = specular;
11670 context->last_was_normal = normal;
11673 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11674 const struct wined3d_state *state, DWORD state_id)
11676 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11677 /* Different vertex shaders potentially require a different vertex attributes setup. */
11678 if (!isStateDirty(context, STATE_VDECL))
11679 context_apply_state(context, state, STATE_VDECL);
11682 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11683 const struct wined3d_state *state, DWORD state_id)
11685 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11686 * winding) are defined in Hull Shaders, while in GLSL those are
11687 * specified in Tessellation Evaluation Shaders. */
11688 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11690 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11691 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11694 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11695 const struct wined3d_state *state, DWORD state_id)
11697 struct glsl_context_data *ctx_data = context->shader_backend_data;
11698 BOOL rasterization_disabled;
11700 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11701 if (ctx_data->rasterization_disabled != rasterization_disabled)
11702 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11703 ctx_data->rasterization_disabled = rasterization_disabled;
11705 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11706 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11707 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11708 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11709 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11712 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11713 const struct wined3d_state *state, DWORD state_id)
11715 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11716 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11717 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11718 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11719 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11720 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11721 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11724 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11725 const struct wined3d_state *state, DWORD state_id)
11727 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11730 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11731 const struct wined3d_state *state, DWORD state_id)
11733 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11736 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11738 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11739 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11740 unsigned int k;
11742 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11743 | WINED3D_SHADER_CONST_FFP_LIGHTS
11744 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11746 if (needs_legacy_glsl_syntax(gl_info))
11748 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11750 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11751 clipplane(context, state, STATE_CLIPPLANE(k));
11754 else
11756 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11760 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11761 const struct wined3d_state *state, DWORD state_id)
11763 /* Table fog behavior depends on the projection matrix. */
11764 if (state->render_states[WINED3D_RS_FOGENABLE]
11765 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11766 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11767 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11770 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11771 const struct wined3d_state *state, DWORD state_id)
11773 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11774 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11775 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11776 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11777 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11778 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11781 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11782 const struct wined3d_state *state, DWORD state_id)
11784 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11787 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11788 const struct wined3d_state *state, DWORD state_id)
11790 DWORD sampler = state_id - STATE_SAMPLER(0);
11791 const struct wined3d_texture *texture = state->textures[sampler];
11792 BOOL np2;
11794 if (!texture)
11795 return;
11797 if (sampler >= WINED3D_MAX_TEXTURES)
11798 return;
11800 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11801 || context->lastWasPow2Texture & (1u << sampler))
11803 if (np2)
11804 context->lastWasPow2Texture |= 1u << sampler;
11805 else
11806 context->lastWasPow2Texture &= ~(1u << sampler);
11808 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11812 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11813 const struct wined3d_state *state, DWORD state_id)
11815 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11818 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11819 const struct wined3d_state *state, DWORD state_id)
11821 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11824 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11825 const struct wined3d_state *state, DWORD state_id)
11827 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11830 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11831 const struct wined3d_state *state, DWORD state_id)
11833 if (!use_vs(state))
11834 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11837 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11838 const struct wined3d_state *state, DWORD state_id)
11840 static unsigned int once;
11842 if (state->primitive_type == WINED3D_PT_POINTLIST
11843 && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11844 FIXME("Non-point sprite points not supported in core profile.\n");
11847 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11848 const struct wined3d_state *state, DWORD state_id)
11850 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11853 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11854 const struct wined3d_state *state, DWORD state_id)
11856 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11857 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11858 UINT index = state_id - STATE_CLIPPLANE(0);
11860 if (index >= gl_info->limits.user_clip_distances)
11861 return;
11863 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11866 static const struct wined3d_state_entry_template glsl_vertex_pipe_vp_states[] =
11868 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11869 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11870 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11871 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11872 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11873 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11874 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11875 /* Clip planes */
11876 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11877 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11878 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11879 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11880 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11881 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11882 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11883 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11884 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11885 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11886 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11887 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11888 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11889 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11890 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11891 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11892 /* Lights */
11893 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11894 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11895 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11896 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11897 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11898 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11899 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11900 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11901 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11902 /* Viewport */
11903 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11904 /* Transform states */
11905 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11906 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11907 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11908 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11909 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11910 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11911 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11912 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11913 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11914 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11915 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11916 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11917 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11918 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11919 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11920 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11921 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11922 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11923 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11924 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11925 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11926 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11927 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11928 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11929 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11930 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11931 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11932 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11933 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11934 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11935 /* Fog */
11936 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11937 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11938 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11939 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11940 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11941 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11942 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11943 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11944 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11945 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11946 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11947 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11948 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11949 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11950 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11951 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11952 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11953 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11954 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11955 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11956 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11957 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11958 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11959 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11960 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11961 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11962 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11963 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11964 /* NP2 texture matrix fixups. They are not needed if
11965 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11966 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11967 * matrix. */
11968 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11969 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11970 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11971 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11972 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11973 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11974 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11975 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11976 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11977 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11978 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11979 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11980 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11981 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11982 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11983 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11984 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11985 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11986 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11987 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11988 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11989 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11990 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11991 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11992 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11993 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11994 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11995 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11998 /* TODO:
11999 * - Implement vertex tweening. */
12000 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
12002 glsl_vertex_pipe_vp_enable,
12003 glsl_vertex_pipe_vp_get_caps,
12004 glsl_vertex_pipe_vp_get_emul_mask,
12005 glsl_vertex_pipe_vp_alloc,
12006 glsl_vertex_pipe_vp_free,
12007 glsl_vertex_pipe_vp_states,
12010 static void glsl_fragment_pipe_enable(const struct wined3d_context *context, BOOL enable)
12012 /* Nothing to do. */
12015 static void glsl_fragment_pipe_get_caps(const struct wined3d_adapter *adapter, struct fragment_caps *caps)
12017 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
12019 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
12020 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
12021 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
12022 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12023 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12024 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12025 | WINED3DTEXOPCAPS_SELECTARG1
12026 | WINED3DTEXOPCAPS_SELECTARG2
12027 | WINED3DTEXOPCAPS_MODULATE4X
12028 | WINED3DTEXOPCAPS_MODULATE2X
12029 | WINED3DTEXOPCAPS_MODULATE
12030 | WINED3DTEXOPCAPS_ADDSIGNED2X
12031 | WINED3DTEXOPCAPS_ADDSIGNED
12032 | WINED3DTEXOPCAPS_ADD
12033 | WINED3DTEXOPCAPS_SUBTRACT
12034 | WINED3DTEXOPCAPS_ADDSMOOTH
12035 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12036 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12037 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12038 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12039 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12040 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12041 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12042 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12043 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12044 | WINED3DTEXOPCAPS_DOTPRODUCT3
12045 | WINED3DTEXOPCAPS_MULTIPLYADD
12046 | WINED3DTEXOPCAPS_LERP
12047 | WINED3DTEXOPCAPS_BUMPENVMAP
12048 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12049 caps->MaxTextureBlendStages = WINED3D_MAX_TEXTURES;
12050 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], WINED3D_MAX_TEXTURES);
12053 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
12055 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
12056 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12057 return 0;
12060 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12062 struct shader_glsl_priv *priv;
12064 if (shader_backend == &glsl_shader_backend)
12066 priv = shader_priv;
12067 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12068 return priv;
12071 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12073 return NULL;
12076 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *param)
12078 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12079 struct glsl_ffp_fragment_shader, entry.entry);
12080 struct glsl_shader_prog_link *program, *program2;
12081 struct glsl_ffp_destroy_ctx *ctx = param;
12082 const struct wined3d_gl_info *gl_info;
12084 gl_info = ctx->context_gl->gl_info;
12085 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12086 struct glsl_shader_prog_link, ps.shader_entry)
12088 delete_glsl_program_entry(ctx->priv, gl_info, program);
12090 GL_EXTCALL(glDeleteShader(shader->id));
12091 heap_free(shader);
12094 /* Context activation is done by the caller. */
12095 static void glsl_fragment_pipe_free(struct wined3d_device *device, struct wined3d_context *context)
12097 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12098 struct shader_glsl_priv *priv = device->fragment_priv;
12099 struct glsl_ffp_destroy_ctx ctx;
12101 ctx.priv = priv;
12102 ctx.context_gl = context_gl;
12103 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12106 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12107 const struct wined3d_state *state, DWORD state_id)
12109 context->last_was_pshader = use_ps(state);
12111 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12114 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12115 const struct wined3d_state *state, DWORD state_id)
12117 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12120 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12121 const struct wined3d_state *state, DWORD state_id)
12123 BOOL use_vshader = use_vs(state);
12124 enum fogsource new_source;
12125 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12126 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12128 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12130 if (!state->render_states[WINED3D_RS_FOGENABLE])
12131 return;
12133 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12135 if (use_vshader)
12136 new_source = FOGSOURCE_VS;
12137 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12138 new_source = FOGSOURCE_COORD;
12139 else
12140 new_source = FOGSOURCE_FFP;
12142 else
12144 new_source = FOGSOURCE_FFP;
12147 if (new_source != context->fog_source || fogstart == fogend)
12149 context->fog_source = new_source;
12150 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12154 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12155 const struct wined3d_state *state, DWORD state_id)
12157 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12159 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12160 if (!shader_glsl_full_ffp_varyings(gl_info))
12161 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12163 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12164 glsl_fragment_pipe_fog(context, state, state_id);
12167 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12168 const struct wined3d_state *state, DWORD state_id)
12170 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12172 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12173 if (!shader_glsl_full_ffp_varyings(gl_info))
12174 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12177 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12178 const struct wined3d_state *state, DWORD state_id)
12180 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12183 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12184 const struct wined3d_state *state, DWORD state_id)
12186 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12189 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12190 const struct wined3d_state *state, DWORD state_id)
12192 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12193 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12194 float ref = wined3d_alpha_ref(state);
12196 if (func)
12198 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12199 checkGLcall("glAlphaFunc");
12203 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12204 const struct wined3d_state *state, DWORD state_id)
12206 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12209 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12210 const struct wined3d_state *state, DWORD state_id)
12212 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12214 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12216 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12217 checkGLcall("glEnable(GL_ALPHA_TEST)");
12219 else
12221 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12222 checkGLcall("glDisable(GL_ALPHA_TEST)");
12226 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12227 const struct wined3d_state *state, DWORD state_id)
12229 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12232 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12233 const struct wined3d_state *state, DWORD state_id)
12235 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12238 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12239 const struct wined3d_state *state, DWORD state_id)
12241 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12244 static const struct wined3d_state_entry_template glsl_fragment_pipe_state_template[] =
12246 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12247 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12248 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12249 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12250 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12251 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12252 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12253 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12254 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12255 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12256 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12257 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12258 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12259 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12260 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12261 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12262 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12263 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12264 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12265 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12266 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12267 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12268 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12269 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12270 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12271 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12272 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12273 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12274 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12275 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12276 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12277 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12278 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12279 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12280 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12281 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12282 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12283 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12284 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12285 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12286 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12287 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12288 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12289 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12290 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12291 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12292 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12293 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12294 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12295 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12296 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12297 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12298 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12299 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12300 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12301 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12302 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12303 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12304 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12305 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12306 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12307 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12308 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12309 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12310 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12311 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12312 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12313 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12314 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12315 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12316 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12317 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12318 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12319 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12320 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12321 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12322 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12323 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12324 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12325 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12326 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12327 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12328 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12329 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12330 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12331 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12332 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12333 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12334 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12335 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12336 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12337 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12338 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12339 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12340 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12341 {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 },
12342 {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 },
12343 {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 },
12344 {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 },
12345 {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 },
12346 {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 },
12347 {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 },
12348 {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 },
12349 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12350 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12351 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12352 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12353 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12354 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12355 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12356 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12357 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12358 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12359 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12360 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12361 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12364 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12366 return TRUE;
12369 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12373 const struct wined3d_fragment_pipe_ops glsl_fragment_pipe =
12375 glsl_fragment_pipe_enable,
12376 glsl_fragment_pipe_get_caps,
12377 glsl_fragment_pipe_get_emul_mask,
12378 glsl_fragment_pipe_alloc,
12379 glsl_fragment_pipe_free,
12380 glsl_fragment_pipe_alloc_context_data,
12381 glsl_fragment_pipe_free_context_data,
12382 shader_glsl_color_fixup_supported,
12383 glsl_fragment_pipe_state_template,
12386 struct glsl_blitter_args
12388 GLenum texture_type;
12389 struct color_fixup_desc fixup;
12390 unsigned short use_colour_key;
12393 struct glsl_blitter_program
12395 struct wine_rb_entry entry;
12396 struct glsl_blitter_args args;
12397 GLuint id;
12400 struct wined3d_glsl_blitter
12402 struct wined3d_blitter blitter;
12403 struct wined3d_string_buffer_list string_buffers;
12404 struct wine_rb_tree programs;
12405 GLuint palette_texture;
12408 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12410 const struct glsl_blitter_args *a = key;
12411 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12413 return memcmp(a, b, sizeof(*a));
12416 /* Context activation is done by the caller. */
12417 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12419 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12420 struct wined3d_context_gl *context_gl = ctx;
12421 const struct wined3d_gl_info *gl_info;
12423 gl_info = context_gl->gl_info;
12424 GL_EXTCALL(glDeleteProgram(program->id));
12425 checkGLcall("glDeleteProgram()");
12426 heap_free(program);
12429 /* Context activation is done by the caller. */
12430 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12432 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12433 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12434 struct wined3d_glsl_blitter *glsl_blitter;
12435 struct wined3d_blitter *next;
12437 if ((next = blitter->next))
12438 next->ops->blitter_destroy(next, context);
12440 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12442 if (glsl_blitter->palette_texture)
12443 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12445 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context_gl);
12446 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12448 heap_free(glsl_blitter);
12451 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12452 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12453 const char *output, const char *tex_type, const char *swizzle)
12455 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12456 shader_addline(buffer, "\nvoid main()\n{\n");
12457 /* The alpha-component contains the palette index. */
12458 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12459 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12460 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12461 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12462 * the middle. */
12463 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12464 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12465 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12466 if (args->use_colour_key)
12467 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12468 shader_addline(buffer, "}\n");
12471 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12472 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12473 const char *tex_type)
12475 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12476 char chroma, luminance;
12477 const char *tex;
12479 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12480 * macropixel, giving effectively 16 bits per pixel. The color consists of
12481 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12482 * luminance values, one for each single pixel it contains, and one U and
12483 * one V value shared between both pixels.
12485 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12486 * component contains the luminance and alpha the chroma. With UYVY it is
12487 * vice versa. Thus take the format into account when generating the read
12488 * swizzles
12490 * Reading the Y value is straightforward - just sample the texture. The
12491 * hardware takes care of filtering in the horizontal and vertical
12492 * direction.
12494 * Reading the U and V values is harder. We have to avoid filtering
12495 * horizontally, because that would mix the U and V values of one pixel or
12496 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12497 * get an unfiltered read, regardless of the filtering setting. Vertical
12498 * filtering works automatically though - the U and V values of two rows
12499 * are mixed nicely.
12501 * Apart of avoiding filtering issues, the code has to know which value it
12502 * just read, and where it can find the other one. To determine this, it
12503 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12504 * accordingly.
12506 * Handling horizontal filtering of U and V values requires reading a 2nd
12507 * pair of pixels, extracting U and V and mixing them. This is not
12508 * implemented yet.
12510 * An alternative implementation idea is to load the texture as A8R8G8B8
12511 * texture, with width / 2. This way one read gives all 3 values, finding
12512 * U and V is easy in an unfiltered situation. Finding the luminance on
12513 * the other hand requires finding out if it is an odd or even pixel. The
12514 * real drawback of this approach is filtering. This would have to be
12515 * emulated completely in the shader, reading up two 2 packed pixels in up
12516 * to 2 rows and interpolating both horizontally and vertically. Beyond
12517 * that it would require adjustments to the texture handling code to deal
12518 * with the width scaling. */
12520 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12522 chroma = 'x';
12523 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12525 else
12527 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12528 luminance = 'x';
12531 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12533 /* First we have to read the chroma values. This means we need at least
12534 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12535 * unmodified chroma, we have to rid ourselves of the filtering when we
12536 * sample the texture. */
12537 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12538 /* We must not allow filtering between pixel x and x+1, this would mix U
12539 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12540 * center is at 0.5, so add 0.5. */
12541 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12542 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12544 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12545 * and 0.75 for the even and odd pixels respectively. */
12546 /* Put the value into either of the chroma values. */
12547 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12548 shader_addline(buffer, " if (even)\n");
12549 shader_addline(buffer, " chroma.y = luminance;\n");
12550 shader_addline(buffer, " else\n");
12551 shader_addline(buffer, " chroma.x = luminance;\n");
12553 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12554 * current one. Otherwise, sample the left pixel. */
12555 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12556 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12558 /* Put the value into the other chroma. */
12559 shader_addline(buffer, " if (even)\n");
12560 shader_addline(buffer, " chroma.x = luminance;\n");
12561 shader_addline(buffer, " else\n");
12562 shader_addline(buffer, " chroma.y = luminance;\n");
12564 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12565 * the current one and lerp the two U and V values. */
12567 /* This gives the correctly filtered luminance value. */
12568 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12571 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12572 const struct wined3d_gl_info *gl_info, const char *tex_type)
12574 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12575 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12577 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12578 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12579 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12580 * have only half the pitch of the luminance plane, the packing into the
12581 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12582 * luminance data it looks approximately like this:
12584 * +----------------------------------+----
12585 * | |
12586 * | |
12587 * | |
12588 * | |
12589 * | | 2
12590 * | LUMINANCE | -
12591 * | | 3
12592 * | |
12593 * | |
12594 * | |
12595 * | |
12596 * +----------------+-----------------+----
12597 * | | |
12598 * | V even rows | V odd rows |
12599 * | | | 1
12600 * +----------------+------------------ -
12601 * | | | 3
12602 * | U even rows | U odd rows |
12603 * | | |
12604 * +----------------+-----------------+----
12605 * | | |
12606 * | 0.5 | 0.5 |
12608 * So it appears as if there are 4 chroma images, but in fact the odd rows
12609 * in the chroma images are in the same row as the even ones. So it is
12610 * kinda tricky to read. */
12612 /* First sample the chroma values. */
12613 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12614 /* The chroma planes have only half the width. */
12615 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12617 /* The first value is between 2/3 and 5/6 of the texture's height, so
12618 * scale+bias the coordinate. Also read the right side of the image when
12619 * reading odd lines.
12621 * Don't forget to clamp the y values in into the range, otherwise we'll
12622 * get filtering bleeding. */
12624 /* Read odd lines from the right side (add 0.5 to the x coordinate). */
12625 shader_addline(buffer, " if (fract(floor(texcoord.y * size.y) * 0.5 + 1.0 / 6.0) >= 0.5)\n");
12626 shader_addline(buffer, " texcoord.x += 0.5;\n");
12628 /* Clamp, keep the half pixel origin in mind. */
12629 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12630 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12632 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12634 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12635 * 6/6th No need to clamp because we're just reusing the already clamped
12636 * value from above. */
12637 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12638 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12640 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12641 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12642 * values from bleeding into the sampled luminance values due to
12643 * filtering. */
12644 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12645 /* Multiply the y coordinate by 2/3 and clamp it. */
12646 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12647 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12650 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12651 const struct wined3d_gl_info *gl_info, const char *tex_type)
12653 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12654 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12656 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12657 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12658 * effective bitdepth is 12 bits per pixel. If the whole texture is
12659 * interpreted as luminance data it looks approximately like this:
12661 * +----------------------------------+----
12662 * | |
12663 * | |
12664 * | |
12665 * | |
12666 * | | 2
12667 * | LUMINANCE | -
12668 * | | 3
12669 * | |
12670 * | |
12671 * | |
12672 * | |
12673 * +----------------------------------+----
12674 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12675 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12676 * | | 1
12677 * | | -
12678 * | | 3
12679 * | |
12680 * | |
12681 * +----------------------------------+---- */
12683 /* First sample the chroma values. */
12684 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12685 /* We only have half the number of chroma pixels. */
12686 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12687 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12689 /* We must not allow filtering horizontally, this would mix U and V.
12690 * Vertical filtering is ok. However, bear in mind that the pixel center
12691 * is at 0.5, so add 0.5. */
12693 /* Convert to non-normalised coordinates so we can find the individual
12694 * pixel. */
12695 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12696 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12697 * 0.5 to hit the center of the pixel. Then convert back to normalised
12698 * coordinates. */
12699 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12700 /* Clamp, keep the half pixel origin in mind. */
12701 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12703 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12704 /* Add 1.0 / size.x to sample the adjacent texel. */
12705 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12706 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12708 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12709 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12710 * values from bleeding into the sampled luminance values due to
12711 * filtering. */
12712 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12713 /* Multiply the y coordinate by 2/3 and clamp it. */
12714 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12715 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12718 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12719 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12720 const char *output, const char *tex_type, const char *swizzle)
12722 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12724 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12725 shader_addline(buffer, "float luminance;\n");
12726 shader_addline(buffer, "vec2 texcoord;\n");
12727 shader_addline(buffer, "vec2 chroma;\n");
12728 shader_addline(buffer, "uniform vec2 size;\n");
12730 shader_addline(buffer, "\nvoid main()\n{\n");
12732 switch (complex_fixup)
12734 case COMPLEX_FIXUP_UYVY:
12735 case COMPLEX_FIXUP_YUY2:
12736 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12737 break;
12739 case COMPLEX_FIXUP_YV12:
12740 gen_yv12_read(buffer, gl_info, tex_type);
12741 break;
12743 case COMPLEX_FIXUP_NV12:
12744 gen_nv12_read(buffer, gl_info, tex_type);
12745 break;
12747 case COMPLEX_FIXUP_YUV:
12748 /* With APPLE_rgb_422, things are much simpler. The only thing we
12749 * have to do here is Y'CbCr to RGB conversion. */
12750 shader_addline(buffer, " vec3 yuv = vec3(texture%s(sampler, out_texcoord.xy));\n",
12751 needs_legacy_glsl_syntax(gl_info) ? tex_type : "");
12752 shader_addline(buffer, " luminance = yuv.y;\n");
12753 shader_addline(buffer, " chroma = yuv.xz;\n");
12754 break;
12756 default:
12757 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12758 string_buffer_free(buffer);
12759 return;
12762 /* Calculate the final result. Formula is taken from
12763 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12764 * ranges from -0.5 to 0.5. */
12765 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12767 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12768 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12769 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12770 if (args->use_colour_key)
12771 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12773 shader_addline(buffer, "}\n");
12776 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12777 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12778 const char *output, const char *tex_type, const char *swizzle)
12780 shader_addline(buffer, "\nvoid main()\n{\n");
12781 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12782 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12783 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12784 if (args->use_colour_key)
12785 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12786 shader_addline(buffer, "}\n");
12789 /* Context activation is done by the caller. */
12790 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12791 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12793 static const struct
12795 GLenum texture_target;
12796 const char texture_type[7];
12797 const char texcoord_swizzle[4];
12799 texture_data[] =
12801 {GL_TEXTURE_2D, "2D", "xy"},
12802 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12803 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12805 static const char vshader_main[] =
12806 "\n"
12807 "void main()\n"
12808 "{\n"
12809 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12810 " out_texcoord = texcoord;\n"
12811 "}\n";
12812 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12813 struct wined3d_string_buffer *buffer, *output;
12814 GLuint program, vshader_id, fshader_id;
12815 const char *tex_type = NULL, *swizzle = NULL, *ptr;
12816 unsigned int i;
12817 GLint loc;
12819 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12821 if (args->texture_type == texture_data[i].texture_target)
12823 tex_type = texture_data[i].texture_type;
12824 swizzle = texture_data[i].texcoord_swizzle;
12825 break;
12828 if (i == ARRAY_SIZE(texture_data))
12830 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12831 return 0;
12834 program = GL_EXTCALL(glCreateProgram());
12836 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12838 buffer = string_buffer_get(&blitter->string_buffers);
12839 shader_glsl_add_version_declaration(buffer, gl_info);
12840 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12841 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12842 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12843 shader_addline(buffer, vshader_main);
12845 ptr = buffer->buffer;
12846 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12847 GL_EXTCALL(glAttachShader(program, vshader_id));
12848 GL_EXTCALL(glDeleteShader(vshader_id));
12850 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12852 string_buffer_clear(buffer);
12853 shader_glsl_add_version_declaration(buffer, gl_info);
12854 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12855 if (args->use_colour_key)
12857 shader_addline(buffer, "uniform struct\n{\n");
12858 shader_addline(buffer, " vec4 low, high;\n");
12859 shader_addline(buffer, "} colour_key;\n");
12861 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12862 /* TODO: Declare the out variable with the correct type (and put it in the
12863 * blitter args). */
12864 if (!use_legacy_fragment_output(gl_info))
12865 shader_addline(buffer, "out vec4 ps_out[1];\n");
12867 output = string_buffer_get(&blitter->string_buffers);
12868 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12870 switch (complex_fixup)
12872 case COMPLEX_FIXUP_P8:
12873 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12874 break;
12875 case COMPLEX_FIXUP_YUY2:
12876 case COMPLEX_FIXUP_UYVY:
12877 case COMPLEX_FIXUP_YV12:
12878 case COMPLEX_FIXUP_NV12:
12879 case COMPLEX_FIXUP_YUV:
12880 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12881 break;
12882 case COMPLEX_FIXUP_NONE:
12883 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12886 string_buffer_release(&blitter->string_buffers, output);
12888 ptr = buffer->buffer;
12889 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12890 string_buffer_release(&blitter->string_buffers, buffer);
12891 GL_EXTCALL(glAttachShader(program, fshader_id));
12892 GL_EXTCALL(glDeleteShader(fshader_id));
12894 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12895 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12897 if (!use_legacy_fragment_output(gl_info))
12898 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12900 GL_EXTCALL(glCompileShader(vshader_id));
12901 print_glsl_info_log(gl_info, vshader_id, FALSE);
12902 GL_EXTCALL(glCompileShader(fshader_id));
12903 print_glsl_info_log(gl_info, fshader_id, FALSE);
12904 GL_EXTCALL(glLinkProgram(program));
12905 shader_glsl_validate_link(gl_info, program);
12907 GL_EXTCALL(glUseProgram(program));
12908 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12909 GL_EXTCALL(glUniform1i(loc, 0));
12910 if (complex_fixup == COMPLEX_FIXUP_P8)
12912 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12913 GL_EXTCALL(glUniform1i(loc, 1));
12916 return program;
12919 /* Context activation is done by the caller. */
12920 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12921 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl)
12923 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12924 const struct wined3d_palette *palette;
12926 palette = texture_gl->t.swapchain ? texture_gl->t.swapchain->palette : NULL;
12928 if (!blitter->palette_texture)
12929 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12931 wined3d_context_gl_active_texture(context_gl, gl_info, 1);
12932 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12933 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12934 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12935 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12937 if (palette)
12939 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12940 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12942 else
12944 static const DWORD black;
12946 FIXME("P8 texture loaded without a palette.\n");
12947 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
12948 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
12951 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
12954 /* Context activation is done by the caller. */
12955 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
12956 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl, BOOL use_colour_key)
12958 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12959 struct glsl_blitter_program *program;
12960 struct glsl_blitter_args args;
12961 struct wine_rb_entry *entry;
12963 memset(&args, 0, sizeof(args));
12964 args.texture_type = texture_gl->target;
12965 args.fixup = texture_gl->t.resource.format->color_fixup;
12966 args.use_colour_key = use_colour_key;
12968 if ((entry = wine_rb_get(&blitter->programs, &args)))
12969 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12971 if (!(program = heap_alloc(sizeof(*program))))
12973 ERR("Failed to allocate blitter program memory.\n");
12974 return NULL;
12977 program->args = args;
12978 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
12980 WARN("Failed to generate blitter program.\n");
12981 heap_free(program);
12982 return NULL;
12985 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
12987 ERR("Failed to store blitter program.\n");
12988 GL_EXTCALL(glDeleteProgram(program->id));
12989 heap_free(program);
12990 return NULL;
12993 return program;
12996 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
12997 const struct wined3d_texture_gl *src_texture, DWORD src_location,
12998 const struct wined3d_texture_gl *dst_texture, DWORD dst_location)
13000 const struct wined3d_resource *src_resource = &src_texture->t.resource;
13001 const struct wined3d_resource *dst_resource = &dst_texture->t.resource;
13002 const struct wined3d_format *src_format = src_resource->format;
13003 const struct wined3d_format *dst_format = dst_resource->format;
13004 bool src_ds, dst_ds;
13005 BOOL decompress;
13007 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13009 src_ds = src_format->depth_size || src_format->stencil_size;
13010 dst_ds = dst_format->depth_size || dst_format->stencil_size;
13011 /* We could in principle support raw depth/stencil <-> colour blits as
13012 * well in some cases, but note that typeless formats like e.g.
13013 * R16_TYPELESS may use a normalised GL format for depth/stencil
13014 * resources, and a floating-point format for colour resources, */
13015 if (src_ds && dst_ds)
13016 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13017 else if (!src_ds && !dst_ds)
13018 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13021 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY
13022 && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13024 TRACE("Unsupported blit_op %#x.\n", blit_op);
13025 return FALSE;
13028 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13029 return FALSE;
13031 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13032 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13034 TRACE("Multi-sample source textures not supported.\n");
13035 return FALSE;
13038 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13040 TRACE("Destination resource does not have GPU access.\n");
13041 return FALSE;
13044 /* We don't necessarily want to blit from resources without
13045 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13046 * compressed textures. */
13047 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
13048 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
13049 if (!decompress && !(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13051 TRACE("Source resource does not have GPU access.\n");
13052 return FALSE;
13055 if (!is_identity_fixup(dst_format->color_fixup)
13056 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13058 TRACE("Destination fixups are not supported.\n");
13059 return FALSE;
13062 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
13063 && !((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
13064 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
13066 TRACE("Destination texture is not FBO attachable.\n");
13067 return FALSE;
13070 TRACE("Returning supported.\n");
13071 return TRUE;
13074 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13075 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13076 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13077 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13078 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
13079 const struct wined3d_format *resolve_format)
13081 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
13082 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
13083 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
13084 struct wined3d_device *device = dst_texture->resource.device;
13085 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13086 struct wined3d_texture *staging_texture = NULL;
13087 struct wined3d_glsl_blitter *glsl_blitter;
13088 struct wined3d_color_key alpha_test_key;
13089 struct glsl_blitter_program *program;
13090 struct wined3d_blitter *next;
13091 unsigned int src_level;
13092 GLint location;
13093 RECT s, d;
13095 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
13096 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
13097 "colour_key %p, filter %s, resolve format %p.\n",
13098 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13099 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13100 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
13102 if (!glsl_blitter_supported(op, context, src_texture_gl, src_location, dst_texture_gl, dst_location))
13104 if (!(next = blitter->next))
13106 ERR("No blitter to handle blit op %#x.\n", op);
13107 return dst_location;
13110 TRACE("Forwarding to blitter %p.\n", next);
13111 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13112 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
13113 resolve_format);
13116 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13118 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13120 struct wined3d_resource_desc desc;
13121 struct wined3d_box upload_box;
13122 HRESULT hr;
13124 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13126 src_level = src_sub_resource_idx % src_texture->level_count;
13127 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13128 desc.format = src_texture->resource.format->id;
13129 desc.multisample_type = src_texture->resource.multisample_type;
13130 desc.multisample_quality = src_texture->resource.multisample_quality;
13131 desc.usage = WINED3DUSAGE_PRIVATE;
13132 desc.bind_flags = 0;
13133 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13134 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13135 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13136 desc.depth = 1;
13137 desc.size = 0;
13139 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13140 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13142 ERR("Failed to create staging texture, hr %#x.\n", hr);
13143 return dst_location;
13146 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13147 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13148 src_texture, src_sub_resource_idx, &upload_box);
13150 src_texture = staging_texture;
13151 src_texture_gl = wined3d_texture_gl(src_texture);
13152 src_sub_resource_idx = 0;
13154 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13155 && (src_texture->sub_resources[src_sub_resource_idx].locations
13156 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13157 && !wined3d_resource_is_offscreen(&src_texture->resource))
13160 /* Without FBO blits transferring from the drawable to the texture is
13161 * expensive, because we have to flip the data in sysmem. Since we can
13162 * flip in the blitter, we don't actually need that flip anyway. So we
13163 * use the surface's texture as scratch texture, and flip the source
13164 * rectangle instead. */
13165 texture2d_load_fb_texture(src_texture_gl, src_sub_resource_idx, FALSE, context);
13167 s = *src_rect;
13168 src_level = src_sub_resource_idx % src_texture->level_count;
13169 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13170 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13171 src_rect = &s;
13173 else
13175 wined3d_texture_load(src_texture, context, FALSE);
13178 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
13179 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13180 else
13181 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13183 wined3d_context_gl_apply_blit_state(context_gl, device);
13185 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13187 d = *dst_rect;
13188 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
13189 dst_rect = &d;
13192 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
13194 GLenum buffer;
13196 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13198 TRACE("Destination texture %p is onscreen.\n", dst_texture);
13199 buffer = wined3d_texture_get_gl_buffer(dst_texture);
13201 else
13203 TRACE("Destination texture %p is offscreen.\n", dst_texture);
13204 buffer = GL_COLOR_ATTACHMENT0;
13206 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
13207 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
13208 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
13209 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
13210 context_invalidate_state(context, STATE_FRAMEBUFFER);
13213 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13215 const struct wined3d_format *f = src_texture->resource.format;
13217 alpha_test_key.color_space_low_value = 0;
13218 alpha_test_key.color_space_high_value = ~(wined3d_mask_from_size(f->alpha_size) << f->alpha_offset);
13219 colour_key = &alpha_test_key;
13221 else if (op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY)
13223 colour_key = NULL;
13226 if (!(program = glsl_blitter_get_program(glsl_blitter, context_gl, src_texture_gl, !!colour_key)))
13228 ERR("Failed to get blitter program.\n");
13229 return dst_location;
13231 GL_EXTCALL(glUseProgram(program->id));
13232 switch (get_complex_fixup(program->args.fixup))
13234 case COMPLEX_FIXUP_P8:
13235 glsl_blitter_upload_palette(glsl_blitter, context_gl, src_texture_gl);
13236 break;
13238 case COMPLEX_FIXUP_YUY2:
13239 case COMPLEX_FIXUP_UYVY:
13240 case COMPLEX_FIXUP_YV12:
13241 case COMPLEX_FIXUP_NV12:
13242 case COMPLEX_FIXUP_YUV:
13243 src_level = src_sub_resource_idx % src_texture->level_count;
13244 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13245 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13246 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13247 break;
13249 default:
13250 break;
13252 if (colour_key)
13254 struct wined3d_color float_key[2];
13256 wined3d_format_get_float_color_key(src_texture->resource.format, colour_key, float_key);
13257 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.low"));
13258 GL_EXTCALL(glUniform4fv(location, 1, &float_key[0].r));
13259 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.high"));
13260 GL_EXTCALL(glUniform4fv(location, 1, &float_key[1].r));
13262 wined3d_context_gl_draw_shaded_quad(context_gl, src_texture_gl, src_sub_resource_idx, src_rect, dst_rect, filter);
13263 GL_EXTCALL(glUseProgram(0));
13265 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13266 gl_info->gl_ops.gl.p_glFlush();
13268 if (staging_texture)
13269 wined3d_texture_decref(staging_texture);
13271 return dst_location;
13274 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13275 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13276 const RECT *draw_rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
13278 struct wined3d_blitter *next;
13280 if ((next = blitter->next))
13281 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13282 clear_rects, draw_rect, flags, color, depth, stencil);
13285 static const struct wined3d_blitter_ops glsl_blitter_ops =
13287 glsl_blitter_destroy,
13288 glsl_blitter_clear,
13289 glsl_blitter_blit,
13292 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13293 const struct wined3d_device *device)
13295 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
13296 struct wined3d_glsl_blitter *blitter;
13298 if (device->shader_backend != &glsl_shader_backend)
13299 return NULL;
13301 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13302 return NULL;
13304 if (!(blitter = heap_alloc(sizeof(*blitter))))
13306 ERR("Failed to allocate blitter.\n");
13307 return NULL;
13310 TRACE("Created blitter %p.\n", blitter);
13312 blitter->blitter.ops = &glsl_blitter_ops;
13313 blitter->blitter.next = *next;
13314 string_buffer_list_init(&blitter->string_buffers);
13315 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13316 blitter->palette_texture = 0;
13317 *next = &blitter->blitter;
13319 return *next;