directmanipulation: Return S_OK form viewport_SetViewportOptions stub.
[wine/zf.git] / dlls / wined3d / glsl_shader.c
blob7423b2003a6aa2133b5fae9f898e923cb4f9ae6f
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
32 #include "config.h"
33 #include "wine/port.h"
35 #include <limits.h>
36 #include <stdio.h>
38 #include "wined3d_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
41 WINE_DECLARE_DEBUG_CHANNEL(d3d);
42 WINE_DECLARE_DEBUG_CHANNEL(winediag);
44 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
45 #define WINED3D_GLSL_SAMPLE_LOD 0x02
46 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
47 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
48 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
50 static const struct
52 unsigned int coord_size;
53 unsigned int resinfo_size;
54 const char *type_part;
56 resource_type_info[] =
58 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
59 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
60 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
61 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
62 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
63 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
64 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
66 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
67 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
68 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
71 static const struct
73 enum wined3d_data_type data_type;
74 const char *glsl_scalar_type;
75 const char *glsl_vector_type;
77 component_type_info[] =
79 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_UNKNOWN */
80 {WINED3D_DATA_UINT, "uint", "uvec"}, /* WINED3D_TYPE_UINT */
81 {WINED3D_DATA_INT, "int", "ivec"}, /* WINED3D_TYPE_INT */
82 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_FLOAT */
85 struct glsl_dst_param
87 char reg_name[150];
88 char mask_str[6];
91 struct glsl_src_param
93 char param_str[200];
96 struct glsl_sample_function
98 struct wined3d_string_buffer *name;
99 unsigned int coord_mask;
100 unsigned int deriv_mask;
101 enum wined3d_data_type data_type;
102 BOOL output_single_component;
103 unsigned int offset_size;
106 enum heap_node_op
108 HEAP_NODE_TRAVERSE_LEFT,
109 HEAP_NODE_TRAVERSE_RIGHT,
110 HEAP_NODE_POP,
113 struct constant_entry
115 unsigned int idx;
116 unsigned int version;
119 struct constant_heap
121 struct constant_entry *entries;
122 BOOL *contained;
123 unsigned int *positions;
124 unsigned int size;
127 /* GLSL shader private data */
128 struct shader_glsl_priv
130 struct wined3d_string_buffer shader_buffer;
131 struct wined3d_string_buffer_list string_buffers;
132 struct wine_rb_tree program_lookup;
133 struct constant_heap vconst_heap;
134 struct constant_heap pconst_heap;
135 unsigned char *stack;
136 UINT next_constant_version;
138 const struct wined3d_vertex_pipe_ops *vertex_pipe;
139 const struct wined3d_fragment_pipe_ops *fragment_pipe;
140 struct wine_rb_tree ffp_vertex_shaders;
141 struct wine_rb_tree ffp_fragment_shaders;
142 BOOL ffp_proj_control;
143 BOOL legacy_lighting;
146 struct glsl_vs_program
148 struct list shader_entry;
149 GLuint id;
150 GLenum vertex_color_clamp;
151 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
152 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
153 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
154 GLint pos_fixup_location;
155 GLint base_vertex_id_location;
157 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
158 GLint projection_matrix_location;
159 GLint normal_matrix_location;
160 GLint texture_matrix_location[WINED3D_MAX_TEXTURES];
161 GLint material_ambient_location;
162 GLint material_diffuse_location;
163 GLint material_specular_location;
164 GLint material_emissive_location;
165 GLint material_shininess_location;
166 GLint light_ambient_location;
167 struct
169 GLint diffuse;
170 GLint specular;
171 GLint ambient;
172 GLint position;
173 GLint direction;
174 GLint range;
175 GLint falloff;
176 GLint c_att;
177 GLint l_att;
178 GLint q_att;
179 GLint cos_htheta;
180 GLint cos_hphi;
181 } light_location[WINED3D_MAX_ACTIVE_LIGHTS];
182 GLint pointsize_location;
183 GLint pointsize_min_location;
184 GLint pointsize_max_location;
185 GLint pointsize_c_att_location;
186 GLint pointsize_l_att_location;
187 GLint pointsize_q_att_location;
188 GLint clip_planes_location;
191 struct glsl_hs_program
193 struct list shader_entry;
194 GLuint id;
197 struct glsl_ds_program
199 struct list shader_entry;
200 GLuint id;
202 GLint pos_fixup_location;
205 struct glsl_gs_program
207 struct list shader_entry;
208 GLuint id;
210 GLint pos_fixup_location;
213 struct glsl_ps_program
215 struct list shader_entry;
216 GLuint id;
217 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
218 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
219 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
220 GLint bumpenv_mat_location[WINED3D_MAX_TEXTURES];
221 GLint bumpenv_lum_scale_location[WINED3D_MAX_TEXTURES];
222 GLint bumpenv_lum_offset_location[WINED3D_MAX_TEXTURES];
223 GLint tss_constant_location[WINED3D_MAX_TEXTURES];
224 GLint tex_factor_location;
225 GLint specular_enable_location;
226 GLint fog_color_location;
227 GLint fog_density_location;
228 GLint fog_end_location;
229 GLint fog_scale_location;
230 GLint alpha_test_ref_location;
231 GLint ycorrection_location;
232 GLint np2_fixup_location;
233 GLint color_key_location;
234 const struct ps_np2fixup_info *np2_fixup_info;
237 struct glsl_cs_program
239 struct list shader_entry;
240 GLuint id;
243 /* Struct to maintain data about a linked GLSL program */
244 struct glsl_shader_prog_link
246 struct wine_rb_entry program_lookup_entry;
247 struct glsl_vs_program vs;
248 struct glsl_hs_program hs;
249 struct glsl_ds_program ds;
250 struct glsl_gs_program gs;
251 struct glsl_ps_program ps;
252 struct glsl_cs_program cs;
253 GLuint id;
254 DWORD constant_update_mask;
255 unsigned int constant_version;
256 DWORD shader_controlled_clip_distances : 1;
257 DWORD clip_distance_mask : 8; /* WINED3D_MAX_CLIP_DISTANCES, 8 */
258 DWORD padding : 23;
261 struct glsl_program_key
263 GLuint vs_id;
264 GLuint hs_id;
265 GLuint ds_id;
266 GLuint gs_id;
267 GLuint ps_id;
268 GLuint cs_id;
271 struct shader_glsl_ctx_priv
273 const struct wined3d_gl_info *gl_info;
274 const struct vs_compile_args *cur_vs_args;
275 const struct ds_compile_args *cur_ds_args;
276 const struct ps_compile_args *cur_ps_args;
277 struct ps_np2fixup_info *cur_np2fixup_info;
278 struct wined3d_string_buffer_list *string_buffers;
281 struct glsl_context_data
283 struct glsl_shader_prog_link *glsl_program;
284 GLenum vertex_color_clamp;
285 BOOL rasterization_disabled;
288 struct glsl_ps_compiled_shader
290 struct ps_compile_args args;
291 struct ps_np2fixup_info np2fixup;
292 GLuint id;
295 struct glsl_vs_compiled_shader
297 struct vs_compile_args args;
298 GLuint id;
301 struct glsl_hs_compiled_shader
303 GLuint id;
306 struct glsl_ds_compiled_shader
308 struct ds_compile_args args;
309 GLuint id;
312 struct glsl_gs_compiled_shader
314 struct gs_compile_args args;
315 GLuint id;
318 struct glsl_cs_compiled_shader
320 GLuint id;
323 struct glsl_shader_private
325 union
327 struct glsl_vs_compiled_shader *vs;
328 struct glsl_hs_compiled_shader *hs;
329 struct glsl_ds_compiled_shader *ds;
330 struct glsl_gs_compiled_shader *gs;
331 struct glsl_ps_compiled_shader *ps;
332 struct glsl_cs_compiled_shader *cs;
333 } gl_shaders;
334 unsigned int num_gl_shaders, shader_array_size;
337 struct glsl_ffp_vertex_shader
339 struct wined3d_ffp_vs_desc desc;
340 GLuint id;
341 struct list linked_programs;
344 struct glsl_ffp_fragment_shader
346 struct ffp_frag_desc entry;
347 GLuint id;
348 struct list linked_programs;
351 struct glsl_ffp_destroy_ctx
353 struct shader_glsl_priv *priv;
354 const struct wined3d_context_gl *context_gl;
357 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
359 static const char *debug_gl_shader_type(GLenum type)
361 switch (type)
363 #define WINED3D_TO_STR(u) case u: return #u
364 WINED3D_TO_STR(GL_VERTEX_SHADER);
365 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
366 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
367 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
368 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
369 WINED3D_TO_STR(GL_COMPUTE_SHADER);
370 #undef WINED3D_TO_STR
371 default:
372 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
376 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
378 switch (type)
380 case WINED3D_SHADER_TYPE_VERTEX:
381 return "vs";
383 case WINED3D_SHADER_TYPE_HULL:
384 return "hs";
386 case WINED3D_SHADER_TYPE_DOMAIN:
387 return "ds";
389 case WINED3D_SHADER_TYPE_GEOMETRY:
390 return "gs";
392 case WINED3D_SHADER_TYPE_PIXEL:
393 return "ps";
395 case WINED3D_SHADER_TYPE_COMPUTE:
396 return "cs";
398 default:
399 FIXME("Unhandled shader type %#x.\n", type);
400 return "unknown";
404 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
406 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
407 return 440;
408 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
409 return 150;
410 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
411 return 130;
412 else
413 return 120;
416 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
417 const struct wined3d_gl_info *gl_info)
419 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
422 unsigned int shader_glsl_full_ffp_varyings(const struct wined3d_gl_info *gl_info)
424 /* On core profile we have to also count diffuse and specular colours and
425 * the fog coordinate. */
426 return gl_info->limits.glsl_varyings >= (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
427 ? WINED3D_MAX_TEXTURES * 4 : (WINED3D_MAX_TEXTURES + 2) * 4 + 1);
430 static void shader_glsl_append_imm_vec(struct wined3d_string_buffer *buffer,
431 const float *values, unsigned int size, const struct wined3d_gl_info *gl_info)
433 const int *int_values = (const int *)values;
434 unsigned int i;
435 char str[17];
437 if (!gl_info->supported[ARB_SHADER_BIT_ENCODING])
439 if (size > 1)
440 shader_addline(buffer, "vec%u(", size);
442 for (i = 0; i < size; ++i)
444 wined3d_ftoa(values[i], str);
445 shader_addline(buffer, i ? ", %s" : "%s", str);
448 if (size > 1)
449 shader_addline(buffer, ")");
451 return;
454 shader_addline(buffer, "intBitsToFloat(");
455 if (size > 1)
456 shader_addline(buffer, "ivec%u(", size);
458 for (i = 0; i < size; ++i)
460 wined3d_ftoa(values[i], str);
461 shader_addline(buffer, i ? ", %#x /* %s */" : "%#x /* %s */", int_values[i], str);
464 if (size > 1)
465 shader_addline(buffer, ")");
466 shader_addline(buffer, ")");
469 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
470 const int *values, unsigned int size)
472 int i;
474 if (!size || size > 4)
476 ERR("Invalid vector size %u.\n", size);
477 return;
480 if (size > 1)
481 shader_addline(buffer, "ivec%u(", size);
483 for (i = 0; i < size; ++i)
484 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
486 if (size > 1)
487 shader_addline(buffer, ")");
490 static const char *get_info_log_line(const char **ptr)
492 const char *p, *q;
494 p = *ptr;
495 if (!(q = strstr(p, "\n")))
497 if (!*p) return NULL;
498 *ptr += strlen(p);
499 return p;
501 *ptr = q + 1;
503 return p;
506 /* Context activation is done by the caller. */
507 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
509 int length = 0;
510 char *log;
512 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
513 return;
515 if (program)
516 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
517 else
518 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
520 /* A size of 1 is just a null-terminated string, so the log should be bigger than
521 * that if there are errors. */
522 if (length > 1)
524 const char *ptr, *line;
526 log = heap_alloc(length);
527 /* The info log is supposed to be zero-terminated, but at least some
528 * versions of fglrx don't terminate the string properly. The reported
529 * length does include the terminator, so explicitly set it to zero
530 * here. */
531 log[length - 1] = 0;
532 if (program)
533 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
534 else
535 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
537 ptr = log;
538 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
540 WARN("Info log received from GLSL shader #%u:\n", id);
541 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
543 else
545 FIXME("Info log received from GLSL shader #%u:\n", id);
546 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
548 heap_free(log);
552 /* Context activation is done by the caller. */
553 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
555 const char *ptr, *line;
557 TRACE("Compiling shader object %u.\n", shader);
559 if (TRACE_ON(d3d_shader))
561 ptr = src;
562 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
565 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
566 checkGLcall("glShaderSource");
567 GL_EXTCALL(glCompileShader(shader));
568 checkGLcall("glCompileShader");
569 print_glsl_info_log(gl_info, shader, FALSE);
572 /* Context activation is done by the caller. */
573 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
575 GLint i, shader_count, source_size = -1;
576 GLuint *shaders;
577 char *source = NULL;
579 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
580 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
582 ERR("Failed to allocate shader array memory.\n");
583 return;
586 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
587 for (i = 0; i < shader_count; ++i)
589 const char *ptr, *line;
590 GLint tmp;
592 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
594 if (source_size < tmp)
596 heap_free(source);
598 if (!(source = heap_alloc_zero(tmp)))
600 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
601 heap_free(shaders);
602 return;
604 source_size = tmp;
607 FIXME("Shader %u:\n", shaders[i]);
608 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
609 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
610 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
611 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
612 FIXME("\n");
614 ptr = source;
615 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
616 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
617 FIXME("\n");
620 heap_free(source);
621 heap_free(shaders);
624 /* Context activation is done by the caller. */
625 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
627 GLint tmp;
629 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
630 return;
632 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
633 if (!tmp)
635 FIXME("Program %u link status invalid.\n", program);
636 shader_glsl_dump_program_source(gl_info, program);
639 print_glsl_info_log(gl_info, program, TRUE);
642 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
644 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
645 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
646 * versions. */
647 return shader_glsl_get_version(gl_info) >= 140;
650 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
652 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
655 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
656 struct shader_glsl_priv *priv, GLuint program_id,
657 const struct wined3d_shader_reg_maps *reg_maps)
659 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
660 struct wined3d_string_buffer *name;
661 unsigned int i, base, count;
662 GLuint block_idx;
664 if (shader_glsl_use_layout_binding_qualifier(gl_info))
665 return;
667 name = string_buffer_get(&priv->string_buffers);
668 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
669 for (i = 0; i < count; ++i)
671 if (!reg_maps->cb_sizes[i])
672 continue;
674 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
675 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
676 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
678 checkGLcall("glUniformBlockBinding");
679 string_buffer_release(&priv->string_buffers, name);
682 /* Context activation is done by the caller. */
683 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
684 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
685 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
687 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
688 unsigned int i, mapped_unit;
689 GLint name_loc;
691 for (i = 0; i < count; ++i)
693 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
694 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
695 if (name_loc == -1)
696 continue;
698 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
699 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
701 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
702 continue;
705 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
706 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
708 checkGLcall("Load sampler bindings");
709 string_buffer_release(&priv->string_buffers, sampler_name);
712 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
713 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
715 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
716 const unsigned int *tex_unit_map;
717 unsigned int base, count;
719 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
720 if (sampler_idx >= count)
721 return WINED3D_UNMAPPED_STAGE;
722 if (!tex_unit_map)
723 return base + sampler_idx;
724 return tex_unit_map[base + sampler_idx];
727 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
728 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
729 unsigned int sampler_idx)
731 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
732 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
733 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
734 else
735 ERR("Unmapped sampler %u.\n", sampler_idx);
738 /* Context activation is done by the caller. */
739 static void shader_glsl_load_samplers(const struct wined3d_context *context,
740 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
742 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
743 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
744 const struct wined3d_shader_version *shader_version;
745 const unsigned int *tex_unit_map;
746 unsigned int base, count;
747 const char *prefix;
749 if (shader_glsl_use_layout_binding_qualifier(gl_info))
750 return;
752 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
753 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
754 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
755 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
758 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
759 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
761 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
763 if (icb)
765 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
766 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
767 GLint icb_location;
769 string_buffer_sprintf(icb_name, "%s_icb", prefix);
770 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
771 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
772 checkGLcall("Load immediate constant buffer");
774 string_buffer_release(&priv->string_buffers, icb_name);
778 /* Context activation is done by the caller. */
779 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
780 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
782 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
783 struct wined3d_string_buffer *name;
784 GLint location;
785 unsigned int i;
787 if (shader_glsl_use_layout_binding_qualifier(gl_info))
788 return;
790 name = string_buffer_get(&priv->string_buffers);
791 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
793 if (!reg_maps->uav_resource_info[i].type)
794 continue;
796 string_buffer_sprintf(name, "%s_image%u", prefix, i);
797 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
798 if (location == -1)
799 continue;
801 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
802 GL_EXTCALL(glUniform1i(location, i));
804 checkGLcall("Load image bindings");
805 string_buffer_release(&priv->string_buffers, name);
808 /* Context activation is done by the caller. */
809 static void shader_glsl_load_program_resources(const struct wined3d_context_gl *context_gl,
810 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
812 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
814 shader_glsl_init_uniform_block_bindings(context_gl->gl_info, priv, program_id, reg_maps);
815 shader_glsl_load_icb(context_gl->gl_info, priv, program_id, reg_maps);
816 /* Texture unit mapping is set up to be the same each time the shader
817 * program is used so we can hardcode the sampler uniform values. */
818 shader_glsl_load_samplers(&context_gl->c, priv, program_id, reg_maps);
821 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
822 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
824 if (varyings && *strings)
826 char *ptr = *strings;
828 varyings[*varying_count] = ptr;
830 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
831 ptr += buffer->content_size + 1;
833 *strings = ptr;
836 *strings_length += buffer->content_size + 1;
837 ++(*varying_count);
840 static void append_transform_feedback_skip_components(const char **varyings,
841 unsigned int *varying_count, char **strings, unsigned int *strings_length,
842 struct wined3d_string_buffer *buffer, unsigned int component_count)
844 unsigned int j;
846 for (j = 0; j < component_count / 4; ++j)
848 string_buffer_sprintf(buffer, "gl_SkipComponents4");
849 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
851 if (component_count % 4)
853 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
854 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
858 static BOOL shader_glsl_generate_transform_feedback_varyings(struct wined3d_string_buffer *buffer,
859 const char **varyings, unsigned int *varying_count, char *strings, unsigned int *strings_length,
860 GLenum buffer_mode, struct wined3d_shader *shader)
862 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
863 unsigned int buffer_idx, count, length, highest_output_slot, stride;
864 unsigned int i, register_idx, component_idx;
865 BOOL have_varyings_to_record = FALSE;
867 count = length = 0;
868 highest_output_slot = 0;
869 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
871 stride = 0;
873 for (i = 0; i < so_desc->element_count; ++i)
875 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
877 highest_output_slot = max(highest_output_slot, e->output_slot);
878 if (e->output_slot != buffer_idx)
879 continue;
881 if (e->stream_idx)
883 FIXME("Unhandled stream %u.\n", e->stream_idx);
884 continue;
887 stride += e->component_count;
889 if (!e->semantic_name)
891 append_transform_feedback_skip_components(varyings, &count,
892 &strings, &length, buffer, e->component_count);
893 continue;
896 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
897 continue;
899 if (component_idx || e->component_count != 4)
901 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
903 FIXME("Unsupported component range %u-%u.\n", component_idx, e->component_count);
904 append_transform_feedback_skip_components(varyings, &count,
905 &strings, &length, buffer, e->component_count);
906 continue;
909 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
910 register_idx, component_idx, component_idx + e->component_count - 1);
911 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
913 else
915 string_buffer_sprintf(buffer, "shader_in_out.reg%u", register_idx);
916 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
919 have_varyings_to_record = TRUE;
922 if (buffer_idx < so_desc->buffer_stride_count
923 && stride < so_desc->buffer_strides[buffer_idx] / 4)
925 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
926 append_transform_feedback_skip_components(varyings, &count,
927 &strings, &length, buffer, component_count);
930 if (highest_output_slot <= buffer_idx)
931 break;
933 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
935 string_buffer_sprintf(buffer, "gl_NextBuffer");
936 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
940 if (varying_count)
941 *varying_count = count;
942 if (strings_length)
943 *strings_length = length;
945 return have_varyings_to_record;
948 static void shader_glsl_init_transform_feedback(const struct wined3d_context_gl *context_gl,
949 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
951 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
952 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
953 struct wined3d_string_buffer *buffer;
954 unsigned int i, count, length;
955 const char **varyings;
956 char *strings;
957 GLenum mode;
959 if (!so_desc)
960 return;
962 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
964 mode = GL_INTERLEAVED_ATTRIBS;
966 else
968 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
970 for (i = 0; i < so_desc->element_count; ++i)
972 if (!so_desc->elements[i].semantic_name)
974 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
975 return;
977 ++element_count[so_desc->elements[i].output_slot];
980 if (element_count[0] == so_desc->element_count)
982 mode = GL_INTERLEAVED_ATTRIBS;
984 else
986 mode = GL_SEPARATE_ATTRIBS;
987 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
989 if (element_count[i] != 1)
990 break;
992 for (; i < ARRAY_SIZE(element_count); ++i)
994 if (element_count[i])
996 FIXME("Only single element per buffer is allowed in separate mode.\n");
997 return;
1003 buffer = string_buffer_get(&priv->string_buffers);
1005 if (!shader_glsl_generate_transform_feedback_varyings(buffer, NULL, &count, NULL, &length, mode, shader))
1007 FIXME("No varyings to record, disabling transform feedback.\n");
1008 shader->u.gs.so_desc = NULL;
1009 string_buffer_release(&priv->string_buffers, buffer);
1010 return;
1013 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
1015 ERR("Out of memory.\n");
1016 string_buffer_release(&priv->string_buffers, buffer);
1017 return;
1019 if (!(strings = heap_calloc(length, sizeof(*strings))))
1021 ERR("Out of memory.\n");
1022 heap_free(varyings);
1023 string_buffer_release(&priv->string_buffers, buffer);
1024 return;
1027 shader_glsl_generate_transform_feedback_varyings(buffer, varyings, NULL, strings, NULL, mode, shader);
1028 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
1029 checkGLcall("glTransformFeedbackVaryings");
1031 heap_free(varyings);
1032 heap_free(strings);
1033 string_buffer_release(&priv->string_buffers, buffer);
1036 /* Context activation is done by the caller. */
1037 static void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
1038 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
1040 unsigned int start = ~0U, end = 0;
1041 int stack_idx = 0;
1042 unsigned int heap_idx = 1;
1043 unsigned int idx;
1045 if (heap->entries[heap_idx].version <= version) return;
1047 idx = heap->entries[heap_idx].idx;
1048 if (constant_locations[idx] != -1)
1049 start = end = idx;
1050 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1052 while (stack_idx >= 0)
1054 /* Note that we fall through to the next case statement. */
1055 switch(stack[stack_idx])
1057 case HEAP_NODE_TRAVERSE_LEFT:
1059 unsigned int left_idx = heap_idx << 1;
1060 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1062 heap_idx = left_idx;
1063 idx = heap->entries[heap_idx].idx;
1064 if (constant_locations[idx] != -1)
1066 if (start > idx)
1067 start = idx;
1068 if (end < idx)
1069 end = idx;
1072 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1073 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1074 break;
1078 case HEAP_NODE_TRAVERSE_RIGHT:
1080 unsigned int right_idx = (heap_idx << 1) + 1;
1081 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1083 heap_idx = right_idx;
1084 idx = heap->entries[heap_idx].idx;
1085 if (constant_locations[idx] != -1)
1087 if (start > idx)
1088 start = idx;
1089 if (end < idx)
1090 end = idx;
1093 stack[stack_idx++] = HEAP_NODE_POP;
1094 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1095 break;
1099 case HEAP_NODE_POP:
1100 heap_idx >>= 1;
1101 --stack_idx;
1102 break;
1105 if (start <= end)
1106 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1107 checkGLcall("walk_constant_heap()");
1110 /* Context activation is done by the caller. */
1111 static void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1112 GLint location, const struct wined3d_vec4 *data)
1114 GLfloat clamped_constant[4];
1116 if (location == -1) return;
1118 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1119 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1120 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1121 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1123 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1126 /* Context activation is done by the caller. */
1127 static void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1128 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1129 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1131 int stack_idx = 0;
1132 unsigned int heap_idx = 1;
1133 unsigned int idx;
1135 if (heap->entries[heap_idx].version <= version) return;
1137 idx = heap->entries[heap_idx].idx;
1138 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1139 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1141 while (stack_idx >= 0)
1143 /* Note that we fall through to the next case statement. */
1144 switch(stack[stack_idx])
1146 case HEAP_NODE_TRAVERSE_LEFT:
1148 unsigned int left_idx = heap_idx << 1;
1149 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1151 heap_idx = left_idx;
1152 idx = heap->entries[heap_idx].idx;
1153 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1155 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1156 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1157 break;
1161 case HEAP_NODE_TRAVERSE_RIGHT:
1163 unsigned int right_idx = (heap_idx << 1) + 1;
1164 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1166 heap_idx = right_idx;
1167 idx = heap->entries[heap_idx].idx;
1168 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1170 stack[stack_idx++] = HEAP_NODE_POP;
1171 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1172 break;
1176 case HEAP_NODE_POP:
1177 heap_idx >>= 1;
1178 --stack_idx;
1179 break;
1182 checkGLcall("walk_constant_heap_clamped()");
1185 /* Context activation is done by the caller. */
1186 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1187 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1188 unsigned char *stack, unsigned int version)
1190 const struct wined3d_shader_lconst *lconst;
1192 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1193 if (shader->reg_maps.shader_version.major == 1
1194 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1195 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1196 else
1197 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1199 if (!shader->load_local_constsF)
1201 TRACE("No need to load local float constants for this shader.\n");
1202 return;
1205 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1206 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1208 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1210 checkGLcall("glUniform4fv()");
1213 /* Context activation is done by the caller. */
1214 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1215 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1217 unsigned int i;
1218 struct list* ptr;
1220 for (i = 0; constants_set; constants_set >>= 1, ++i)
1222 if (!(constants_set & 1)) continue;
1224 /* We found this uniform name in the program - go ahead and send the data */
1225 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1228 /* Load immediate constants */
1229 ptr = list_head(&shader->constantsI);
1230 while (ptr)
1232 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1233 unsigned int idx = lconst->idx;
1234 const GLint *values = (const GLint *)lconst->value;
1236 /* We found this uniform name in the program - go ahead and send the data */
1237 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1238 ptr = list_next(&shader->constantsI, ptr);
1240 checkGLcall("glUniform4iv()");
1243 /* Context activation is done by the caller. */
1244 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1245 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1247 unsigned int i;
1248 struct list* ptr;
1250 for (i = 0; constants_set; constants_set >>= 1, ++i)
1252 if (!(constants_set & 1)) continue;
1254 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1257 /* Load immediate constants */
1258 ptr = list_head(&shader->constantsB);
1259 while (ptr)
1261 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1262 unsigned int idx = lconst->idx;
1263 const GLint *values = (const GLint *)lconst->value;
1265 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1266 ptr = list_next(&shader->constantsB, ptr);
1268 checkGLcall("glUniform1iv()");
1271 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1273 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1276 /* Context activation is done by the caller (state handler). */
1277 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1278 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1280 struct
1282 float sx, sy;
1284 np2fixup_constants[WINED3D_MAX_FRAGMENT_SAMPLERS];
1285 UINT fixup = ps->np2_fixup_info->active;
1286 UINT i;
1288 for (i = 0; fixup; fixup >>= 1, ++i)
1290 const struct wined3d_texture *tex = state->textures[i];
1291 unsigned char idx = ps->np2_fixup_info->idx[i];
1293 if (!tex)
1295 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1296 continue;
1299 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1300 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1303 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1306 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1308 struct wined3d_matrix temp;
1309 unsigned int i, j;
1311 for (i = 0; i < 4; ++i)
1312 for (j = 0; j < 4; ++j)
1313 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1315 *out = temp;
1318 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context_gl *context_gl,
1319 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1321 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1322 struct wined3d_matrix mv;
1323 float mat[3 * 3];
1325 if (prog->vs.normal_matrix_location == -1)
1326 return;
1328 get_modelview_matrix(&context_gl->c, state, 0, &mv);
1329 compute_normal_matrix(mat, context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING, &mv);
1331 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1332 checkGLcall("glUniformMatrix3fv");
1335 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context_gl *context_gl,
1336 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1338 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1339 struct wined3d_matrix mat;
1341 if (tex >= WINED3D_MAX_TEXTURES)
1342 return;
1343 if (prog->vs.texture_matrix_location[tex] == -1)
1344 return;
1346 get_texture_matrix(&context_gl->c, state, tex, &mat);
1347 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1348 checkGLcall("glUniformMatrix4fv");
1351 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context_gl *context_gl,
1352 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1354 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1356 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1358 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1359 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1361 else
1363 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1365 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1367 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1368 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1369 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1370 checkGLcall("setting FFP material uniforms");
1373 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context_gl *context_gl,
1374 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1376 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1377 struct wined3d_color color;
1379 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1380 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1381 checkGLcall("glUniform3fv");
1384 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context_gl *context_gl,
1385 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1386 struct glsl_shader_prog_link *prog)
1388 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1389 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1390 struct wined3d_vec4 vec4;
1392 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1393 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1394 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1396 switch (light_info->OriginalParms.type)
1398 case WINED3D_LIGHT_POINT:
1399 wined3d_vec4_transform(&vec4, &light_info->position, view);
1400 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1401 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1402 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1403 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1404 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1405 break;
1407 case WINED3D_LIGHT_SPOT:
1408 wined3d_vec4_transform(&vec4, &light_info->position, view);
1409 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1411 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1412 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1414 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1415 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1416 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1417 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1418 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1419 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1420 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1421 break;
1423 case WINED3D_LIGHT_DIRECTIONAL:
1424 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1425 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1426 break;
1428 case WINED3D_LIGHT_PARALLELPOINT:
1429 wined3d_vec4_transform(&vec4, &light_info->position, view);
1430 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1431 break;
1433 default:
1434 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1436 checkGLcall("setting FFP lights uniforms");
1439 static void shader_glsl_pointsize_uniform(const struct wined3d_context_gl *context_gl,
1440 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1442 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1443 float size, att[3];
1444 float min, max;
1446 get_pointsize_minmax(&context_gl->c, state, &min, &max);
1448 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1449 checkGLcall("glUniform1f");
1450 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1451 checkGLcall("glUniform1f");
1453 get_pointsize(&context_gl->c, state, &size, att);
1455 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1456 checkGLcall("glUniform1f");
1457 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1458 checkGLcall("glUniform1f");
1459 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1460 checkGLcall("glUniform1f");
1461 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1462 checkGLcall("glUniform1f");
1465 static void shader_glsl_load_fog_uniform(const struct wined3d_context_gl *context_gl,
1466 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1468 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1469 struct wined3d_color color;
1470 float start, end, scale;
1471 union
1473 DWORD d;
1474 float f;
1475 } tmpvalue;
1477 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1478 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1479 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1480 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1481 get_fog_start_end(&context_gl->c, state, &start, &end);
1482 scale = 1.0f / (end - start);
1483 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1484 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1485 checkGLcall("fog emulation uniforms");
1488 static void shader_glsl_clip_plane_uniform(const struct wined3d_context_gl *context_gl,
1489 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1491 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1492 struct wined3d_matrix matrix;
1493 struct wined3d_vec4 plane;
1495 plane = state->clip_planes[index];
1497 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1498 if (!use_vs(state))
1500 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1501 transpose_matrix(&matrix, &matrix);
1502 wined3d_vec4_transform(&plane, &plane, &matrix);
1505 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1508 /* Context activation is done by the caller (state handler). */
1509 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1510 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1512 struct wined3d_color float_key[2];
1513 const struct wined3d_texture *texture = state->textures[0];
1515 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1516 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1519 /* Context activation is done by the caller (state handler). */
1520 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1521 const struct wined3d_state *state)
1523 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1524 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1525 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1526 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1527 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1528 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1529 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1530 struct shader_glsl_priv *priv = shader_priv;
1531 unsigned int constant_version;
1532 DWORD update_mask;
1533 int i;
1535 /* No GLSL program set - nothing to do. */
1536 if (!prog)
1537 return;
1539 constant_version = prog->constant_version;
1540 update_mask = context->constant_update_mask & prog->constant_update_mask;
1542 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1543 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1544 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1546 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1547 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1548 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1550 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1551 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1552 vshader->reg_maps.boolean_constants);
1554 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1556 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1557 shader_glsl_clip_plane_uniform(context_gl, state, i, prog);
1560 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1561 shader_glsl_pointsize_uniform(context_gl, state, prog);
1563 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1565 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1566 max(state->viewport_count, 1) : 1;
1567 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1568 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1569 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1570 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1571 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1572 else
1573 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1574 checkGLcall("glUniform4fv");
1577 if (update_mask & WINED3D_SHADER_CONST_BASE_VERTEX_ID)
1579 GL_EXTCALL(glUniform1i(prog->vs.base_vertex_id_location, state->base_vertex_index));
1580 checkGLcall("base vertex id");
1583 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1585 struct wined3d_matrix mat;
1587 get_modelview_matrix(context, state, 0, &mat);
1588 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1589 checkGLcall("glUniformMatrix4fv");
1591 shader_glsl_ffp_vertex_normalmatrix_uniform(context_gl, state, prog);
1594 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1596 struct wined3d_matrix mat;
1598 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1600 if (prog->vs.modelview_matrix_location[i] == -1)
1601 break;
1603 get_modelview_matrix(context, state, i, &mat);
1604 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1605 checkGLcall("glUniformMatrix4fv");
1609 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1611 struct wined3d_matrix projection;
1613 get_projection_matrix(context, state, &projection);
1614 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1615 checkGLcall("glUniformMatrix4fv");
1618 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1620 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1621 shader_glsl_ffp_vertex_texmatrix_uniform(context_gl, state, i, prog);
1624 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1625 shader_glsl_ffp_vertex_material_uniform(context_gl, state, prog);
1627 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1629 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1630 DWORD point_count = 0;
1631 DWORD spot_count = 0;
1632 DWORD directional_count = 0;
1633 DWORD parallel_point_count = 0;
1635 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1637 if (!state->light_state.lights[i])
1638 continue;
1640 switch (state->light_state.lights[i]->OriginalParms.type)
1642 case WINED3D_LIGHT_POINT:
1643 ++point_count;
1644 break;
1645 case WINED3D_LIGHT_SPOT:
1646 ++spot_count;
1647 break;
1648 case WINED3D_LIGHT_DIRECTIONAL:
1649 ++directional_count;
1650 break;
1651 case WINED3D_LIGHT_PARALLELPOINT:
1652 ++parallel_point_count;
1653 break;
1654 default:
1655 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
1656 break;
1659 point_idx = 0;
1660 spot_idx = point_idx + point_count;
1661 directional_idx = spot_idx + spot_count;
1662 parallel_point_idx = directional_idx + directional_count;
1664 shader_glsl_ffp_vertex_lightambient_uniform(context_gl, state, prog);
1665 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1667 const struct wined3d_light_info *light_info = state->light_state.lights[i];
1668 unsigned int idx;
1670 if (!light_info)
1671 continue;
1673 switch (light_info->OriginalParms.type)
1675 case WINED3D_LIGHT_POINT:
1676 idx = point_idx++;
1677 break;
1678 case WINED3D_LIGHT_SPOT:
1679 idx = spot_idx++;
1680 break;
1681 case WINED3D_LIGHT_DIRECTIONAL:
1682 idx = directional_idx++;
1683 break;
1684 case WINED3D_LIGHT_PARALLELPOINT:
1685 idx = parallel_point_idx++;
1686 break;
1687 default:
1688 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1689 continue;
1691 shader_glsl_ffp_vertex_light_uniform(context_gl, state, idx, light_info, prog);
1695 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1696 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1697 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1699 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1700 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1701 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1703 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1704 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1705 pshader->reg_maps.boolean_constants);
1707 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1709 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1711 if (prog->ps.bumpenv_mat_location[i] == -1)
1712 continue;
1714 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1715 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1717 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1719 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1720 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1721 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1722 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1726 checkGLcall("bump env uniforms");
1729 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1731 const struct wined3d_vec4 correction_params =
1733 /* Position is relative to the framebuffer, not the viewport. */
1734 context->render_offscreen ? 0.0f : (float)state->fb.render_targets[0]->height,
1735 context->render_offscreen ? 1.0f : -1.0f,
1736 0.0f,
1737 0.0f,
1740 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1743 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1744 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1745 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1746 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1748 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1750 struct wined3d_color color;
1752 if (prog->ps.tex_factor_location != -1)
1754 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1755 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1758 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1759 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1760 else
1761 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1763 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1765 if (prog->ps.tss_constant_location[i] == -1)
1766 continue;
1768 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1769 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1772 checkGLcall("fixed function uniforms");
1775 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1776 shader_glsl_load_fog_uniform(context_gl, state, prog);
1778 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1780 float ref = wined3d_alpha_ref(state);
1782 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1783 checkGLcall("alpha test emulation uniform");
1786 if (priv->next_constant_version == UINT_MAX)
1788 TRACE("Max constant version reached, resetting to 0.\n");
1789 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1790 priv->next_constant_version = 1;
1792 else
1794 prog->constant_version = priv->next_constant_version++;
1798 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1800 struct constant_entry *entries = heap->entries;
1801 unsigned int *positions = heap->positions;
1802 unsigned int heap_idx, parent_idx;
1804 if (!heap->contained[idx])
1806 heap_idx = heap->size++;
1807 heap->contained[idx] = TRUE;
1809 else
1811 heap_idx = positions[idx];
1814 while (heap_idx > 1)
1816 parent_idx = heap_idx >> 1;
1818 if (new_version <= entries[parent_idx].version) break;
1820 entries[heap_idx] = entries[parent_idx];
1821 positions[entries[parent_idx].idx] = heap_idx;
1822 heap_idx = parent_idx;
1825 entries[heap_idx].version = new_version;
1826 entries[heap_idx].idx = idx;
1827 positions[idx] = heap_idx;
1830 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1832 struct shader_glsl_priv *priv = device->shader_priv;
1833 struct constant_heap *heap = &priv->vconst_heap;
1834 UINT i;
1836 for (i = start; i < count + start; ++i)
1838 update_heap_entry(heap, i, priv->next_constant_version);
1842 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1844 struct shader_glsl_priv *priv = device->shader_priv;
1845 struct constant_heap *heap = &priv->pconst_heap;
1846 UINT i;
1848 for (i = start; i < count + start; ++i)
1850 update_heap_entry(heap, i, priv->next_constant_version);
1854 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1856 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1857 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1858 if(shader_major > 3) return ret;
1860 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1861 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1862 return ret;
1865 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1867 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
1870 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1872 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
1873 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
1876 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
1878 return shader_glsl_get_version(gl_info) >= 150;
1881 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1883 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1886 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1887 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1889 va_list args;
1890 int ret;
1892 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1893 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1894 for (;;)
1896 va_start(args, format);
1897 ret = shader_vaddline(buffer, format, args);
1898 va_end(args);
1899 if (!ret)
1900 return;
1901 if (!string_buffer_resize(buffer, ret))
1902 return;
1906 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1907 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1909 va_list args;
1910 int ret;
1912 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1913 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1914 for (;;)
1916 va_start(args, format);
1917 ret = shader_vaddline(buffer, format, args);
1918 va_end(args);
1919 if (!ret)
1920 return;
1921 if (!string_buffer_resize(buffer, ret))
1922 return;
1926 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
1928 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
1931 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
1933 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
1936 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
1938 switch (mode)
1940 case WINED3DSIM_CONSTANT:
1941 return "flat ";
1942 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
1943 return "noperspective ";
1944 default:
1945 FIXME("Unhandled interpolation mode %#x.\n", mode);
1946 case WINED3DSIM_NONE:
1947 case WINED3DSIM_LINEAR:
1948 return "";
1952 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
1953 const DWORD *packed_interpolation_mode, unsigned int register_idx)
1955 return wined3d_extract_bits(packed_interpolation_mode,
1956 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
1959 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
1960 struct wined3d_string_buffer *buffer, unsigned int element_count,
1961 const DWORD *interpolation_mode, BOOL unroll)
1963 enum wined3d_shader_interpolation_mode mode;
1964 unsigned int i;
1966 if (shader_glsl_use_interface_blocks(gl_info))
1968 if (unroll)
1970 shader_addline(buffer, "in shader_in_out {\n");
1971 for (i = 0; i < element_count; ++i)
1973 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
1974 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
1976 shader_addline(buffer, "} shader_in;\n");
1978 else
1980 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
1983 else
1985 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
1989 static BOOL needs_interpolation_qualifiers_for_shader_outputs(const struct wined3d_gl_info *gl_info)
1991 /* In GLSL 4.40+ it is fine to specify interpolation qualifiers only in
1992 * fragment shaders. In older GLSL versions interpolation qualifiers must
1993 * match between shader stages. */
1994 return gl_info->glsl_version < MAKEDWORD_VERSION(4, 40);
1997 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
1998 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
1999 const DWORD *interpolation_mode)
2001 enum wined3d_shader_interpolation_mode mode;
2002 unsigned int i;
2004 if (shader_glsl_use_interface_blocks(gl_info))
2006 if (rasterizer_setup)
2008 shader_addline(buffer, "out shader_in_out {\n");
2009 for (i = 0; i < element_count; ++i)
2011 const char *interpolation_qualifiers = "";
2012 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2014 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2015 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2017 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2019 shader_addline(buffer, "} shader_out;\n");
2021 else
2023 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2026 else
2028 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2032 static BOOL use_legacy_fragment_output(const struct wined3d_gl_info *gl_info)
2034 /* Technically 1.30 does support user-defined fragment shader outputs but
2035 * we might not have glBindFragDataLocation() available (i.e. GL version
2036 * might be < 3.0). */
2037 return gl_info->glsl_version <= MAKEDWORD_VERSION(1, 30);
2040 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2042 return use_legacy_fragment_output(gl_info) ? "gl_FragData" : "ps_out";
2045 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2047 switch (primitive_type)
2049 case WINED3D_PT_POINTLIST:
2050 return "points";
2052 case WINED3D_PT_LINELIST:
2053 return "lines";
2055 case WINED3D_PT_LINESTRIP:
2056 return "line_strip";
2058 case WINED3D_PT_TRIANGLELIST:
2059 return "triangles";
2061 case WINED3D_PT_TRIANGLESTRIP:
2062 return "triangle_strip";
2064 case WINED3D_PT_LINELIST_ADJ:
2065 return "lines_adjacency";
2067 case WINED3D_PT_TRIANGLELIST_ADJ:
2068 return "triangles_adjacency";
2070 default:
2071 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2072 return "";
2076 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2078 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2079 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2080 DWORD input_reg_used = shader->u.ps.input_reg_used;
2081 unsigned int i;
2083 if (reg_maps->shader_version.major < 3)
2084 return input_reg_used & (1u << idx);
2086 for (i = 0; i < input_signature->element_count; ++i)
2088 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2090 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2091 continue;
2093 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2094 && input->semantic_idx == idx)
2095 return input_reg_used & (1u << input->register_idx);
2097 return FALSE;
2100 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2101 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2103 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2105 if (version->major >= 4)
2106 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2107 else
2108 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2111 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2112 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2113 unsigned int index)
2115 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2116 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2117 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2118 index, scalar_type, scalar_type, index);
2121 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2122 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2124 unsigned int index = e->register_idx;
2125 enum wined3d_component_type type;
2127 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2129 shader_addline(buffer, "uniform int base_vertex_id;\n");
2130 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID - base_vertex_id), 0.0, 0.0, 0.0);\n", index);
2131 return;
2133 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2135 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2136 index);
2137 return;
2139 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2140 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2142 if (shader_glsl_use_explicit_attrib_location(gl_info))
2143 shader_addline(buffer, "layout(location = %u) ", index);
2145 type = e->component_type;
2146 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2148 FIXME("Unhandled type %#x.\n", type);
2149 type = WINED3D_TYPE_FLOAT;
2151 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2152 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2153 else
2154 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2155 component_type_info[type].glsl_vector_type,
2156 component_type_info[type].glsl_scalar_type, index);
2159 /** Generate the variable & register declarations for the GLSL output target */
2160 static void shader_generate_glsl_declarations(const struct wined3d_context_gl *context_gl,
2161 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2162 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2164 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2165 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2166 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2167 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2168 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2169 unsigned int uniform_block_base, uniform_block_count;
2170 enum wined3d_shader_resource_type resource_type;
2171 const struct wined3d_shader_lconst *lconst;
2172 const char *prefix;
2173 unsigned int i;
2174 DWORD map;
2176 if (wined3d_settings.strict_shader_math)
2177 shader_addline(buffer, "#pragma optionNV(fastmath off)\n");
2179 prefix = shader_glsl_get_prefix(version->type);
2181 /* Prototype the subroutines */
2182 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2184 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2187 /* Declare the constants (aka uniforms) */
2188 if (shader->limits->constant_float > 0)
2190 unsigned max_constantsF;
2192 /* Unless the shader uses indirect addressing, always declare the
2193 * maximum array size and ignore that we need some uniforms privately.
2194 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2195 * and immediate values, still declare VC[256]. If the shader needs
2196 * more uniforms than we have it won't work in any case. If it uses
2197 * less, the compiler will figure out which uniforms are really used
2198 * and strip them out. This allows a shader to use c255 on a dx9 card,
2199 * as long as it doesn't also use all the other constants.
2201 * If the shader uses indirect addressing the compiler must assume
2202 * that all declared uniforms are used. In this case, declare only the
2203 * amount that we're assured to have.
2205 * Thus we run into problems in these two cases:
2206 * 1) The shader really uses more uniforms than supported.
2207 * 2) The shader uses indirect addressing, less constants than
2208 * supported, but uses a constant index > #supported consts. */
2209 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2211 /* No indirect addressing here. */
2212 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2214 else
2216 if (reg_maps->usesrelconstF)
2218 /* Subtract the other potential uniforms from the max
2219 * available (bools, ints, and 1 row of projection matrix).
2220 * Subtract another uniform for immediate values, which have
2221 * to be loaded via uniform by the driver as well. The shader
2222 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2223 * shader code, so one vec4 should be enough. (Unfortunately
2224 * the Nvidia driver doesn't store 128 and -128 in one float).
2226 * Writing gl_ClipVertex requires one uniform for each
2227 * clipplane as well. */
2228 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2229 if (vs_args->clip_enabled)
2230 max_constantsF -= gl_info->limits.user_clip_distances;
2231 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2232 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2233 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2234 * for now take this into account when calculating the number of available constants
2236 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2237 /* Set by driver quirks in directx.c */
2238 max_constantsF -= gl_info->reserved_glsl_constants;
2240 if (max_constantsF < shader->limits->constant_float)
2242 static unsigned int once;
2244 if (!once++)
2245 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2246 " it may not render correctly.\n");
2247 else
2248 WARN("The hardware does not support enough uniform components to run this shader.\n");
2251 else
2253 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2256 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2257 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2260 /* Always declare the full set of constants, the compiler can remove the
2261 * unused ones because d3d doesn't (yet) support indirect int and bool
2262 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2263 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2264 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2266 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2267 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2269 /* Declare immediate constant buffer */
2270 if (reg_maps->icb)
2271 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2273 /* Declare constant buffers */
2274 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2275 &uniform_block_base, &uniform_block_count);
2276 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2278 if (reg_maps->cb_sizes[i])
2280 shader_addline(buffer, "layout(std140");
2281 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2282 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2283 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2284 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2288 /* Declare texture samplers */
2289 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2291 struct wined3d_shader_sampler_map_entry *entry;
2292 const char *sampler_type_prefix, *sampler_type;
2293 BOOL shadow_sampler, tex_rect;
2295 entry = &reg_maps->sampler_map.entries[i];
2297 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2299 ERR("Invalid resource index %u.\n", entry->resource_idx);
2300 continue;
2303 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2305 case WINED3D_DATA_FLOAT:
2306 case WINED3D_DATA_UNORM:
2307 case WINED3D_DATA_SNORM:
2308 sampler_type_prefix = "";
2309 break;
2311 case WINED3D_DATA_INT:
2312 sampler_type_prefix = "i";
2313 break;
2315 case WINED3D_DATA_UINT:
2316 sampler_type_prefix = "u";
2317 break;
2319 default:
2320 sampler_type_prefix = "";
2321 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2322 break;
2325 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2326 resource_type = version->type == WINED3D_SHADER_TYPE_PIXEL
2327 ? pixelshader_get_resource_type(reg_maps, entry->resource_idx, ps_args->tex_types)
2328 : reg_maps->resource_info[entry->resource_idx].type;
2330 switch (resource_type)
2332 case WINED3D_SHADER_RESOURCE_BUFFER:
2333 sampler_type = "samplerBuffer";
2334 break;
2336 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2337 if (shadow_sampler)
2338 sampler_type = "sampler1DShadow";
2339 else
2340 sampler_type = "sampler1D";
2341 break;
2343 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2344 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2345 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2346 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2347 if (shadow_sampler)
2349 if (tex_rect)
2350 sampler_type = "sampler2DRectShadow";
2351 else
2352 sampler_type = "sampler2DShadow";
2354 else
2356 if (tex_rect)
2357 sampler_type = "sampler2DRect";
2358 else
2359 sampler_type = "sampler2D";
2361 break;
2363 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2364 if (shadow_sampler)
2365 FIXME("Unsupported 3D shadow sampler.\n");
2366 sampler_type = "sampler3D";
2367 break;
2369 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2370 if (shadow_sampler)
2371 sampler_type = "samplerCubeShadow";
2372 else
2373 sampler_type = "samplerCube";
2374 break;
2376 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2377 if (shadow_sampler)
2378 sampler_type = "sampler1DArrayShadow";
2379 else
2380 sampler_type = "sampler1DArray";
2381 break;
2383 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2384 if (shadow_sampler)
2385 sampler_type = "sampler2DArrayShadow";
2386 else
2387 sampler_type = "sampler2DArray";
2388 break;
2390 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2391 if (shadow_sampler)
2392 sampler_type = "samplerCubeArrayShadow";
2393 else
2394 sampler_type = "samplerCubeArray";
2395 break;
2397 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2398 sampler_type = "sampler2DMS";
2399 break;
2401 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2402 sampler_type = "sampler2DMSArray";
2403 break;
2405 default:
2406 sampler_type = "unsupported_sampler";
2407 FIXME("Unhandled resource type %#x.\n", resource_type);
2408 break;
2411 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2412 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, version, entry->bind_idx);
2413 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2414 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2417 /* Declare images */
2418 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2420 const char *image_type_prefix, *image_type, *read_format;
2422 if (!reg_maps->uav_resource_info[i].type)
2423 continue;
2425 switch (reg_maps->uav_resource_info[i].data_type)
2427 case WINED3D_DATA_FLOAT:
2428 case WINED3D_DATA_UNORM:
2429 case WINED3D_DATA_SNORM:
2430 image_type_prefix = "";
2431 read_format = "r32f";
2432 break;
2434 case WINED3D_DATA_INT:
2435 image_type_prefix = "i";
2436 read_format = "r32i";
2437 break;
2439 case WINED3D_DATA_UINT:
2440 image_type_prefix = "u";
2441 read_format = "r32ui";
2442 break;
2444 default:
2445 image_type_prefix = "";
2446 read_format = "";
2447 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2448 break;
2451 switch (reg_maps->uav_resource_info[i].type)
2453 case WINED3D_SHADER_RESOURCE_BUFFER:
2454 image_type = "imageBuffer";
2455 break;
2457 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2458 image_type = "image1D";
2459 break;
2461 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2462 image_type = "image2D";
2463 break;
2465 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2466 image_type = "image3D";
2467 break;
2469 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2470 image_type = "image1DArray";
2471 break;
2473 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2474 image_type = "image2DArray";
2475 break;
2477 default:
2478 image_type = "unsupported_image";
2479 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2480 break;
2483 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2484 shader_addline(buffer, "layout(binding = %u)\n", i);
2485 if (reg_maps->uav_read_mask & (1u << i))
2486 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2487 read_format, image_type_prefix, image_type, prefix, i);
2488 else
2489 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2490 image_type_prefix, image_type, prefix, i);
2492 if (reg_maps->uav_counter_mask & (1u << i))
2493 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2494 i, prefix, i);
2497 /* Declare address variables */
2498 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2500 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2503 /* Declare output register temporaries */
2504 if (shader->limits->packed_output)
2505 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2507 /* Declare temporary variables */
2508 if (reg_maps->temporary_count)
2510 for (i = 0; i < reg_maps->temporary_count; ++i)
2511 shader_addline(buffer, "vec4 R%u;\n", i);
2513 else if (version->major < 4)
2515 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2517 if (map & 1)
2518 shader_addline(buffer, "vec4 R%u;\n", i);
2522 /* Declare indexable temporary variables */
2523 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2525 if (idx_temp_reg->component_count != 4)
2526 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2527 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2530 /* Declare loop registers aLx */
2531 if (version->major < 4)
2533 for (i = 0; i < reg_maps->loop_depth; ++i)
2535 shader_addline(buffer, "int aL%u;\n", i);
2536 shader_addline(buffer, "int tmpInt%u;\n", i);
2540 /* Temporary variables for matrix operations */
2541 shader_addline(buffer, "vec4 tmp0;\n");
2542 shader_addline(buffer, "vec4 tmp1;\n");
2544 if (!shader->load_local_constsF)
2546 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2548 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2549 shader_glsl_append_imm_vec(buffer, (const float *)lconst->value, ARRAY_SIZE(lconst->value), gl_info);
2550 shader_addline(buffer, ";\n");
2555 /* Prototypes */
2556 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
2557 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2558 enum wined3d_data_type data_type);
2560 /** Used for opcode modifiers - They multiply the result by the specified amount */
2561 static const char * const shift_glsl_tab[] = {
2562 "", /* 0 (none) */
2563 "2.0 * ", /* 1 (x2) */
2564 "4.0 * ", /* 2 (x4) */
2565 "8.0 * ", /* 3 (x8) */
2566 "16.0 * ", /* 4 (x16) */
2567 "32.0 * ", /* 5 (x32) */
2568 "", /* 6 (x64) */
2569 "", /* 7 (x128) */
2570 "", /* 8 (d256) */
2571 "", /* 9 (d128) */
2572 "", /* 10 (d64) */
2573 "", /* 11 (d32) */
2574 "0.0625 * ", /* 12 (d16) */
2575 "0.125 * ", /* 13 (d8) */
2576 "0.25 * ", /* 14 (d4) */
2577 "0.5 * " /* 15 (d2) */
2580 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2581 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2582 const char *in_reg, const char *in_regswizzle, char *out_str)
2584 switch (src_modifier)
2586 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2587 case WINED3DSPSM_DW:
2588 case WINED3DSPSM_NONE:
2589 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2590 break;
2591 case WINED3DSPSM_NEG:
2592 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2593 break;
2594 case WINED3DSPSM_NOT:
2595 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2596 break;
2597 case WINED3DSPSM_BIAS:
2598 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2599 break;
2600 case WINED3DSPSM_BIASNEG:
2601 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2602 break;
2603 case WINED3DSPSM_SIGN:
2604 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2605 break;
2606 case WINED3DSPSM_SIGNNEG:
2607 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2608 break;
2609 case WINED3DSPSM_COMP:
2610 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2611 break;
2612 case WINED3DSPSM_X2:
2613 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2614 break;
2615 case WINED3DSPSM_X2NEG:
2616 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2617 break;
2618 case WINED3DSPSM_ABS:
2619 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2620 break;
2621 case WINED3DSPSM_ABSNEG:
2622 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2623 break;
2624 default:
2625 FIXME("Unhandled modifier %u\n", src_modifier);
2626 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2630 static void shader_glsl_fixup_scalar_register_variable(struct wined3d_string_buffer *register_name,
2631 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2633 /* The ARB_shading_language_420pack extension allows swizzle operations on
2634 * scalars. */
2635 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2636 string_buffer_sprintf(register_name, "%s", glsl_variable);
2637 else
2638 string_buffer_sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2641 /** Writes the GLSL variable name that corresponds to the register that the
2642 * DX opcode parameter is trying to access */
2643 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2644 enum wined3d_data_type data_type, struct wined3d_string_buffer *register_name,
2645 BOOL *is_swizzled, const struct wined3d_shader_context *ctx)
2647 /* oPos, oFog and oPts in D3D */
2648 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2650 const struct wined3d_shader *shader = ctx->shader;
2651 const struct wined3d_shader_reg_maps *reg_maps = ctx->reg_maps;
2652 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2653 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2654 const char *prefix = shader_glsl_get_prefix(version->type);
2655 const struct wined3d_gl_info *gl_info = priv->gl_info;
2656 struct glsl_src_param rel_param0, rel_param1;
2658 if (reg->idx[0].offset != ~0u && reg->idx[0].rel_addr)
2659 shader_glsl_add_src_param_ext(ctx, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0,
2660 &rel_param0, reg->idx[0].rel_addr->reg.data_type);
2661 if (reg->idx[1].offset != ~0u && reg->idx[1].rel_addr)
2662 shader_glsl_add_src_param_ext(ctx, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0,
2663 &rel_param1, reg->idx[1].rel_addr->reg.data_type);
2664 if (is_swizzled)
2665 *is_swizzled = FALSE;
2667 switch (reg->type)
2669 case WINED3DSPR_TEMP:
2670 string_buffer_sprintf(register_name, "R%u", reg->idx[0].offset);
2671 break;
2673 case WINED3DSPR_INPUT:
2674 case WINED3DSPR_INCONTROLPOINT:
2675 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2677 if (reg->idx[0].rel_addr)
2678 FIXME("VS3 input registers relative addressing.\n");
2679 if (is_swizzled && priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2680 *is_swizzled = TRUE;
2681 if (reg->idx[0].rel_addr)
2683 string_buffer_sprintf(register_name, "%s_in[%s + %u]",
2684 prefix, rel_param0.param_str, reg->idx[0].offset);
2686 else
2688 string_buffer_sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2690 break;
2693 if (version->type == WINED3D_SHADER_TYPE_HULL
2694 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2695 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2697 if (reg->idx[0].rel_addr)
2699 if (reg->idx[1].rel_addr)
2700 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2701 rel_param0.param_str, reg->idx[0].offset,
2702 rel_param1.param_str, reg->idx[1].offset);
2703 else
2704 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2705 rel_param0.param_str, reg->idx[0].offset,
2706 reg->idx[1].offset);
2708 else if (reg->idx[1].rel_addr)
2709 string_buffer_sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2710 rel_param1.param_str, reg->idx[1].offset);
2711 else
2712 string_buffer_sprintf(register_name, "shader_in[%u].reg[%u]",
2713 reg->idx[0].offset, reg->idx[1].offset);
2714 break;
2717 /* pixel shaders >= 3.0 */
2718 if (version->major >= 3)
2720 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2721 unsigned int in_count = vec4_varyings(version->major, gl_info);
2723 if (reg->idx[0].rel_addr)
2725 /* Removing a + 0 would be an obvious optimization, but
2726 * OS X doesn't see the NOP operation there. */
2727 if (idx)
2729 if (needs_legacy_glsl_syntax(gl_info)
2730 && shader->u.ps.declared_in_count > in_count)
2732 string_buffer_sprintf(register_name,
2733 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2734 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2735 prefix, rel_param0.param_str, idx);
2737 else
2739 string_buffer_sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2742 else
2744 if (needs_legacy_glsl_syntax(gl_info)
2745 && shader->u.ps.declared_in_count > in_count)
2747 string_buffer_sprintf(register_name,
2748 "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2749 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2750 prefix, rel_param0.param_str);
2752 else
2754 string_buffer_sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2758 else
2760 if (idx == in_count)
2761 string_buffer_sprintf(register_name, "gl_Color");
2762 else if (idx == in_count + 1)
2763 string_buffer_sprintf(register_name, "gl_SecondaryColor");
2764 else
2765 string_buffer_sprintf(register_name, "%s_in[%u]", prefix, idx);
2768 else
2770 if (!reg->idx[0].offset)
2771 string_buffer_sprintf(register_name, "ffp_varying_diffuse");
2772 else
2773 string_buffer_sprintf(register_name, "ffp_varying_specular");
2774 break;
2776 break;
2778 case WINED3DSPR_CONST:
2780 /* Relative addressing */
2781 if (reg->idx[0].rel_addr)
2783 if (wined3d_settings.check_float_constants)
2784 string_buffer_sprintf(register_name,
2785 "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2786 rel_param0.param_str, reg->idx[0].offset,
2787 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2788 prefix, rel_param0.param_str, reg->idx[0].offset);
2789 else if (reg->idx[0].offset)
2790 string_buffer_sprintf(register_name, "%s_c[%s + %u]",
2791 prefix, rel_param0.param_str, reg->idx[0].offset);
2792 else
2793 string_buffer_sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2795 else
2797 if (shader_constant_is_local(shader, reg->idx[0].offset))
2798 string_buffer_sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2799 else
2800 string_buffer_sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2803 break;
2805 case WINED3DSPR_CONSTINT:
2806 string_buffer_sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2807 break;
2809 case WINED3DSPR_CONSTBOOL:
2810 string_buffer_sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2811 break;
2813 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2814 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2815 string_buffer_sprintf(register_name, "T%u", reg->idx[0].offset);
2816 else
2817 string_buffer_sprintf(register_name, "A%u", reg->idx[0].offset);
2818 break;
2820 case WINED3DSPR_LOOP:
2821 string_buffer_sprintf(register_name, "aL%u", ctx->state->current_loop_reg - 1);
2822 break;
2824 case WINED3DSPR_SAMPLER:
2825 string_buffer_sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2826 break;
2828 case WINED3DSPR_COLOROUT:
2829 if (reg->idx[0].offset >= gl_info->limits.buffers)
2830 WARN("Write to render target %u, only %d supported.\n",
2831 reg->idx[0].offset, gl_info->limits.buffers);
2833 string_buffer_sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2834 break;
2836 case WINED3DSPR_RASTOUT:
2837 string_buffer_sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2838 break;
2840 case WINED3DSPR_DEPTHOUT:
2841 case WINED3DSPR_DEPTHOUTGE:
2842 case WINED3DSPR_DEPTHOUTLE:
2843 string_buffer_sprintf(register_name, "gl_FragDepth");
2844 break;
2846 case WINED3DSPR_ATTROUT:
2847 if (!reg->idx[0].offset)
2848 string_buffer_sprintf(register_name, "%s_out[8]", prefix);
2849 else
2850 string_buffer_sprintf(register_name, "%s_out[9]", prefix);
2851 break;
2853 case WINED3DSPR_TEXCRDOUT:
2854 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2855 if (reg->idx[0].rel_addr)
2856 string_buffer_sprintf(register_name, "%s_out[%s + %u]",
2857 prefix, rel_param0.param_str, reg->idx[0].offset);
2858 else
2859 string_buffer_sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2860 break;
2862 case WINED3DSPR_MISCTYPE:
2863 if (!reg->idx[0].offset)
2865 /* vPos */
2866 string_buffer_sprintf(register_name, "vpos");
2868 else if (reg->idx[0].offset == 1)
2870 /* Note that gl_FrontFacing is a bool, while vFace is
2871 * a float for which the sign determines front/back */
2872 string_buffer_sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2874 else
2876 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2877 string_buffer_sprintf(register_name, "unrecognized_register");
2879 break;
2881 case WINED3DSPR_IMMCONST:
2882 switch (reg->immconst_type)
2884 case WINED3D_IMMCONST_SCALAR:
2885 switch (data_type)
2887 case WINED3D_DATA_UNORM:
2888 case WINED3D_DATA_SNORM:
2889 case WINED3D_DATA_FLOAT:
2890 string_buffer_clear(register_name);
2891 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 1, gl_info);
2892 break;
2893 case WINED3D_DATA_INT:
2894 string_buffer_sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2895 break;
2896 case WINED3D_DATA_RESOURCE:
2897 case WINED3D_DATA_SAMPLER:
2898 case WINED3D_DATA_UINT:
2899 string_buffer_sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2900 break;
2901 default:
2902 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2903 break;
2905 break;
2907 case WINED3D_IMMCONST_VEC4:
2908 switch (data_type)
2910 case WINED3D_DATA_UNORM:
2911 case WINED3D_DATA_SNORM:
2912 case WINED3D_DATA_FLOAT:
2913 string_buffer_clear(register_name);
2914 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 4, gl_info);
2915 break;
2916 case WINED3D_DATA_INT:
2917 string_buffer_sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2918 reg->u.immconst_data[0], reg->u.immconst_data[1],
2919 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2920 break;
2921 case WINED3D_DATA_RESOURCE:
2922 case WINED3D_DATA_SAMPLER:
2923 case WINED3D_DATA_UINT:
2924 string_buffer_sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2925 reg->u.immconst_data[0], reg->u.immconst_data[1],
2926 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2927 break;
2928 default:
2929 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2930 break;
2932 break;
2934 default:
2935 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2936 string_buffer_sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2938 break;
2940 case WINED3DSPR_CONSTBUFFER:
2941 if (reg->idx[1].rel_addr)
2942 string_buffer_sprintf(register_name, "%s_cb%u[%s + %u]",
2943 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2944 else
2945 string_buffer_sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2946 break;
2948 case WINED3DSPR_IMMCONSTBUFFER:
2949 if (reg->idx[0].rel_addr)
2950 string_buffer_sprintf(register_name, "%s_icb[%s + %u]",
2951 prefix, rel_param0.param_str, reg->idx[0].offset);
2952 else
2953 string_buffer_sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2954 break;
2956 case WINED3DSPR_PRIMID:
2957 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2958 string_buffer_sprintf(register_name, "gl_PrimitiveIDIn");
2959 else
2960 string_buffer_sprintf(register_name, "gl_PrimitiveID");
2961 break;
2963 case WINED3DSPR_IDXTEMP:
2964 if (reg->idx[1].rel_addr)
2965 string_buffer_sprintf(register_name, "X%u[%s + %u]",
2966 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2967 else
2968 string_buffer_sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2969 break;
2971 case WINED3DSPR_LOCALTHREADINDEX:
2972 shader_glsl_fixup_scalar_register_variable(register_name,
2973 "int(gl_LocalInvocationIndex)", gl_info);
2974 break;
2976 case WINED3DSPR_GSINSTID:
2977 case WINED3DSPR_OUTPOINTID:
2978 shader_glsl_fixup_scalar_register_variable(register_name,
2979 "gl_InvocationID", gl_info);
2980 break;
2982 case WINED3DSPR_THREADID:
2983 string_buffer_sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2984 break;
2986 case WINED3DSPR_THREADGROUPID:
2987 string_buffer_sprintf(register_name, "ivec3(gl_WorkGroupID)");
2988 break;
2990 case WINED3DSPR_LOCALTHREADID:
2991 string_buffer_sprintf(register_name, "ivec3(gl_LocalInvocationID)");
2992 break;
2994 case WINED3DSPR_FORKINSTID:
2995 case WINED3DSPR_JOININSTID:
2996 shader_glsl_fixup_scalar_register_variable(register_name,
2997 "phase_instance_id", gl_info);
2998 break;
3000 case WINED3DSPR_TESSCOORD:
3001 string_buffer_sprintf(register_name, "gl_TessCoord");
3002 break;
3004 case WINED3DSPR_OUTCONTROLPOINT:
3005 if (reg->idx[0].rel_addr)
3007 if (reg->idx[1].rel_addr)
3008 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3009 rel_param0.param_str, reg->idx[0].offset,
3010 rel_param1.param_str, reg->idx[1].offset);
3011 else
3012 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3013 rel_param0.param_str, reg->idx[0].offset,
3014 reg->idx[1].offset);
3016 else if (reg->idx[1].rel_addr)
3018 string_buffer_sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3019 reg->idx[0].offset, rel_param1.param_str,
3020 reg->idx[1].offset);
3022 else
3024 string_buffer_sprintf(register_name, "shader_out[%u].reg[%u]",
3025 reg->idx[0].offset, reg->idx[1].offset);
3027 break;
3029 case WINED3DSPR_PATCHCONST:
3030 if (version->type == WINED3D_SHADER_TYPE_HULL)
3031 string_buffer_sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3032 else
3033 string_buffer_sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3034 break;
3036 case WINED3DSPR_COVERAGE:
3037 string_buffer_sprintf(register_name, "gl_SampleMaskIn[0]");
3038 break;
3040 case WINED3DSPR_SAMPLEMASK:
3041 string_buffer_sprintf(register_name, "sample_mask");
3042 break;
3044 default:
3045 FIXME("Unhandled register type %#x.\n", reg->type);
3046 string_buffer_sprintf(register_name, "unrecognised_register");
3047 break;
3051 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3053 *str++ = '.';
3054 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3055 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3056 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3057 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3058 *str = '\0';
3061 /* Get the GLSL write mask for the destination register */
3062 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3064 DWORD mask = param->write_mask;
3066 if (shader_is_scalar(&param->reg))
3068 mask = WINED3DSP_WRITEMASK_0;
3069 *write_mask = '\0';
3071 else
3073 shader_glsl_write_mask_to_str(mask, write_mask);
3076 return mask;
3079 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3081 unsigned int size = 0;
3083 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3084 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3085 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3086 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3088 return size;
3091 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3092 unsigned int component_idx)
3094 /* swizzle bits fields: wwzzyyxx */
3095 return (swizzle >> (2 * component_idx)) & 0x3;
3098 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3100 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3101 * but addressed as "rgba". To fix this we need to swap the register's x
3102 * and z components. */
3103 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3104 unsigned int i;
3106 *str++ = '.';
3107 for (i = 0; i < 4; ++i)
3109 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3110 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3112 *str = '\0';
3115 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3116 BOOL fixup, DWORD mask, char *swizzle_str)
3118 if (shader_is_scalar(&param->reg))
3119 *swizzle_str = '\0';
3120 else
3121 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3124 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3125 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type, unsigned int size)
3127 if (dst_data_type == src_data_type)
3129 string_buffer_sprintf(dst_param, "%s", src_param);
3130 return;
3133 if (src_data_type == WINED3D_DATA_FLOAT)
3135 switch (dst_data_type)
3137 case WINED3D_DATA_INT:
3138 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3139 return;
3140 case WINED3D_DATA_RESOURCE:
3141 case WINED3D_DATA_SAMPLER:
3142 case WINED3D_DATA_UINT:
3143 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3144 return;
3145 default:
3146 break;
3150 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3152 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3153 return;
3156 if (src_data_type == WINED3D_DATA_INT)
3158 switch (dst_data_type)
3160 case WINED3D_DATA_FLOAT:
3161 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3162 return;
3163 case WINED3D_DATA_UINT:
3164 if (size == 1)
3165 string_buffer_sprintf(dst_param, "uint(%s)", src_param);
3166 else
3167 string_buffer_sprintf(dst_param, "uvec%u(%s)", size, src_param);
3168 return;
3169 default:
3170 break;
3174 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3175 string_buffer_sprintf(dst_param, "%s", src_param);
3178 /* From a given parameter token, generate the corresponding GLSL string.
3179 * Also, return the actual register name and swizzle in case the
3180 * caller needs this information as well. */
3181 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
3182 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3183 enum wined3d_data_type data_type)
3185 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3186 struct wined3d_string_buffer *param_str = string_buffer_get(priv->string_buffers);
3187 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3188 enum wined3d_data_type param_data_type;
3189 BOOL is_color = FALSE;
3190 char swizzle_str[6];
3191 unsigned int size;
3193 glsl_src->param_str[0] = '\0';
3194 swizzle_str[0] = '\0';
3196 shader_glsl_get_register_name(&wined3d_src->reg, data_type, reg_name, &is_color, ctx);
3197 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3199 switch (wined3d_src->reg.type)
3201 case WINED3DSPR_IMMCONST:
3202 param_data_type = data_type;
3203 size = wined3d_src->reg.immconst_type == WINED3D_IMMCONST_SCALAR ? 1 : 4;
3204 break;
3205 case WINED3DSPR_FORKINSTID:
3206 case WINED3DSPR_GSINSTID:
3207 case WINED3DSPR_JOININSTID:
3208 case WINED3DSPR_LOCALTHREADINDEX:
3209 case WINED3DSPR_OUTPOINTID:
3210 case WINED3DSPR_PRIMID:
3211 param_data_type = WINED3D_DATA_INT;
3212 size = 1;
3213 break;
3214 case WINED3DSPR_LOCALTHREADID:
3215 case WINED3DSPR_THREADGROUPID:
3216 case WINED3DSPR_THREADID:
3217 param_data_type = WINED3D_DATA_INT;
3218 size = 3;
3219 break;
3220 default:
3221 param_data_type = WINED3D_DATA_FLOAT;
3222 size = 4;
3223 break;
3226 shader_glsl_sprintf_cast(param_str, reg_name->buffer, data_type, param_data_type, size);
3227 shader_glsl_gen_modifier(wined3d_src->modifiers, param_str->buffer, swizzle_str, glsl_src->param_str);
3229 string_buffer_release(priv->string_buffers, reg_name);
3230 string_buffer_release(priv->string_buffers, param_str);
3233 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3234 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3236 shader_glsl_add_src_param_ext(ins->ctx, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3239 /* From a given parameter token, generate the corresponding GLSL string.
3240 * Also, return the actual register name and swizzle in case the
3241 * caller needs this information as well. */
3242 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3243 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3245 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3246 struct wined3d_string_buffer *reg_name;
3247 size_t len;
3249 glsl_dst->mask_str[0] = '\0';
3251 reg_name = string_buffer_get(priv->string_buffers);
3252 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type, reg_name, NULL, ins->ctx);
3253 len = min(reg_name->content_size, ARRAY_SIZE(glsl_dst->reg_name) - 1);
3254 memcpy(glsl_dst->reg_name, reg_name->buffer, len);
3255 glsl_dst->reg_name[len] = '\0';
3256 string_buffer_release(priv->string_buffers, reg_name);
3258 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3261 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3262 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3263 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3264 enum wined3d_data_type data_type)
3266 struct glsl_dst_param glsl_dst;
3267 DWORD mask;
3269 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3271 switch (data_type)
3273 case WINED3D_DATA_FLOAT:
3274 shader_addline(buffer, "%s%s = %s(",
3275 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3276 break;
3277 case WINED3D_DATA_INT:
3278 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3279 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3280 break;
3281 case WINED3D_DATA_RESOURCE:
3282 case WINED3D_DATA_SAMPLER:
3283 case WINED3D_DATA_UINT:
3284 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3285 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3286 break;
3287 default:
3288 FIXME("Unhandled data type %#x.\n", data_type);
3289 shader_addline(buffer, "%s%s = %s(",
3290 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3291 break;
3295 return mask;
3298 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3299 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3301 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3304 /** Process GLSL instruction modifiers */
3305 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3307 struct glsl_dst_param dst_param;
3308 DWORD modifiers;
3310 if (!ins->dst_count) return;
3312 modifiers = ins->dst[0].modifiers;
3313 if (!modifiers) return;
3315 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3317 if (modifiers & WINED3DSPDM_SATURATE)
3319 /* _SAT means to clamp the value of the register to between 0 and 1 */
3320 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3321 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3324 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3326 FIXME("_centroid modifier not handled\n");
3329 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3331 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3335 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3337 switch (op)
3339 case WINED3D_SHADER_REL_OP_GT: return ">";
3340 case WINED3D_SHADER_REL_OP_EQ: return "==";
3341 case WINED3D_SHADER_REL_OP_GE: return ">=";
3342 case WINED3D_SHADER_REL_OP_LT: return "<";
3343 case WINED3D_SHADER_REL_OP_NE: return "!=";
3344 case WINED3D_SHADER_REL_OP_LE: return "<=";
3345 default:
3346 FIXME("Unrecognized operator %#x.\n", op);
3347 return "(\?\?)";
3351 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3353 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3356 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3357 unsigned int *coord_size, unsigned int *deriv_size)
3359 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3360 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3362 *coord_size = resource_type_info[resource_type].coord_size;
3363 *deriv_size = *coord_size;
3364 if (is_array)
3365 --(*deriv_size);
3368 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3369 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3371 enum wined3d_shader_resource_type resource_type;
3372 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3373 const struct wined3d_gl_info *gl_info = priv->gl_info;
3374 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3375 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3376 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3377 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3378 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3379 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3380 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3381 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3382 const char *base = "texture", *type_part = "", *suffix = "";
3383 unsigned int coord_size, deriv_size;
3385 resource_type = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3386 ? pixelshader_get_resource_type(ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
3387 : ctx->reg_maps->resource_info[resource_idx].type;
3389 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3391 if (resource_type >= ARRAY_SIZE(resource_type_info))
3393 ERR("Unexpected resource type %#x.\n", resource_type);
3394 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3397 /* Note that there's no such thing as a projected cube texture. */
3398 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3399 projected = FALSE;
3401 if (needs_legacy_glsl_syntax(gl_info))
3403 if (shadow)
3404 base = "shadow";
3406 type_part = resource_type_info[resource_type].type_part;
3407 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3408 type_part = "2DRect";
3409 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3410 FIXME("Unhandled resource type %#x.\n", resource_type);
3412 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3414 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3415 suffix = "ARB";
3416 else
3417 FIXME("Unsupported grad function.\n");
3421 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3423 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3424 if (flags & ~texel_fetch_flags)
3425 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3427 base = "texelFetch";
3428 type_part = "";
3431 sample_function->name = string_buffer_get(priv->string_buffers);
3432 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3433 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3435 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3436 if (shadow)
3437 ++coord_size;
3438 sample_function->offset_size = offset ? deriv_size : 0;
3439 sample_function->coord_mask = (1u << coord_size) - 1;
3440 sample_function->deriv_mask = (1u << deriv_size) - 1;
3441 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3444 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3445 struct glsl_sample_function *sample_function)
3447 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3449 string_buffer_release(priv->string_buffers, sample_function->name);
3452 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3453 BOOL sign_fixup, enum fixup_channel_source channel_source)
3455 switch(channel_source)
3457 case CHANNEL_SOURCE_ZERO:
3458 strcat(arguments, "0.0");
3459 break;
3461 case CHANNEL_SOURCE_ONE:
3462 strcat(arguments, "1.0");
3463 break;
3465 case CHANNEL_SOURCE_X:
3466 strcat(arguments, reg_name);
3467 strcat(arguments, ".x");
3468 break;
3470 case CHANNEL_SOURCE_Y:
3471 strcat(arguments, reg_name);
3472 strcat(arguments, ".y");
3473 break;
3475 case CHANNEL_SOURCE_Z:
3476 strcat(arguments, reg_name);
3477 strcat(arguments, ".z");
3478 break;
3480 case CHANNEL_SOURCE_W:
3481 strcat(arguments, reg_name);
3482 strcat(arguments, ".w");
3483 break;
3485 default:
3486 FIXME("Unhandled channel source %#x\n", channel_source);
3487 strcat(arguments, "undefined");
3488 break;
3491 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3494 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3495 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3497 unsigned int mask_size, remaining;
3498 DWORD fixup_mask = 0;
3499 char arguments[256];
3500 char mask_str[6];
3502 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3503 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3504 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3505 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3506 if (!(mask &= fixup_mask))
3507 return;
3509 if (is_complex_fixup(fixup))
3511 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3512 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3513 return;
3516 shader_glsl_write_mask_to_str(mask, mask_str);
3517 mask_size = shader_glsl_get_write_mask_size(mask);
3519 arguments[0] = '\0';
3520 remaining = mask_size;
3521 if (mask & WINED3DSP_WRITEMASK_0)
3523 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3524 if (--remaining) strcat(arguments, ", ");
3526 if (mask & WINED3DSP_WRITEMASK_1)
3528 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3529 if (--remaining) strcat(arguments, ", ");
3531 if (mask & WINED3DSP_WRITEMASK_2)
3533 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3534 if (--remaining) strcat(arguments, ", ");
3536 if (mask & WINED3DSP_WRITEMASK_3)
3538 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3539 if (--remaining) strcat(arguments, ", ");
3542 if (mask_size > 1)
3543 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3544 else
3545 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3548 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3550 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3551 struct wined3d_string_buffer *reg_name;
3553 reg_name = string_buffer_get(priv->string_buffers);
3554 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, NULL, ins->ctx);
3555 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name->buffer, ins->dst[0].write_mask, fixup);
3556 string_buffer_release(priv->string_buffers, reg_name);
3559 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3560 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3561 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3562 const char *coord_reg_fmt, ...)
3564 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3565 char dst_swizzle[6];
3566 struct color_fixup_desc fixup;
3567 BOOL np2_fixup = FALSE;
3568 va_list args;
3569 int ret;
3571 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3573 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3574 * We actually rely on it for vertex shaders and SM4+. */
3575 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3577 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3578 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3580 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3581 np2_fixup = TRUE;
3583 else
3585 fixup = COLOR_FIXUP_IDENTITY;
3588 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3590 if (sample_function->output_single_component)
3591 shader_addline(ins->ctx->buffer, "vec4(");
3593 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3594 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3596 for (;;)
3598 va_start(args, coord_reg_fmt);
3599 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3600 va_end(args);
3601 if (!ret)
3602 break;
3603 if (!string_buffer_resize(ins->ctx->buffer, ret))
3604 break;
3607 if (np2_fixup)
3609 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3610 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3612 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3614 case 1:
3615 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3616 idx >> 1, (idx % 2) ? "z" : "x");
3617 break;
3618 case 2:
3619 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3620 idx >> 1, (idx % 2) ? "zw" : "xy");
3621 break;
3622 case 3:
3623 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3624 idx >> 1, (idx % 2) ? "zw" : "xy");
3625 break;
3626 case 4:
3627 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3628 idx >> 1, (idx % 2) ? "zw" : "xy");
3629 break;
3632 if (dx && dy)
3633 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3634 else if (bias)
3635 shader_addline(ins->ctx->buffer, ", %s", bias);
3636 if (sample_function->offset_size)
3638 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3639 shader_addline(ins->ctx->buffer, ", ");
3640 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3642 shader_addline(ins->ctx->buffer, ")");
3644 if (sample_function->output_single_component)
3645 shader_addline(ins->ctx->buffer, ")");
3647 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3649 if (!is_identity_fixup(fixup))
3650 shader_glsl_color_correction(ins, fixup);
3653 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3655 /* Write the final position.
3657 * OpenGL coordinates specify the center of the pixel while D3D coords
3658 * specify the corner. The offsets are stored in z and w in
3659 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3660 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3661 * a MAD. */
3662 if (use_viewport_index)
3664 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3665 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3667 else
3669 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3670 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3673 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3674 * in utils.c
3676 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3677 * shaders are run before the homogeneous divide, so we have to take the w
3678 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3679 * z = z * 2 - w. */
3680 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3683 /*****************************************************************************
3684 * Begin processing individual instruction opcodes
3685 ****************************************************************************/
3687 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3689 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3690 struct glsl_src_param src0_param;
3691 struct glsl_src_param src1_param;
3692 DWORD write_mask;
3693 const char *op;
3695 /* Determine the GLSL operator to use based on the opcode */
3696 switch (ins->handler_idx)
3698 case WINED3DSIH_ADD: op = "+"; break;
3699 case WINED3DSIH_AND: op = "&"; break;
3700 case WINED3DSIH_DIV: op = "/"; break;
3701 case WINED3DSIH_IADD: op = "+"; break;
3702 case WINED3DSIH_ISHL: op = "<<"; break;
3703 case WINED3DSIH_ISHR: op = ">>"; break;
3704 case WINED3DSIH_MUL: op = "*"; break;
3705 case WINED3DSIH_OR: op = "|"; break;
3706 case WINED3DSIH_SUB: op = "-"; break;
3707 case WINED3DSIH_USHR: op = ">>"; break;
3708 case WINED3DSIH_XOR: op = "^"; break;
3709 default:
3710 op = "<unhandled operator>";
3711 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3712 break;
3715 write_mask = shader_glsl_append_dst(buffer, ins);
3716 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3717 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3718 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3721 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3723 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3724 struct glsl_src_param src0_param;
3725 struct glsl_src_param src1_param;
3726 unsigned int mask_size;
3727 DWORD write_mask;
3728 const char *op;
3730 write_mask = shader_glsl_append_dst(buffer, ins);
3731 mask_size = shader_glsl_get_write_mask_size(write_mask);
3732 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3733 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3735 if (mask_size > 1)
3737 switch (ins->handler_idx)
3739 case WINED3DSIH_EQ: op = "equal"; break;
3740 case WINED3DSIH_IEQ: op = "equal"; break;
3741 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3742 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3743 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3744 case WINED3DSIH_LT: op = "lessThan"; break;
3745 case WINED3DSIH_ILT: op = "lessThan"; break;
3746 case WINED3DSIH_ULT: op = "lessThan"; break;
3747 case WINED3DSIH_NE: op = "notEqual"; break;
3748 case WINED3DSIH_INE: op = "notEqual"; break;
3749 default:
3750 op = "<unhandled operator>";
3751 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3752 break;
3755 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3756 mask_size, op, src0_param.param_str, src1_param.param_str);
3758 else
3760 switch (ins->handler_idx)
3762 case WINED3DSIH_EQ: op = "=="; break;
3763 case WINED3DSIH_IEQ: op = "=="; break;
3764 case WINED3DSIH_GE: op = ">="; break;
3765 case WINED3DSIH_IGE: op = ">="; break;
3766 case WINED3DSIH_UGE: op = ">="; break;
3767 case WINED3DSIH_LT: op = "<"; break;
3768 case WINED3DSIH_ILT: op = "<"; break;
3769 case WINED3DSIH_ULT: op = "<"; break;
3770 case WINED3DSIH_NE: op = "!="; break;
3771 case WINED3DSIH_INE: op = "!="; break;
3772 default:
3773 op = "<unhandled operator>";
3774 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3775 break;
3778 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3779 src0_param.param_str, op, src1_param.param_str);
3783 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3785 struct glsl_src_param src_param;
3786 DWORD write_mask;
3787 const char *op;
3789 switch (ins->handler_idx)
3791 case WINED3DSIH_INEG: op = "-"; break;
3792 case WINED3DSIH_NOT: op = "~"; break;
3793 default:
3794 op = "<unhandled operator>";
3795 ERR("Unhandled opcode %s.\n",
3796 debug_d3dshaderinstructionhandler(ins->handler_idx));
3797 break;
3800 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3801 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3802 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3805 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3807 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3808 struct glsl_src_param src0_param;
3809 struct glsl_src_param src1_param;
3810 DWORD write_mask;
3812 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3813 * If not, we can emulate it. */
3814 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3815 FIXME("64-bit integer multiplies not implemented.\n");
3817 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3819 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3820 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3821 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3823 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3824 src0_param.param_str, src1_param.param_str);
3828 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3830 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3831 struct glsl_src_param src0_param, src1_param;
3832 DWORD write_mask;
3834 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3836 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3838 char dst_mask[6];
3840 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3841 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3842 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3843 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3844 dst_mask, src0_param.param_str, src1_param.param_str);
3846 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[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);
3849 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3851 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3852 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3854 else
3856 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3857 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3858 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3859 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3862 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3864 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3865 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3866 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3867 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3871 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3872 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3874 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3875 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3876 const struct wined3d_gl_info *gl_info = priv->gl_info;
3877 struct glsl_src_param src0_param;
3878 DWORD write_mask;
3880 write_mask = shader_glsl_append_dst(buffer, ins);
3881 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3883 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3884 * shader versions WINED3DSIO_MOVA is used for this. */
3885 if (ins->ctx->reg_maps->shader_version.major == 1
3886 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3887 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3889 /* This is a simple floor() */
3890 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3891 if (mask_size > 1) {
3892 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3893 } else {
3894 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3897 else if (ins->handler_idx == WINED3DSIH_MOVA)
3899 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3901 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3903 if (mask_size > 1)
3904 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3905 else
3906 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3908 else
3910 if (mask_size > 1)
3911 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3912 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3913 else
3914 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3915 src0_param.param_str, src0_param.param_str);
3918 else
3920 shader_addline(buffer, "%s);\n", src0_param.param_str);
3924 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3925 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3927 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3928 struct glsl_src_param src0_param;
3929 struct glsl_src_param src1_param;
3930 DWORD dst_write_mask, src_write_mask;
3931 unsigned int dst_size;
3933 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3934 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3936 /* dp4 works on vec4, dp3 on vec3, etc. */
3937 if (ins->handler_idx == WINED3DSIH_DP4)
3938 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3939 else if (ins->handler_idx == WINED3DSIH_DP3)
3940 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3941 else
3942 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3944 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3945 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3947 if (dst_size > 1) {
3948 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3949 } else {
3950 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3954 /* Note that this instruction has some restrictions. The destination write mask
3955 * can't contain the w component, and the source swizzles have to be .xyzw */
3956 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3958 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3959 struct glsl_src_param src0_param;
3960 struct glsl_src_param src1_param;
3961 char dst_mask[6];
3963 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3964 shader_glsl_append_dst(ins->ctx->buffer, ins);
3965 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3966 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3967 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3970 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3972 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3974 if (!stream)
3975 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3976 else
3977 FIXME("Unhandled primitive stream %u.\n", stream);
3980 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3981 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3982 * GLSL uses the value as-is. */
3983 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3985 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3986 struct glsl_src_param src0_param;
3987 struct glsl_src_param src1_param;
3988 DWORD dst_write_mask;
3989 unsigned int dst_size;
3991 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3992 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3994 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3995 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3997 if (dst_size > 1)
3999 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4000 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4002 else
4004 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4005 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4009 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4010 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4012 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4013 struct glsl_src_param src_param;
4014 const char *instruction;
4015 DWORD write_mask;
4016 unsigned i;
4018 /* Determine the GLSL function to use based on the opcode */
4019 /* TODO: Possibly make this a table for faster lookups */
4020 switch (ins->handler_idx)
4022 case WINED3DSIH_ABS: instruction = "abs"; break;
4023 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4024 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4025 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4026 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4027 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4028 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
4029 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
4030 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
4031 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4032 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4033 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4034 case WINED3DSIH_FRC: instruction = "fract"; break;
4035 case WINED3DSIH_IMAX: instruction = "max"; break;
4036 case WINED3DSIH_IMIN: instruction = "min"; break;
4037 case WINED3DSIH_MAX: instruction = "max"; break;
4038 case WINED3DSIH_MIN: instruction = "min"; break;
4039 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4040 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4041 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4042 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4043 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4044 case WINED3DSIH_UMAX: instruction = "max"; break;
4045 case WINED3DSIH_UMIN: instruction = "min"; break;
4046 default: instruction = "";
4047 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4048 break;
4051 write_mask = shader_glsl_append_dst(buffer, ins);
4053 /* In D3D bits are numbered from the most significant bit. */
4054 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4055 shader_addline(buffer, "31 - ");
4056 shader_addline(buffer, "%s(", instruction);
4058 if (ins->src_count)
4060 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4061 shader_addline(buffer, "%s", src_param.param_str);
4062 for (i = 1; i < ins->src_count; ++i)
4064 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4065 shader_addline(buffer, ", %s", src_param.param_str);
4069 shader_addline(buffer, "));\n");
4072 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4074 struct wined3d_shader_dst_param dst;
4075 struct glsl_src_param src;
4076 DWORD write_mask;
4077 const char *fmt;
4078 unsigned int i;
4080 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4081 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4083 dst = ins->dst[0];
4084 for (i = 0; i < 4; ++i)
4086 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4087 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4088 &dst, dst.reg.data_type)))
4089 continue;
4091 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4092 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4096 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4098 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4099 struct wined3d_shader_dst_param dst;
4100 struct glsl_src_param src[4];
4101 const char *instruction;
4102 BOOL tmp_dst = FALSE;
4103 char mask_char[6];
4104 unsigned int i, j;
4105 DWORD write_mask;
4107 switch (ins->handler_idx)
4109 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4110 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4111 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4112 default:
4113 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4114 return;
4117 for (i = 0; i < ins->src_count; ++i)
4119 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4120 && ins->dst[0].reg.type == ins->src[i].reg.type)
4121 tmp_dst = TRUE;
4124 dst = ins->dst[0];
4125 for (i = 0; i < 4; ++i)
4127 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4128 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4129 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4130 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4131 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, dst.reg.data_type)))
4132 continue;
4134 for (j = 0; j < ins->src_count; ++j)
4135 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4136 shader_addline(buffer, "%s(", instruction);
4137 for (j = 0; j < ins->src_count - 2; ++j)
4138 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4139 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4142 if (tmp_dst)
4144 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
4145 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4146 shader_addline(buffer, "tmp0%s);\n", mask_char);
4150 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4152 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4154 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4155 struct glsl_src_param src_param;
4156 unsigned int mask_size;
4157 DWORD write_mask;
4158 char dst_mask[6];
4160 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4161 mask_size = shader_glsl_get_write_mask_size(write_mask);
4162 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4164 if (mask_size > 3)
4165 shader_addline(buffer, "tmp0.x = dot(vec3(%s), vec3(%s));\n",
4166 src_param.param_str, src_param.param_str);
4167 else
4168 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4169 src_param.param_str, src_param.param_str);
4170 shader_glsl_append_dst(buffer, ins);
4172 shader_addline(buffer, "tmp0.x == 0.0 ? %s : (%s * inversesqrt(tmp0.x)));\n",
4173 src_param.param_str, src_param.param_str);
4176 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4178 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4179 ins->ctx->reg_maps->shader_version.minor);
4180 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4181 struct glsl_src_param src0_param;
4182 const char *prefix, *suffix;
4183 unsigned int dst_size;
4184 DWORD dst_write_mask;
4186 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4187 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4189 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4190 dst_write_mask = WINED3DSP_WRITEMASK_3;
4192 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4194 switch (ins->handler_idx)
4196 case WINED3DSIH_EXP:
4197 case WINED3DSIH_EXPP:
4198 prefix = "exp2(";
4199 suffix = ")";
4200 break;
4202 case WINED3DSIH_LOG:
4203 case WINED3DSIH_LOGP:
4204 prefix = "log2(abs(";
4205 suffix = "))";
4206 break;
4208 case WINED3DSIH_RCP:
4209 prefix = "1.0 / ";
4210 suffix = "";
4211 break;
4213 case WINED3DSIH_RSQ:
4214 prefix = "inversesqrt(abs(";
4215 suffix = "))";
4216 break;
4218 default:
4219 prefix = "";
4220 suffix = "";
4221 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4222 break;
4225 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4226 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4227 else
4228 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4231 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4232 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4233 * dst.x = 2^(floor(src))
4234 * dst.y = src - floor(src)
4235 * dst.z = 2^src (partial precision is allowed, but optional)
4236 * dst.w = 1.0;
4237 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4238 * dst = 2^src; (partial precision is allowed, but optional)
4240 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4242 if (ins->ctx->reg_maps->shader_version.major < 2)
4244 struct glsl_src_param src_param;
4245 char dst_mask[6];
4247 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4249 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4250 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4251 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4252 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4254 shader_glsl_append_dst(ins->ctx->buffer, ins);
4255 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4256 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4257 return;
4260 shader_glsl_scalar_op(ins);
4263 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4264 const char *vector_constructor, const char *scalar_constructor)
4266 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4267 struct glsl_src_param src_param;
4268 unsigned int mask_size;
4269 DWORD write_mask;
4271 write_mask = shader_glsl_append_dst(buffer, ins);
4272 mask_size = shader_glsl_get_write_mask_size(write_mask);
4273 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4275 if (mask_size > 1)
4276 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4277 else
4278 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4281 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4283 shader_glsl_cast(ins, "ivec", "int");
4286 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4288 shader_glsl_cast(ins, "uvec", "uint");
4291 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4293 shader_glsl_cast(ins, "vec", "float");
4296 /** Process signed comparison opcodes in GLSL. */
4297 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4299 struct glsl_src_param src0_param;
4300 struct glsl_src_param src1_param;
4301 DWORD write_mask;
4302 unsigned int mask_size;
4304 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4305 mask_size = shader_glsl_get_write_mask_size(write_mask);
4306 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4307 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4309 if (mask_size > 1) {
4310 const char *compare;
4312 switch(ins->handler_idx)
4314 case WINED3DSIH_SLT: compare = "lessThan"; break;
4315 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4316 default: compare = "";
4317 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4320 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4321 src0_param.param_str, src1_param.param_str);
4322 } else {
4323 switch(ins->handler_idx)
4325 case WINED3DSIH_SLT:
4326 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4327 * to return 0.0 but step returns 1.0 because step is not < x
4328 * An alternative is a bvec compare padded with an unused second component.
4329 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4330 * issue. Playing with not() is not possible either because not() does not accept
4331 * a scalar.
4333 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4334 src0_param.param_str, src1_param.param_str);
4335 break;
4336 case WINED3DSIH_SGE:
4337 /* Here we can use the step() function and safe a conditional */
4338 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4339 break;
4340 default:
4341 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4347 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4349 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4350 struct wined3d_shader_dst_param dst[2];
4351 struct glsl_src_param src[3];
4352 unsigned int i, j, k;
4353 char mask_char[6];
4354 DWORD write_mask;
4355 BOOL tmp_dst[2];
4357 for (i = 0; i < ins->dst_count; ++i)
4359 tmp_dst[i] = FALSE;
4360 for (j = 0; j < ins->src_count; ++j)
4362 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4363 && ins->dst[i].reg.type == ins->src[j].reg.type)
4364 tmp_dst[i] = TRUE;
4368 dst[0] = ins->dst[0];
4369 dst[1] = ins->dst[1];
4370 for (i = 0; i < 4; ++i)
4372 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4374 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4375 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4376 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4377 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], dst[j].reg.data_type)))
4378 continue;
4380 for (k = 0; k < ARRAY_SIZE(src); ++k)
4381 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4383 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4384 src[0].param_str, src[1].param_str, src[2].param_str);
4388 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4390 if (tmp_dst[i])
4392 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4393 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], ins->dst[i].reg.data_type);
4394 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4399 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4401 const char *condition_prefix, *condition_suffix;
4402 struct wined3d_shader_dst_param dst;
4403 struct glsl_src_param src0_param;
4404 struct glsl_src_param src1_param;
4405 struct glsl_src_param src2_param;
4406 BOOL temp_destination = FALSE;
4407 DWORD cmp_channel = 0;
4408 unsigned int i, j;
4409 char mask_char[6];
4410 DWORD write_mask;
4412 switch (ins->handler_idx)
4414 case WINED3DSIH_CMP:
4415 condition_prefix = "";
4416 condition_suffix = " >= 0.0";
4417 break;
4419 case WINED3DSIH_CND:
4420 condition_prefix = "";
4421 condition_suffix = " > 0.5";
4422 break;
4424 case WINED3DSIH_MOVC:
4425 condition_prefix = "bool(";
4426 condition_suffix = ")";
4427 break;
4429 default:
4430 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4431 condition_prefix = "<unhandled prefix>";
4432 condition_suffix = "<unhandled suffix>";
4433 break;
4436 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4438 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4439 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4440 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4441 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4443 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4444 condition_prefix, src0_param.param_str, condition_suffix,
4445 src1_param.param_str, src2_param.param_str);
4446 return;
4449 dst = ins->dst[0];
4451 /* Splitting the instruction up in multiple lines imposes a problem:
4452 * The first lines may overwrite source parameters of the following lines.
4453 * Deal with that by using a temporary destination register if needed. */
4454 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4455 && ins->src[0].reg.type == dst.reg.type)
4456 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4457 && ins->src[1].reg.type == dst.reg.type)
4458 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4459 && ins->src[2].reg.type == dst.reg.type))
4460 temp_destination = TRUE;
4462 /* Cycle through all source0 channels. */
4463 for (i = 0; i < 4; ++i)
4465 write_mask = 0;
4466 /* Find the destination channels which use the current source0 channel. */
4467 for (j = 0; j < 4; ++j)
4469 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4471 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4472 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4475 dst.write_mask = ins->dst[0].write_mask & write_mask;
4477 if (temp_destination)
4479 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4480 continue;
4481 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4483 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4484 continue;
4486 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4487 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4488 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4490 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4491 condition_prefix, src0_param.param_str, condition_suffix,
4492 src1_param.param_str, src2_param.param_str);
4495 if (temp_destination)
4497 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4498 shader_glsl_append_dst(ins->ctx->buffer, ins);
4499 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4503 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4504 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4505 * the compare is done per component of src0. */
4506 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4508 struct glsl_src_param src0_param;
4509 struct glsl_src_param src1_param;
4510 struct glsl_src_param src2_param;
4511 DWORD write_mask;
4512 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4513 ins->ctx->reg_maps->shader_version.minor);
4515 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4517 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4518 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4519 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4520 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4522 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4523 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4524 else
4525 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4526 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4527 return;
4530 shader_glsl_conditional_move(ins);
4533 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4534 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4536 struct glsl_src_param src0_param;
4537 struct glsl_src_param src1_param;
4538 struct glsl_src_param src2_param;
4539 DWORD write_mask;
4541 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4542 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4543 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4544 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4545 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4546 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4549 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4550 Vertex shaders to GLSL codes */
4551 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4553 int i;
4554 int nComponents = 0;
4555 struct wined3d_shader_dst_param tmp_dst = {{0}};
4556 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4557 struct wined3d_shader_instruction tmp_ins;
4559 memset(&tmp_ins, 0, sizeof(tmp_ins));
4561 /* Set constants for the temporary argument */
4562 tmp_ins.ctx = ins->ctx;
4563 tmp_ins.dst_count = 1;
4564 tmp_ins.dst = &tmp_dst;
4565 tmp_ins.src_count = 2;
4566 tmp_ins.src = tmp_src;
4568 switch(ins->handler_idx)
4570 case WINED3DSIH_M4x4:
4571 nComponents = 4;
4572 tmp_ins.handler_idx = WINED3DSIH_DP4;
4573 break;
4574 case WINED3DSIH_M4x3:
4575 nComponents = 3;
4576 tmp_ins.handler_idx = WINED3DSIH_DP4;
4577 break;
4578 case WINED3DSIH_M3x4:
4579 nComponents = 4;
4580 tmp_ins.handler_idx = WINED3DSIH_DP3;
4581 break;
4582 case WINED3DSIH_M3x3:
4583 nComponents = 3;
4584 tmp_ins.handler_idx = WINED3DSIH_DP3;
4585 break;
4586 case WINED3DSIH_M3x2:
4587 nComponents = 2;
4588 tmp_ins.handler_idx = WINED3DSIH_DP3;
4589 break;
4590 default:
4591 break;
4594 tmp_dst = ins->dst[0];
4595 tmp_src[0] = ins->src[0];
4596 tmp_src[1] = ins->src[1];
4597 for (i = 0; i < nComponents; ++i)
4599 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4600 shader_glsl_dot(&tmp_ins);
4601 ++tmp_src[1].reg.idx[0].offset;
4606 The LRP instruction performs a component-wise linear interpolation
4607 between the second and third operands using the first operand as the
4608 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4609 This is equivalent to mix(src2, src1, src0);
4611 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4613 struct glsl_src_param src0_param;
4614 struct glsl_src_param src1_param;
4615 struct glsl_src_param src2_param;
4616 DWORD write_mask;
4618 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4620 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4621 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4622 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4624 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4625 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4628 /** Process the WINED3DSIO_LIT instruction in GLSL:
4629 * dst.x = dst.w = 1.0
4630 * dst.y = (src0.x > 0) ? src0.x
4631 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4632 * where src.w is clamped at +- 128
4634 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4636 struct glsl_src_param src0_param;
4637 struct glsl_src_param src1_param;
4638 struct glsl_src_param src3_param;
4639 char dst_mask[6];
4641 shader_glsl_append_dst(ins->ctx->buffer, ins);
4642 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4644 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4645 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4646 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4648 /* The sdk specifies the instruction like this
4649 * dst.x = 1.0;
4650 * if(src.x > 0.0) dst.y = src.x
4651 * else dst.y = 0.0.
4652 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4653 * else dst.z = 0.0;
4654 * dst.w = 1.0;
4655 * (where power = src.w clamped between -128 and 128)
4657 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4658 * dst.x = 1.0 ... No further explanation needed
4659 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4660 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4661 * dst.w = 1.0. ... Nothing fancy.
4663 * So we still have one conditional in there. So do this:
4664 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4666 * 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),
4667 * 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.
4668 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4670 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4671 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4672 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4674 shader_addline(ins->ctx->buffer,
4675 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4676 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4677 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4678 src0_param.param_str, src3_param.param_str, dst_mask);
4681 /** Process the WINED3DSIO_DST instruction in GLSL:
4682 * dst.x = 1.0
4683 * dst.y = src0.x * src0.y
4684 * dst.z = src0.z
4685 * dst.w = src1.w
4687 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4689 struct glsl_src_param src0y_param;
4690 struct glsl_src_param src0z_param;
4691 struct glsl_src_param src1y_param;
4692 struct glsl_src_param src1w_param;
4693 char dst_mask[6];
4695 shader_glsl_append_dst(ins->ctx->buffer, ins);
4696 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4698 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4699 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4700 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4701 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4703 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4704 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4707 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4708 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4709 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4711 * dst.x = cos(src0.?)
4712 * dst.y = sin(src0.?)
4713 * dst.z = dst.z
4714 * dst.w = dst.w
4716 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4718 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4719 struct glsl_src_param src0_param;
4720 DWORD write_mask;
4722 if (ins->ctx->reg_maps->shader_version.major < 4)
4724 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4726 write_mask = shader_glsl_append_dst(buffer, ins);
4727 switch (write_mask)
4729 case WINED3DSP_WRITEMASK_0:
4730 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4731 break;
4733 case WINED3DSP_WRITEMASK_1:
4734 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4735 break;
4737 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4738 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4739 src0_param.param_str, src0_param.param_str);
4740 break;
4742 default:
4743 ERR("Write mask should be .x, .y or .xy\n");
4744 break;
4747 return;
4750 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4753 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4755 char dst_mask[6];
4757 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4758 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4759 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4761 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4762 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4763 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4765 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4766 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4768 else
4770 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4771 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4772 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4775 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4777 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4778 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4779 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4783 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4784 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4785 * generate invalid code
4787 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4789 struct glsl_src_param src0_param;
4790 DWORD write_mask;
4792 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4793 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4795 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4798 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4799 * Start a for() loop where src1.y is the initial value of aL,
4800 * increment aL by src1.z for a total of src1.x iterations.
4801 * Need to use a temporary variable for this operation.
4803 /* FIXME: I don't think nested loops will work correctly this way. */
4804 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4806 struct wined3d_shader_parser_state *state = ins->ctx->state;
4807 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4808 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4809 const struct wined3d_shader *shader = ins->ctx->shader;
4810 const struct wined3d_shader_lconst *constant;
4811 struct wined3d_string_buffer *reg_name;
4812 const DWORD *control_values = NULL;
4814 if (ins->ctx->reg_maps->shader_version.major < 4)
4816 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4817 * class hardware doesn't support real varying indexing, but Microsoft
4818 * designed this feature for Shader model 2.x+. If the loop control is
4819 * known at compile time, the GLSL compiler can unroll the loop, and
4820 * replace indirect addressing with direct addressing. */
4821 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4823 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4825 if (constant->idx == ins->src[1].reg.idx[0].offset)
4827 control_values = constant->value;
4828 break;
4833 if (control_values)
4835 struct wined3d_shader_loop_control loop_control;
4836 loop_control.count = control_values[0];
4837 loop_control.start = control_values[1];
4838 loop_control.step = (int)control_values[2];
4840 if (loop_control.step > 0)
4842 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4843 state->current_loop_depth, loop_control.start,
4844 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4845 state->current_loop_depth, loop_control.step);
4847 else if (loop_control.step < 0)
4849 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4850 state->current_loop_depth, loop_control.start,
4851 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4852 state->current_loop_depth, loop_control.step);
4854 else
4856 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4857 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4858 state->current_loop_depth, loop_control.count,
4859 state->current_loop_depth);
4862 else
4864 reg_name = string_buffer_get(priv->string_buffers);
4865 shader_glsl_get_register_name(&ins->src[1].reg, ins->src[1].reg.data_type, reg_name, NULL, ins->ctx);
4867 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4868 state->current_loop_depth, state->current_loop_reg, reg_name->buffer,
4869 state->current_loop_depth, reg_name->buffer,
4870 state->current_loop_depth, state->current_loop_reg, reg_name->buffer);
4872 string_buffer_release(priv->string_buffers, reg_name);
4876 ++state->current_loop_reg;
4878 else
4880 shader_addline(buffer, "for (;;)\n{\n");
4883 ++state->current_loop_depth;
4886 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4888 struct wined3d_shader_parser_state *state = ins->ctx->state;
4890 shader_addline(ins->ctx->buffer, "}\n");
4892 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4894 --state->current_loop_depth;
4895 --state->current_loop_reg;
4898 if (ins->handler_idx == WINED3DSIH_ENDREP)
4900 --state->current_loop_depth;
4904 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4906 struct wined3d_shader_parser_state *state = ins->ctx->state;
4907 const struct wined3d_shader *shader = ins->ctx->shader;
4908 const struct wined3d_shader_lconst *constant;
4909 struct glsl_src_param src0_param;
4910 const DWORD *control_values = NULL;
4912 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4913 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4915 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4917 if (constant->idx == ins->src[0].reg.idx[0].offset)
4919 control_values = constant->value;
4920 break;
4925 if (control_values)
4927 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4928 state->current_loop_depth, state->current_loop_depth,
4929 control_values[0], state->current_loop_depth);
4931 else
4933 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4934 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4935 state->current_loop_depth, state->current_loop_depth,
4936 src0_param.param_str, state->current_loop_depth);
4939 ++state->current_loop_depth;
4942 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4944 struct glsl_src_param src0_param;
4946 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4947 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4950 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4952 struct glsl_src_param src0_param;
4954 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4955 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4958 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4960 shader_addline(ins->ctx->buffer, "default:\n");
4963 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
4965 struct glsl_src_param src_param;
4966 const char *condition;
4968 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
4969 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4970 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
4973 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4975 shader_glsl_generate_condition(ins);
4976 shader_addline(ins->ctx->buffer, "{\n");
4979 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4981 struct glsl_src_param src0_param;
4982 struct glsl_src_param src1_param;
4984 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4985 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4987 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4988 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4991 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4993 shader_addline(ins->ctx->buffer, "} else {\n");
4996 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4998 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4999 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5000 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5002 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5003 if (!priv->gl_info->supported[ARB_CLIP_CONTROL])
5004 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5006 if (!stream)
5007 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5008 else
5009 FIXME("Unhandled primitive stream %u.\n", stream);
5012 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5014 shader_addline(ins->ctx->buffer, "break;\n");
5017 /* FIXME: According to MSDN the compare is done per component. */
5018 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5020 struct glsl_src_param src0_param;
5021 struct glsl_src_param src1_param;
5023 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5024 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5026 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5027 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5030 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5032 const char *op;
5034 switch (ins->handler_idx)
5036 case WINED3DSIH_BREAKP:
5037 op = "break;";
5038 break;
5039 case WINED3DSIH_CONTINUEP:
5040 op = "continue;";
5041 break;
5042 case WINED3DSIH_RETP:
5043 op = "return;";
5044 break;
5045 default:
5046 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5047 return;
5050 shader_glsl_generate_condition(ins);
5051 if (ins->handler_idx == WINED3DSIH_RETP)
5053 shader_addline(ins->ctx->buffer, "{\n");
5054 shader_glsl_generate_shader_epilogue(ins->ctx);
5056 shader_addline(ins->ctx->buffer, " %s\n", op);
5057 if (ins->handler_idx == WINED3DSIH_RETP)
5058 shader_addline(ins->ctx->buffer, "}\n");
5061 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5063 shader_addline(ins->ctx->buffer, "continue;\n");
5066 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5068 shader_addline(ins->ctx->buffer, "}\n");
5069 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5071 /* Subroutines appear at the end of the shader. */
5072 ins->ctx->state->in_subroutine = TRUE;
5075 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5077 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5080 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5082 struct glsl_src_param src1_param;
5084 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5085 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5086 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5089 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5091 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5093 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5095 shader_glsl_generate_shader_epilogue(ins->ctx);
5096 shader_addline(ins->ctx->buffer, "return;\n");
5100 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5102 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5103 ins->ctx->reg_maps->shader_version.minor);
5104 struct glsl_sample_function sample_function;
5105 DWORD sample_flags = 0;
5106 DWORD resource_idx;
5107 DWORD mask = 0, swizzle;
5108 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5109 enum wined3d_shader_resource_type resource_type;
5111 /* 1.0-1.4: Use destination register as sampler source.
5112 * 2.0+: Use provided sampler source. */
5113 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5114 resource_idx = ins->dst[0].reg.idx[0].offset;
5115 else
5116 resource_idx = ins->src[1].reg.idx[0].offset;
5118 resource_type = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
5119 ? pixelshader_get_resource_type(ins->ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
5120 : ins->ctx->reg_maps->resource_info[resource_idx].type;
5122 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5124 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5125 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5127 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5128 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5130 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5131 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5133 case WINED3D_TTFF_COUNT1:
5134 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5135 break;
5136 case WINED3D_TTFF_COUNT2:
5137 mask = WINED3DSP_WRITEMASK_1;
5138 break;
5139 case WINED3D_TTFF_COUNT3:
5140 mask = WINED3DSP_WRITEMASK_2;
5141 break;
5142 case WINED3D_TTFF_COUNT4:
5143 case WINED3D_TTFF_DISABLE:
5144 mask = WINED3DSP_WRITEMASK_3;
5145 break;
5149 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5151 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5153 if (src_mod == WINED3DSPSM_DZ) {
5154 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5155 mask = WINED3DSP_WRITEMASK_2;
5156 } else if (src_mod == WINED3DSPSM_DW) {
5157 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5158 mask = WINED3DSP_WRITEMASK_3;
5161 else
5163 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5164 && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5166 /* ps 2.0 texldp instruction always divides by the fourth component. */
5167 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5168 mask = WINED3DSP_WRITEMASK_3;
5172 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5173 mask |= sample_function.coord_mask;
5174 sample_function.coord_mask = mask;
5176 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5177 else swizzle = ins->src[1].swizzle;
5179 /* 1.0-1.3: Use destination register as coordinate source.
5180 1.4+: Use provided coordinate source register. */
5181 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5183 char coord_mask[6];
5184 shader_glsl_write_mask_to_str(mask, coord_mask);
5185 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5186 "T%u%s", resource_idx, coord_mask);
5188 else
5190 struct glsl_src_param coord_param;
5191 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5192 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5194 struct glsl_src_param bias;
5195 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5196 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5197 NULL, "%s", coord_param.param_str);
5198 } else {
5199 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5200 "%s", coord_param.param_str);
5203 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5206 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5208 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5209 const struct wined3d_gl_info *gl_info = priv->gl_info;
5210 struct glsl_src_param coord_param, dx_param, dy_param;
5211 struct glsl_sample_function sample_function;
5212 DWORD sampler_idx;
5213 DWORD swizzle = ins->src[1].swizzle;
5215 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5217 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5218 shader_glsl_tex(ins);
5219 return;
5222 sampler_idx = ins->src[1].reg.idx[0].offset;
5224 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5225 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5226 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5227 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5229 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5230 NULL, NULL, "%s", coord_param.param_str);
5231 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5234 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5236 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5237 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5238 const struct wined3d_gl_info *gl_info = priv->gl_info;
5239 struct glsl_src_param coord_param, lod_param;
5240 struct glsl_sample_function sample_function;
5241 DWORD swizzle = ins->src[1].swizzle;
5242 DWORD sampler_idx;
5244 sampler_idx = ins->src[1].reg.idx[0].offset;
5246 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5247 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5249 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5251 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5252 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5254 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5255 * However, the NVIDIA drivers allow them in fragment shaders as well,
5256 * even without the appropriate extension. */
5257 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5259 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5260 "%s", coord_param.param_str);
5261 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5264 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5265 unsigned int resource_idx, unsigned int sampler_idx)
5267 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5268 unsigned int i;
5270 for (i = 0; i < sampler_map->count; ++i)
5272 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5273 return entries[i].bind_idx;
5276 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5278 return ~0u;
5281 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5283 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5284 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5285 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5286 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5287 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5288 struct glsl_src_param structure_idx, offset, data, data2;
5289 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5290 enum wined3d_shader_resource_type resource_type;
5291 struct wined3d_string_buffer *address;
5292 enum wined3d_data_type data_type;
5293 unsigned int resource_idx, stride;
5294 const char *op, *resource;
5295 DWORD coord_mask;
5296 BOOL is_tgsm;
5298 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5299 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5300 if (is_tgsm)
5302 if (resource_idx >= reg_maps->tgsm_count)
5304 ERR("Invalid TGSM index %u.\n", resource_idx);
5305 return;
5307 resource = "g";
5308 data_type = WINED3D_DATA_UINT;
5309 coord_mask = 1;
5310 stride = reg_maps->tgsm[resource_idx].stride;
5312 else
5314 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5316 ERR("Invalid UAV index %u.\n", resource_idx);
5317 return;
5319 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5320 if (resource_type >= ARRAY_SIZE(resource_type_info))
5322 ERR("Unexpected resource type %#x.\n", resource_type);
5323 return;
5325 resource = "image";
5326 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5327 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5328 stride = reg_maps->uav_resource_info[resource_idx].stride;
5331 switch (ins->handler_idx)
5333 case WINED3DSIH_ATOMIC_AND:
5334 case WINED3DSIH_IMM_ATOMIC_AND:
5335 if (is_tgsm)
5336 op = "atomicAnd";
5337 else
5338 op = "imageAtomicAnd";
5339 break;
5340 case WINED3DSIH_ATOMIC_CMP_STORE:
5341 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5342 if (is_tgsm)
5343 op = "atomicCompSwap";
5344 else
5345 op = "imageAtomicCompSwap";
5346 break;
5347 case WINED3DSIH_ATOMIC_IADD:
5348 case WINED3DSIH_IMM_ATOMIC_IADD:
5349 if (is_tgsm)
5350 op = "atomicAdd";
5351 else
5352 op = "imageAtomicAdd";
5353 break;
5354 case WINED3DSIH_ATOMIC_IMAX:
5355 case WINED3DSIH_IMM_ATOMIC_IMAX:
5356 if (is_tgsm)
5357 op = "atomicMax";
5358 else
5359 op = "imageAtomicMax";
5360 if (data_type != WINED3D_DATA_INT)
5362 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5363 return;
5365 break;
5366 case WINED3DSIH_ATOMIC_IMIN:
5367 case WINED3DSIH_IMM_ATOMIC_IMIN:
5368 if (is_tgsm)
5369 op = "atomicMin";
5370 else
5371 op = "imageAtomicMin";
5372 if (data_type != WINED3D_DATA_INT)
5374 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5375 return;
5377 break;
5378 case WINED3DSIH_ATOMIC_OR:
5379 case WINED3DSIH_IMM_ATOMIC_OR:
5380 if (is_tgsm)
5381 op = "atomicOr";
5382 else
5383 op = "imageAtomicOr";
5384 break;
5385 case WINED3DSIH_ATOMIC_UMAX:
5386 case WINED3DSIH_IMM_ATOMIC_UMAX:
5387 if (is_tgsm)
5388 op = "atomicMax";
5389 else
5390 op = "imageAtomicMax";
5391 if (data_type != WINED3D_DATA_UINT)
5393 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5394 return;
5396 break;
5397 case WINED3DSIH_ATOMIC_UMIN:
5398 case WINED3DSIH_IMM_ATOMIC_UMIN:
5399 if (is_tgsm)
5400 op = "atomicMin";
5401 else
5402 op = "imageAtomicMin";
5403 if (data_type != WINED3D_DATA_UINT)
5405 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5406 return;
5408 break;
5409 case WINED3DSIH_ATOMIC_XOR:
5410 case WINED3DSIH_IMM_ATOMIC_XOR:
5411 if (is_tgsm)
5412 op = "atomicXor";
5413 else
5414 op = "imageAtomicXor";
5415 break;
5416 case WINED3DSIH_IMM_ATOMIC_EXCH:
5417 if (is_tgsm)
5418 op = "atomicExchange";
5419 else
5420 op = "imageAtomicExchange";
5421 break;
5422 default:
5423 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5424 return;
5427 address = string_buffer_get(priv->string_buffers);
5428 if (stride)
5430 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5431 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5432 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5434 else
5436 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5437 string_buffer_sprintf(address, "%s", offset.param_str);
5438 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5439 shader_addline(address, "/ 4");
5442 if (is_imm_instruction)
5443 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5445 if (is_tgsm)
5446 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5447 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5448 else
5449 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5450 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5452 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5453 shader_addline(buffer, "%s", data.param_str);
5454 if (ins->src_count >= 3)
5456 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5457 shader_addline(buffer, ", %s", data2.param_str);
5460 if (is_imm_instruction)
5461 shader_addline(buffer, ")");
5462 shader_addline(buffer, ");\n");
5464 string_buffer_release(priv->string_buffers, address);
5467 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5469 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5470 const char *op;
5472 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5473 op = "atomicCounterIncrement";
5474 else
5475 op = "atomicCounterDecrement";
5477 shader_glsl_append_dst(ins->ctx->buffer, ins);
5478 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5481 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5483 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5484 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5485 enum wined3d_shader_resource_type resource_type;
5486 struct glsl_src_param image_coord_param;
5487 enum wined3d_data_type data_type;
5488 DWORD coord_mask, write_mask;
5489 unsigned int uav_idx;
5490 char dst_swizzle[6];
5492 uav_idx = ins->src[1].reg.idx[0].offset;
5493 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5495 ERR("Invalid UAV index %u.\n", uav_idx);
5496 return;
5498 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5499 if (resource_type >= ARRAY_SIZE(resource_type_info))
5501 ERR("Unexpected resource type %#x.\n", resource_type);
5502 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5504 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5505 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5507 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5508 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5510 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5511 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5512 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5515 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5517 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5518 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5519 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5520 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5521 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5522 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5523 struct glsl_src_param structure_idx, offset;
5524 struct wined3d_string_buffer *address;
5525 struct wined3d_shader_dst_param dst;
5526 const char *function, *resource;
5528 resource_idx = src->reg.idx[0].offset;
5529 if (src->reg.type == WINED3DSPR_RESOURCE)
5531 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5533 ERR("Invalid resource index %u.\n", resource_idx);
5534 return;
5536 stride = reg_maps->resource_info[resource_idx].stride;
5537 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5538 function = "texelFetch";
5539 resource = "sampler";
5541 else if (src->reg.type == WINED3DSPR_UAV)
5543 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5545 ERR("Invalid UAV index %u.\n", resource_idx);
5546 return;
5548 stride = reg_maps->uav_resource_info[resource_idx].stride;
5549 bind_idx = resource_idx;
5550 function = "imageLoad";
5551 resource = "image";
5553 else
5555 if (resource_idx >= reg_maps->tgsm_count)
5557 ERR("Invalid TGSM index %u.\n", resource_idx);
5558 return;
5560 stride = reg_maps->tgsm[resource_idx].stride;
5561 bind_idx = resource_idx;
5562 function = NULL;
5563 resource = "g";
5566 address = string_buffer_get(priv->string_buffers);
5567 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5569 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5570 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5572 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5573 shader_addline(address, "%s / 4", offset.param_str);
5575 dst = ins->dst[0];
5576 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5578 /* The instruction is split into multiple lines. The first lines may
5579 * overwrite source parameters of the following lines. */
5580 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5581 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5584 for (i = 0; i < 4; ++i)
5586 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5587 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5588 continue;
5590 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5591 if (function)
5592 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5593 function, prefix, resource, bind_idx, address->buffer, swizzle);
5594 else
5595 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5596 prefix, resource, bind_idx, address->buffer, swizzle);
5599 string_buffer_release(priv->string_buffers, address);
5602 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5604 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5605 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5606 struct glsl_src_param image_coord_param, image_data_param;
5607 enum wined3d_shader_resource_type resource_type;
5608 enum wined3d_data_type data_type;
5609 unsigned int uav_idx;
5610 DWORD coord_mask;
5612 uav_idx = ins->dst[0].reg.idx[0].offset;
5613 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5615 ERR("Invalid UAV index %u.\n", uav_idx);
5616 return;
5618 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5619 if (resource_type >= ARRAY_SIZE(resource_type_info))
5621 ERR("Unexpected resource type %#x.\n", resource_type);
5622 return;
5624 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5625 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5627 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5628 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5629 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5630 shader_glsl_get_prefix(version->type), uav_idx,
5631 image_coord_param.param_str, image_data_param.param_str);
5634 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5636 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5637 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5638 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5639 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5640 struct glsl_src_param structure_idx, offset, data;
5641 unsigned int i, resource_idx, stride, src_idx = 0;
5642 struct wined3d_string_buffer *address;
5643 DWORD write_mask;
5644 BOOL is_tgsm;
5646 resource_idx = ins->dst[0].reg.idx[0].offset;
5647 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5648 if (is_tgsm)
5650 if (resource_idx >= reg_maps->tgsm_count)
5652 ERR("Invalid TGSM index %u.\n", resource_idx);
5653 return;
5655 stride = reg_maps->tgsm[resource_idx].stride;
5657 else
5659 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5661 ERR("Invalid UAV index %u.\n", resource_idx);
5662 return;
5664 stride = reg_maps->uav_resource_info[resource_idx].stride;
5667 address = string_buffer_get(priv->string_buffers);
5668 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5670 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5671 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5673 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5674 shader_addline(address, "%s / 4", offset.param_str);
5676 for (i = 0; i < 4; ++i)
5678 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5679 continue;
5681 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5683 if (is_tgsm)
5684 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5685 prefix, resource_idx, address->buffer, i, data.param_str);
5686 else
5687 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5688 prefix, resource_idx, address->buffer, i, data.param_str);
5691 string_buffer_release(priv->string_buffers, address);
5694 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5696 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5697 unsigned int sync_flags = ins->flags;
5699 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5701 shader_addline(buffer, "barrier();\n");
5702 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5705 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5707 shader_addline(buffer, "memoryBarrierShared();\n");
5708 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5711 if (sync_flags)
5712 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5715 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5716 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5718 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5719 unsigned int idx = reg->idx[0].offset;
5721 if (reg->type == WINED3DSPR_RESOURCE)
5723 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5725 ERR("Invalid resource index %u.\n", idx);
5726 return NULL;
5728 return &reg_maps->resource_info[idx];
5731 if (reg->type == WINED3DSPR_UAV)
5733 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5735 ERR("Invalid UAV index %u.\n", idx);
5736 return NULL;
5738 return &reg_maps->uav_resource_info[idx];
5741 FIXME("Unhandled register type %#x.\n", reg->type);
5742 return NULL;
5745 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5747 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5748 const struct wined3d_shader_resource_info *resource_info;
5749 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5750 unsigned int resource_idx;
5751 char dst_swizzle[6];
5752 DWORD write_mask;
5754 write_mask = shader_glsl_append_dst(buffer, ins);
5755 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5757 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5758 return;
5759 resource_idx = ins->src[0].reg.idx[0].offset;
5761 shader_addline(buffer, "ivec2(");
5762 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5764 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5765 resource_idx, WINED3D_SAMPLER_DEFAULT);
5766 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5768 else
5770 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5772 if (resource_info->stride)
5773 shader_addline(buffer, " / %u", resource_info->stride);
5774 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5775 shader_addline(buffer, " * 4");
5776 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5779 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5781 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5782 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5785 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5787 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5790 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5792 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5793 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5794 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5795 const struct wined3d_gl_info *gl_info = priv->gl_info;
5796 enum wined3d_shader_resource_type resource_type;
5797 enum wined3d_shader_register_type reg_type;
5798 unsigned int resource_idx, bind_idx, i;
5799 enum wined3d_data_type dst_data_type;
5800 struct glsl_src_param lod_param;
5801 BOOL supports_mipmaps;
5802 char dst_swizzle[6];
5803 DWORD write_mask;
5805 dst_data_type = ins->dst[0].reg.data_type;
5806 if (ins->flags == WINED3DSI_RESINFO_UINT)
5807 dst_data_type = WINED3D_DATA_UINT;
5808 else if (ins->flags)
5809 FIXME("Unhandled flags %#x.\n", ins->flags);
5811 reg_type = ins->src[1].reg.type;
5812 resource_idx = ins->src[1].reg.idx[0].offset;
5813 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5814 if (reg_type == WINED3DSPR_RESOURCE)
5816 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5817 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5818 resource_idx, WINED3D_SAMPLER_DEFAULT);
5820 else
5822 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5823 bind_idx = resource_idx;
5826 if (resource_type >= ARRAY_SIZE(resource_type_info))
5828 ERR("Unexpected resource type %#x.\n", resource_type);
5829 return;
5832 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], dst_data_type);
5833 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5835 if (dst_data_type == WINED3D_DATA_UINT)
5836 shader_addline(buffer, "uvec4(");
5837 else
5838 shader_addline(buffer, "vec4(");
5840 if (reg_type == WINED3DSPR_RESOURCE)
5842 shader_addline(buffer, "textureSize(%s_sampler%u",
5843 shader_glsl_get_prefix(version->type), bind_idx);
5845 else
5847 shader_addline(buffer, "imageSize(%s_image%u",
5848 shader_glsl_get_prefix(version->type), bind_idx);
5851 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5852 if (supports_mipmaps)
5853 shader_addline(buffer, ", %s", lod_param.param_str);
5854 shader_addline(buffer, "), ");
5856 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5857 shader_addline(buffer, "0, ");
5859 if (supports_mipmaps)
5861 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5863 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5864 shader_glsl_get_prefix(version->type), bind_idx);
5866 else
5868 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
5869 shader_addline(buffer, "1");
5872 else
5874 shader_addline(buffer, "1");
5877 shader_addline(buffer, ")%s);\n", dst_swizzle);
5880 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
5882 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5883 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5884 const struct wined3d_gl_info *gl_info = priv->gl_info;
5885 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5886 const struct wined3d_shader_dst_param *dst = ins->dst;
5887 const struct wined3d_shader_src_param *src = ins->src;
5888 enum wined3d_shader_resource_type resource_type;
5889 enum wined3d_data_type dst_data_type;
5890 unsigned int resource_idx, bind_idx;
5891 char dst_swizzle[6];
5892 DWORD write_mask;
5894 dst_data_type = dst->reg.data_type;
5895 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
5896 dst_data_type = WINED3D_DATA_UINT;
5897 else if (ins->flags)
5898 FIXME("Unhandled flags %#x.\n", ins->flags);
5900 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, dst_data_type);
5901 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
5903 if (dst_data_type == WINED3D_DATA_UINT)
5904 shader_addline(buffer, "uvec4(");
5905 else
5906 shader_addline(buffer, "vec4(");
5908 if (src->reg.type == WINED3DSPR_RASTERIZER)
5910 if (gl_info->supported[ARB_SAMPLE_SHADING])
5912 shader_addline(buffer, "gl_NumSamples");
5914 else
5916 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
5917 shader_addline(buffer, "1");
5920 else
5922 resource_idx = src->reg.idx[0].offset;
5923 resource_type = reg_maps->resource_info[resource_idx].type;
5924 if (resource_type >= ARRAY_SIZE(resource_type_info))
5926 ERR("Unexpected resource type %#x.\n", resource_type);
5927 return;
5929 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5931 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
5933 shader_addline(buffer, "textureSamples(%s_sampler%u)",
5934 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
5936 else
5938 FIXME("textureSamples() is not supported.\n");
5939 shader_addline(buffer, "1");
5943 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
5946 static void shader_glsl_interpolate(const struct wined3d_shader_instruction *ins)
5948 const struct wined3d_shader_src_param *input = &ins->src[0];
5949 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5950 struct glsl_src_param sample_param;
5951 char dst_swizzle[6];
5952 DWORD write_mask;
5954 write_mask = shader_glsl_append_dst(buffer, ins);
5955 shader_glsl_get_swizzle(input, FALSE, write_mask, dst_swizzle);
5957 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &sample_param);
5958 shader_addline(buffer, "interpolateAtSample(shader_in.reg%u, %s)%s);\n",
5959 input->reg.idx[0].offset, sample_param.param_str, dst_swizzle);
5962 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5964 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5965 struct glsl_src_param coord_param, lod_param, sample_param;
5966 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5967 struct glsl_sample_function sample_function;
5968 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5969 BOOL has_lod_param;
5971 if (wined3d_shader_instruction_has_texel_offset(ins))
5972 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5974 resource_idx = ins->src[1].reg.idx[0].offset;
5975 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5977 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5979 ERR("Invalid resource index %u.\n", resource_idx);
5980 return;
5982 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
5984 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5985 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5986 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5987 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5988 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
5990 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
5991 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5992 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
5994 else
5996 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5997 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5998 "%s", coord_param.param_str);
6000 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6003 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6005 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6006 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6007 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6008 struct glsl_sample_function sample_function;
6009 DWORD flags = 0;
6011 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6012 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6013 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6014 flags |= WINED3D_GLSL_SAMPLE_LOD;
6015 if (wined3d_shader_instruction_has_texel_offset(ins))
6016 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6018 resource_idx = ins->src[1].reg.idx[0].offset;
6019 sampler_idx = ins->src[2].reg.idx[0].offset;
6021 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6022 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6024 switch (ins->handler_idx)
6026 case WINED3DSIH_SAMPLE:
6027 break;
6028 case WINED3DSIH_SAMPLE_B:
6029 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6030 lod_param_str = lod_param.param_str;
6031 break;
6032 case WINED3DSIH_SAMPLE_GRAD:
6033 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6034 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6035 dx_param_str = dx_param.param_str;
6036 dy_param_str = dy_param.param_str;
6037 break;
6038 case WINED3DSIH_SAMPLE_LOD:
6039 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6040 lod_param_str = lod_param.param_str;
6041 break;
6042 default:
6043 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6044 break;
6047 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6048 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6049 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6050 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6053 /* Unless EXT_texture_shadow_lod is available, GLSL doesn't provide a function
6054 * to sample from level zero with depth comparison for array textures and cube
6055 * textures. We use textureGrad*() to implement sample_c_lz in that case. */
6056 static void shader_glsl_gen_sample_c_lz_emulation(const struct wined3d_shader_instruction *ins,
6057 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6058 unsigned int coord_size, const char *coord_param, const char *ref_param)
6060 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6061 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6062 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6063 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6064 char dst_swizzle[6];
6066 WARN("Emitting textureGrad() for sample_c_lz.\n");
6068 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6069 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], sample_function->data_type);
6070 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6071 sample_function->offset_size ? "Offset" : "",
6072 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6073 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6074 if (sample_function->offset_size)
6076 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6077 shader_addline(buffer, ", ");
6078 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6080 shader_addline(buffer, "))%s);\n", dst_swizzle);
6083 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6085 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6086 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6087 const struct wined3d_shader_resource_info *resource_info;
6088 const struct wined3d_gl_info *gl_info = priv->gl_info;
6089 struct glsl_src_param coord_param, compare_param;
6090 struct glsl_sample_function sample_function;
6091 const char *lod_param = NULL;
6092 unsigned int coord_size;
6093 DWORD flags = 0;
6095 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6097 lod_param = "0";
6098 flags |= WINED3D_GLSL_SAMPLE_LOD;
6101 if (wined3d_shader_instruction_has_texel_offset(ins))
6102 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6104 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6105 return;
6106 resource_idx = ins->src[1].reg.idx[0].offset;
6107 sampler_idx = ins->src[2].reg.idx[0].offset;
6109 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6110 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6111 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6112 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6113 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6114 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6115 && !gl_info->supported[EXT_TEXTURE_SHADOW_LOD]
6116 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6117 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6119 shader_glsl_gen_sample_c_lz_emulation(ins, sampler_bind_idx, &sample_function,
6120 coord_size, coord_param.param_str, compare_param.param_str);
6122 else
6124 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6125 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6126 coord_size, coord_param.param_str, compare_param.param_str);
6128 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6131 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6133 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6134 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6135 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6136 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6137 struct glsl_src_param coord_param, compare_param, offset_param;
6138 const struct wined3d_gl_info *gl_info = priv->gl_info;
6139 const struct wined3d_shader_resource_info *resource_info;
6140 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6141 unsigned int coord_size, offset_size;
6142 char dst_swizzle[6];
6143 BOOL has_offset;
6145 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6147 FIXME("OpenGL implementation does not support textureGather.\n");
6148 return;
6151 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6152 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6153 || wined3d_shader_instruction_has_texel_offset(ins);
6155 resource_param_idx =
6156 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6157 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6158 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6159 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6160 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6162 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6163 return;
6165 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6167 ERR("Unexpected resource type %#x.\n", resource_info->type);
6168 return;
6170 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6172 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6173 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], resource_info->data_type);
6175 shader_glsl_add_src_param(ins, &ins->src[0], (1u << coord_size) - 1, &coord_param);
6177 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6178 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6179 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6181 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6182 shader_addline(buffer, ", %s", compare_param.param_str);
6184 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6186 shader_glsl_add_src_param(ins, &ins->src[1], (1u << offset_size) - 1, &offset_param);
6187 shader_addline(buffer, ", %s", offset_param.param_str);
6189 else if (has_offset)
6191 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6192 shader_addline(buffer, ", ");
6193 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6195 if (component_idx)
6196 shader_addline(buffer, ", %u", component_idx);
6198 shader_addline(buffer, ")%s);\n", dst_swizzle);
6201 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6203 /* FIXME: Make this work for more than just 2D textures */
6204 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6205 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6207 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6209 char dst_mask[6];
6211 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6212 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6213 ins->dst[0].reg.idx[0].offset, dst_mask);
6215 else
6217 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6218 DWORD reg = ins->src[0].reg.idx[0].offset;
6219 char dst_swizzle[6];
6221 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6223 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6225 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6226 struct glsl_src_param div_param;
6227 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6229 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6231 if (mask_size > 1)
6232 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6233 else
6234 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6236 else
6238 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6243 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6244 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6245 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6246 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6248 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6249 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6250 struct glsl_sample_function sample_function;
6251 struct glsl_src_param src0_param;
6252 UINT mask_size;
6254 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6256 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6257 * scalar, and projected sampling would require 4.
6259 * It is a dependent read - not valid with conditional NP2 textures
6261 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6262 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6264 switch(mask_size)
6266 case 1:
6267 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6268 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6269 break;
6271 case 2:
6272 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6273 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6274 break;
6276 case 3:
6277 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6278 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6279 break;
6281 default:
6282 FIXME("Unexpected mask size %u\n", mask_size);
6283 break;
6285 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6288 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6289 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6290 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6292 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6293 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6294 struct glsl_src_param src0_param;
6295 DWORD dst_mask;
6296 unsigned int mask_size;
6298 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6299 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6300 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6302 if (mask_size > 1) {
6303 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6304 } else {
6305 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6309 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6310 * Calculate the depth as dst.x / dst.y */
6311 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6313 struct glsl_dst_param dst_param;
6315 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6317 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6318 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6319 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6320 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6321 * >= 1.0 or < 0.0
6323 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6324 dst_param.reg_name, dst_param.reg_name);
6327 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6328 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6329 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6330 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6332 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6334 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6335 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6336 struct glsl_src_param src0_param;
6338 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6340 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6341 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6344 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6345 * Calculate the 1st of a 2-row matrix multiplication. */
6346 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6348 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6349 DWORD reg = ins->dst[0].reg.idx[0].offset;
6350 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6351 struct glsl_src_param src0_param;
6353 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6354 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6357 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6358 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6359 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6361 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6362 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6363 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6364 DWORD reg = ins->dst[0].reg.idx[0].offset;
6365 struct glsl_src_param src0_param;
6367 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6368 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6369 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6372 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6374 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6375 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6376 struct glsl_sample_function sample_function;
6377 DWORD reg = ins->dst[0].reg.idx[0].offset;
6378 struct glsl_src_param src0_param;
6380 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6381 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6383 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6385 /* Sample the texture using the calculated coordinates */
6386 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6387 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6390 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6391 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6392 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6394 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6395 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6396 struct glsl_sample_function sample_function;
6397 DWORD reg = ins->dst[0].reg.idx[0].offset;
6398 struct glsl_src_param src0_param;
6400 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6401 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6403 /* Dependent read, not valid with conditional NP2 */
6404 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6406 /* Sample the texture using the calculated coordinates */
6407 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6408 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6410 tex_mx->current_row = 0;
6413 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6414 * Perform the 3rd row of a 3x3 matrix multiply */
6415 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6417 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6418 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6419 DWORD reg = ins->dst[0].reg.idx[0].offset;
6420 struct glsl_src_param src0_param;
6421 char dst_mask[6];
6423 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6425 shader_glsl_append_dst(ins->ctx->buffer, ins);
6426 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6427 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6429 tex_mx->current_row = 0;
6432 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6433 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6434 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6436 struct glsl_src_param src0_param;
6437 struct glsl_src_param src1_param;
6438 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6439 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6440 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6441 struct glsl_sample_function sample_function;
6442 DWORD reg = ins->dst[0].reg.idx[0].offset;
6443 char coord_mask[6];
6445 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6446 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6448 /* Perform the last matrix multiply operation */
6449 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6450 /* Reflection calculation */
6451 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6453 /* Dependent read, not valid with conditional NP2 */
6454 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6455 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6457 /* Sample the texture */
6458 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6459 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6460 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6462 tex_mx->current_row = 0;
6465 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6466 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6467 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6469 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6470 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6471 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6472 struct glsl_sample_function sample_function;
6473 DWORD reg = ins->dst[0].reg.idx[0].offset;
6474 struct glsl_src_param src0_param;
6475 char coord_mask[6];
6477 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6479 /* Perform the last matrix multiply operation */
6480 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6482 /* Construct the eye-ray vector from w coordinates */
6483 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6484 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6485 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6487 /* Dependent read, not valid with conditional NP2 */
6488 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6489 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6491 /* Sample the texture using the calculated coordinates */
6492 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6493 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6494 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6496 tex_mx->current_row = 0;
6499 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6500 * Apply a fake bump map transform.
6501 * texbem is pshader <= 1.3 only, this saves a few version checks
6503 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6505 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6506 struct glsl_sample_function sample_function;
6507 struct glsl_src_param coord_param;
6508 DWORD sampler_idx;
6509 DWORD mask;
6510 DWORD flags;
6511 char coord_mask[6];
6513 sampler_idx = ins->dst[0].reg.idx[0].offset;
6514 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6515 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6517 /* Dependent read, not valid with conditional NP2 */
6518 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6519 mask = sample_function.coord_mask;
6521 shader_glsl_write_mask_to_str(mask, coord_mask);
6523 /* With projected textures, texbem only divides the static texture coord,
6524 * not the displacement, so we can't let GL handle this. */
6525 if (flags & WINED3D_PSARGS_PROJECTED)
6527 DWORD div_mask=0;
6528 char coord_div_mask[3];
6529 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6531 case WINED3D_TTFF_COUNT1:
6532 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6533 break;
6534 case WINED3D_TTFF_COUNT2:
6535 div_mask = WINED3DSP_WRITEMASK_1;
6536 break;
6537 case WINED3D_TTFF_COUNT3:
6538 div_mask = WINED3DSP_WRITEMASK_2;
6539 break;
6540 case WINED3D_TTFF_COUNT4:
6541 case WINED3D_TTFF_DISABLE:
6542 div_mask = WINED3DSP_WRITEMASK_3;
6543 break;
6545 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6546 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6549 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6551 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6552 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6553 coord_param.param_str, coord_mask);
6555 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6557 struct glsl_src_param luminance_param;
6558 struct glsl_dst_param dst_param;
6560 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6561 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6563 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6564 dst_param.reg_name, dst_param.mask_str,
6565 luminance_param.param_str, sampler_idx, sampler_idx);
6567 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6570 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6572 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6573 struct glsl_src_param src0_param, src1_param;
6575 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6576 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6578 shader_glsl_append_dst(ins->ctx->buffer, ins);
6579 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6580 src0_param.param_str, sampler_idx, src1_param.param_str);
6583 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6584 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6585 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6587 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6588 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6589 struct glsl_sample_function sample_function;
6590 struct wined3d_string_buffer *reg_name;
6592 reg_name = string_buffer_get(priv->string_buffers);
6593 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6595 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6596 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6597 "%s.wx", reg_name->buffer);
6598 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6600 string_buffer_release(priv->string_buffers, reg_name);
6603 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6604 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6605 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6607 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6608 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6609 struct glsl_sample_function sample_function;
6610 struct wined3d_string_buffer *reg_name;
6612 reg_name = string_buffer_get(priv->string_buffers);
6613 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6615 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6616 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6617 "%s.yz", reg_name->buffer);
6618 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6620 string_buffer_release(priv->string_buffers, reg_name);
6623 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6624 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6625 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6627 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6628 struct glsl_sample_function sample_function;
6629 struct glsl_src_param src0_param;
6631 /* Dependent read, not valid with conditional NP2 */
6632 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6633 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6635 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6636 "%s", src0_param.param_str);
6637 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6640 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6641 * If any of the first 3 components are < 0, discard this pixel */
6642 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6644 if (ins->ctx->reg_maps->shader_version.major >= 4)
6646 shader_glsl_generate_condition(ins);
6647 shader_addline(ins->ctx->buffer, " discard;\n");
6649 else
6651 struct glsl_dst_param dst_param;
6653 /* The argument is a destination parameter, and no writemasks are allowed */
6654 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6656 /* 2.0 shaders compare all 4 components in texkill. */
6657 if (ins->ctx->reg_maps->shader_version.major >= 2)
6658 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6659 /* 1.x shaders only compare the first 3 components, probably due to
6660 * the nature of the texkill instruction as a tex* instruction, and
6661 * phase, which kills all .w components. Even if all 4 components are
6662 * defined, only the first 3 are used. */
6663 else
6664 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6668 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6669 * dst = dot2(src0, src1) + src2 */
6670 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6672 struct glsl_src_param src0_param;
6673 struct glsl_src_param src1_param;
6674 struct glsl_src_param src2_param;
6675 DWORD write_mask;
6676 unsigned int mask_size;
6678 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6679 mask_size = shader_glsl_get_write_mask_size(write_mask);
6681 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6682 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6683 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6685 if (mask_size > 1) {
6686 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6687 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6688 } else {
6689 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6690 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6694 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6695 const struct wined3d_shader_signature *input_signature,
6696 const struct wined3d_shader_reg_maps *reg_maps,
6697 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6699 unsigned int i;
6701 for (i = 0; i < input_signature->element_count; ++i)
6703 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6704 const char *semantic_name;
6705 UINT semantic_idx;
6706 char reg_mask[6];
6708 /* Unused */
6709 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6710 continue;
6712 semantic_name = input->semantic_name;
6713 semantic_idx = input->semantic_idx;
6714 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6716 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6718 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6720 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6721 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6723 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6725 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6727 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6729 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6730 input->register_idx, reg_mask);
6732 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6734 if (gl_info->supported[ARB_SAMPLE_SHADING])
6735 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6736 input->register_idx, reg_mask);
6737 else
6738 FIXME("ARB_sample_shading is not supported.\n");
6740 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6742 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6743 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6744 input->register_idx, reg_mask);
6745 else
6746 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6748 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6750 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6751 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6752 input->register_idx, reg_mask);
6753 else
6754 FIXME("ARB_viewport_array is not supported.\n");
6756 else
6758 if (input->sysval_semantic)
6759 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6760 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6761 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6762 shader_glsl_shader_input_name(gl_info),
6763 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6766 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6768 if (args->pointsprite)
6769 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6770 shader->u.ps.input_reg_map[input->register_idx]);
6771 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6772 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6773 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6774 needs_legacy_glsl_syntax(gl_info)
6775 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6776 else
6777 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6778 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6780 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6782 if (!semantic_idx)
6783 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6784 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6785 else if (semantic_idx == 1)
6786 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6787 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6788 else
6789 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6790 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6792 else
6794 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6795 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6800 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6802 struct glsl_program_key key;
6804 key.vs_id = entry->vs.id;
6805 key.hs_id = entry->hs.id;
6806 key.ds_id = entry->ds.id;
6807 key.gs_id = entry->gs.id;
6808 key.ps_id = entry->ps.id;
6809 key.cs_id = entry->cs.id;
6811 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6813 ERR("Failed to insert program entry.\n");
6817 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6818 const struct glsl_program_key *key)
6820 struct wine_rb_entry *entry;
6822 entry = wine_rb_get(&priv->program_lookup, key);
6823 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6826 /* Context activation is done by the caller. */
6827 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6828 struct glsl_shader_prog_link *entry)
6830 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6832 GL_EXTCALL(glDeleteProgram(entry->id));
6833 if (entry->vs.id)
6834 list_remove(&entry->vs.shader_entry);
6835 if (entry->hs.id)
6836 list_remove(&entry->hs.shader_entry);
6837 if (entry->ds.id)
6838 list_remove(&entry->ds.shader_entry);
6839 if (entry->gs.id)
6840 list_remove(&entry->gs.shader_entry);
6841 if (entry->ps.id)
6842 list_remove(&entry->ps.shader_entry);
6843 if (entry->cs.id)
6844 list_remove(&entry->cs.shader_entry);
6845 heap_free(entry);
6848 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6849 const struct wined3d_gl_info *gl_info, const DWORD *map,
6850 const struct wined3d_shader_signature *input_signature,
6851 const struct wined3d_shader_reg_maps *reg_maps_in,
6852 const struct wined3d_shader_signature *output_signature,
6853 const struct wined3d_shader_reg_maps *reg_maps_out)
6855 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6856 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6857 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6858 unsigned int in_count = vec4_varyings(3, gl_info);
6859 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6860 DWORD in_idx, *set = NULL;
6861 unsigned int i, j;
6862 char reg_mask[6];
6864 set = heap_calloc(max_varyings, sizeof(*set));
6866 for (i = 0; i < input_signature->element_count; ++i)
6868 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6870 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6871 continue;
6873 in_idx = map[input->register_idx];
6874 /* Declared, but not read register */
6875 if (in_idx == ~0u)
6876 continue;
6877 if (in_idx >= max_varyings)
6879 FIXME("More input varyings declared than supported, expect issues.\n");
6880 continue;
6883 if (in_idx == in_count)
6884 string_buffer_sprintf(destination, "gl_FrontColor");
6885 else if (in_idx == in_count + 1)
6886 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6887 else
6888 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6890 if (!set[in_idx])
6891 set[in_idx] = ~0u;
6893 for (j = 0; j < output_signature->element_count; ++j)
6895 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6896 DWORD mask;
6898 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6899 || input->semantic_idx != output->semantic_idx
6900 || strcmp(input->semantic_name, output->semantic_name)
6901 || !(mask = input->mask & output->mask))
6902 continue;
6904 if (set[in_idx] == ~0u)
6905 set[in_idx] = 0;
6906 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6907 shader_glsl_write_mask_to_str(mask, reg_mask);
6909 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6910 destination->buffer, reg_mask, output->register_idx, reg_mask);
6914 for (i = 0; i < max_varyings; ++i)
6916 unsigned int size;
6918 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6919 continue;
6921 if (set[i] == ~0u)
6922 set[i] = 0;
6924 size = 0;
6925 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6926 reg_mask[size++] = 'x';
6927 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6928 reg_mask[size++] = 'y';
6929 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6930 reg_mask[size++] = 'z';
6931 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6932 reg_mask[size++] = 'w';
6933 reg_mask[size] = '\0';
6935 if (i == in_count)
6936 string_buffer_sprintf(destination, "gl_FrontColor");
6937 else if (i == in_count + 1)
6938 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6939 else
6940 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6942 if (size == 1)
6943 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6944 else
6945 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6948 heap_free(set);
6949 string_buffer_release(&priv->string_buffers, destination);
6952 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6953 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6954 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6955 BOOL rasterizer_setup)
6957 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6958 char reg_mask[6];
6959 unsigned int i;
6961 for (i = 0; i < output_signature->element_count; ++i)
6963 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6965 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6966 continue;
6968 if (output->stream_idx)
6969 continue;
6971 if (output->register_idx >= input_count)
6972 continue;
6974 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6976 shader_addline(buffer,
6977 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
6978 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
6982 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
6983 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
6985 unsigned int i, clip_or_cull_index;
6986 const char *name;
6987 char reg_mask[6];
6989 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
6990 /* Assign consecutive indices starting from 0. */
6991 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
6992 for (i = 0; i < 4; ++i)
6994 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
6995 continue;
6997 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
6998 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
6999 name, clip_or_cull_index, element->register_idx, reg_mask);
7000 ++clip_or_cull_index;
7004 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7005 const struct wined3d_gl_info *gl_info, const DWORD *map,
7006 const struct wined3d_shader_signature *input_signature,
7007 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7008 const struct wined3d_shader_signature *output_signature,
7009 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7011 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7012 const char *semantic_name;
7013 unsigned int semantic_idx;
7014 char reg_mask[6];
7015 unsigned int i;
7017 /* First, sort out position and point size system values. */
7018 for (i = 0; i < output_signature->element_count; ++i)
7020 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7022 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7023 continue;
7025 if (output->stream_idx)
7026 continue;
7028 semantic_name = output->semantic_name;
7029 semantic_idx = output->semantic_idx;
7030 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7032 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7034 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7035 reg_mask, output->register_idx, reg_mask);
7037 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7039 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7040 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7042 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7044 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7045 output->register_idx, reg_mask);
7047 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7049 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7050 output->register_idx, reg_mask);
7052 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7054 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7056 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7058 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7060 else if (output->sysval_semantic)
7062 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7066 /* Then, setup the pixel shader input. */
7067 if (reg_maps_out->shader_version.major < 4)
7068 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7069 output_signature, reg_maps_out);
7070 else
7071 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7074 /* Context activation is done by the caller. */
7075 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7076 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7077 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7079 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7080 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7081 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7082 const char *semantic_name;
7083 UINT semantic_idx;
7084 char reg_mask[6];
7085 unsigned int i;
7086 GLuint ret;
7088 string_buffer_clear(buffer);
7090 shader_glsl_add_version_declaration(buffer, gl_info);
7092 if (per_vertex_point_size)
7094 shader_addline(buffer, "uniform struct\n{\n");
7095 shader_addline(buffer, " float size_min;\n");
7096 shader_addline(buffer, " float size_max;\n");
7097 shader_addline(buffer, "} ffp_point;\n");
7100 if (ps_major < 3)
7102 DWORD colors_written_mask[2] = {0};
7103 DWORD texcoords_written_mask[WINED3D_MAX_TEXTURES] = {0};
7105 if (!legacy_syntax)
7107 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7108 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7109 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7110 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7113 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7115 for (i = 0; i < vs->output_signature.element_count; ++i)
7117 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7118 DWORD write_mask;
7120 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7121 continue;
7123 semantic_name = output->semantic_name;
7124 semantic_idx = output->semantic_idx;
7125 write_mask = output->mask;
7126 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7128 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7130 if (legacy_syntax)
7131 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7132 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7133 else
7134 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7135 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7137 colors_written_mask[semantic_idx] = write_mask;
7139 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7141 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7142 reg_mask, output->register_idx, reg_mask);
7144 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7146 if (semantic_idx < WINED3D_MAX_TEXTURES)
7148 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7149 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7150 semantic_idx, reg_mask, output->register_idx, reg_mask);
7151 texcoords_written_mask[semantic_idx] = write_mask;
7154 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7156 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7157 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7159 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7161 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7162 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7163 output->register_idx, reg_mask[1]);
7167 for (i = 0; i < 2; ++i)
7169 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7171 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7172 if (!i)
7173 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7174 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7175 reg_mask, reg_mask);
7176 else
7177 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7178 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7179 reg_mask, reg_mask);
7182 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
7184 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7185 continue;
7187 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7189 if (!shader_glsl_full_ffp_varyings(gl_info) && !texcoords_written_mask[i])
7190 continue;
7192 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7193 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7194 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7198 else
7200 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7202 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7203 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7204 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7205 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7208 shader_addline(buffer, "}\n");
7210 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7211 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7212 shader_glsl_compile(gl_info, ret, buffer->buffer);
7214 return ret;
7217 static void shader_glsl_generate_stream_output_setup(struct wined3d_string_buffer *buffer,
7218 const struct wined3d_shader *shader)
7220 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
7221 unsigned int i, register_idx, component_idx;
7223 shader_addline(buffer, "out shader_in_out\n{\n");
7224 for (i = 0; i < so_desc->element_count; ++i)
7226 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7228 if (e->stream_idx)
7230 FIXME("Unhandled stream %u.\n", e->stream_idx);
7231 continue;
7233 if (!e->semantic_name)
7234 continue;
7235 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7236 continue;
7238 if (component_idx || e->component_count != 4)
7240 if (e->component_count == 1)
7241 shader_addline(buffer, "float");
7242 else
7243 shader_addline(buffer, "vec%u", e->component_count);
7245 shader_addline(buffer, " reg%u_%u_%u;\n",
7246 register_idx, component_idx, component_idx + e->component_count - 1);
7248 else
7250 shader_addline(buffer, "vec4 reg%u;\n", register_idx);
7253 shader_addline(buffer, "} shader_out;\n");
7255 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7256 shader->limits->packed_output);
7257 for (i = 0; i < so_desc->element_count; ++i)
7259 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7261 if (e->stream_idx)
7263 FIXME("Unhandled stream %u.\n", e->stream_idx);
7264 continue;
7266 if (!e->semantic_name)
7267 continue;
7268 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7269 continue;
7271 if (component_idx || e->component_count != 4)
7273 DWORD write_mask;
7274 char str_mask[6];
7276 write_mask = ((1u << e->component_count) - 1) << component_idx;
7277 shader_glsl_write_mask_to_str(write_mask, str_mask);
7278 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7279 register_idx, component_idx, component_idx + e->component_count - 1,
7280 register_idx, str_mask);
7282 else
7284 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n", register_idx, register_idx);
7287 shader_addline(buffer, "}\n");
7290 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7291 const struct wined3d_shader *shader, unsigned int input_count,
7292 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7294 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7295 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7297 if (rasterizer_setup)
7298 input_count = min(vec4_varyings(4, gl_info), input_count);
7300 if (input_count)
7301 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7303 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7304 prefix, shader->limits->packed_output);
7306 if (rasterizer_setup)
7307 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7308 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7309 else
7310 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7311 &shader->reg_maps, "shader_out", rasterizer_setup);
7313 shader_addline(buffer, "}\n");
7316 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7317 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7318 const char *reg_mask)
7320 if (!constant->sysval_semantic)
7322 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7323 return;
7326 switch (constant->sysval_semantic)
7328 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7329 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7330 case WINED3D_SV_TESS_FACTOR_LINEDET:
7331 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7332 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7333 break;
7334 case WINED3D_SV_TESS_FACTOR_QUADINT:
7335 case WINED3D_SV_TESS_FACTOR_TRIINT:
7336 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7337 break;
7338 default:
7339 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7340 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7344 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7345 const struct wined3d_shader_signature *signature, BOOL input_setup)
7347 unsigned int i, register_count, user_constant_index, user_constant_count;
7349 register_count = user_constant_count = 0;
7350 for (i = 0; i < signature->element_count; ++i)
7352 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7353 register_count = max(constant->register_idx + 1, register_count);
7354 if (!constant->sysval_semantic)
7355 ++user_constant_count;
7358 if (user_constant_count)
7359 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7360 input_setup ? "in" : "out", user_constant_count);
7361 if (input_setup)
7362 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7364 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7365 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7367 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7368 char reg_mask[6];
7370 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7372 if (input_setup)
7373 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7374 else
7375 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7377 shader_addline(buffer, " = ");
7379 if (input_setup)
7380 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7381 else
7382 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7384 shader_addline(buffer, ";\n");
7386 shader_addline(buffer, "}\n");
7389 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7390 const struct wined3d_gl_info *gl_info)
7392 const char *output = get_fragment_output(gl_info);
7394 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7395 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7396 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7397 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7398 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7399 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7402 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7403 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7405 const char *output = get_fragment_output(gl_info);
7407 switch (mode)
7409 case WINED3D_FFP_PS_FOG_OFF:
7410 return;
7412 case WINED3D_FFP_PS_FOG_LINEAR:
7413 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7414 break;
7416 case WINED3D_FFP_PS_FOG_EXP:
7417 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7418 break;
7420 case WINED3D_FFP_PS_FOG_EXP2:
7421 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7422 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7423 break;
7425 default:
7426 ERR("Invalid fog mode %#x.\n", mode);
7427 return;
7430 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7431 output, output);
7434 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7435 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7437 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7438 * flipping all the operators here, just negate the comparison below. */
7439 static const char * const comparison_operator[] =
7441 "", /* WINED3D_CMP_NEVER */
7442 "<", /* WINED3D_CMP_LESS */
7443 "==", /* WINED3D_CMP_EQUAL */
7444 "<=", /* WINED3D_CMP_LESSEQUAL */
7445 ">", /* WINED3D_CMP_GREATER */
7446 "!=", /* WINED3D_CMP_NOTEQUAL */
7447 ">=", /* WINED3D_CMP_GREATEREQUAL */
7448 "" /* WINED3D_CMP_ALWAYS */
7451 if (alpha_func == WINED3D_CMP_ALWAYS)
7452 return;
7454 if (alpha_func != WINED3D_CMP_NEVER)
7455 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7456 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7457 shader_addline(buffer, " discard;\n");
7460 static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *buffer,
7461 const char *colour, const char *colour_key_low, const char *colour_key_high)
7463 shader_addline(buffer, "if (all(greaterThanEqual(%s, %s)) && all(lessThan(%s, %s)))\n",
7464 colour, colour_key_low, colour, colour_key_high);
7465 shader_addline(buffer, " discard;\n");
7468 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7469 const struct wined3d_gl_info *gl_info)
7471 if (gl_info->supported[ARB_CULL_DISTANCE])
7472 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7473 if (gl_info->supported[ARB_GPU_SHADER5])
7474 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7475 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7476 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7477 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7478 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7479 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7480 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7481 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7482 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7483 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7484 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7485 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7486 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7487 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7488 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7489 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7490 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7491 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7492 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7493 if (gl_info->supported[ARB_TEXTURE_GATHER])
7494 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7495 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7496 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7497 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7498 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7499 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7500 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7501 if (gl_info->supported[EXT_GPU_SHADER4])
7502 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7503 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7504 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7505 if (gl_info->supported[EXT_TEXTURE_SHADOW_LOD])
7506 shader_addline(buffer, "#extension GL_EXT_texture_shadow_lod : enable\n");
7509 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7510 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7511 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7513 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7514 struct wined3d_string_buffer *src, *assignment;
7515 enum wined3d_data_type dst_data_type;
7516 const char *swizzle;
7517 unsigned int i;
7519 if (output_signature->element_count)
7521 src = string_buffer_get(string_buffers);
7522 assignment = string_buffer_get(string_buffers);
7523 for (i = 0; i < output_signature->element_count; ++i)
7525 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7527 /* register_idx is set to ~0u for non-color outputs. */
7528 if (output->register_idx == ~0u)
7529 continue;
7530 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7532 FIXME("Unhandled component type %#x.\n", output->component_type);
7533 continue;
7535 dst_data_type = component_type_info[output->component_type].data_type;
7536 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7537 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7538 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT, 4);
7539 swizzle = args->rt_alpha_swizzle & (1u << output->semantic_idx) ? ".argb" : "";
7540 shader_addline(buffer, "%s%s;\n", assignment->buffer, swizzle);
7542 string_buffer_release(string_buffers, src);
7543 string_buffer_release(string_buffers, assignment);
7545 else
7547 DWORD mask = shader->reg_maps.rt_mask;
7549 while (mask)
7551 i = wined3d_bit_scan(&mask);
7552 swizzle = args->rt_alpha_swizzle & (1u << i) ? ".argb" : "";
7553 shader_addline(buffer, "color_out%u = ps_out[%u]%s;\n", i, i, swizzle);
7558 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7559 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7560 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7562 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7564 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7565 if (reg_maps->shader_version.major < 2)
7566 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7568 if (args->srgb_correction)
7569 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7571 /* SM < 3 does not replace the fog stage. */
7572 if (reg_maps->shader_version.major < 3)
7573 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7575 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7577 if (reg_maps->sample_mask)
7578 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7580 if (!use_legacy_fragment_output(gl_info))
7581 shader_glsl_generate_color_output(buffer, gl_info, shader, args, string_buffers);
7584 /* Context activation is done by the caller. */
7585 static GLuint shader_glsl_generate_fragment_shader(const struct wined3d_context_gl *context_gl,
7586 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7587 const struct wined3d_shader *shader, const struct ps_compile_args *args,
7588 struct ps_np2fixup_info *np2fixup_info)
7590 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7591 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7592 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7593 const char *prefix = shader_glsl_get_prefix(version->type);
7594 BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7595 unsigned int i, extra_constants_needed = 0;
7596 struct shader_glsl_ctx_priv priv_ctx;
7597 GLuint shader_id;
7598 DWORD map;
7600 memset(&priv_ctx, 0, sizeof(priv_ctx));
7601 priv_ctx.gl_info = gl_info;
7602 priv_ctx.cur_ps_args = args;
7603 priv_ctx.cur_np2fixup_info = np2fixup_info;
7604 priv_ctx.string_buffers = string_buffers;
7606 shader_glsl_add_version_declaration(buffer, gl_info);
7608 shader_glsl_enable_extensions(buffer, gl_info);
7609 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7610 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7611 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7612 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7613 if (shader_glsl_use_explicit_attrib_location(gl_info))
7614 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7615 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7616 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7617 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7618 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7619 if (gl_info->supported[ARB_SAMPLE_SHADING])
7620 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7621 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7622 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7623 /* The spec says that it doesn't have to be explicitly enabled, but the
7624 * nvidia drivers write a warning if we don't do so. */
7625 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7626 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7628 /* Base Declarations */
7629 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7631 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7633 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7634 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7635 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7636 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7639 /* Declare uniforms for NP2 texcoord fixup:
7640 * This is NOT done inside the loop that declares the texture samplers
7641 * since the NP2 fixup code is currently only used for the GeforceFX
7642 * series and when forcing the ARB_npot extension off. Modern cards just
7643 * skip the code anyway, so put it inside a separate loop. */
7644 if (args->np2_fixup)
7646 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7647 unsigned int cur = 0;
7649 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7650 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7651 * samplerNP2Fixup stores texture dimensions and is updated through
7652 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7654 for (i = 0; i < shader->limits->sampler; ++i)
7656 enum wined3d_shader_resource_type resource_type;
7658 resource_type = pixelshader_get_resource_type(reg_maps, i, args->tex_types);
7660 if (!resource_type || !(args->np2_fixup & (1u << i)))
7661 continue;
7663 if (resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7665 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7666 continue;
7669 fixup->idx[i] = cur++;
7672 fixup->num_consts = (cur + 1) >> 1;
7673 fixup->active = args->np2_fixup;
7674 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7677 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7679 shader_addline(buffer, "uniform struct\n{\n");
7680 shader_addline(buffer, " vec4 color;\n");
7681 shader_addline(buffer, " float density;\n");
7682 shader_addline(buffer, " float end;\n");
7683 shader_addline(buffer, " float scale;\n");
7684 shader_addline(buffer, "} ffp_fog;\n");
7686 if (legacy_syntax)
7688 if (glsl_is_color_reg_read(shader, 0))
7689 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7690 if (glsl_is_color_reg_read(shader, 1))
7691 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7692 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7693 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7695 else
7697 if (glsl_is_color_reg_read(shader, 0))
7698 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7699 if (glsl_is_color_reg_read(shader, 1))
7700 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7701 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7702 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7703 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7707 if (version->major >= 3)
7709 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7711 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7712 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7713 shader->u.ps.interpolation_mode, version->major >= 4);
7714 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7717 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7719 if (!(map & 1))
7720 continue;
7722 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7724 if (reg_maps->luminanceparams & (1u << i))
7726 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7727 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7728 extra_constants_needed++;
7731 extra_constants_needed++;
7734 if (args->srgb_correction)
7736 shader_addline(buffer, "const vec4 srgb_const0 = ");
7737 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
7738 shader_addline(buffer, ";\n");
7739 shader_addline(buffer, "const vec4 srgb_const1 = ");
7740 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
7741 shader_addline(buffer, ";\n");
7743 if (reg_maps->vpos || reg_maps->usesdsy)
7745 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7747 ++extra_constants_needed;
7748 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7750 if (reg_maps->vpos)
7752 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7754 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7755 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7756 args->render_offscreen ? "" : "origin_upper_left, ");
7757 else if (!args->render_offscreen)
7758 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7760 shader_addline(buffer, "vec4 vpos;\n");
7764 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7765 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7767 if (!use_legacy_fragment_output(gl_info))
7769 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7771 if (args->dual_source_blend)
7772 shader_addline(buffer, "vec4 ps_out[2];\n");
7773 else
7774 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7775 if (output_signature->element_count)
7777 for (i = 0; i < output_signature->element_count; ++i)
7779 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7781 if (output->register_idx == ~0u)
7782 continue;
7783 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7785 FIXME("Unhandled component type %#x.\n", output->component_type);
7786 continue;
7788 if (shader_glsl_use_explicit_attrib_location(gl_info))
7790 if (args->dual_source_blend)
7791 shader_addline(buffer, "layout(location = 0, index = %u) ", output->semantic_idx);
7792 else
7793 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7795 shader_addline(buffer, "out %s4 color_out%u;\n",
7796 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7799 else
7801 DWORD mask = reg_maps->rt_mask;
7803 while (mask)
7805 i = wined3d_bit_scan(&mask);
7806 if (shader_glsl_use_explicit_attrib_location(gl_info))
7808 if (args->dual_source_blend)
7809 shader_addline(buffer, "layout(location = 0, index = %u) ", i);
7810 else
7811 shader_addline(buffer, "layout(location = %u) ", i);
7813 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7818 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7819 FIXME("Insufficient uniforms to run this shader.\n");
7821 if (shader->u.ps.force_early_depth_stencil)
7822 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7824 shader_addline(buffer, "void main()\n{\n");
7826 if (reg_maps->sample_mask)
7827 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7829 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7830 * add approximately 0.5. This causes off-by-one problems as spotted by
7831 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7832 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7833 * causes precision troubles when we just subtract 0.5.
7835 * To deal with that, just floor() the position. This will eliminate the
7836 * fraction on all cards.
7838 * TODO: Test how this behaves with multisampling.
7840 * An advantage of floor is that it works even if the driver doesn't add
7841 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7842 * to return in gl_FragCoord, even though coordinates specify the pixel
7843 * centers instead of the pixel corners. This code will behave correctly
7844 * on drivers that returns integer values. */
7845 if (reg_maps->vpos)
7847 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7848 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7849 else if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7850 shader_addline(buffer,
7851 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7852 else
7853 shader_addline(buffer,
7854 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7857 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7859 unsigned int i;
7860 WORD map = reg_maps->texcoord;
7862 if (legacy_syntax)
7864 if (glsl_is_color_reg_read(shader, 0))
7865 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7866 if (glsl_is_color_reg_read(shader, 1))
7867 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7870 for (i = 0; map; map >>= 1, ++i)
7872 if (map & 1)
7874 if (args->pointsprite)
7875 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7876 else if (args->texcoords_initialized & (1u << i))
7877 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7878 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7879 else
7880 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7881 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7885 if (legacy_syntax)
7886 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7889 /* Pack 3.0 inputs */
7890 if (reg_maps->shader_version.major >= 3)
7891 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7892 reg_maps->shader_version.major >= 4);
7894 /* Base Shader Body */
7895 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7896 return 0;
7898 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7899 if (reg_maps->shader_version.major < 4)
7900 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
7902 shader_addline(buffer, "}\n");
7904 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7905 TRACE("Compiling shader object %u.\n", shader_id);
7906 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7908 return shader_id;
7911 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7912 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7913 const struct vs_compile_args *args)
7915 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7916 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7917 unsigned int i;
7919 /* Unpack outputs. */
7920 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7922 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7923 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7924 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7925 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7927 if (reg_maps->shader_version.major < 3)
7929 if (args->fog_src == VS_FOG_Z)
7930 shader_addline(buffer, "%s = gl_Position.z;\n",
7931 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7932 else if (!reg_maps->fog)
7933 shader_addline(buffer, "%s = 0.0;\n",
7934 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7937 /* We always store the clipplanes without y inversion. */
7938 if (args->clip_enabled)
7940 if (legacy_syntax)
7941 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7942 else
7943 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7944 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7947 if (args->point_size && !args->per_vertex_point_size)
7948 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7950 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7951 shader_glsl_fixup_position(buffer, FALSE);
7954 /* Context activation is done by the caller. */
7955 static GLuint shader_glsl_generate_vertex_shader(const struct wined3d_context_gl *context_gl,
7956 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7958 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7959 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7960 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7961 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7962 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7963 struct shader_glsl_ctx_priv priv_ctx;
7964 GLuint shader_id;
7965 unsigned int i;
7967 memset(&priv_ctx, 0, sizeof(priv_ctx));
7968 priv_ctx.gl_info = gl_info;
7969 priv_ctx.cur_vs_args = args;
7970 priv_ctx.string_buffers = string_buffers;
7972 shader_glsl_add_version_declaration(buffer, gl_info);
7974 shader_glsl_enable_extensions(buffer, gl_info);
7975 if (gl_info->supported[ARB_DRAW_INSTANCED])
7976 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
7977 if (shader_glsl_use_explicit_attrib_location(gl_info))
7978 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7979 if (gl_info->supported[ARB_SHADER_VIEWPORT_LAYER_ARRAY])
7980 shader_addline(buffer, "#extension GL_ARB_shader_viewport_layer_array : enable\n");
7982 /* Base Declarations */
7983 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7985 for (i = 0; i < shader->input_signature.element_count; ++i)
7986 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
7988 if (args->point_size && !args->per_vertex_point_size)
7990 shader_addline(buffer, "uniform struct\n{\n");
7991 shader_addline(buffer, " float size;\n");
7992 shader_addline(buffer, " float size_min;\n");
7993 shader_addline(buffer, " float size_max;\n");
7994 shader_addline(buffer, "} ffp_point;\n");
7997 if (!needs_legacy_glsl_syntax(gl_info))
7999 if (args->clip_enabled)
8000 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8002 if (version->major < 3)
8004 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8005 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8006 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
8007 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8011 if (version->major < 4)
8012 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8014 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8015 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8017 if (reg_maps->shader_version.major >= 4)
8018 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8019 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8021 shader_addline(buffer, "void main()\n{\n");
8023 if (reg_maps->input_rel_addressing)
8025 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8026 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8027 for (i = 0; i < shader->input_signature.element_count; ++i)
8029 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8030 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8034 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8035 return 0;
8037 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8038 if (reg_maps->shader_version.major < 4)
8039 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8041 shader_addline(buffer, "}\n");
8043 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8044 TRACE("Compiling shader object %u.\n", shader_id);
8045 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8047 return shader_id;
8050 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8051 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8053 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8054 char reg_mask[6];
8055 unsigned int i;
8057 for (i = 0; i < output_signature->element_count; ++i)
8059 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8061 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8062 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8063 output->register_idx, reg_mask, output->register_idx, reg_mask);
8067 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8068 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8069 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8070 const char *phase_name, unsigned phase_idx)
8072 unsigned int i;
8073 HRESULT hr;
8075 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8076 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8077 for (i = 0; i < phase->temporary_count; ++i)
8078 shader_addline(buffer, "vec4 R%u;\n", i);
8079 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8080 shader_addline(buffer, "}\n");
8081 return hr;
8084 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8085 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8087 if (phase->instance_count)
8089 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8090 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8091 shader_addline(buffer, "}\n");
8093 else
8095 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8099 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context_gl *context_gl,
8100 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8102 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8103 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8104 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8105 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8106 const struct wined3d_hull_shader *hs = &shader->u.hs;
8107 const struct wined3d_shader_phase *phase;
8108 struct shader_glsl_ctx_priv priv_ctx;
8109 GLuint shader_id;
8110 unsigned int i;
8112 memset(&priv_ctx, 0, sizeof(priv_ctx));
8113 priv_ctx.gl_info = gl_info;
8114 priv_ctx.string_buffers = string_buffers;
8116 shader_glsl_add_version_declaration(buffer, gl_info);
8118 shader_glsl_enable_extensions(buffer, gl_info);
8119 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8121 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8123 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8125 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8126 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8128 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8130 if (hs->phases.control_point)
8132 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8133 shader->limits->packed_output);
8134 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8135 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8136 shader_addline(buffer, "}\n");
8139 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8140 if ((phase = hs->phases.control_point))
8142 for (i = 0; i < phase->temporary_count; ++i)
8143 shader_addline(buffer, "vec4 R%u;\n", i);
8144 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8145 return 0;
8146 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8148 else
8150 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8152 shader_addline(buffer, "}\n");
8154 for (i = 0; i < hs->phases.fork_count; ++i)
8156 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8157 &hs->phases.fork[i], "fork", i)))
8158 return 0;
8161 for (i = 0; i < hs->phases.join_count; ++i)
8163 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8164 &hs->phases.join[i], "join", i)))
8165 return 0;
8168 shader_addline(buffer, "void main()\n{\n");
8169 shader_addline(buffer, "hs_control_point_phase();\n");
8170 if (reg_maps->vocp)
8171 shader_addline(buffer, "barrier();\n");
8172 for (i = 0; i < hs->phases.fork_count; ++i)
8173 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8174 for (i = 0; i < hs->phases.join_count; ++i)
8175 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8176 shader_addline(buffer, "setup_patch_constant_output();\n");
8177 shader_addline(buffer, "}\n");
8179 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8180 TRACE("Compiling shader object %u.\n", shader_id);
8181 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8183 return shader_id;
8186 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8187 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8188 const struct ds_compile_args *args)
8190 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8192 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8193 shader_glsl_fixup_position(buffer, FALSE);
8196 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context_gl *context_gl,
8197 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8199 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8200 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8201 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8202 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8203 struct shader_glsl_ctx_priv priv_ctx;
8204 GLuint shader_id;
8206 memset(&priv_ctx, 0, sizeof(priv_ctx));
8207 priv_ctx.gl_info = gl_info;
8208 priv_ctx.cur_ds_args = args;
8209 priv_ctx.string_buffers = string_buffers;
8211 shader_glsl_add_version_declaration(buffer, gl_info);
8213 shader_glsl_enable_extensions(buffer, gl_info);
8214 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8216 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8218 shader_addline(buffer, "layout(");
8219 switch (shader->u.ds.tessellator_domain)
8221 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8222 shader_addline(buffer, "isolines");
8223 break;
8224 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8225 shader_addline(buffer, "quads");
8226 break;
8227 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8228 shader_addline(buffer, "triangles");
8229 break;
8231 switch (args->tessellator_output_primitive)
8233 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8234 if (args->render_offscreen)
8235 shader_addline(buffer, ", ccw");
8236 else
8237 shader_addline(buffer, ", cw");
8238 break;
8239 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8240 if (args->render_offscreen)
8241 shader_addline(buffer, ", cw");
8242 else
8243 shader_addline(buffer, ", ccw");
8244 break;
8245 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8246 shader_addline(buffer, ", point_mode");
8247 break;
8248 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8249 break;
8251 switch (args->tessellator_partitioning)
8253 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8254 shader_addline(buffer, ", fractional_odd_spacing");
8255 break;
8256 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8257 shader_addline(buffer, ", fractional_even_spacing");
8258 break;
8259 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8260 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8261 shader_addline(buffer, ", equal_spacing");
8262 break;
8264 shader_addline(buffer, ") in;\n");
8266 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8268 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8269 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8271 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8272 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8273 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8275 shader_addline(buffer, "void main()\n{\n");
8276 shader_addline(buffer, "setup_patch_constant_input();\n");
8278 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8279 return 0;
8281 shader_addline(buffer, "}\n");
8283 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8284 TRACE("Compiling shader object %u.\n", shader_id);
8285 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8287 return shader_id;
8290 /* Context activation is done by the caller. */
8291 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context_gl *context_gl,
8292 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8294 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8295 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8296 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8297 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8298 const struct wined3d_shader_signature_element *output;
8299 enum wined3d_primitive_type primitive_type;
8300 struct shader_glsl_ctx_priv priv_ctx;
8301 unsigned int max_vertices;
8302 unsigned int i, j;
8303 GLuint shader_id;
8305 memset(&priv_ctx, 0, sizeof(priv_ctx));
8306 priv_ctx.gl_info = gl_info;
8307 priv_ctx.string_buffers = string_buffers;
8309 shader_glsl_add_version_declaration(buffer, gl_info);
8311 shader_glsl_enable_extensions(buffer, gl_info);
8313 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8315 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8316 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8317 if (shader->u.gs.instance_count > 1)
8318 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8319 shader_addline(buffer, ") in;\n");
8321 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8322 if (!(max_vertices = shader->u.gs.vertices_out))
8324 switch (args->primitive_type)
8326 case WINED3D_PT_POINTLIST:
8327 max_vertices = 1;
8328 break;
8329 case WINED3D_PT_LINELIST:
8330 max_vertices = 2;
8331 break;
8332 case WINED3D_PT_TRIANGLELIST:
8333 max_vertices = 3;
8334 break;
8335 default:
8336 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8337 break;
8340 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8341 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8342 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8344 if (!gl_info->supported[ARB_CLIP_CONTROL])
8346 shader_addline(buffer, "uniform vec4 pos_fixup");
8347 if (reg_maps->viewport_array)
8348 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8349 shader_addline(buffer, ";\n");
8352 if (is_rasterization_disabled(shader))
8354 shader_glsl_generate_stream_output_setup(buffer, shader);
8356 else
8358 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8359 gl_info, TRUE, args->interpolation_mode);
8362 shader_addline(buffer, "void main()\n{\n");
8363 if (shader->function)
8365 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8366 return 0;
8368 else
8370 for (i = 0; i < max_vertices; ++i)
8372 for (j = 0; j < shader->output_signature.element_count; ++j)
8374 output = &shader->output_signature.elements[j];
8375 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8376 output->register_idx, i, output->register_idx);
8378 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8379 if (!gl_info->supported[ARB_CLIP_CONTROL])
8380 shader_glsl_fixup_position(buffer, FALSE);
8381 shader_addline(buffer, "EmitVertex();\n");
8384 shader_addline(buffer, "}\n");
8386 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8387 TRACE("Compiling shader object %u.\n", shader_id);
8388 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8390 return shader_id;
8393 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8395 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8396 const struct wined3d_gl_info *gl_info = priv->gl_info;
8397 struct wined3d_string_buffer *buffer = ctx->buffer;
8398 const struct wined3d_shader *shader = ctx->shader;
8400 switch (shader->reg_maps.shader_version.type)
8402 case WINED3D_SHADER_TYPE_PIXEL:
8403 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8404 break;
8405 case WINED3D_SHADER_TYPE_VERTEX:
8406 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8407 break;
8408 case WINED3D_SHADER_TYPE_DOMAIN:
8409 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8410 break;
8411 case WINED3D_SHADER_TYPE_GEOMETRY:
8412 case WINED3D_SHADER_TYPE_COMPUTE:
8413 break;
8414 default:
8415 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8416 break;
8420 /* Context activation is done by the caller. */
8421 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context_gl *context_gl,
8422 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8423 const struct wined3d_shader *shader)
8425 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8426 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8427 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8428 struct shader_glsl_ctx_priv priv_ctx;
8429 GLuint shader_id;
8430 unsigned int i;
8432 memset(&priv_ctx, 0, sizeof(priv_ctx));
8433 priv_ctx.gl_info = gl_info;
8434 priv_ctx.string_buffers = string_buffers;
8436 shader_glsl_add_version_declaration(buffer, gl_info);
8438 shader_glsl_enable_extensions(buffer, gl_info);
8439 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8441 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8443 for (i = 0; i < reg_maps->tgsm_count; ++i)
8445 if (reg_maps->tgsm[i].size)
8446 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8449 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8450 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8452 shader_addline(buffer, "void main()\n{\n");
8453 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8454 shader_addline(buffer, "}\n");
8456 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8457 TRACE("Compiling shader object %u.\n", shader_id);
8458 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8460 return shader_id;
8463 static GLuint find_glsl_fragment_shader(const struct wined3d_context_gl *context_gl,
8464 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8465 struct wined3d_shader *shader,
8466 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8468 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8469 struct glsl_shader_private *shader_data;
8470 struct ps_np2fixup_info *np2fixup;
8471 UINT i;
8472 DWORD new_size;
8473 GLuint ret;
8475 if (!shader->backend_data)
8477 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8479 ERR("Failed to allocate backend data.\n");
8480 return 0;
8483 shader_data = shader->backend_data;
8484 gl_shaders = shader_data->gl_shaders.ps;
8486 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8487 * so a linear search is more performant than a hashmap or a binary search
8488 * (cache coherency etc)
8490 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8492 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8494 if (args->np2_fixup)
8495 *np2fixup_info = &gl_shaders[i].np2fixup;
8496 return gl_shaders[i].id;
8500 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8501 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8503 if (shader_data->num_gl_shaders)
8505 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8506 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8508 else
8510 new_array = heap_alloc(sizeof(*gl_shaders));
8511 new_size = 1;
8514 if(!new_array) {
8515 ERR("Out of memory\n");
8516 return 0;
8518 shader_data->gl_shaders.ps = new_array;
8519 shader_data->shader_array_size = new_size;
8520 gl_shaders = new_array;
8523 gl_shaders[shader_data->num_gl_shaders].args = *args;
8525 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8526 memset(np2fixup, 0, sizeof(*np2fixup));
8527 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8529 string_buffer_clear(buffer);
8530 ret = shader_glsl_generate_fragment_shader(context_gl, buffer, string_buffers, shader, args, np2fixup);
8531 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8533 return ret;
8536 static BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8537 const DWORD use_map)
8539 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8540 return FALSE;
8541 if ((stored->clip_enabled) != new->clip_enabled)
8542 return FALSE;
8543 if (stored->point_size != new->point_size)
8544 return FALSE;
8545 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8546 return FALSE;
8547 if (stored->flatshading != new->flatshading)
8548 return FALSE;
8549 if (stored->next_shader_type != new->next_shader_type)
8550 return FALSE;
8551 if (stored->next_shader_input_count != new->next_shader_input_count)
8552 return FALSE;
8553 if (stored->fog_src != new->fog_src)
8554 return FALSE;
8555 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8558 static GLuint find_glsl_vertex_shader(const struct wined3d_context_gl *context_gl,
8559 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct vs_compile_args *args)
8561 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8562 uint32_t use_map = context_gl->c.stream_info.use_map;
8563 struct glsl_shader_private *shader_data;
8564 unsigned int i, new_size;
8565 GLuint ret;
8567 if (!shader->backend_data)
8569 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8571 ERR("Failed to allocate backend data.\n");
8572 return 0;
8575 shader_data = shader->backend_data;
8576 gl_shaders = shader_data->gl_shaders.vs;
8578 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8579 * so a linear search is more performant than a hashmap or a binary search
8580 * (cache coherency etc)
8582 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8584 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8585 return gl_shaders[i].id;
8588 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8590 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8592 if (shader_data->num_gl_shaders)
8594 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8595 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8597 else
8599 new_array = heap_alloc(sizeof(*gl_shaders));
8600 new_size = 1;
8603 if(!new_array) {
8604 ERR("Out of memory\n");
8605 return 0;
8607 shader_data->gl_shaders.vs = new_array;
8608 shader_data->shader_array_size = new_size;
8609 gl_shaders = new_array;
8612 gl_shaders[shader_data->num_gl_shaders].args = *args;
8614 string_buffer_clear(&priv->shader_buffer);
8615 ret = shader_glsl_generate_vertex_shader(context_gl, priv, shader, args);
8616 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8618 return ret;
8621 static GLuint find_glsl_hull_shader(const struct wined3d_context_gl *context_gl,
8622 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8624 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8625 struct glsl_shader_private *shader_data;
8626 unsigned int new_size;
8627 GLuint ret;
8629 if (!shader->backend_data)
8631 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8633 ERR("Failed to allocate backend data.\n");
8634 return 0;
8637 shader_data = shader->backend_data;
8638 gl_shaders = shader_data->gl_shaders.hs;
8640 if (shader_data->num_gl_shaders > 0)
8642 assert(shader_data->num_gl_shaders == 1);
8643 return gl_shaders[0].id;
8646 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8648 assert(!shader_data->gl_shaders.hs);
8649 new_size = 1;
8650 if (!(new_array = heap_alloc(sizeof(*new_array))))
8652 ERR("Failed to allocate GL shaders array.\n");
8653 return 0;
8655 shader_data->gl_shaders.hs = new_array;
8656 shader_data->shader_array_size = new_size;
8657 gl_shaders = new_array;
8659 string_buffer_clear(&priv->shader_buffer);
8660 ret = shader_glsl_generate_hull_shader(context_gl, priv, shader);
8661 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8663 return ret;
8666 static GLuint find_glsl_domain_shader(const struct wined3d_context_gl *context_gl,
8667 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8669 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8670 struct glsl_shader_private *shader_data;
8671 unsigned int i, new_size;
8672 GLuint ret;
8674 if (!shader->backend_data)
8676 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8678 ERR("Failed to allocate backend data.\n");
8679 return 0;
8682 shader_data = shader->backend_data;
8683 gl_shaders = shader_data->gl_shaders.ds;
8685 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8687 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8688 return gl_shaders[i].id;
8691 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8693 if (shader_data->num_gl_shaders)
8695 new_size = shader_data->shader_array_size + 1;
8696 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8698 else
8700 new_array = heap_alloc(sizeof(*new_array));
8701 new_size = 1;
8704 if (!new_array)
8706 ERR("Failed to allocate GL shaders array.\n");
8707 return 0;
8709 shader_data->gl_shaders.ds = new_array;
8710 shader_data->shader_array_size = new_size;
8711 gl_shaders = new_array;
8713 string_buffer_clear(&priv->shader_buffer);
8714 ret = shader_glsl_generate_domain_shader(context_gl, priv, shader, args);
8715 gl_shaders[shader_data->num_gl_shaders].args = *args;
8716 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8718 return ret;
8721 static GLuint find_glsl_geometry_shader(const struct wined3d_context_gl *context_gl,
8722 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8724 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8725 struct glsl_shader_private *shader_data;
8726 unsigned int i, new_size;
8727 GLuint ret;
8729 if (!shader->backend_data)
8731 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8733 ERR("Failed to allocate backend data.\n");
8734 return 0;
8737 shader_data = shader->backend_data;
8738 gl_shaders = shader_data->gl_shaders.gs;
8740 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8742 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8743 return gl_shaders[i].id;
8746 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8748 if (shader_data->num_gl_shaders)
8750 new_size = shader_data->shader_array_size + 1;
8751 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8753 else
8755 new_array = heap_alloc(sizeof(*new_array));
8756 new_size = 1;
8759 if (!new_array)
8761 ERR("Failed to allocate GL shaders array.\n");
8762 return 0;
8764 shader_data->gl_shaders.gs = new_array;
8765 shader_data->shader_array_size = new_size;
8766 gl_shaders = new_array;
8768 string_buffer_clear(&priv->shader_buffer);
8769 ret = shader_glsl_generate_geometry_shader(context_gl, priv, shader, args);
8770 gl_shaders[shader_data->num_gl_shaders].args = *args;
8771 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8773 return ret;
8776 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8778 switch (mcs)
8780 case WINED3D_MCS_MATERIAL:
8781 return material;
8782 case WINED3D_MCS_COLOR1:
8783 return "ffp_attrib_diffuse";
8784 case WINED3D_MCS_COLOR2:
8785 return "ffp_attrib_specular";
8786 default:
8787 ERR("Invalid material color source %#x.\n", mcs);
8788 return "<invalid>";
8792 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8793 const struct wined3d_ffp_vs_settings *settings, unsigned int idx, BOOL legacy_lighting)
8795 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8796 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8797 if (settings->localviewer)
8798 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8799 else
8800 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8801 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0%s) specular +="
8802 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n",
8803 legacy_lighting ? " && ffp_material.shininess > 0.0" : "", idx);
8806 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8807 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8809 const char *diffuse, *specular, *emissive, *ambient;
8810 unsigned int i, idx;
8812 if (!settings->lighting)
8814 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8815 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8816 return;
8819 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8820 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8821 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8822 shader_addline(buffer, "vec3 dir, dst;\n");
8823 shader_addline(buffer, "float att, t;\n");
8825 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8826 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8827 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8828 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8830 idx = 0;
8831 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8833 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8834 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8835 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8836 shader_addline(buffer, "dst.x = 1.0;\n");
8837 if (legacy_lighting)
8839 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8840 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8841 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8843 else
8845 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8847 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8848 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8849 if (!legacy_lighting)
8850 shader_addline(buffer, "att = 1.0 / att;\n");
8851 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8852 if (!settings->normal)
8854 shader_addline(buffer, "}\n");
8855 continue;
8857 shader_addline(buffer, "dir = normalize(dir);\n");
8858 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8859 shader_addline(buffer, "}\n");
8862 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8864 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8865 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8866 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8867 shader_addline(buffer, "dst.x = 1.0;\n");
8868 if (legacy_lighting)
8870 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8871 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8872 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8874 else
8876 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8878 shader_addline(buffer, "dir = normalize(dir);\n");
8879 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8880 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8881 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8882 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8883 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8884 idx, idx, idx, idx);
8885 if (legacy_lighting)
8886 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8887 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8888 idx, idx, idx);
8889 else
8890 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8891 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8892 idx, idx, idx);
8893 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8894 if (!settings->normal)
8896 shader_addline(buffer, "}\n");
8897 continue;
8899 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8900 shader_addline(buffer, "}\n");
8903 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8905 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8906 if (!settings->normal)
8907 continue;
8908 shader_addline(buffer, "att = 1.0;\n");
8909 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8910 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8913 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8915 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8916 if (!settings->normal)
8917 continue;
8918 shader_addline(buffer, "att = 1.0;\n");
8919 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8920 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8923 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8924 ambient, diffuse, emissive);
8925 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8926 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8929 /* Context activation is done by the caller. */
8930 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8931 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8933 static const struct attrib_info
8935 const char type[6];
8936 const char name[24];
8938 attrib_info[] =
8940 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8941 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8942 /* TODO: Indexed vertex blending */
8943 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8944 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8945 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8946 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8947 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8949 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8950 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8951 BOOL output_legacy_fogcoord = legacy_syntax;
8952 BOOL legacy_lighting = priv->legacy_lighting;
8953 GLuint shader_obj;
8954 unsigned int i;
8956 string_buffer_clear(buffer);
8958 shader_glsl_add_version_declaration(buffer, gl_info);
8960 if (shader_glsl_use_explicit_attrib_location(gl_info))
8961 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8963 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
8965 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
8967 if (shader_glsl_use_explicit_attrib_location(gl_info))
8968 shader_addline(buffer, "layout(location = %u) ", i);
8969 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
8971 shader_addline(buffer, "\n");
8973 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
8974 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
8975 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
8976 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", WINED3D_MAX_TEXTURES);
8978 shader_addline(buffer, "uniform struct\n{\n");
8979 shader_addline(buffer, " vec4 emissive;\n");
8980 shader_addline(buffer, " vec4 ambient;\n");
8981 shader_addline(buffer, " vec4 diffuse;\n");
8982 shader_addline(buffer, " vec4 specular;\n");
8983 shader_addline(buffer, " float shininess;\n");
8984 shader_addline(buffer, "} ffp_material;\n");
8986 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
8987 shader_addline(buffer, "uniform struct\n{\n");
8988 shader_addline(buffer, " vec4 diffuse;\n");
8989 shader_addline(buffer, " vec4 specular;\n");
8990 shader_addline(buffer, " vec4 ambient;\n");
8991 shader_addline(buffer, " vec4 position;\n");
8992 shader_addline(buffer, " vec3 direction;\n");
8993 shader_addline(buffer, " float range;\n");
8994 shader_addline(buffer, " float falloff;\n");
8995 shader_addline(buffer, " float c_att;\n");
8996 shader_addline(buffer, " float l_att;\n");
8997 shader_addline(buffer, " float q_att;\n");
8998 shader_addline(buffer, " float cos_htheta;\n");
8999 shader_addline(buffer, " float cos_hphi;\n");
9000 shader_addline(buffer, "} ffp_light[%u];\n", WINED3D_MAX_ACTIVE_LIGHTS);
9002 if (settings->point_size)
9004 shader_addline(buffer, "uniform struct\n{\n");
9005 shader_addline(buffer, " float size;\n");
9006 shader_addline(buffer, " float size_min;\n");
9007 shader_addline(buffer, " float size_max;\n");
9008 shader_addline(buffer, " float c_att;\n");
9009 shader_addline(buffer, " float l_att;\n");
9010 shader_addline(buffer, " float q_att;\n");
9011 shader_addline(buffer, "} ffp_point;\n");
9014 if (legacy_syntax)
9016 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9017 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9018 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9019 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9021 else
9023 if (settings->clipping)
9024 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9026 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9027 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9028 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9029 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9032 shader_addline(buffer, "\nvoid main()\n{\n");
9033 shader_addline(buffer, "float m;\n");
9034 shader_addline(buffer, "vec3 r;\n");
9036 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9038 if (attrib_info[i].name[0])
9039 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9040 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9042 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9044 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9045 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9046 && settings->texcoords & (1u << i))
9047 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9050 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9052 if (settings->transformed)
9054 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9055 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9056 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9058 else
9060 for (i = 0; i < settings->vertexblends; ++i)
9061 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9063 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9064 for (i = 0; i < settings->vertexblends + 1; ++i)
9065 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9067 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9068 if (settings->clipping)
9070 if (legacy_syntax)
9071 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9072 else
9073 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9074 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9076 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9079 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9080 if (settings->normal)
9082 if (!settings->vertexblends)
9084 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9086 else
9088 for (i = 0; i < settings->vertexblends + 1; ++i)
9089 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9092 if (settings->normalize)
9093 shader_addline(buffer, "normal = normalize(normal);\n");
9096 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9097 if (legacy_syntax)
9099 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9100 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9102 else
9104 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9105 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9108 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9110 BOOL output_legacy_texcoord = legacy_syntax;
9112 switch (settings->texgen[i] & 0xffff0000)
9114 case WINED3DTSS_TCI_PASSTHRU:
9115 if (settings->texcoords & (1u << i))
9116 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9117 i, i, i);
9118 else if (shader_glsl_full_ffp_varyings(gl_info))
9119 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9120 else
9121 output_legacy_texcoord = FALSE;
9122 break;
9124 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9125 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9126 break;
9128 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9129 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9130 break;
9132 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9133 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9134 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9135 break;
9137 case WINED3DTSS_TCI_SPHEREMAP:
9138 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9139 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9140 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9141 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9142 break;
9144 default:
9145 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9146 break;
9148 if (output_legacy_texcoord)
9149 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9152 switch (settings->fog_mode)
9154 case WINED3D_FFP_VS_FOG_OFF:
9155 output_legacy_fogcoord = FALSE;
9156 break;
9158 case WINED3D_FFP_VS_FOG_FOGCOORD:
9159 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9160 break;
9162 case WINED3D_FFP_VS_FOG_RANGE:
9163 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9164 break;
9166 case WINED3D_FFP_VS_FOG_DEPTH:
9167 if (settings->ortho_fog)
9169 if (gl_info->supported[ARB_CLIP_CONTROL])
9170 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9171 else
9172 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9173 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9175 else if (settings->transformed)
9177 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9179 else
9181 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9183 break;
9185 default:
9186 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9187 break;
9189 if (output_legacy_fogcoord)
9190 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9192 if (settings->point_size)
9194 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9195 " + ffp_point.l_att * length(ec_pos.xyz)"
9196 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9197 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9198 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9201 shader_addline(buffer, "}\n");
9203 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9204 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9206 return shader_obj;
9209 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9210 DWORD argnum, unsigned int stage, DWORD arg)
9212 const char *ret;
9214 if (arg == ARG_UNUSED)
9215 return "<unused arg>";
9217 switch (arg & WINED3DTA_SELECTMASK)
9219 case WINED3DTA_DIFFUSE:
9220 ret = "ffp_varying_diffuse";
9221 break;
9223 case WINED3DTA_CURRENT:
9224 ret = "ret";
9225 break;
9227 case WINED3DTA_TEXTURE:
9228 switch (stage)
9230 case 0: ret = "tex0"; break;
9231 case 1: ret = "tex1"; break;
9232 case 2: ret = "tex2"; break;
9233 case 3: ret = "tex3"; break;
9234 case 4: ret = "tex4"; break;
9235 case 5: ret = "tex5"; break;
9236 case 6: ret = "tex6"; break;
9237 case 7: ret = "tex7"; break;
9238 default:
9239 ret = "<invalid texture>";
9240 break;
9242 break;
9244 case WINED3DTA_TFACTOR:
9245 ret = "tex_factor";
9246 break;
9248 case WINED3DTA_SPECULAR:
9249 ret = "ffp_varying_specular";
9250 break;
9252 case WINED3DTA_TEMP:
9253 ret = "temp_reg";
9254 break;
9256 case WINED3DTA_CONSTANT:
9257 switch (stage)
9259 case 0: ret = "tss_const0"; break;
9260 case 1: ret = "tss_const1"; break;
9261 case 2: ret = "tss_const2"; break;
9262 case 3: ret = "tss_const3"; break;
9263 case 4: ret = "tss_const4"; break;
9264 case 5: ret = "tss_const5"; break;
9265 case 6: ret = "tss_const6"; break;
9266 case 7: ret = "tss_const7"; break;
9267 default:
9268 ret = "<invalid constant>";
9269 break;
9271 break;
9273 default:
9274 return "<unhandled arg>";
9277 if (arg & WINED3DTA_COMPLEMENT)
9279 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9280 if (argnum == 0)
9281 ret = "arg0";
9282 else if (argnum == 1)
9283 ret = "arg1";
9284 else if (argnum == 2)
9285 ret = "arg2";
9288 if (arg & WINED3DTA_ALPHAREPLICATE)
9290 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9291 if (argnum == 0)
9292 ret = "arg0";
9293 else if (argnum == 1)
9294 ret = "arg1";
9295 else if (argnum == 2)
9296 ret = "arg2";
9299 return ret;
9302 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9303 BOOL alpha, BOOL tmp_dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9305 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9307 if (color && alpha)
9308 dstmask = "";
9309 else if (color)
9310 dstmask = ".xyz";
9311 else
9312 dstmask = ".w";
9314 dstreg = tmp_dst ? "temp_reg" : "ret";
9316 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9317 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9318 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9320 switch (op)
9322 case WINED3D_TOP_DISABLE:
9323 break;
9325 case WINED3D_TOP_SELECT_ARG1:
9326 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9327 break;
9329 case WINED3D_TOP_SELECT_ARG2:
9330 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9331 break;
9333 case WINED3D_TOP_MODULATE:
9334 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9335 break;
9337 case WINED3D_TOP_MODULATE_4X:
9338 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9339 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9340 break;
9342 case WINED3D_TOP_MODULATE_2X:
9343 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9344 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9345 break;
9347 case WINED3D_TOP_ADD:
9348 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9349 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9350 break;
9352 case WINED3D_TOP_ADD_SIGNED:
9353 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9354 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9355 break;
9357 case WINED3D_TOP_ADD_SIGNED_2X:
9358 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9359 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9360 break;
9362 case WINED3D_TOP_SUBTRACT:
9363 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9364 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9365 break;
9367 case WINED3D_TOP_ADD_SMOOTH:
9368 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9369 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9370 break;
9372 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9373 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9374 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9375 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9376 break;
9378 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9379 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9380 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9381 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9382 break;
9384 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9385 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9386 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9387 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9388 break;
9390 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9391 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9392 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9393 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9394 break;
9396 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9397 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9398 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9399 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9400 break;
9402 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9403 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9404 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9405 break;
9407 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9408 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9409 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9410 break;
9412 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9413 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9414 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9415 break;
9416 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9417 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9418 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9419 break;
9421 case WINED3D_TOP_BUMPENVMAP:
9422 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9423 /* These are handled in the first pass, nothing to do. */
9424 break;
9426 case WINED3D_TOP_DOTPRODUCT3:
9427 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9428 dstreg, dstmask, arg1, arg2, dstmask);
9429 break;
9431 case WINED3D_TOP_MULTIPLY_ADD:
9432 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9433 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9434 break;
9436 case WINED3D_TOP_LERP:
9437 /* MSDN isn't quite right here. */
9438 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9439 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9440 break;
9442 default:
9443 FIXME("Unhandled operation %#x.\n", op);
9444 break;
9448 /* Context activation is done by the caller. */
9449 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9450 const struct ffp_frag_settings *settings, const struct wined3d_context_gl *context_gl)
9452 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9453 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9454 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9455 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9456 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
9457 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9458 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9459 UINT lowest_disabled_stage;
9460 GLuint shader_id;
9461 DWORD arg0, arg1, arg2;
9462 unsigned int stage;
9464 string_buffer_clear(buffer);
9466 /* Find out which textures are read */
9467 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9469 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9470 break;
9472 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9473 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9474 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9476 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9477 || (stage == 0 && settings->color_key_enabled))
9478 tex_map |= 1u << stage;
9479 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9480 tfactor_used = TRUE;
9481 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9482 tempreg_used = TRUE;
9483 if (settings->op[stage].tmp_dst)
9484 tempreg_used = TRUE;
9485 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9486 tss_const_map |= 1u << stage;
9488 switch (settings->op[stage].cop)
9490 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9491 lum_map |= 1u << stage;
9492 /* fall through */
9493 case WINED3D_TOP_BUMPENVMAP:
9494 bump_map |= 1u << stage;
9495 /* fall through */
9496 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9497 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9498 tex_map |= 1u << stage;
9499 break;
9501 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9502 tfactor_used = TRUE;
9503 break;
9505 default:
9506 break;
9509 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9510 continue;
9512 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9513 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9514 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9516 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9517 tex_map |= 1u << stage;
9518 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9519 tfactor_used = TRUE;
9520 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9521 tempreg_used = TRUE;
9522 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9523 tss_const_map |= 1u << stage;
9525 lowest_disabled_stage = stage;
9527 shader_glsl_add_version_declaration(buffer, gl_info);
9529 if (shader_glsl_use_explicit_attrib_location(gl_info))
9530 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9531 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9532 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9533 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9534 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9536 if (!use_legacy_fragment_output(gl_info))
9538 shader_addline(buffer, "vec4 ps_out[1];\n");
9539 if (shader_glsl_use_explicit_attrib_location(gl_info))
9540 shader_addline(buffer, "layout(location = 0) ");
9541 shader_addline(buffer, "out vec4 color_out0;\n");
9544 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9545 shader_addline(buffer, "vec4 ret;\n");
9546 if (tempreg_used || settings->sRGB_write)
9547 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9548 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9550 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9552 const char *sampler_type;
9554 if (tss_const_map & (1u << stage))
9555 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9557 if (!(tex_map & (1u << stage)))
9558 continue;
9560 switch (settings->op[stage].tex_type)
9562 case WINED3D_GL_RES_TYPE_TEX_1D:
9563 sampler_type = "1D";
9564 break;
9565 case WINED3D_GL_RES_TYPE_TEX_2D:
9566 sampler_type = "2D";
9567 break;
9568 case WINED3D_GL_RES_TYPE_TEX_3D:
9569 sampler_type = "3D";
9570 break;
9571 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9572 sampler_type = "Cube";
9573 break;
9574 case WINED3D_GL_RES_TYPE_TEX_RECT:
9575 sampler_type = "2DRect";
9576 break;
9577 default:
9578 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9579 sampler_type = NULL;
9580 break;
9582 if (sampler_type)
9584 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9585 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
9586 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9589 shader_addline(buffer, "vec4 tex%u;\n", stage);
9591 if (!(bump_map & (1u << stage)))
9592 continue;
9593 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9595 if (!(lum_map & (1u << stage)))
9596 continue;
9597 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9598 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9600 if (tfactor_used)
9601 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9602 if (settings->color_key_enabled)
9603 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9604 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9606 if (settings->sRGB_write)
9608 shader_addline(buffer, "const vec4 srgb_const0 = ");
9609 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
9610 shader_addline(buffer, ";\n");
9611 shader_addline(buffer, "const vec4 srgb_const1 = ");
9612 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
9613 shader_addline(buffer, ";\n");
9616 shader_addline(buffer, "uniform struct\n{\n");
9617 shader_addline(buffer, " vec4 color;\n");
9618 shader_addline(buffer, " float density;\n");
9619 shader_addline(buffer, " float end;\n");
9620 shader_addline(buffer, " float scale;\n");
9621 shader_addline(buffer, "} ffp_fog;\n");
9623 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9624 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9626 if (legacy_syntax)
9628 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9629 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9630 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9631 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9632 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9634 else
9636 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9637 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9638 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9639 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9640 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9643 shader_addline(buffer, "void main()\n{\n");
9645 if (legacy_syntax)
9647 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9648 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9651 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9653 if (tex_map & (1u << stage))
9655 if (settings->pointsprite)
9656 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9657 else if (settings->texcoords_initialized & (1u << stage))
9658 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9659 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9660 else
9661 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9665 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9666 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9668 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9669 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9671 /* Generate texture sampling instructions */
9672 for (stage = 0; stage < WINED3D_MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9674 const char *texture_function, *coord_mask;
9675 BOOL proj;
9677 if (!(tex_map & (1u << stage)))
9678 continue;
9680 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9682 proj = FALSE;
9684 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9685 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9687 proj = TRUE;
9689 else
9691 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9692 proj = TRUE;
9695 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9696 proj = FALSE;
9698 switch (settings->op[stage].tex_type)
9700 case WINED3D_GL_RES_TYPE_TEX_1D:
9701 texture_function = "texture1D";
9702 coord_mask = "x";
9703 break;
9704 case WINED3D_GL_RES_TYPE_TEX_2D:
9705 texture_function = "texture2D";
9706 coord_mask = "xy";
9707 break;
9708 case WINED3D_GL_RES_TYPE_TEX_3D:
9709 texture_function = "texture3D";
9710 coord_mask = "xyz";
9711 break;
9712 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9713 texture_function = "textureCube";
9714 coord_mask = "xyz";
9715 break;
9716 case WINED3D_GL_RES_TYPE_TEX_RECT:
9717 texture_function = "texture2DRect";
9718 coord_mask = "xy";
9719 break;
9720 default:
9721 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9722 texture_function = "";
9723 coord_mask = "xyzw";
9724 proj = FALSE;
9725 break;
9727 if (!legacy_syntax)
9728 texture_function = "texture";
9730 if (stage > 0
9731 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9732 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9734 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9736 /* With projective textures, texbem only divides the static
9737 * texture coordinate, not the displacement, so multiply the
9738 * displacement with the dividing parameter before passing it to
9739 * TXP. */
9740 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9742 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9744 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9745 stage, stage);
9746 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9748 else
9750 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9751 stage, stage);
9752 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9755 else
9757 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9760 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ret.%s%s);\n",
9761 stage, texture_function, proj ? "Proj" : "", stage, coord_mask, proj ? "w" : "");
9763 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9764 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9765 stage, stage - 1, stage - 1, stage - 1);
9767 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9769 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9770 stage, texture_function, proj ? "Proj" : "", stage, stage);
9772 else
9774 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].%s%s);\n",
9775 stage, texture_function, proj ? "Proj" : "", stage, stage, coord_mask, proj ? "w" : "");
9778 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9779 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9780 settings->op[stage].color_fixup);
9783 if (settings->color_key_enabled)
9784 shader_glsl_generate_colour_key_test(buffer, "tex0", "color_key[0]", "color_key[1]");
9786 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9788 /* Generate the main shader */
9789 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9791 BOOL op_equal;
9793 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9794 break;
9796 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9797 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9798 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9799 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9800 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9801 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9802 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9803 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9804 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9805 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9806 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9807 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9808 else
9809 op_equal = settings->op[stage].aop == settings->op[stage].cop
9810 && settings->op[stage].carg0 == settings->op[stage].aarg0
9811 && settings->op[stage].carg1 == settings->op[stage].aarg1
9812 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9814 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9816 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9817 settings->op[stage].cop, settings->op[stage].carg0,
9818 settings->op[stage].carg1, settings->op[stage].carg2);
9820 else if (op_equal)
9822 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9823 settings->op[stage].cop, settings->op[stage].carg0,
9824 settings->op[stage].carg1, settings->op[stage].carg2);
9826 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9827 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9829 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9830 settings->op[stage].cop, settings->op[stage].carg0,
9831 settings->op[stage].carg1, settings->op[stage].carg2);
9832 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9833 settings->op[stage].aop, settings->op[stage].aarg0,
9834 settings->op[stage].aarg1, settings->op[stage].aarg2);
9838 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9839 get_fragment_output(gl_info));
9841 if (settings->sRGB_write)
9842 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9844 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9846 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9847 if (!use_legacy_fragment_output(gl_info))
9848 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9850 shader_addline(buffer, "}\n");
9852 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9853 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9855 string_buffer_release(&priv->string_buffers, tex_reg_name);
9856 return shader_id;
9859 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9860 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9862 struct glsl_ffp_vertex_shader *shader;
9863 const struct wine_rb_entry *entry;
9865 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9866 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9868 if (!(shader = heap_alloc(sizeof(*shader))))
9869 return NULL;
9871 shader->desc.settings = *settings;
9872 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9873 list_init(&shader->linked_programs);
9874 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9875 ERR("Failed to insert ffp vertex shader.\n");
9877 return shader;
9880 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9881 const struct ffp_frag_settings *args, const struct wined3d_context_gl *context_gl)
9883 struct glsl_ffp_fragment_shader *glsl_desc;
9884 const struct ffp_frag_desc *desc;
9886 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9887 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9889 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9890 return NULL;
9892 glsl_desc->entry.settings = *args;
9893 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context_gl);
9894 list_init(&glsl_desc->linked_programs);
9895 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9897 return glsl_desc;
9901 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9902 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9904 unsigned int i;
9905 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9907 for (i = 0; i < vs_c_count; ++i)
9909 string_buffer_sprintf(name, "vs_c[%u]", i);
9910 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9912 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9914 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9916 string_buffer_sprintf(name, "vs_i[%u]", i);
9917 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9920 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9922 string_buffer_sprintf(name, "vs_b[%u]", i);
9923 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9926 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9927 vs->base_vertex_id_location = GL_EXTCALL(glGetUniformLocation(program_id, "base_vertex_id"));
9929 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9931 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9932 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9934 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9935 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9936 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9938 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9939 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9941 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9942 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9943 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9944 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9945 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9946 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9947 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
9949 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9950 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9951 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9952 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9953 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9954 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9955 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9956 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9957 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
9958 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9959 string_buffer_sprintf(name, "ffp_light[%u].range", i);
9960 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9961 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
9962 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9963 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
9964 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9965 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
9966 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9967 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
9968 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9969 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
9970 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9971 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
9972 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9974 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
9975 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
9976 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
9977 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
9978 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
9979 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
9980 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
9982 string_buffer_release(&priv->string_buffers, name);
9985 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
9986 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
9988 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9991 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
9992 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
9994 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9997 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
9998 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10000 unsigned int i;
10001 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10003 for (i = 0; i < ps_c_count; ++i)
10005 string_buffer_sprintf(name, "ps_c[%u]", i);
10006 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10008 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10010 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10012 string_buffer_sprintf(name, "ps_i[%u]", i);
10013 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10016 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10018 string_buffer_sprintf(name, "ps_b[%u]", i);
10019 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10022 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10024 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10025 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10026 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10027 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10028 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10029 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10030 string_buffer_sprintf(name, "tss_const%u", i);
10031 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10034 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10035 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10037 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10038 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10039 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10040 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10042 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10044 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10045 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10046 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10048 string_buffer_release(&priv->string_buffers, name);
10051 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10052 const struct wined3d_context_gl *context_gl, struct wined3d_shader *shader)
10054 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10055 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10056 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10057 struct glsl_cs_compiled_shader *gl_shaders;
10058 struct glsl_shader_private *shader_data;
10059 struct glsl_shader_prog_link *entry;
10060 GLuint shader_id, program_id;
10062 if (!(entry = heap_alloc(sizeof(*entry))))
10064 ERR("Out of memory.\n");
10065 return E_OUTOFMEMORY;
10068 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10070 ERR("Failed to allocate backend data.\n");
10071 heap_free(entry);
10072 return E_OUTOFMEMORY;
10074 shader_data = shader->backend_data;
10076 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10078 ERR("Failed to allocate GL shader array.\n");
10079 heap_free(entry);
10080 heap_free(shader->backend_data);
10081 shader->backend_data = NULL;
10082 return E_OUTOFMEMORY;
10084 shader_data->shader_array_size = 1;
10085 gl_shaders = shader_data->gl_shaders.cs;
10087 TRACE("Compiling compute shader %p.\n", shader);
10089 string_buffer_clear(buffer);
10090 shader_id = shader_glsl_generate_compute_shader(context_gl, buffer, &priv->string_buffers, shader);
10091 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10093 program_id = GL_EXTCALL(glCreateProgram());
10094 TRACE("Created new GLSL shader program %u.\n", program_id);
10096 entry->id = program_id;
10097 entry->vs.id = 0;
10098 entry->hs.id = 0;
10099 entry->ds.id = 0;
10100 entry->gs.id = 0;
10101 entry->ps.id = 0;
10102 entry->cs.id = shader_id;
10103 entry->constant_version = 0;
10104 entry->shader_controlled_clip_distances = 0;
10105 entry->ps.np2_fixup_info = NULL;
10106 add_glsl_program_entry(priv, entry);
10108 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10109 GL_EXTCALL(glAttachShader(program_id, shader_id));
10110 checkGLcall("glAttachShader");
10112 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10114 TRACE("Linking GLSL shader program %u.\n", program_id);
10115 GL_EXTCALL(glLinkProgram(program_id));
10116 shader_glsl_validate_link(gl_info, program_id);
10118 GL_EXTCALL(glUseProgram(program_id));
10119 checkGLcall("glUseProgram");
10120 shader_glsl_load_program_resources(context_gl, priv, program_id, shader);
10121 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10123 entry->constant_update_mask = 0;
10125 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10126 checkGLcall("glUseProgram");
10127 return WINED3D_OK;
10130 static GLuint find_glsl_compute_shader(const struct wined3d_context_gl *context_gl,
10131 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10133 struct glsl_shader_private *shader_data;
10135 if (!shader->backend_data)
10137 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10138 if (FAILED(shader_glsl_compile_compute_shader(priv, context_gl, shader)))
10140 ERR("Failed to compile compute shader %p.\n", shader);
10141 return 0;
10144 shader_data = shader->backend_data;
10145 return shader_data->gl_shaders.cs[0].id;
10148 /* Context activation is done by the caller. */
10149 static void set_glsl_compute_shader_program(const struct wined3d_context_gl *context_gl,
10150 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10152 struct glsl_shader_prog_link *entry;
10153 struct wined3d_shader *shader;
10154 struct glsl_program_key key;
10155 GLuint cs_id;
10157 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10158 return;
10160 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10162 WARN("Compute shader is NULL.\n");
10163 ctx_data->glsl_program = NULL;
10164 return;
10167 cs_id = find_glsl_compute_shader(context_gl, priv, shader);
10168 memset(&key, 0, sizeof(key));
10169 key.cs_id = cs_id;
10170 if (!(entry = get_glsl_program_entry(priv, &key)))
10171 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10172 ctx_data->glsl_program = entry;
10175 /* Context activation is done by the caller. */
10176 static void set_glsl_shader_program(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
10177 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10179 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
10180 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10181 const struct wined3d_shader *pre_rasterization_shader;
10182 const struct ps_np2fixup_info *np2fixup_info = NULL;
10183 struct wined3d_shader *hshader, *dshader, *gshader;
10184 struct glsl_shader_prog_link *entry = NULL;
10185 struct wined3d_shader *vshader = NULL;
10186 struct wined3d_shader *pshader = NULL;
10187 GLuint reorder_shader_id = 0;
10188 struct glsl_program_key key;
10189 GLuint program_id;
10190 unsigned int i;
10191 GLuint vs_id = 0;
10192 GLuint hs_id = 0;
10193 GLuint ds_id = 0;
10194 GLuint gs_id = 0;
10195 GLuint ps_id = 0;
10196 struct list *ps_list, *vs_list;
10197 WORD attribs_map;
10198 struct wined3d_string_buffer *tmp_name;
10200 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10202 vs_id = ctx_data->glsl_program->vs.id;
10203 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10205 if (use_vs(state))
10206 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10208 else if (use_vs(state))
10210 struct vs_compile_args vs_compile_args;
10212 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10214 find_vs_compile_args(state, vshader, context_gl->c.stream_info.swizzle_map, &vs_compile_args, &context_gl->c);
10215 vs_id = find_glsl_vertex_shader(context_gl, priv, vshader, &vs_compile_args);
10216 vs_list = &vshader->linked_programs;
10218 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10220 struct glsl_ffp_vertex_shader *ffp_shader;
10221 struct wined3d_ffp_vs_settings settings;
10223 wined3d_ffp_get_vs_settings(&context_gl->c, state, &settings);
10224 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10225 vs_id = ffp_shader->id;
10226 vs_list = &ffp_shader->linked_programs;
10229 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10230 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10231 hs_id = ctx_data->glsl_program->hs.id;
10232 else if (hshader)
10233 hs_id = find_glsl_hull_shader(context_gl, priv, hshader);
10235 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10236 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10238 ds_id = ctx_data->glsl_program->ds.id;
10240 else if (dshader)
10242 struct ds_compile_args args;
10244 find_ds_compile_args(state, dshader, &args, &context_gl->c);
10245 ds_id = find_glsl_domain_shader(context_gl, priv, dshader, &args);
10248 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10249 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10251 gs_id = ctx_data->glsl_program->gs.id;
10253 else if (gshader)
10255 struct gs_compile_args args;
10257 find_gs_compile_args(state, gshader, &args, &context_gl->c);
10258 gs_id = find_glsl_geometry_shader(context_gl, priv, gshader, &args);
10261 /* A pixel shader is not used when rasterization is disabled. */
10262 if (is_rasterization_disabled(gshader))
10264 ps_id = 0;
10265 ps_list = NULL;
10267 else if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10269 ps_id = ctx_data->glsl_program->ps.id;
10270 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10272 if (use_ps(state))
10273 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10275 else if (use_ps(state))
10277 struct ps_compile_args ps_compile_args;
10278 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10279 find_ps_compile_args(state, pshader, context_gl->c.stream_info.position_transformed,
10280 &ps_compile_args, &context_gl->c);
10281 ps_id = find_glsl_fragment_shader(context_gl, &priv->shader_buffer, &priv->string_buffers,
10282 pshader, &ps_compile_args, &np2fixup_info);
10283 ps_list = &pshader->linked_programs;
10285 else if (priv->fragment_pipe == &glsl_fragment_pipe
10286 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10288 struct glsl_ffp_fragment_shader *ffp_shader;
10289 struct ffp_frag_settings settings;
10291 gen_ffp_frag_op(&context_gl->c, state, &settings, FALSE);
10292 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context_gl);
10293 ps_id = ffp_shader->id;
10294 ps_list = &ffp_shader->linked_programs;
10297 key.vs_id = vs_id;
10298 key.hs_id = hs_id;
10299 key.ds_id = ds_id;
10300 key.gs_id = gs_id;
10301 key.ps_id = ps_id;
10302 key.cs_id = 0;
10303 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10305 ctx_data->glsl_program = entry;
10306 return;
10309 /* If we get to this point, then no matching program exists, so we create one */
10310 program_id = GL_EXTCALL(glCreateProgram());
10311 TRACE("Created new GLSL shader program %u.\n", program_id);
10313 /* Create the entry */
10314 entry = heap_alloc(sizeof(*entry));
10315 entry->id = program_id;
10316 entry->vs.id = vs_id;
10317 entry->hs.id = hs_id;
10318 entry->ds.id = ds_id;
10319 entry->gs.id = gs_id;
10320 entry->ps.id = ps_id;
10321 entry->cs.id = 0;
10322 entry->constant_version = 0;
10323 entry->shader_controlled_clip_distances = 0;
10324 entry->ps.np2_fixup_info = np2fixup_info;
10325 /* Add the hash table entry */
10326 add_glsl_program_entry(priv, entry);
10328 /* Set the current program */
10329 ctx_data->glsl_program = entry;
10331 /* Attach GLSL vshader */
10332 if (vs_id)
10334 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10335 GL_EXTCALL(glAttachShader(program_id, vs_id));
10336 checkGLcall("glAttachShader");
10338 list_add_head(vs_list, &entry->vs.shader_entry);
10341 if (vshader)
10343 attribs_map = vshader->reg_maps.input_registers;
10344 if (vshader->reg_maps.shader_version.major < 4)
10346 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10347 state->primitive_type == WINED3D_PT_POINTLIST && vshader->reg_maps.point_size,
10348 d3d_info->emulated_flatshading
10349 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10350 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10351 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10352 checkGLcall("glAttachShader");
10353 /* Flag the reorder function for deletion, it will be freed
10354 * automatically when the program is destroyed. */
10355 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10358 else
10360 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10363 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10365 /* Bind vertex attributes to a corresponding index number to match
10366 * the same index numbers as ARB_vertex_programs (makes loading
10367 * vertex attributes simpler). With this method, we can use the
10368 * exact same code to load the attributes later for both ARB and
10369 * GLSL shaders.
10371 * We have to do this here because we need to know the Program ID
10372 * in order to make the bindings work, and it has to be done prior
10373 * to linking the GLSL program. */
10374 tmp_name = string_buffer_get(&priv->string_buffers);
10375 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10377 if (!(attribs_map & 1))
10378 continue;
10380 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10381 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10382 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10384 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10385 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10386 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10387 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10390 checkGLcall("glBindAttribLocation");
10392 if (!use_legacy_fragment_output(gl_info))
10394 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
10396 string_buffer_sprintf(tmp_name, "color_out%u", i);
10397 if (state->blend_state && state->blend_state->dual_source)
10398 GL_EXTCALL(glBindFragDataLocationIndexed(program_id, 0, i, tmp_name->buffer));
10399 else
10400 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10401 checkGLcall("glBindFragDataLocation");
10404 string_buffer_release(&priv->string_buffers, tmp_name);
10407 if (hshader)
10409 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10410 GL_EXTCALL(glAttachShader(program_id, hs_id));
10411 checkGLcall("glAttachShader");
10413 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10416 if (dshader)
10418 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10419 GL_EXTCALL(glAttachShader(program_id, ds_id));
10420 checkGLcall("glAttachShader");
10422 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10425 if (gshader)
10427 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10428 GL_EXTCALL(glAttachShader(program_id, gs_id));
10429 checkGLcall("glAttachShader");
10431 shader_glsl_init_transform_feedback(context_gl, priv, program_id, gshader);
10433 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10436 /* Attach GLSL pshader */
10437 if (ps_id)
10439 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10440 GL_EXTCALL(glAttachShader(program_id, ps_id));
10441 checkGLcall("glAttachShader");
10443 list_add_head(ps_list, &entry->ps.shader_entry);
10446 /* Link the program */
10447 TRACE("Linking GLSL shader program %u.\n", program_id);
10448 GL_EXTCALL(glLinkProgram(program_id));
10449 shader_glsl_validate_link(gl_info, program_id);
10451 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10452 vshader ? vshader->limits->constant_float : 0);
10453 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10454 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10455 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10456 pshader ? pshader->limits->constant_float : 0);
10457 checkGLcall("find glsl program uniform locations");
10459 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10460 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10462 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10463 entry->shader_controlled_clip_distances = 1;
10464 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10467 if (needs_legacy_glsl_syntax(gl_info))
10469 if (pshader && pshader->reg_maps.shader_version.major >= 3
10470 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10472 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10473 entry->vs.vertex_color_clamp = GL_FALSE;
10475 else
10477 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10480 else
10482 /* With core profile we never change vertex_color_clamp from
10483 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10484 * glClampColorARB(). */
10485 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10488 /* Set the shader to allow uniform loading on it */
10489 GL_EXTCALL(glUseProgram(program_id));
10490 checkGLcall("glUseProgram");
10492 entry->constant_update_mask = 0;
10493 if (vshader)
10495 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10496 if (vshader->reg_maps.integer_constants)
10497 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10498 if (vshader->reg_maps.boolean_constants)
10499 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10500 if (entry->vs.pos_fixup_location != -1)
10501 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10502 if (entry->vs.base_vertex_id_location != -1)
10503 entry->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
10505 shader_glsl_load_program_resources(context_gl, priv, program_id, vshader);
10507 else
10509 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10510 | WINED3D_SHADER_CONST_FFP_PROJ;
10512 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10514 if (entry->vs.modelview_matrix_location[i] != -1)
10516 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10517 break;
10521 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10523 if (entry->vs.texture_matrix_location[i] != -1)
10525 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10526 break;
10529 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10530 || entry->vs.material_specular_location != -1
10531 || entry->vs.material_emissive_location != -1
10532 || entry->vs.material_shininess_location != -1)
10533 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10534 if (entry->vs.light_ambient_location != -1)
10535 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10537 if (entry->vs.clip_planes_location != -1)
10538 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10539 if (entry->vs.pointsize_min_location != -1)
10540 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10542 if (hshader)
10543 shader_glsl_load_program_resources(context_gl, priv, program_id, hshader);
10545 if (dshader)
10547 if (entry->ds.pos_fixup_location != -1)
10548 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10550 shader_glsl_load_program_resources(context_gl, priv, program_id, dshader);
10553 if (gshader)
10555 if (entry->gs.pos_fixup_location != -1)
10556 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10558 shader_glsl_load_program_resources(context_gl, priv, program_id, gshader);
10561 if (ps_id)
10563 if (pshader)
10565 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10566 if (pshader->reg_maps.integer_constants)
10567 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10568 if (pshader->reg_maps.boolean_constants)
10569 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10570 if (entry->ps.ycorrection_location != -1)
10571 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10573 shader_glsl_load_program_resources(context_gl, priv, program_id, pshader);
10574 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10576 else
10578 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10580 shader_glsl_load_samplers(&context_gl->c, priv, program_id, NULL);
10583 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10585 if (entry->ps.bumpenv_mat_location[i] != -1)
10587 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10588 break;
10592 if (entry->ps.fog_color_location != -1)
10593 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10594 if (entry->ps.alpha_test_ref_location != -1)
10595 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10596 if (entry->ps.np2_fixup_location != -1)
10597 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10598 if (entry->ps.color_key_location != -1)
10599 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10603 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10605 struct wined3d_device *device = shader->device;
10606 struct wined3d_context *context;
10608 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10610 context = context_acquire(device, NULL, 0);
10611 shader_glsl_compile_compute_shader(shader_priv, wined3d_context_gl(context), shader);
10612 context_release(context);
10616 /* Context activation is done by the caller. */
10617 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10618 const struct wined3d_state *state)
10620 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10621 struct glsl_context_data *ctx_data = context->shader_backend_data;
10622 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10623 struct shader_glsl_priv *priv = shader_priv;
10624 struct glsl_shader_prog_link *glsl_program;
10625 GLenum current_vertex_color_clamp;
10626 GLuint program_id, prev_id;
10628 priv->vertex_pipe->vp_enable(context, !use_vs(state));
10629 priv->fragment_pipe->fp_enable(context, !use_ps(state));
10631 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10632 set_glsl_shader_program(context_gl, state, priv, ctx_data);
10633 glsl_program = ctx_data->glsl_program;
10635 if (glsl_program)
10637 program_id = glsl_program->id;
10638 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10639 if (glsl_program->shader_controlled_clip_distances)
10640 wined3d_context_gl_enable_clip_distances(context_gl, glsl_program->clip_distance_mask);
10642 else
10644 program_id = 0;
10645 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10648 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10650 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10651 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10653 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10654 checkGLcall("glClampColorARB");
10656 else
10658 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10662 TRACE("Using GLSL program %u.\n", program_id);
10664 if (prev_id != program_id)
10666 GL_EXTCALL(glUseProgram(program_id));
10667 checkGLcall("glUseProgram");
10669 if (glsl_program)
10670 context->constant_update_mask |= glsl_program->constant_update_mask;
10673 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10676 /* Context activation is done by the caller. */
10677 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10678 const struct wined3d_state *state)
10680 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10681 struct glsl_context_data *ctx_data = context->shader_backend_data;
10682 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10683 struct shader_glsl_priv *priv = shader_priv;
10684 GLuint program_id, prev_id;
10686 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10687 set_glsl_compute_shader_program(context_gl, state, priv, ctx_data);
10688 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10690 TRACE("Using GLSL program %u.\n", program_id);
10692 if (prev_id != program_id)
10694 GL_EXTCALL(glUseProgram(program_id));
10695 checkGLcall("glUseProgram");
10698 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10699 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10700 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10701 | (1u << WINED3D_SHADER_TYPE_HULL)
10702 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10705 /* "context" is not necessarily the currently active context. */
10706 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10708 struct glsl_context_data *ctx_data = context->shader_backend_data;
10710 ctx_data->glsl_program = NULL;
10711 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10712 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10713 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10714 | (1u << WINED3D_SHADER_TYPE_HULL)
10715 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10716 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10719 /* Context activation is done by the caller. */
10720 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10722 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10723 struct glsl_context_data *ctx_data = context->shader_backend_data;
10724 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10725 struct shader_glsl_priv *priv = shader_priv;
10727 shader_glsl_invalidate_current_program(context);
10728 GL_EXTCALL(glUseProgram(0));
10729 checkGLcall("glUseProgram");
10731 priv->vertex_pipe->vp_enable(context, FALSE);
10732 priv->fragment_pipe->fp_enable(context, FALSE);
10734 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10736 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10737 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10738 checkGLcall("glClampColorARB");
10742 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10743 const struct glsl_shader_prog_link *program)
10745 const struct glsl_context_data *ctx_data;
10746 struct wined3d_context *context;
10747 unsigned int i;
10749 for (i = 0; i < device->context_count; ++i)
10751 context = device->contexts[i];
10752 ctx_data = context->shader_backend_data;
10754 if (ctx_data->glsl_program == program)
10755 shader_glsl_invalidate_current_program(context);
10759 static void shader_glsl_destroy(struct wined3d_shader *shader)
10761 struct glsl_shader_private *shader_data = shader->backend_data;
10762 struct wined3d_device *device = shader->device;
10763 struct shader_glsl_priv *priv = device->shader_priv;
10764 const struct wined3d_gl_info *gl_info;
10765 const struct list *linked_programs;
10766 struct wined3d_context *context;
10768 if (!shader_data || !shader_data->num_gl_shaders)
10770 heap_free(shader_data);
10771 shader->backend_data = NULL;
10772 return;
10775 context = context_acquire(device, NULL, 0);
10776 gl_info = wined3d_context_gl(context)->gl_info;
10778 TRACE("Deleting linked programs.\n");
10779 linked_programs = &shader->linked_programs;
10780 if (linked_programs->next)
10782 struct glsl_shader_prog_link *entry, *entry2;
10783 UINT i;
10785 switch (shader->reg_maps.shader_version.type)
10787 case WINED3D_SHADER_TYPE_PIXEL:
10789 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10791 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10793 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10794 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10795 checkGLcall("glDeleteShader");
10797 heap_free(shader_data->gl_shaders.ps);
10799 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10800 struct glsl_shader_prog_link, ps.shader_entry)
10802 shader_glsl_invalidate_contexts_program(device, entry);
10803 delete_glsl_program_entry(priv, gl_info, entry);
10806 break;
10809 case WINED3D_SHADER_TYPE_VERTEX:
10811 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10813 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10815 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10816 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10817 checkGLcall("glDeleteShader");
10819 heap_free(shader_data->gl_shaders.vs);
10821 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10822 struct glsl_shader_prog_link, vs.shader_entry)
10824 shader_glsl_invalidate_contexts_program(device, entry);
10825 delete_glsl_program_entry(priv, gl_info, entry);
10828 break;
10831 case WINED3D_SHADER_TYPE_HULL:
10833 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10835 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10837 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10838 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10839 checkGLcall("glDeleteShader");
10841 heap_free(shader_data->gl_shaders.hs);
10843 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10844 struct glsl_shader_prog_link, hs.shader_entry)
10846 shader_glsl_invalidate_contexts_program(device, entry);
10847 delete_glsl_program_entry(priv, gl_info, entry);
10850 break;
10853 case WINED3D_SHADER_TYPE_DOMAIN:
10855 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10857 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10859 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10860 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10861 checkGLcall("glDeleteShader");
10863 heap_free(shader_data->gl_shaders.ds);
10865 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10866 struct glsl_shader_prog_link, ds.shader_entry)
10868 shader_glsl_invalidate_contexts_program(device, entry);
10869 delete_glsl_program_entry(priv, gl_info, entry);
10872 break;
10875 case WINED3D_SHADER_TYPE_GEOMETRY:
10877 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10879 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10881 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10882 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10883 checkGLcall("glDeleteShader");
10885 heap_free(shader_data->gl_shaders.gs);
10887 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10888 struct glsl_shader_prog_link, gs.shader_entry)
10890 shader_glsl_invalidate_contexts_program(device, entry);
10891 delete_glsl_program_entry(priv, gl_info, entry);
10894 break;
10897 case WINED3D_SHADER_TYPE_COMPUTE:
10899 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10901 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10903 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10904 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10905 checkGLcall("glDeleteShader");
10907 heap_free(shader_data->gl_shaders.cs);
10909 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10910 struct glsl_shader_prog_link, cs.shader_entry)
10912 shader_glsl_invalidate_contexts_program(device, entry);
10913 delete_glsl_program_entry(priv, gl_info, entry);
10916 break;
10919 default:
10920 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10921 break;
10925 heap_free(shader->backend_data);
10926 shader->backend_data = NULL;
10928 context_release(context);
10931 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10933 const struct glsl_program_key *k = key;
10934 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10935 const struct glsl_shader_prog_link, program_lookup_entry);
10937 if (k->vs_id > prog->vs.id) return 1;
10938 else if (k->vs_id < prog->vs.id) return -1;
10940 if (k->gs_id > prog->gs.id) return 1;
10941 else if (k->gs_id < prog->gs.id) return -1;
10943 if (k->ps_id > prog->ps.id) return 1;
10944 else if (k->ps_id < prog->ps.id) return -1;
10946 if (k->hs_id > prog->hs.id) return 1;
10947 else if (k->hs_id < prog->hs.id) return -1;
10949 if (k->ds_id > prog->ds.id) return 1;
10950 else if (k->ds_id < prog->ds.id) return -1;
10952 if (k->cs_id > prog->cs.id) return 1;
10953 else if (k->cs_id < prog->cs.id) return -1;
10955 return 0;
10958 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10960 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10961 + constant_count * sizeof(*heap->contained)
10962 + constant_count * sizeof(*heap->positions);
10963 void *mem;
10965 if (!(mem = heap_alloc(size)))
10967 ERR("Failed to allocate memory\n");
10968 return FALSE;
10971 heap->entries = mem;
10972 heap->entries[1].version = 0;
10973 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
10974 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
10975 heap->positions = (unsigned int *)(heap->contained + constant_count);
10976 heap->size = 1;
10978 return TRUE;
10981 static void constant_heap_free(struct constant_heap *heap)
10983 heap_free(heap->entries);
10986 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
10987 const struct wined3d_fragment_pipe_ops *fragment_pipe)
10989 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
10990 struct fragment_caps fragment_caps;
10991 void *vertex_priv, *fragment_priv;
10992 struct shader_glsl_priv *priv;
10994 if (!(priv = heap_alloc_zero(sizeof(*priv))))
10995 return E_OUTOFMEMORY;
10997 string_buffer_list_init(&priv->string_buffers);
10999 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11001 ERR("Failed to initialize vertex pipe.\n");
11002 heap_free(priv);
11003 return E_FAIL;
11006 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11008 ERR("Failed to initialize fragment pipe.\n");
11009 vertex_pipe->vp_free(device, NULL);
11010 heap_free(priv);
11011 return E_FAIL;
11014 if (!string_buffer_init(&priv->shader_buffer))
11016 ERR("Failed to initialize shader buffer.\n");
11017 goto fail;
11020 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11022 ERR("Failed to allocate memory.\n");
11023 goto fail;
11026 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11028 ERR("Failed to initialize vertex shader constant heap\n");
11029 goto fail;
11032 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11034 ERR("Failed to initialize pixel shader constant heap\n");
11035 goto fail;
11038 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11040 priv->next_constant_version = 1;
11041 priv->vertex_pipe = vertex_pipe;
11042 priv->fragment_pipe = fragment_pipe;
11043 fragment_pipe->get_caps(device->adapter, &fragment_caps);
11044 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11045 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11047 device->vertex_priv = vertex_priv;
11048 device->fragment_priv = fragment_priv;
11049 device->shader_priv = priv;
11051 return WINED3D_OK;
11053 fail:
11054 constant_heap_free(&priv->pconst_heap);
11055 constant_heap_free(&priv->vconst_heap);
11056 heap_free(priv->stack);
11057 string_buffer_free(&priv->shader_buffer);
11058 fragment_pipe->free_private(device, NULL);
11059 vertex_pipe->vp_free(device, NULL);
11060 heap_free(priv);
11061 return E_OUTOFMEMORY;
11064 /* Context activation is done by the caller. */
11065 static void shader_glsl_free(struct wined3d_device *device, struct wined3d_context *context)
11067 struct shader_glsl_priv *priv = device->shader_priv;
11069 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11070 constant_heap_free(&priv->pconst_heap);
11071 constant_heap_free(&priv->vconst_heap);
11072 heap_free(priv->stack);
11073 string_buffer_list_cleanup(&priv->string_buffers);
11074 string_buffer_free(&priv->shader_buffer);
11075 priv->fragment_pipe->free_private(device, context);
11076 priv->vertex_pipe->vp_free(device, context);
11078 heap_free(device->shader_priv);
11079 device->shader_priv = NULL;
11082 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11084 struct glsl_context_data *ctx_data;
11086 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11087 return FALSE;
11088 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11089 context->shader_backend_data = ctx_data;
11090 return TRUE;
11093 static void shader_glsl_free_context_data(struct wined3d_context *context)
11095 heap_free(context->shader_backend_data);
11098 static void shader_glsl_init_context_state(struct wined3d_context *context)
11100 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11101 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11103 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11104 checkGLcall("GL_PROGRAM_POINT_SIZE");
11107 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11109 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11110 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11111 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11113 if (shader_model_4
11114 && gl_info->supported[ARB_COMPUTE_SHADER]
11115 && gl_info->supported[ARB_CULL_DISTANCE]
11116 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11117 && gl_info->supported[ARB_GPU_SHADER5]
11118 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11119 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11120 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11121 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11122 && gl_info->supported[ARB_TESSELLATION_SHADER]
11123 && gl_info->supported[ARB_TEXTURE_GATHER]
11124 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11125 return 5;
11127 if (shader_model_4)
11128 return 4;
11130 /* Support for texldd and texldl instructions in pixel shaders is required
11131 * for SM3. */
11132 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11133 return 3;
11135 return 2;
11138 static void shader_glsl_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
11140 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11141 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11143 TRACE("Shader model %u.\n", shader_model);
11145 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11146 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11147 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11148 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11149 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11150 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11152 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11153 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11155 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11156 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11157 caps->varying_count = gl_info->limits.glsl_varyings;
11159 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11160 * Direct3D minimum requirement.
11162 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11163 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11165 * The problem is that the refrast clamps temporary results in the shader to
11166 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11167 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11168 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11169 * offer a way to query this.
11171 if (shader_model >= 4)
11172 caps->ps_1x_max_value = FLT_MAX;
11173 else
11174 caps->ps_1x_max_value = 1024.0f;
11176 /* Ideally we'd only set caps like sRGB writes here if supported by both
11177 * the shader backend and the fragment pipe, but we can get called before
11178 * shader_glsl_alloc(). */
11179 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11180 | WINED3D_SHADER_CAP_SRGB_WRITE;
11181 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
11182 caps->wined3d_caps |= WINED3D_SHADER_CAP_OUTPUT_INTERPOLATION;
11183 if (shader_glsl_full_ffp_varyings(gl_info))
11184 caps->wined3d_caps |= WINED3D_SHADER_CAP_FULL_FFP_VARYINGS;
11187 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11189 /* We support everything except YUV conversions. */
11190 return !is_complex_fixup(fixup);
11193 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11195 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11196 /* WINED3DSIH_ADD */ shader_glsl_binop,
11197 /* WINED3DSIH_AND */ shader_glsl_binop,
11198 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11199 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11200 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11201 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11202 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11203 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11204 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11205 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11206 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11207 /* WINED3DSIH_BEM */ shader_glsl_bem,
11208 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11209 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11210 /* WINED3DSIH_BREAK */ shader_glsl_break,
11211 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11212 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11213 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11214 /* WINED3DSIH_CALL */ shader_glsl_call,
11215 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11216 /* WINED3DSIH_CASE */ shader_glsl_case,
11217 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11218 /* WINED3DSIH_CND */ shader_glsl_cnd,
11219 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11220 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11221 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11222 /* WINED3DSIH_CRS */ shader_glsl_cross,
11223 /* WINED3DSIH_CUT */ shader_glsl_cut,
11224 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11225 /* WINED3DSIH_DCL */ shader_glsl_nop,
11226 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11227 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11228 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11229 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11230 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11231 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11232 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11233 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11234 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11235 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11236 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11237 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11238 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11239 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11240 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11241 /* WINED3DSIH_DCL_INPUT_PS_SGV */ shader_glsl_nop,
11242 /* WINED3DSIH_DCL_INPUT_PS_SIV */ shader_glsl_nop,
11243 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11244 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11245 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11246 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11247 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11248 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11249 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11250 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11251 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11252 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11253 /* WINED3DSIH_DCL_STREAM */ NULL,
11254 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11255 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11256 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11257 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11258 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11259 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11260 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11261 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11262 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11263 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11264 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11265 /* WINED3DSIH_DEF */ shader_glsl_nop,
11266 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11267 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11268 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11269 /* WINED3DSIH_DIV */ shader_glsl_binop,
11270 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11271 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11272 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11273 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11274 /* WINED3DSIH_DST */ shader_glsl_dst,
11275 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11276 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11277 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11278 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11279 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11280 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11281 /* WINED3DSIH_ELSE */ shader_glsl_else,
11282 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11283 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11284 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11285 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11286 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11287 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11288 /* WINED3DSIH_EQ */ shader_glsl_relop,
11289 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ shader_glsl_interpolate,
11290 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11291 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11292 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11293 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11294 /* WINED3DSIH_FCALL */ NULL,
11295 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11296 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11297 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11298 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11299 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11300 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11301 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11302 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11303 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11304 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11305 /* WINED3DSIH_GE */ shader_glsl_relop,
11306 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11307 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11308 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11309 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11310 /* WINED3DSIH_IADD */ shader_glsl_binop,
11311 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11312 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11313 /* WINED3DSIH_IF */ shader_glsl_if,
11314 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11315 /* WINED3DSIH_IGE */ shader_glsl_relop,
11316 /* WINED3DSIH_ILT */ shader_glsl_relop,
11317 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11318 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11319 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11320 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11321 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11322 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11323 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11324 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11325 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11326 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11327 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11328 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11329 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11330 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11331 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11332 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11333 /* WINED3DSIH_INE */ shader_glsl_relop,
11334 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11335 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11336 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11337 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11338 /* WINED3DSIH_LABEL */ shader_glsl_label,
11339 /* WINED3DSIH_LD */ shader_glsl_ld,
11340 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11341 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11342 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11343 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11344 /* WINED3DSIH_LIT */ shader_glsl_lit,
11345 /* WINED3DSIH_LOD */ NULL,
11346 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11347 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11348 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11349 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11350 /* WINED3DSIH_LT */ shader_glsl_relop,
11351 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11352 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11353 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11354 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11355 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11356 /* WINED3DSIH_MAD */ shader_glsl_mad,
11357 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11358 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11359 /* WINED3DSIH_MOV */ shader_glsl_mov,
11360 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11361 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11362 /* WINED3DSIH_MUL */ shader_glsl_binop,
11363 /* WINED3DSIH_NE */ shader_glsl_relop,
11364 /* WINED3DSIH_NOP */ shader_glsl_nop,
11365 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11366 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11367 /* WINED3DSIH_OR */ shader_glsl_binop,
11368 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11369 /* WINED3DSIH_POW */ shader_glsl_pow,
11370 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11371 /* WINED3DSIH_REP */ shader_glsl_rep,
11372 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11373 /* WINED3DSIH_RET */ shader_glsl_ret,
11374 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11375 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11376 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11377 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11378 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11379 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11380 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11381 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11382 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11383 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11384 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11385 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11386 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11387 /* WINED3DSIH_SAMPLE_POS */ NULL,
11388 /* WINED3DSIH_SETP */ NULL,
11389 /* WINED3DSIH_SGE */ shader_glsl_compare,
11390 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11391 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11392 /* WINED3DSIH_SLT */ shader_glsl_compare,
11393 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11394 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11395 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11396 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11397 /* WINED3DSIH_SUB */ shader_glsl_binop,
11398 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11399 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11400 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11401 /* WINED3DSIH_TEX */ shader_glsl_tex,
11402 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11403 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11404 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11405 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11406 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11407 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11408 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11409 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11410 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11411 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11412 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11413 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11414 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11415 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11416 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11417 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11418 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11419 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11420 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11421 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11422 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11423 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11424 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11425 /* WINED3DSIH_UGE */ shader_glsl_relop,
11426 /* WINED3DSIH_ULT */ shader_glsl_relop,
11427 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11428 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11429 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11430 /* WINED3DSIH_USHR */ shader_glsl_binop,
11431 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11432 /* WINED3DSIH_XOR */ shader_glsl_binop,
11435 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11436 SHADER_HANDLER hw_fct;
11438 /* Select handler */
11439 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11441 /* Unhandled opcode */
11442 if (!hw_fct)
11444 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11445 return;
11447 hw_fct(ins);
11449 shader_glsl_add_instruction_modifiers(ins);
11452 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11454 struct shader_glsl_priv *priv = shader_priv;
11456 return priv->ffp_proj_control;
11459 const struct wined3d_shader_backend_ops glsl_shader_backend =
11461 shader_glsl_handle_instruction,
11462 shader_glsl_precompile,
11463 shader_glsl_select,
11464 shader_glsl_select_compute,
11465 shader_glsl_disable,
11466 shader_glsl_update_float_vertex_constants,
11467 shader_glsl_update_float_pixel_constants,
11468 shader_glsl_load_constants,
11469 shader_glsl_destroy,
11470 shader_glsl_alloc,
11471 shader_glsl_free,
11472 shader_glsl_allocate_context_data,
11473 shader_glsl_free_context_data,
11474 shader_glsl_init_context_state,
11475 shader_glsl_get_caps,
11476 shader_glsl_color_fixup_supported,
11477 shader_glsl_has_ffp_proj_control,
11480 static void glsl_vertex_pipe_vp_enable(const struct wined3d_context *context, BOOL enable) {}
11482 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_adapter *adapter, struct wined3d_vertex_caps *caps)
11484 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11486 caps->xyzrhw = TRUE;
11487 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11488 caps->ffp_generic_attributes = TRUE;
11489 caps->max_active_lights = WINED3D_MAX_ACTIVE_LIGHTS;
11490 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11491 caps->max_vertex_blend_matrix_index = 0;
11492 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11493 | WINED3DVTXPCAPS_MATERIALSOURCE7
11494 | WINED3DVTXPCAPS_VERTEXFOG
11495 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11496 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11497 | WINED3DVTXPCAPS_LOCALVIEWER
11498 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11499 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11500 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11501 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11504 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11506 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11507 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11508 return 0;
11511 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11513 struct shader_glsl_priv *priv;
11515 if (shader_backend == &glsl_shader_backend)
11517 priv = shader_priv;
11518 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11519 return priv;
11522 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11524 return NULL;
11527 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *param)
11529 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11530 struct glsl_ffp_vertex_shader, desc.entry);
11531 struct glsl_shader_prog_link *program, *program2;
11532 struct glsl_ffp_destroy_ctx *ctx = param;
11533 const struct wined3d_gl_info *gl_info;
11535 gl_info = ctx->context_gl->gl_info;
11536 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11537 struct glsl_shader_prog_link, vs.shader_entry)
11539 delete_glsl_program_entry(ctx->priv, gl_info, program);
11541 GL_EXTCALL(glDeleteShader(shader->id));
11542 heap_free(shader);
11545 /* Context activation is done by the caller. */
11546 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device, struct wined3d_context *context)
11548 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11549 struct shader_glsl_priv *priv = device->vertex_priv;
11550 struct glsl_ffp_destroy_ctx ctx;
11552 ctx.priv = priv;
11553 ctx.context_gl = context_gl;
11554 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11557 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11558 const struct wined3d_state *state, DWORD state_id) {}
11560 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11561 const struct wined3d_state *state, DWORD state_id)
11563 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11566 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11567 const struct wined3d_state *state, DWORD state_id)
11569 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11570 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11571 BOOL specular = !!(context->stream_info.use_map & (1u << WINED3D_FFP_SPECULAR));
11572 BOOL diffuse = !!(context->stream_info.use_map & (1u << WINED3D_FFP_DIFFUSE));
11573 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11574 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11575 BOOL transformed = context->stream_info.position_transformed;
11576 BOOL wasrhw = context->last_was_rhw;
11577 unsigned int i;
11579 context->last_was_rhw = transformed;
11581 /* If the vertex declaration contains a transformed position attribute,
11582 * the draw uses the fixed function vertex pipeline regardless of any
11583 * vertex shader set by the application. */
11584 if (transformed != wasrhw
11585 || context->stream_info.swizzle_map != context->last_swizzle_map)
11586 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11588 context->last_swizzle_map = context->stream_info.swizzle_map;
11590 if (!use_vs(state))
11592 if (context->last_was_vshader)
11594 if (legacy_clip_planes)
11595 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11596 clipplane(context, state, STATE_CLIPPLANE(i));
11597 else
11598 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11601 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11603 /* Because of settings->texcoords, we have to regenerate the vertex
11604 * shader on a vdecl change if there aren't enough varyings to just
11605 * always output all the texture coordinates.
11607 * Likewise, we have to invalidate the shader when using per-vertex
11608 * colours and diffuse/specular attribute presence changes, or when
11609 * normal presence changes. */
11610 if (!shader_glsl_full_ffp_varyings(gl_info) || (state->render_states[WINED3D_RS_COLORVERTEX]
11611 && (diffuse != context->last_was_diffuse || specular != context->last_was_specular))
11612 || normal != context->last_was_normal)
11613 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11615 if (use_ps(state)
11616 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11617 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11618 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11620 else
11622 if (!context->last_was_vshader)
11624 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11625 if (legacy_clip_planes)
11626 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11627 clipplane(context, state, STATE_CLIPPLANE(i));
11628 else
11629 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11633 context->last_was_vshader = use_vs(state);
11634 context->last_was_diffuse = diffuse;
11635 context->last_was_specular = specular;
11636 context->last_was_normal = normal;
11639 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11640 const struct wined3d_state *state, DWORD state_id)
11642 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11643 /* Different vertex shaders potentially require a different vertex attributes setup. */
11644 if (!isStateDirty(context, STATE_VDECL))
11645 context_apply_state(context, state, STATE_VDECL);
11648 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11649 const struct wined3d_state *state, DWORD state_id)
11651 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11652 * winding) are defined in Hull Shaders, while in GLSL those are
11653 * specified in Tessellation Evaluation Shaders. */
11654 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11656 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11657 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11660 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11661 const struct wined3d_state *state, DWORD state_id)
11663 struct glsl_context_data *ctx_data = context->shader_backend_data;
11664 BOOL rasterization_disabled;
11666 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11667 if (ctx_data->rasterization_disabled != rasterization_disabled)
11668 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11669 ctx_data->rasterization_disabled = rasterization_disabled;
11671 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11672 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11673 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11674 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11675 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11678 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11679 const struct wined3d_state *state, DWORD state_id)
11681 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11682 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11683 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11684 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11685 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11686 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11687 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11690 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11691 const struct wined3d_state *state, DWORD state_id)
11693 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11696 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11697 const struct wined3d_state *state, DWORD state_id)
11699 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11702 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11704 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11705 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11706 unsigned int k;
11708 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11709 | WINED3D_SHADER_CONST_FFP_LIGHTS
11710 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11712 if (needs_legacy_glsl_syntax(gl_info))
11714 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11716 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11717 clipplane(context, state, STATE_CLIPPLANE(k));
11720 else
11722 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11726 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11727 const struct wined3d_state *state, DWORD state_id)
11729 /* Table fog behavior depends on the projection matrix. */
11730 if (state->render_states[WINED3D_RS_FOGENABLE]
11731 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11732 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11733 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11736 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11737 const struct wined3d_state *state, DWORD state_id)
11739 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11740 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11741 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11742 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11743 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11744 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11747 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11748 const struct wined3d_state *state, DWORD state_id)
11750 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11753 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11754 const struct wined3d_state *state, DWORD state_id)
11756 DWORD sampler = state_id - STATE_SAMPLER(0);
11757 const struct wined3d_texture *texture = state->textures[sampler];
11758 BOOL np2;
11760 if (!texture)
11761 return;
11763 if (sampler >= WINED3D_MAX_TEXTURES)
11764 return;
11766 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11767 || context->lastWasPow2Texture & (1u << sampler))
11769 if (np2)
11770 context->lastWasPow2Texture |= 1u << sampler;
11771 else
11772 context->lastWasPow2Texture &= ~(1u << sampler);
11774 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11778 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11779 const struct wined3d_state *state, DWORD state_id)
11781 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11784 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11785 const struct wined3d_state *state, DWORD state_id)
11787 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11790 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11791 const struct wined3d_state *state, DWORD state_id)
11793 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11796 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11797 const struct wined3d_state *state, DWORD state_id)
11799 if (!use_vs(state))
11800 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11803 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11804 const struct wined3d_state *state, DWORD state_id)
11806 static unsigned int once;
11808 if (state->primitive_type == WINED3D_PT_POINTLIST
11809 && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11810 FIXME("Non-point sprite points not supported in core profile.\n");
11813 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11814 const struct wined3d_state *state, DWORD state_id)
11816 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11819 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11820 const struct wined3d_state *state, DWORD state_id)
11822 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11823 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11824 UINT index = state_id - STATE_CLIPPLANE(0);
11826 if (index >= gl_info->limits.user_clip_distances)
11827 return;
11829 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11832 static const struct wined3d_state_entry_template glsl_vertex_pipe_vp_states[] =
11834 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11835 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11836 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11837 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11838 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11839 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11840 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11841 /* Clip planes */
11842 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11843 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11844 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11845 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11846 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11847 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11848 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11849 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11850 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11851 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11852 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11853 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11854 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11855 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11856 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11857 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11858 /* Lights */
11859 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11860 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11861 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11862 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11863 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11864 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11865 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11866 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11867 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11868 /* Viewport */
11869 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11870 /* Transform states */
11871 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11872 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11873 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11874 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11875 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11876 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11877 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11878 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11879 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11880 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11881 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11882 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11883 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11884 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11885 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11886 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11887 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11888 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11889 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11890 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11891 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11892 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11893 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11894 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11895 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11896 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11897 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11898 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11899 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11900 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11901 /* Fog */
11902 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11903 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11904 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11905 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11906 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11907 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11908 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11909 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11910 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11911 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11912 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11913 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11914 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11915 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11916 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11917 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11918 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11919 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11920 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11921 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11922 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11923 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11924 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11925 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11926 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11927 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11928 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11929 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11930 /* NP2 texture matrix fixups. They are not needed if
11931 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11932 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11933 * matrix. */
11934 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11935 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11936 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11937 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11938 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11939 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11940 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11941 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11942 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11943 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11944 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11945 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11946 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11947 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11948 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11949 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11950 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11951 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11952 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11953 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11954 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11955 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11956 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11957 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11958 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11959 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
11960 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
11961 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
11964 /* TODO:
11965 * - Implement vertex tweening. */
11966 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
11968 glsl_vertex_pipe_vp_enable,
11969 glsl_vertex_pipe_vp_get_caps,
11970 glsl_vertex_pipe_vp_get_emul_mask,
11971 glsl_vertex_pipe_vp_alloc,
11972 glsl_vertex_pipe_vp_free,
11973 glsl_vertex_pipe_vp_states,
11976 static void glsl_fragment_pipe_enable(const struct wined3d_context *context, BOOL enable)
11978 /* Nothing to do. */
11981 static void glsl_fragment_pipe_get_caps(const struct wined3d_adapter *adapter, struct fragment_caps *caps)
11983 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11985 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
11986 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
11987 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
11988 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
11989 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
11990 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
11991 | WINED3DTEXOPCAPS_SELECTARG1
11992 | WINED3DTEXOPCAPS_SELECTARG2
11993 | WINED3DTEXOPCAPS_MODULATE4X
11994 | WINED3DTEXOPCAPS_MODULATE2X
11995 | WINED3DTEXOPCAPS_MODULATE
11996 | WINED3DTEXOPCAPS_ADDSIGNED2X
11997 | WINED3DTEXOPCAPS_ADDSIGNED
11998 | WINED3DTEXOPCAPS_ADD
11999 | WINED3DTEXOPCAPS_SUBTRACT
12000 | WINED3DTEXOPCAPS_ADDSMOOTH
12001 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12002 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12003 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12004 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12005 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12006 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12007 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12008 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12009 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12010 | WINED3DTEXOPCAPS_DOTPRODUCT3
12011 | WINED3DTEXOPCAPS_MULTIPLYADD
12012 | WINED3DTEXOPCAPS_LERP
12013 | WINED3DTEXOPCAPS_BUMPENVMAP
12014 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12015 caps->MaxTextureBlendStages = WINED3D_MAX_TEXTURES;
12016 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], WINED3D_MAX_TEXTURES);
12019 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
12021 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
12022 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12023 return 0;
12026 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12028 struct shader_glsl_priv *priv;
12030 if (shader_backend == &glsl_shader_backend)
12032 priv = shader_priv;
12033 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12034 return priv;
12037 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12039 return NULL;
12042 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *param)
12044 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12045 struct glsl_ffp_fragment_shader, entry.entry);
12046 struct glsl_shader_prog_link *program, *program2;
12047 struct glsl_ffp_destroy_ctx *ctx = param;
12048 const struct wined3d_gl_info *gl_info;
12050 gl_info = ctx->context_gl->gl_info;
12051 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12052 struct glsl_shader_prog_link, ps.shader_entry)
12054 delete_glsl_program_entry(ctx->priv, gl_info, program);
12056 GL_EXTCALL(glDeleteShader(shader->id));
12057 heap_free(shader);
12060 /* Context activation is done by the caller. */
12061 static void glsl_fragment_pipe_free(struct wined3d_device *device, struct wined3d_context *context)
12063 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12064 struct shader_glsl_priv *priv = device->fragment_priv;
12065 struct glsl_ffp_destroy_ctx ctx;
12067 ctx.priv = priv;
12068 ctx.context_gl = context_gl;
12069 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12072 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12073 const struct wined3d_state *state, DWORD state_id)
12075 context->last_was_pshader = use_ps(state);
12077 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12080 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12081 const struct wined3d_state *state, DWORD state_id)
12083 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12086 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12087 const struct wined3d_state *state, DWORD state_id)
12089 BOOL use_vshader = use_vs(state);
12090 enum fogsource new_source;
12091 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12092 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12094 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12096 if (!state->render_states[WINED3D_RS_FOGENABLE])
12097 return;
12099 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12101 if (use_vshader)
12102 new_source = FOGSOURCE_VS;
12103 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12104 new_source = FOGSOURCE_COORD;
12105 else
12106 new_source = FOGSOURCE_FFP;
12108 else
12110 new_source = FOGSOURCE_FFP;
12113 if (new_source != context->fog_source || fogstart == fogend)
12115 context->fog_source = new_source;
12116 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12120 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12121 const struct wined3d_state *state, DWORD state_id)
12123 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12125 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12126 if (!shader_glsl_full_ffp_varyings(gl_info))
12127 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12129 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12130 glsl_fragment_pipe_fog(context, state, state_id);
12133 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12134 const struct wined3d_state *state, DWORD state_id)
12136 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12138 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12139 if (!shader_glsl_full_ffp_varyings(gl_info))
12140 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12143 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12144 const struct wined3d_state *state, DWORD state_id)
12146 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12149 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12150 const struct wined3d_state *state, DWORD state_id)
12152 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12155 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12156 const struct wined3d_state *state, DWORD state_id)
12158 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12159 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12160 float ref = wined3d_alpha_ref(state);
12162 if (func)
12164 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12165 checkGLcall("glAlphaFunc");
12169 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12170 const struct wined3d_state *state, DWORD state_id)
12172 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12175 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12176 const struct wined3d_state *state, DWORD state_id)
12178 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12180 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12182 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12183 checkGLcall("glEnable(GL_ALPHA_TEST)");
12185 else
12187 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12188 checkGLcall("glDisable(GL_ALPHA_TEST)");
12192 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12193 const struct wined3d_state *state, DWORD state_id)
12195 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12198 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12199 const struct wined3d_state *state, DWORD state_id)
12201 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12204 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12205 const struct wined3d_state *state, DWORD state_id)
12207 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12210 static const struct wined3d_state_entry_template glsl_fragment_pipe_state_template[] =
12212 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12213 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12214 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12215 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12216 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12217 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12218 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12219 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12220 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12221 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12222 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12223 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12224 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12225 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12226 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12227 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12228 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12229 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12230 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12231 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12232 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12233 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12234 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12235 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12236 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12237 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12238 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12239 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12240 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12241 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12242 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12243 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12244 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12245 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12246 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12247 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12248 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12249 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12250 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12251 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12252 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12253 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12254 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12255 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12256 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12257 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12258 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12259 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12260 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12261 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12262 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12263 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12264 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12265 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12266 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12267 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12268 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12269 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12270 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12271 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12272 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12273 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12274 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12275 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12276 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12277 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12278 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12279 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12280 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12281 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12282 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12283 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12284 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12285 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12286 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12287 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12288 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12289 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12290 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12291 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12292 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12293 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12294 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12295 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12296 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12297 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12298 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12299 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12300 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12301 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12302 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12303 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12304 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12305 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12306 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12307 {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 },
12308 {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 },
12309 {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 },
12310 {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 },
12311 {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 },
12312 {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 },
12313 {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 },
12314 {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 },
12315 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12316 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12317 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12318 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12319 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12320 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12321 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12322 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12323 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12324 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12325 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12326 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12327 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12330 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12332 return TRUE;
12335 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12339 const struct wined3d_fragment_pipe_ops glsl_fragment_pipe =
12341 glsl_fragment_pipe_enable,
12342 glsl_fragment_pipe_get_caps,
12343 glsl_fragment_pipe_get_emul_mask,
12344 glsl_fragment_pipe_alloc,
12345 glsl_fragment_pipe_free,
12346 glsl_fragment_pipe_alloc_context_data,
12347 glsl_fragment_pipe_free_context_data,
12348 shader_glsl_color_fixup_supported,
12349 glsl_fragment_pipe_state_template,
12352 struct glsl_blitter_args
12354 GLenum texture_type;
12355 struct color_fixup_desc fixup;
12356 unsigned short use_colour_key;
12359 struct glsl_blitter_program
12361 struct wine_rb_entry entry;
12362 struct glsl_blitter_args args;
12363 GLuint id;
12366 struct wined3d_glsl_blitter
12368 struct wined3d_blitter blitter;
12369 struct wined3d_string_buffer_list string_buffers;
12370 struct wine_rb_tree programs;
12371 GLuint palette_texture;
12374 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12376 const struct glsl_blitter_args *a = key;
12377 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12379 return memcmp(a, b, sizeof(*a));
12382 /* Context activation is done by the caller. */
12383 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12385 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12386 struct wined3d_context_gl *context_gl = ctx;
12387 const struct wined3d_gl_info *gl_info;
12389 gl_info = context_gl->gl_info;
12390 GL_EXTCALL(glDeleteProgram(program->id));
12391 checkGLcall("glDeleteProgram()");
12392 heap_free(program);
12395 /* Context activation is done by the caller. */
12396 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12398 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12399 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12400 struct wined3d_glsl_blitter *glsl_blitter;
12401 struct wined3d_blitter *next;
12403 if ((next = blitter->next))
12404 next->ops->blitter_destroy(next, context);
12406 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12408 if (glsl_blitter->palette_texture)
12409 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12411 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context_gl);
12412 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12414 heap_free(glsl_blitter);
12417 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12418 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12419 const char *output, const char *tex_type, const char *swizzle)
12421 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12422 shader_addline(buffer, "\nvoid main()\n{\n");
12423 /* The alpha-component contains the palette index. */
12424 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12425 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12426 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12427 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12428 * the middle. */
12429 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12430 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12431 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12432 if (args->use_colour_key)
12433 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12434 shader_addline(buffer, "}\n");
12437 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12438 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12439 const char *tex_type)
12441 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12442 char chroma, luminance;
12443 const char *tex;
12445 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12446 * macropixel, giving effectively 16 bits per pixel. The color consists of
12447 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12448 * luminance values, one for each single pixel it contains, and one U and
12449 * one V value shared between both pixels.
12451 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12452 * component contains the luminance and alpha the chroma. With UYVY it is
12453 * vice versa. Thus take the format into account when generating the read
12454 * swizzles
12456 * Reading the Y value is straightforward - just sample the texture. The
12457 * hardware takes care of filtering in the horizontal and vertical
12458 * direction.
12460 * Reading the U and V values is harder. We have to avoid filtering
12461 * horizontally, because that would mix the U and V values of one pixel or
12462 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12463 * get an unfiltered read, regardless of the filtering setting. Vertical
12464 * filtering works automatically though - the U and V values of two rows
12465 * are mixed nicely.
12467 * Apart of avoiding filtering issues, the code has to know which value it
12468 * just read, and where it can find the other one. To determine this, it
12469 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12470 * accordingly.
12472 * Handling horizontal filtering of U and V values requires reading a 2nd
12473 * pair of pixels, extracting U and V and mixing them. This is not
12474 * implemented yet.
12476 * An alternative implementation idea is to load the texture as A8R8G8B8
12477 * texture, with width / 2. This way one read gives all 3 values, finding
12478 * U and V is easy in an unfiltered situation. Finding the luminance on
12479 * the other hand requires finding out if it is an odd or even pixel. The
12480 * real drawback of this approach is filtering. This would have to be
12481 * emulated completely in the shader, reading up two 2 packed pixels in up
12482 * to 2 rows and interpolating both horizontally and vertically. Beyond
12483 * that it would require adjustments to the texture handling code to deal
12484 * with the width scaling. */
12486 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12488 chroma = 'x';
12489 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12491 else
12493 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12494 luminance = 'x';
12497 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12499 /* First we have to read the chroma values. This means we need at least
12500 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12501 * unmodified chroma, we have to rid ourselves of the filtering when we
12502 * sample the texture. */
12503 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12504 /* We must not allow filtering between pixel x and x+1, this would mix U
12505 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12506 * center is at 0.5, so add 0.5. */
12507 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12508 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12510 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12511 * and 0.75 for the even and odd pixels respectively. */
12512 /* Put the value into either of the chroma values. */
12513 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12514 shader_addline(buffer, " if (even)\n");
12515 shader_addline(buffer, " chroma.y = luminance;\n");
12516 shader_addline(buffer, " else\n");
12517 shader_addline(buffer, " chroma.x = luminance;\n");
12519 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12520 * current one. Otherwise, sample the left pixel. */
12521 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12522 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12524 /* Put the value into the other chroma. */
12525 shader_addline(buffer, " if (even)\n");
12526 shader_addline(buffer, " chroma.x = luminance;\n");
12527 shader_addline(buffer, " else\n");
12528 shader_addline(buffer, " chroma.y = luminance;\n");
12530 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12531 * the current one and lerp the two U and V values. */
12533 /* This gives the correctly filtered luminance value. */
12534 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12537 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12538 const struct wined3d_gl_info *gl_info, const char *tex_type)
12540 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12541 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12543 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12544 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12545 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12546 * have only half the pitch of the luminance plane, the packing into the
12547 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12548 * luminance data it looks approximately like this:
12550 * +----------------------------------+----
12551 * | |
12552 * | |
12553 * | |
12554 * | |
12555 * | | 2
12556 * | LUMINANCE | -
12557 * | | 3
12558 * | |
12559 * | |
12560 * | |
12561 * | |
12562 * +----------------+-----------------+----
12563 * | | |
12564 * | V even rows | V odd rows |
12565 * | | | 1
12566 * +----------------+------------------ -
12567 * | | | 3
12568 * | U even rows | U odd rows |
12569 * | | |
12570 * +----------------+-----------------+----
12571 * | | |
12572 * | 0.5 | 0.5 |
12574 * So it appears as if there are 4 chroma images, but in fact the odd rows
12575 * in the chroma images are in the same row as the even ones. So it is
12576 * kinda tricky to read. */
12578 /* First sample the chroma values. */
12579 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12580 /* The chroma planes have only half the width. */
12581 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12583 /* The first value is between 2/3 and 5/6 of the texture's height, so
12584 * scale+bias the coordinate. Also read the right side of the image when
12585 * reading odd lines.
12587 * Don't forget to clamp the y values in into the range, otherwise we'll
12588 * get filtering bleeding. */
12590 /* Read odd lines from the right side (add 0.5 to the x coordinate). */
12591 shader_addline(buffer, " if (fract(floor(texcoord.y * size.y) * 0.5 + 1.0 / 6.0) >= 0.5)\n");
12592 shader_addline(buffer, " texcoord.x += 0.5;\n");
12594 /* Clamp, keep the half pixel origin in mind. */
12595 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12596 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12598 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12600 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12601 * 6/6th No need to clamp because we're just reusing the already clamped
12602 * value from above. */
12603 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12604 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12606 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12607 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12608 * values from bleeding into the sampled luminance values due to
12609 * filtering. */
12610 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12611 /* Multiply the y coordinate by 2/3 and clamp it. */
12612 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12613 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12616 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12617 const struct wined3d_gl_info *gl_info, const char *tex_type)
12619 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12620 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12622 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12623 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12624 * effective bitdepth is 12 bits per pixel. If the whole texture is
12625 * interpreted as luminance data it looks approximately like this:
12627 * +----------------------------------+----
12628 * | |
12629 * | |
12630 * | |
12631 * | |
12632 * | | 2
12633 * | LUMINANCE | -
12634 * | | 3
12635 * | |
12636 * | |
12637 * | |
12638 * | |
12639 * +----------------------------------+----
12640 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12641 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12642 * | | 1
12643 * | | -
12644 * | | 3
12645 * | |
12646 * | |
12647 * +----------------------------------+---- */
12649 /* First sample the chroma values. */
12650 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12651 /* We only have half the number of chroma pixels. */
12652 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12653 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12655 /* We must not allow filtering horizontally, this would mix U and V.
12656 * Vertical filtering is ok. However, bear in mind that the pixel center
12657 * is at 0.5, so add 0.5. */
12659 /* Convert to non-normalised coordinates so we can find the individual
12660 * pixel. */
12661 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12662 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12663 * 0.5 to hit the center of the pixel. Then convert back to normalised
12664 * coordinates. */
12665 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12666 /* Clamp, keep the half pixel origin in mind. */
12667 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12669 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12670 /* Add 1.0 / size.x to sample the adjacent texel. */
12671 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12672 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12674 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12675 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12676 * values from bleeding into the sampled luminance values due to
12677 * filtering. */
12678 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12679 /* Multiply the y coordinate by 2/3 and clamp it. */
12680 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12681 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12684 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12685 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12686 const char *output, const char *tex_type, const char *swizzle)
12688 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12690 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12691 shader_addline(buffer, "float luminance;\n");
12692 shader_addline(buffer, "vec2 texcoord;\n");
12693 shader_addline(buffer, "vec2 chroma;\n");
12694 shader_addline(buffer, "uniform vec2 size;\n");
12696 shader_addline(buffer, "\nvoid main()\n{\n");
12698 switch (complex_fixup)
12700 case COMPLEX_FIXUP_UYVY:
12701 case COMPLEX_FIXUP_YUY2:
12702 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12703 break;
12705 case COMPLEX_FIXUP_YV12:
12706 gen_yv12_read(buffer, gl_info, tex_type);
12707 break;
12709 case COMPLEX_FIXUP_NV12:
12710 gen_nv12_read(buffer, gl_info, tex_type);
12711 break;
12713 case COMPLEX_FIXUP_YUV:
12714 /* With APPLE_rgb_422, things are much simpler. The only thing we
12715 * have to do here is Y'CbCr to RGB conversion. */
12716 shader_addline(buffer, " vec3 yuv = vec3(texture%s(sampler, out_texcoord.xy));\n",
12717 needs_legacy_glsl_syntax(gl_info) ? tex_type : "");
12718 shader_addline(buffer, " luminance = yuv.y;\n");
12719 shader_addline(buffer, " chroma = yuv.xz;\n");
12720 break;
12722 default:
12723 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12724 string_buffer_free(buffer);
12725 return;
12728 /* Calculate the final result. Formula is taken from
12729 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12730 * ranges from -0.5 to 0.5. */
12731 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12733 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12734 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12735 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12736 if (args->use_colour_key)
12737 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12739 shader_addline(buffer, "}\n");
12742 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12743 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12744 const char *output, const char *tex_type, const char *swizzle)
12746 shader_addline(buffer, "\nvoid main()\n{\n");
12747 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12748 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12749 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12750 if (args->use_colour_key)
12751 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12752 shader_addline(buffer, "}\n");
12755 /* Context activation is done by the caller. */
12756 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12757 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12759 static const struct
12761 GLenum texture_target;
12762 const char texture_type[7];
12763 const char texcoord_swizzle[4];
12765 texture_data[] =
12767 {GL_TEXTURE_2D, "2D", "xy"},
12768 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12769 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12771 static const char vshader_main[] =
12772 "\n"
12773 "void main()\n"
12774 "{\n"
12775 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12776 " out_texcoord = texcoord;\n"
12777 "}\n";
12778 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12779 struct wined3d_string_buffer *buffer, *output;
12780 GLuint program, vshader_id, fshader_id;
12781 const char *tex_type = NULL, *swizzle = NULL, *ptr;
12782 unsigned int i;
12783 GLint loc;
12785 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12787 if (args->texture_type == texture_data[i].texture_target)
12789 tex_type = texture_data[i].texture_type;
12790 swizzle = texture_data[i].texcoord_swizzle;
12791 break;
12794 if (i == ARRAY_SIZE(texture_data))
12796 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12797 return 0;
12800 program = GL_EXTCALL(glCreateProgram());
12802 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12804 buffer = string_buffer_get(&blitter->string_buffers);
12805 shader_glsl_add_version_declaration(buffer, gl_info);
12806 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12807 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12808 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12809 shader_addline(buffer, vshader_main);
12811 ptr = buffer->buffer;
12812 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12813 GL_EXTCALL(glAttachShader(program, vshader_id));
12814 GL_EXTCALL(glDeleteShader(vshader_id));
12816 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12818 string_buffer_clear(buffer);
12819 shader_glsl_add_version_declaration(buffer, gl_info);
12820 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12821 if (args->use_colour_key)
12823 shader_addline(buffer, "uniform struct\n{\n");
12824 shader_addline(buffer, " vec4 low, high;\n");
12825 shader_addline(buffer, "} colour_key;\n");
12827 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12828 /* TODO: Declare the out variable with the correct type (and put it in the
12829 * blitter args). */
12830 if (!use_legacy_fragment_output(gl_info))
12831 shader_addline(buffer, "out vec4 ps_out[1];\n");
12833 output = string_buffer_get(&blitter->string_buffers);
12834 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12836 switch (complex_fixup)
12838 case COMPLEX_FIXUP_P8:
12839 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12840 break;
12841 case COMPLEX_FIXUP_YUY2:
12842 case COMPLEX_FIXUP_UYVY:
12843 case COMPLEX_FIXUP_YV12:
12844 case COMPLEX_FIXUP_NV12:
12845 case COMPLEX_FIXUP_YUV:
12846 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12847 break;
12848 case COMPLEX_FIXUP_NONE:
12849 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12852 string_buffer_release(&blitter->string_buffers, output);
12854 ptr = buffer->buffer;
12855 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12856 string_buffer_release(&blitter->string_buffers, buffer);
12857 GL_EXTCALL(glAttachShader(program, fshader_id));
12858 GL_EXTCALL(glDeleteShader(fshader_id));
12860 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12861 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12863 if (!use_legacy_fragment_output(gl_info))
12864 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12866 GL_EXTCALL(glCompileShader(vshader_id));
12867 print_glsl_info_log(gl_info, vshader_id, FALSE);
12868 GL_EXTCALL(glCompileShader(fshader_id));
12869 print_glsl_info_log(gl_info, fshader_id, FALSE);
12870 GL_EXTCALL(glLinkProgram(program));
12871 shader_glsl_validate_link(gl_info, program);
12873 GL_EXTCALL(glUseProgram(program));
12874 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12875 GL_EXTCALL(glUniform1i(loc, 0));
12876 if (complex_fixup == COMPLEX_FIXUP_P8)
12878 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12879 GL_EXTCALL(glUniform1i(loc, 1));
12882 return program;
12885 /* Context activation is done by the caller. */
12886 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12887 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl)
12889 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12890 const struct wined3d_palette *palette;
12892 palette = texture_gl->t.swapchain ? texture_gl->t.swapchain->palette : NULL;
12894 if (!blitter->palette_texture)
12895 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12897 wined3d_context_gl_active_texture(context_gl, gl_info, 1);
12898 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12899 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12900 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12901 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12903 if (palette)
12905 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12906 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12908 else
12910 static const DWORD black;
12912 FIXME("P8 texture loaded without a palette.\n");
12913 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
12914 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
12917 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
12920 /* Context activation is done by the caller. */
12921 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
12922 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl, BOOL use_colour_key)
12924 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12925 struct glsl_blitter_program *program;
12926 struct glsl_blitter_args args;
12927 struct wine_rb_entry *entry;
12929 memset(&args, 0, sizeof(args));
12930 args.texture_type = texture_gl->target;
12931 args.fixup = texture_gl->t.resource.format->color_fixup;
12932 args.use_colour_key = use_colour_key;
12934 if ((entry = wine_rb_get(&blitter->programs, &args)))
12935 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12937 if (!(program = heap_alloc(sizeof(*program))))
12939 ERR("Failed to allocate blitter program memory.\n");
12940 return NULL;
12943 program->args = args;
12944 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
12946 WARN("Failed to generate blitter program.\n");
12947 heap_free(program);
12948 return NULL;
12951 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
12953 ERR("Failed to store blitter program.\n");
12954 GL_EXTCALL(glDeleteProgram(program->id));
12955 heap_free(program);
12956 return NULL;
12959 return program;
12962 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
12963 const struct wined3d_texture_gl *src_texture, DWORD src_location,
12964 const struct wined3d_texture_gl *dst_texture, DWORD dst_location)
12966 const struct wined3d_resource *src_resource = &src_texture->t.resource;
12967 const struct wined3d_resource *dst_resource = &dst_texture->t.resource;
12968 const struct wined3d_format *src_format = src_resource->format;
12969 const struct wined3d_format *dst_format = dst_resource->format;
12970 BOOL decompress;
12972 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
12974 if (dst_format->depth_size || dst_format->stencil_size)
12975 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
12976 else
12977 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
12980 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY
12981 && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
12983 TRACE("Unsupported blit_op %#x.\n", blit_op);
12984 return FALSE;
12987 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
12988 return FALSE;
12990 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
12991 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
12993 TRACE("Multi-sample source textures not supported.\n");
12994 return FALSE;
12997 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
12999 TRACE("Destination resource does not have GPU access.\n");
13000 return FALSE;
13003 /* We don't necessarily want to blit from resources without
13004 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13005 * compressed textures. */
13006 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
13007 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
13008 if (!decompress && !(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13010 TRACE("Source resource does not have GPU access.\n");
13011 return FALSE;
13014 if (!is_identity_fixup(dst_format->color_fixup)
13015 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13017 TRACE("Destination fixups are not supported.\n");
13018 return FALSE;
13021 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
13022 && !((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
13023 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
13025 TRACE("Destination texture is not FBO attachable.\n");
13026 return FALSE;
13029 TRACE("Returning supported.\n");
13030 return TRUE;
13033 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13034 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13035 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13036 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13037 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter)
13039 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
13040 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
13041 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
13042 struct wined3d_device *device = dst_texture->resource.device;
13043 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13044 struct wined3d_texture *staging_texture = NULL;
13045 struct wined3d_glsl_blitter *glsl_blitter;
13046 struct wined3d_color_key alpha_test_key;
13047 struct glsl_blitter_program *program;
13048 struct wined3d_blitter *next;
13049 unsigned int src_level;
13050 GLint location;
13051 RECT s, d;
13053 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, src_rect %s, "
13054 "dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, colour_key %p, filter %s.\n",
13055 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13056 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13057 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter));
13059 if (!glsl_blitter_supported(op, context, src_texture_gl, src_location, dst_texture_gl, dst_location))
13061 if (!(next = blitter->next))
13063 ERR("No blitter to handle blit op %#x.\n", op);
13064 return dst_location;
13067 TRACE("Forwarding to blitter %p.\n", next);
13068 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13069 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter);
13072 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13074 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13076 struct wined3d_resource_desc desc;
13077 struct wined3d_box upload_box;
13078 HRESULT hr;
13080 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13082 src_level = src_sub_resource_idx % src_texture->level_count;
13083 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13084 desc.format = src_texture->resource.format->id;
13085 desc.multisample_type = src_texture->resource.multisample_type;
13086 desc.multisample_quality = src_texture->resource.multisample_quality;
13087 desc.usage = WINED3DUSAGE_PRIVATE;
13088 desc.bind_flags = 0;
13089 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13090 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13091 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13092 desc.depth = 1;
13093 desc.size = 0;
13095 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13096 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13098 ERR("Failed to create staging texture, hr %#x.\n", hr);
13099 return dst_location;
13102 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13103 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13104 src_texture, src_sub_resource_idx, &upload_box);
13106 src_texture = staging_texture;
13107 src_texture_gl = wined3d_texture_gl(src_texture);
13108 src_sub_resource_idx = 0;
13110 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13111 && (src_texture->sub_resources[src_sub_resource_idx].locations
13112 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13113 && !wined3d_resource_is_offscreen(&src_texture->resource))
13116 /* Without FBO blits transferring from the drawable to the texture is
13117 * expensive, because we have to flip the data in sysmem. Since we can
13118 * flip in the blitter, we don't actually need that flip anyway. So we
13119 * use the surface's texture as scratch texture, and flip the source
13120 * rectangle instead. */
13121 texture2d_load_fb_texture(src_texture_gl, src_sub_resource_idx, FALSE, context);
13123 s = *src_rect;
13124 src_level = src_sub_resource_idx % src_texture->level_count;
13125 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13126 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13127 src_rect = &s;
13129 else
13131 wined3d_texture_load(src_texture, context, FALSE);
13134 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
13135 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13136 else
13137 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13139 wined3d_context_gl_apply_blit_state(context_gl, device);
13141 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13143 d = *dst_rect;
13144 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
13145 dst_rect = &d;
13148 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
13150 GLenum buffer;
13152 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13154 TRACE("Destination texture %p is onscreen.\n", dst_texture);
13155 buffer = wined3d_texture_get_gl_buffer(dst_texture);
13157 else
13159 TRACE("Destination texture %p is offscreen.\n", dst_texture);
13160 buffer = GL_COLOR_ATTACHMENT0;
13162 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
13163 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
13164 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
13165 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
13166 context_invalidate_state(context, STATE_FRAMEBUFFER);
13169 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13171 const struct wined3d_format *f = src_texture->resource.format;
13173 alpha_test_key.color_space_low_value = 0;
13174 alpha_test_key.color_space_high_value = ~(((1u << f->alpha_size) - 1) << f->alpha_offset);
13175 colour_key = &alpha_test_key;
13177 else if (op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY)
13179 colour_key = NULL;
13182 if (!(program = glsl_blitter_get_program(glsl_blitter, context_gl, src_texture_gl, !!colour_key)))
13184 ERR("Failed to get blitter program.\n");
13185 return dst_location;
13187 GL_EXTCALL(glUseProgram(program->id));
13188 switch (get_complex_fixup(program->args.fixup))
13190 case COMPLEX_FIXUP_P8:
13191 glsl_blitter_upload_palette(glsl_blitter, context_gl, src_texture_gl);
13192 break;
13194 case COMPLEX_FIXUP_YUY2:
13195 case COMPLEX_FIXUP_UYVY:
13196 case COMPLEX_FIXUP_YV12:
13197 case COMPLEX_FIXUP_NV12:
13198 case COMPLEX_FIXUP_YUV:
13199 src_level = src_sub_resource_idx % src_texture->level_count;
13200 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13201 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13202 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13203 break;
13205 default:
13206 break;
13208 if (colour_key)
13210 struct wined3d_color float_key[2];
13212 wined3d_format_get_float_color_key(src_texture->resource.format, colour_key, float_key);
13213 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.low"));
13214 GL_EXTCALL(glUniform4fv(location, 1, &float_key[0].r));
13215 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.high"));
13216 GL_EXTCALL(glUniform4fv(location, 1, &float_key[1].r));
13218 wined3d_context_gl_draw_shaded_quad(context_gl, src_texture_gl, src_sub_resource_idx, src_rect, dst_rect, filter);
13219 GL_EXTCALL(glUseProgram(0));
13221 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13222 gl_info->gl_ops.gl.p_glFlush();
13224 if (staging_texture)
13225 wined3d_texture_decref(staging_texture);
13227 return dst_location;
13230 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13231 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13232 const RECT *draw_rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
13234 struct wined3d_blitter *next;
13236 if ((next = blitter->next))
13237 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13238 clear_rects, draw_rect, flags, color, depth, stencil);
13241 static const struct wined3d_blitter_ops glsl_blitter_ops =
13243 glsl_blitter_destroy,
13244 glsl_blitter_clear,
13245 glsl_blitter_blit,
13248 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13249 const struct wined3d_device *device)
13251 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
13252 struct wined3d_glsl_blitter *blitter;
13254 if (device->shader_backend != &glsl_shader_backend)
13255 return NULL;
13257 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13258 return NULL;
13260 if (!(blitter = heap_alloc(sizeof(*blitter))))
13262 ERR("Failed to allocate blitter.\n");
13263 return NULL;
13266 TRACE("Created blitter %p.\n", blitter);
13268 blitter->blitter.ops = &glsl_blitter_ops;
13269 blitter->blitter.next = *next;
13270 string_buffer_list_init(&blitter->string_buffers);
13271 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13272 blitter->palette_texture = 0;
13273 *next = &blitter->blitter;
13275 return *next;