wined3d: Use wined3d_mask_from_size() in shader_glsl_generate_stream_output_setup().
[wine.git] / dlls / wined3d / glsl_shader.c
bloba49644a8fc5459f95d42238804a0eb614fdf3168
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"
34 #include <limits.h>
35 #include <stdio.h>
37 #include "wined3d_private.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d);
41 WINE_DECLARE_DEBUG_CHANNEL(winediag);
43 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
44 #define WINED3D_GLSL_SAMPLE_LOD 0x02
45 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
46 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
47 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
49 static const struct
51 unsigned int coord_size;
52 unsigned int resinfo_size;
53 const char *type_part;
55 resource_type_info[] =
57 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
58 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
59 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
60 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
61 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
62 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
63 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
64 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
65 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
66 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
67 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
70 static const struct
72 enum wined3d_data_type data_type;
73 const char *glsl_scalar_type;
74 const char *glsl_vector_type;
76 component_type_info[] =
78 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_UNKNOWN */
79 {WINED3D_DATA_UINT, "uint", "uvec"}, /* WINED3D_TYPE_UINT */
80 {WINED3D_DATA_INT, "int", "ivec"}, /* WINED3D_TYPE_INT */
81 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_FLOAT */
84 struct glsl_dst_param
86 char reg_name[150];
87 char mask_str[6];
90 struct glsl_src_param
92 char param_str[200];
95 struct glsl_sample_function
97 struct wined3d_string_buffer *name;
98 unsigned int coord_mask;
99 unsigned int deriv_mask;
100 enum wined3d_data_type data_type;
101 BOOL output_single_component;
102 unsigned int offset_size;
105 enum heap_node_op
107 HEAP_NODE_TRAVERSE_LEFT,
108 HEAP_NODE_TRAVERSE_RIGHT,
109 HEAP_NODE_POP,
112 struct constant_entry
114 unsigned int idx;
115 unsigned int version;
118 struct constant_heap
120 struct constant_entry *entries;
121 BOOL *contained;
122 unsigned int *positions;
123 unsigned int size;
126 /* GLSL shader private data */
127 struct shader_glsl_priv
129 struct wined3d_string_buffer shader_buffer;
130 struct wined3d_string_buffer_list string_buffers;
131 struct wine_rb_tree program_lookup;
132 struct constant_heap vconst_heap;
133 struct constant_heap pconst_heap;
134 unsigned char *stack;
135 UINT next_constant_version;
137 const struct wined3d_vertex_pipe_ops *vertex_pipe;
138 const struct wined3d_fragment_pipe_ops *fragment_pipe;
139 struct wine_rb_tree ffp_vertex_shaders;
140 struct wine_rb_tree ffp_fragment_shaders;
141 BOOL ffp_proj_control;
142 BOOL legacy_lighting;
145 struct glsl_vs_program
147 struct list shader_entry;
148 GLuint id;
149 GLenum vertex_color_clamp;
150 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
151 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
152 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
153 GLint pos_fixup_location;
154 GLint base_vertex_id_location;
156 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
157 GLint projection_matrix_location;
158 GLint normal_matrix_location;
159 GLint texture_matrix_location[WINED3D_MAX_TEXTURES];
160 GLint material_ambient_location;
161 GLint material_diffuse_location;
162 GLint material_specular_location;
163 GLint material_emissive_location;
164 GLint material_shininess_location;
165 GLint light_ambient_location;
166 struct
168 GLint diffuse;
169 GLint specular;
170 GLint ambient;
171 GLint position;
172 GLint direction;
173 GLint range;
174 GLint falloff;
175 GLint c_att;
176 GLint l_att;
177 GLint q_att;
178 GLint cos_htheta;
179 GLint cos_hphi;
180 } light_location[WINED3D_MAX_ACTIVE_LIGHTS];
181 GLint pointsize_location;
182 GLint pointsize_min_location;
183 GLint pointsize_max_location;
184 GLint pointsize_c_att_location;
185 GLint pointsize_l_att_location;
186 GLint pointsize_q_att_location;
187 GLint clip_planes_location;
190 struct glsl_hs_program
192 struct list shader_entry;
193 GLuint id;
196 struct glsl_ds_program
198 struct list shader_entry;
199 GLuint id;
201 GLint pos_fixup_location;
204 struct glsl_gs_program
206 struct list shader_entry;
207 GLuint id;
209 GLint pos_fixup_location;
212 struct glsl_ps_program
214 struct list shader_entry;
215 GLuint id;
216 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
217 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
218 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
219 GLint bumpenv_mat_location[WINED3D_MAX_TEXTURES];
220 GLint bumpenv_lum_scale_location[WINED3D_MAX_TEXTURES];
221 GLint bumpenv_lum_offset_location[WINED3D_MAX_TEXTURES];
222 GLint tss_constant_location[WINED3D_MAX_TEXTURES];
223 GLint tex_factor_location;
224 GLint specular_enable_location;
225 GLint fog_color_location;
226 GLint fog_density_location;
227 GLint fog_end_location;
228 GLint fog_scale_location;
229 GLint alpha_test_ref_location;
230 GLint ycorrection_location;
231 GLint np2_fixup_location;
232 GLint color_key_location;
233 const struct ps_np2fixup_info *np2_fixup_info;
236 struct glsl_cs_program
238 struct list shader_entry;
239 GLuint id;
242 /* Struct to maintain data about a linked GLSL program */
243 struct glsl_shader_prog_link
245 struct wine_rb_entry program_lookup_entry;
246 struct glsl_vs_program vs;
247 struct glsl_hs_program hs;
248 struct glsl_ds_program ds;
249 struct glsl_gs_program gs;
250 struct glsl_ps_program ps;
251 struct glsl_cs_program cs;
252 GLuint id;
253 DWORD constant_update_mask;
254 unsigned int constant_version;
255 DWORD shader_controlled_clip_distances : 1;
256 DWORD clip_distance_mask : 8; /* WINED3D_MAX_CLIP_DISTANCES, 8 */
257 DWORD padding : 23;
260 struct glsl_program_key
262 GLuint vs_id;
263 GLuint hs_id;
264 GLuint ds_id;
265 GLuint gs_id;
266 GLuint ps_id;
267 GLuint cs_id;
270 struct shader_glsl_ctx_priv
272 const struct wined3d_gl_info *gl_info;
273 const struct vs_compile_args *cur_vs_args;
274 const struct ds_compile_args *cur_ds_args;
275 const struct ps_compile_args *cur_ps_args;
276 struct ps_np2fixup_info *cur_np2fixup_info;
277 struct wined3d_string_buffer_list *string_buffers;
280 struct glsl_context_data
282 struct glsl_shader_prog_link *glsl_program;
283 GLenum vertex_color_clamp;
284 BOOL rasterization_disabled;
287 struct glsl_ps_compiled_shader
289 struct ps_compile_args args;
290 struct ps_np2fixup_info np2fixup;
291 GLuint id;
294 struct glsl_vs_compiled_shader
296 struct vs_compile_args args;
297 GLuint id;
300 struct glsl_hs_compiled_shader
302 GLuint id;
305 struct glsl_ds_compiled_shader
307 struct ds_compile_args args;
308 GLuint id;
311 struct glsl_gs_compiled_shader
313 struct gs_compile_args args;
314 GLuint id;
317 struct glsl_cs_compiled_shader
319 GLuint id;
322 struct glsl_shader_private
324 union
326 struct glsl_vs_compiled_shader *vs;
327 struct glsl_hs_compiled_shader *hs;
328 struct glsl_ds_compiled_shader *ds;
329 struct glsl_gs_compiled_shader *gs;
330 struct glsl_ps_compiled_shader *ps;
331 struct glsl_cs_compiled_shader *cs;
332 } gl_shaders;
333 unsigned int num_gl_shaders, shader_array_size;
336 struct glsl_ffp_vertex_shader
338 struct wined3d_ffp_vs_desc desc;
339 GLuint id;
340 struct list linked_programs;
343 struct glsl_ffp_fragment_shader
345 struct ffp_frag_desc entry;
346 GLuint id;
347 struct list linked_programs;
350 struct glsl_ffp_destroy_ctx
352 struct shader_glsl_priv *priv;
353 const struct wined3d_context_gl *context_gl;
356 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
358 static const char *debug_gl_shader_type(GLenum type)
360 switch (type)
362 #define WINED3D_TO_STR(u) case u: return #u
363 WINED3D_TO_STR(GL_VERTEX_SHADER);
364 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
365 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
366 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
367 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
368 WINED3D_TO_STR(GL_COMPUTE_SHADER);
369 #undef WINED3D_TO_STR
370 default:
371 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
375 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
377 switch (type)
379 case WINED3D_SHADER_TYPE_VERTEX:
380 return "vs";
382 case WINED3D_SHADER_TYPE_HULL:
383 return "hs";
385 case WINED3D_SHADER_TYPE_DOMAIN:
386 return "ds";
388 case WINED3D_SHADER_TYPE_GEOMETRY:
389 return "gs";
391 case WINED3D_SHADER_TYPE_PIXEL:
392 return "ps";
394 case WINED3D_SHADER_TYPE_COMPUTE:
395 return "cs";
397 default:
398 FIXME("Unhandled shader type %#x.\n", type);
399 return "unknown";
403 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
405 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
406 return 440;
407 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
408 return 150;
409 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
410 return 130;
411 else
412 return 120;
415 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
416 const struct wined3d_gl_info *gl_info)
418 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
421 static unsigned int shader_glsl_full_ffp_varyings(const struct wined3d_gl_info *gl_info)
423 /* On core profile we have to also count diffuse and specular colours and
424 * the fog coordinate. */
425 return gl_info->limits.glsl_varyings >= (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
426 ? WINED3D_MAX_TEXTURES * 4 : (WINED3D_MAX_TEXTURES + 2) * 4 + 1);
429 static void shader_glsl_append_imm_vec(struct wined3d_string_buffer *buffer,
430 const float *values, unsigned int size, const struct wined3d_gl_info *gl_info)
432 const int *int_values = (const int *)values;
433 unsigned int i;
434 char str[17];
436 if (!gl_info->supported[ARB_SHADER_BIT_ENCODING])
438 if (size > 1)
439 shader_addline(buffer, "vec%u(", size);
441 for (i = 0; i < size; ++i)
443 wined3d_ftoa(values[i], str);
444 shader_addline(buffer, i ? ", %s" : "%s", str);
447 if (size > 1)
448 shader_addline(buffer, ")");
450 return;
453 shader_addline(buffer, "intBitsToFloat(");
454 if (size > 1)
455 shader_addline(buffer, "ivec%u(", size);
457 for (i = 0; i < size; ++i)
459 wined3d_ftoa(values[i], str);
460 shader_addline(buffer, i ? ", %#x /* %s */" : "%#x /* %s */", int_values[i], str);
463 if (size > 1)
464 shader_addline(buffer, ")");
465 shader_addline(buffer, ")");
468 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
469 const int *values, unsigned int size)
471 int i;
473 if (!size || size > 4)
475 ERR("Invalid vector size %u.\n", size);
476 return;
479 if (size > 1)
480 shader_addline(buffer, "ivec%u(", size);
482 for (i = 0; i < size; ++i)
483 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
485 if (size > 1)
486 shader_addline(buffer, ")");
489 static const char *get_info_log_line(const char **ptr)
491 const char *p, *q;
493 p = *ptr;
494 if (!(q = strstr(p, "\n")))
496 if (!*p) return NULL;
497 *ptr += strlen(p);
498 return p;
500 *ptr = q + 1;
502 return p;
505 /* Context activation is done by the caller. */
506 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
508 int length = 0;
509 char *log;
511 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
512 return;
514 if (program)
515 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
516 else
517 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
519 /* A size of 1 is just a null-terminated string, so the log should be bigger than
520 * that if there are errors. */
521 if (length > 1)
523 const char *ptr, *line;
525 log = heap_alloc(length);
526 /* The info log is supposed to be zero-terminated, but at least some
527 * versions of fglrx don't terminate the string properly. The reported
528 * length does include the terminator, so explicitly set it to zero
529 * here. */
530 log[length - 1] = 0;
531 if (program)
532 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
533 else
534 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
536 ptr = log;
537 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
539 WARN("Info log received from GLSL shader #%u:\n", id);
540 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
542 else
544 FIXME("Info log received from GLSL shader #%u:\n", id);
545 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
547 heap_free(log);
551 /* Context activation is done by the caller. */
552 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
554 const char *ptr, *line;
556 TRACE("Compiling shader object %u.\n", shader);
558 if (TRACE_ON(d3d_shader))
560 ptr = src;
561 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
564 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
565 checkGLcall("glShaderSource");
566 GL_EXTCALL(glCompileShader(shader));
567 checkGLcall("glCompileShader");
568 print_glsl_info_log(gl_info, shader, FALSE);
571 /* Context activation is done by the caller. */
572 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
574 GLint i, shader_count, source_size = -1;
575 GLuint *shaders;
576 char *source = NULL;
578 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
579 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
581 ERR("Failed to allocate shader array memory.\n");
582 return;
585 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
586 for (i = 0; i < shader_count; ++i)
588 const char *ptr, *line;
589 GLint tmp;
591 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
593 if (source_size < tmp)
595 heap_free(source);
597 if (!(source = heap_alloc_zero(tmp)))
599 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
600 heap_free(shaders);
601 return;
603 source_size = tmp;
606 FIXME("Shader %u:\n", shaders[i]);
607 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
608 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
609 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
610 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
611 FIXME("\n");
613 ptr = source;
614 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
615 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
616 FIXME("\n");
619 heap_free(source);
620 heap_free(shaders);
623 /* Context activation is done by the caller. */
624 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
626 GLint tmp;
628 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
629 return;
631 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
632 if (!tmp)
634 FIXME("Program %u link status invalid.\n", program);
635 shader_glsl_dump_program_source(gl_info, program);
638 print_glsl_info_log(gl_info, program, TRUE);
641 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
643 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
644 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
645 * versions. */
646 return shader_glsl_get_version(gl_info) >= 140;
649 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
651 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
654 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
655 struct shader_glsl_priv *priv, GLuint program_id,
656 const struct wined3d_shader_reg_maps *reg_maps)
658 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
659 struct wined3d_string_buffer *name;
660 unsigned int i, base, count;
661 GLuint block_idx;
663 if (shader_glsl_use_layout_binding_qualifier(gl_info))
664 return;
666 name = string_buffer_get(&priv->string_buffers);
667 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
668 for (i = 0; i < count; ++i)
670 if (!reg_maps->cb_sizes[i])
671 continue;
673 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
674 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
675 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
677 checkGLcall("glUniformBlockBinding");
678 string_buffer_release(&priv->string_buffers, name);
681 /* Context activation is done by the caller. */
682 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
683 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
684 unsigned int base, unsigned int count, const DWORD *tex_unit_map)
686 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
687 unsigned int i, mapped_unit;
688 GLint name_loc;
690 for (i = 0; i < count; ++i)
692 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
693 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
694 if (name_loc == -1)
695 continue;
697 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
698 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
700 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
701 continue;
704 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
705 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
707 checkGLcall("Load sampler bindings");
708 string_buffer_release(&priv->string_buffers, sampler_name);
711 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
712 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
714 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
715 const unsigned int *tex_unit_map;
716 unsigned int base, count;
718 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
719 if (sampler_idx >= count)
720 return WINED3D_UNMAPPED_STAGE;
721 if (!tex_unit_map)
722 return base + sampler_idx;
723 return tex_unit_map[base + sampler_idx];
726 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
727 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
728 unsigned int sampler_idx)
730 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
731 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
732 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
733 else
734 ERR("Unmapped sampler %u.\n", sampler_idx);
737 /* Context activation is done by the caller. */
738 static void shader_glsl_load_samplers(const struct wined3d_context *context,
739 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
741 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
742 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
743 const struct wined3d_shader_version *shader_version;
744 const unsigned int *tex_unit_map;
745 unsigned int base, count;
746 const char *prefix;
748 if (shader_glsl_use_layout_binding_qualifier(gl_info))
749 return;
751 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
752 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
753 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
754 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
757 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
758 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
760 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
762 if (icb)
764 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
765 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
766 GLint icb_location;
768 string_buffer_sprintf(icb_name, "%s_icb", prefix);
769 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
770 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
771 checkGLcall("Load immediate constant buffer");
773 string_buffer_release(&priv->string_buffers, icb_name);
777 /* Context activation is done by the caller. */
778 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
779 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
781 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
782 struct wined3d_string_buffer *name;
783 GLint location;
784 unsigned int i;
786 if (shader_glsl_use_layout_binding_qualifier(gl_info))
787 return;
789 name = string_buffer_get(&priv->string_buffers);
790 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
792 if (!reg_maps->uav_resource_info[i].type)
793 continue;
795 string_buffer_sprintf(name, "%s_image%u", prefix, i);
796 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
797 if (location == -1)
798 continue;
800 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
801 GL_EXTCALL(glUniform1i(location, i));
803 checkGLcall("Load image bindings");
804 string_buffer_release(&priv->string_buffers, name);
807 /* Context activation is done by the caller. */
808 static void shader_glsl_load_program_resources(const struct wined3d_context_gl *context_gl,
809 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
811 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
813 shader_glsl_init_uniform_block_bindings(context_gl->gl_info, priv, program_id, reg_maps);
814 shader_glsl_load_icb(context_gl->gl_info, priv, program_id, reg_maps);
815 /* Texture unit mapping is set up to be the same each time the shader
816 * program is used so we can hardcode the sampler uniform values. */
817 shader_glsl_load_samplers(&context_gl->c, priv, program_id, reg_maps);
820 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
821 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
823 if (varyings && *strings)
825 char *ptr = *strings;
827 varyings[*varying_count] = ptr;
829 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
830 ptr += buffer->content_size + 1;
832 *strings = ptr;
835 *strings_length += buffer->content_size + 1;
836 ++(*varying_count);
839 static void append_transform_feedback_skip_components(const char **varyings,
840 unsigned int *varying_count, char **strings, unsigned int *strings_length,
841 struct wined3d_string_buffer *buffer, unsigned int component_count)
843 unsigned int j;
845 for (j = 0; j < component_count / 4; ++j)
847 string_buffer_sprintf(buffer, "gl_SkipComponents4");
848 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
850 if (component_count % 4)
852 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
853 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
857 static BOOL shader_glsl_generate_transform_feedback_varyings(struct wined3d_string_buffer *buffer,
858 const char **varyings, unsigned int *varying_count, char *strings, unsigned int *strings_length,
859 GLenum buffer_mode, struct wined3d_shader *shader)
861 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
862 unsigned int buffer_idx, count, length, highest_output_slot, stride;
863 unsigned int i, register_idx, component_idx;
864 BOOL have_varyings_to_record = FALSE;
866 count = length = 0;
867 highest_output_slot = 0;
868 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
870 stride = 0;
872 for (i = 0; i < so_desc->element_count; ++i)
874 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
876 highest_output_slot = max(highest_output_slot, e->output_slot);
877 if (e->output_slot != buffer_idx)
878 continue;
880 if (e->stream_idx)
882 FIXME("Unhandled stream %u.\n", e->stream_idx);
883 continue;
886 stride += e->component_count;
888 if (!e->semantic_name)
890 append_transform_feedback_skip_components(varyings, &count,
891 &strings, &length, buffer, e->component_count);
892 continue;
895 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
896 continue;
898 if (component_idx || e->component_count != 4)
900 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
902 FIXME("Unsupported component range %u-%u.\n", component_idx, e->component_count);
903 append_transform_feedback_skip_components(varyings, &count,
904 &strings, &length, buffer, e->component_count);
905 continue;
908 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
909 register_idx, component_idx, component_idx + e->component_count - 1);
910 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
912 else
914 string_buffer_sprintf(buffer, "shader_in_out.reg%u", register_idx);
915 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
918 have_varyings_to_record = TRUE;
921 if (buffer_idx < so_desc->buffer_stride_count
922 && stride < so_desc->buffer_strides[buffer_idx] / 4)
924 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
925 append_transform_feedback_skip_components(varyings, &count,
926 &strings, &length, buffer, component_count);
929 if (highest_output_slot <= buffer_idx)
930 break;
932 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
934 string_buffer_sprintf(buffer, "gl_NextBuffer");
935 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
939 if (varying_count)
940 *varying_count = count;
941 if (strings_length)
942 *strings_length = length;
944 return have_varyings_to_record;
947 static void shader_glsl_init_transform_feedback(const struct wined3d_context_gl *context_gl,
948 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
950 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
951 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
952 struct wined3d_string_buffer *buffer;
953 unsigned int i, count, length;
954 const char **varyings;
955 char *strings;
956 GLenum mode;
958 if (!so_desc)
959 return;
961 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
963 mode = GL_INTERLEAVED_ATTRIBS;
965 else
967 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
969 for (i = 0; i < so_desc->element_count; ++i)
971 if (!so_desc->elements[i].semantic_name)
973 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
974 return;
976 ++element_count[so_desc->elements[i].output_slot];
979 if (element_count[0] == so_desc->element_count)
981 mode = GL_INTERLEAVED_ATTRIBS;
983 else
985 mode = GL_SEPARATE_ATTRIBS;
986 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
988 if (element_count[i] != 1)
989 break;
991 for (; i < ARRAY_SIZE(element_count); ++i)
993 if (element_count[i])
995 FIXME("Only single element per buffer is allowed in separate mode.\n");
996 return;
1002 buffer = string_buffer_get(&priv->string_buffers);
1004 if (!shader_glsl_generate_transform_feedback_varyings(buffer, NULL, &count, NULL, &length, mode, shader))
1006 FIXME("No varyings to record, disabling transform feedback.\n");
1007 shader->u.gs.so_desc = NULL;
1008 string_buffer_release(&priv->string_buffers, buffer);
1009 return;
1012 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
1014 ERR("Out of memory.\n");
1015 string_buffer_release(&priv->string_buffers, buffer);
1016 return;
1018 if (!(strings = heap_calloc(length, sizeof(*strings))))
1020 ERR("Out of memory.\n");
1021 heap_free(varyings);
1022 string_buffer_release(&priv->string_buffers, buffer);
1023 return;
1026 shader_glsl_generate_transform_feedback_varyings(buffer, varyings, NULL, strings, NULL, mode, shader);
1027 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
1028 checkGLcall("glTransformFeedbackVaryings");
1030 heap_free(varyings);
1031 heap_free(strings);
1032 string_buffer_release(&priv->string_buffers, buffer);
1035 /* Context activation is done by the caller. */
1036 static void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
1037 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
1039 unsigned int start = ~0U, end = 0;
1040 int stack_idx = 0;
1041 unsigned int heap_idx = 1;
1042 unsigned int idx;
1044 if (heap->entries[heap_idx].version <= version) return;
1046 idx = heap->entries[heap_idx].idx;
1047 if (constant_locations[idx] != -1)
1048 start = end = idx;
1049 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1051 while (stack_idx >= 0)
1053 /* Note that we fall through to the next case statement. */
1054 switch(stack[stack_idx])
1056 case HEAP_NODE_TRAVERSE_LEFT:
1058 unsigned int left_idx = heap_idx << 1;
1059 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1061 heap_idx = left_idx;
1062 idx = heap->entries[heap_idx].idx;
1063 if (constant_locations[idx] != -1)
1065 if (start > idx)
1066 start = idx;
1067 if (end < idx)
1068 end = idx;
1071 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1072 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1073 break;
1077 case HEAP_NODE_TRAVERSE_RIGHT:
1079 unsigned int right_idx = (heap_idx << 1) + 1;
1080 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1082 heap_idx = right_idx;
1083 idx = heap->entries[heap_idx].idx;
1084 if (constant_locations[idx] != -1)
1086 if (start > idx)
1087 start = idx;
1088 if (end < idx)
1089 end = idx;
1092 stack[stack_idx++] = HEAP_NODE_POP;
1093 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1094 break;
1098 case HEAP_NODE_POP:
1099 heap_idx >>= 1;
1100 --stack_idx;
1101 break;
1104 if (start <= end)
1105 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1106 checkGLcall("walk_constant_heap()");
1109 /* Context activation is done by the caller. */
1110 static void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1111 GLint location, const struct wined3d_vec4 *data)
1113 GLfloat clamped_constant[4];
1115 if (location == -1) return;
1117 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1118 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1119 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1120 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1122 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1125 /* Context activation is done by the caller. */
1126 static void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1127 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1128 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1130 int stack_idx = 0;
1131 unsigned int heap_idx = 1;
1132 unsigned int idx;
1134 if (heap->entries[heap_idx].version <= version) return;
1136 idx = heap->entries[heap_idx].idx;
1137 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1138 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1140 while (stack_idx >= 0)
1142 /* Note that we fall through to the next case statement. */
1143 switch(stack[stack_idx])
1145 case HEAP_NODE_TRAVERSE_LEFT:
1147 unsigned int left_idx = heap_idx << 1;
1148 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1150 heap_idx = left_idx;
1151 idx = heap->entries[heap_idx].idx;
1152 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1154 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1155 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1156 break;
1160 case HEAP_NODE_TRAVERSE_RIGHT:
1162 unsigned int right_idx = (heap_idx << 1) + 1;
1163 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1165 heap_idx = right_idx;
1166 idx = heap->entries[heap_idx].idx;
1167 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1169 stack[stack_idx++] = HEAP_NODE_POP;
1170 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1171 break;
1175 case HEAP_NODE_POP:
1176 heap_idx >>= 1;
1177 --stack_idx;
1178 break;
1181 checkGLcall("walk_constant_heap_clamped()");
1184 /* Context activation is done by the caller. */
1185 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1186 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1187 unsigned char *stack, unsigned int version)
1189 const struct wined3d_shader_lconst *lconst;
1191 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1192 if (shader->reg_maps.shader_version.major == 1
1193 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1194 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1195 else
1196 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1198 if (!shader->load_local_constsF)
1200 TRACE("No need to load local float constants for this shader.\n");
1201 return;
1204 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1205 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1207 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1209 checkGLcall("glUniform4fv()");
1212 /* Context activation is done by the caller. */
1213 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1214 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
1216 unsigned int i;
1217 struct list* ptr;
1219 for (i = 0; constants_set; constants_set >>= 1, ++i)
1221 if (!(constants_set & 1)) continue;
1223 /* We found this uniform name in the program - go ahead and send the data */
1224 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1227 /* Load immediate constants */
1228 ptr = list_head(&shader->constantsI);
1229 while (ptr)
1231 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1232 unsigned int idx = lconst->idx;
1233 const GLint *values = (const GLint *)lconst->value;
1235 /* We found this uniform name in the program - go ahead and send the data */
1236 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1237 ptr = list_next(&shader->constantsI, ptr);
1239 checkGLcall("glUniform4iv()");
1242 /* Context activation is done by the caller. */
1243 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1244 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
1246 unsigned int i;
1247 struct list* ptr;
1249 for (i = 0; constants_set; constants_set >>= 1, ++i)
1251 if (!(constants_set & 1)) continue;
1253 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1256 /* Load immediate constants */
1257 ptr = list_head(&shader->constantsB);
1258 while (ptr)
1260 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1261 unsigned int idx = lconst->idx;
1262 const GLint *values = (const GLint *)lconst->value;
1264 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1265 ptr = list_next(&shader->constantsB, ptr);
1267 checkGLcall("glUniform1iv()");
1270 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1272 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1275 /* Context activation is done by the caller (state handler). */
1276 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1277 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1279 struct
1281 float sx, sy;
1283 np2fixup_constants[WINED3D_MAX_FRAGMENT_SAMPLERS];
1284 UINT fixup = ps->np2_fixup_info->active;
1285 UINT i;
1287 for (i = 0; fixup; fixup >>= 1, ++i)
1289 const struct wined3d_texture *tex = state->textures[i];
1290 unsigned char idx = ps->np2_fixup_info->idx[i];
1292 if (!tex)
1294 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1295 continue;
1298 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1299 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1302 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1305 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1307 struct wined3d_matrix temp;
1308 unsigned int i, j;
1310 for (i = 0; i < 4; ++i)
1311 for (j = 0; j < 4; ++j)
1312 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1314 *out = temp;
1317 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context_gl *context_gl,
1318 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1320 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1321 struct wined3d_matrix mv;
1322 float mat[3 * 3];
1324 if (prog->vs.normal_matrix_location == -1)
1325 return;
1327 get_modelview_matrix(&context_gl->c, state, 0, &mv);
1328 compute_normal_matrix(mat, context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING, &mv);
1330 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1331 checkGLcall("glUniformMatrix3fv");
1334 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context_gl *context_gl,
1335 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1337 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1338 struct wined3d_matrix mat;
1340 if (tex >= WINED3D_MAX_TEXTURES)
1341 return;
1342 if (prog->vs.texture_matrix_location[tex] == -1)
1343 return;
1345 get_texture_matrix(&context_gl->c, state, tex, &mat);
1346 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1347 checkGLcall("glUniformMatrix4fv");
1350 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context_gl *context_gl,
1351 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1353 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1355 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1357 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1358 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1360 else
1362 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1364 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1366 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1367 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1368 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1369 checkGLcall("setting FFP material uniforms");
1372 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context_gl *context_gl,
1373 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1375 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1376 struct wined3d_color color;
1378 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1379 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1380 checkGLcall("glUniform3fv");
1383 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context_gl *context_gl,
1384 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1385 struct glsl_shader_prog_link *prog)
1387 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1388 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1389 struct wined3d_vec4 vec4;
1391 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1392 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1393 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1395 switch (light_info->OriginalParms.type)
1397 case WINED3D_LIGHT_POINT:
1398 wined3d_vec4_transform(&vec4, &light_info->position, view);
1399 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1400 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1401 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1402 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1403 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1404 break;
1406 case WINED3D_LIGHT_SPOT:
1407 wined3d_vec4_transform(&vec4, &light_info->position, view);
1408 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1410 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1411 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1413 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1414 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1415 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1416 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1417 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1418 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1419 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1420 break;
1422 case WINED3D_LIGHT_DIRECTIONAL:
1423 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1424 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1425 break;
1427 case WINED3D_LIGHT_PARALLELPOINT:
1428 wined3d_vec4_transform(&vec4, &light_info->position, view);
1429 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1430 break;
1432 default:
1433 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1435 checkGLcall("setting FFP lights uniforms");
1438 static void shader_glsl_pointsize_uniform(const struct wined3d_context_gl *context_gl,
1439 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1441 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1442 float size, att[3];
1443 float min, max;
1445 get_pointsize_minmax(&context_gl->c, state, &min, &max);
1447 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1448 checkGLcall("glUniform1f");
1449 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1450 checkGLcall("glUniform1f");
1452 get_pointsize(&context_gl->c, state, &size, att);
1454 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1455 checkGLcall("glUniform1f");
1456 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1457 checkGLcall("glUniform1f");
1458 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1459 checkGLcall("glUniform1f");
1460 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1461 checkGLcall("glUniform1f");
1464 static void shader_glsl_load_fog_uniform(const struct wined3d_context_gl *context_gl,
1465 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1467 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1468 struct wined3d_color color;
1469 float start, end, scale;
1470 union
1472 DWORD d;
1473 float f;
1474 } tmpvalue;
1476 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1477 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1478 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1479 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1480 get_fog_start_end(&context_gl->c, state, &start, &end);
1481 scale = 1.0f / (end - start);
1482 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1483 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1484 checkGLcall("fog emulation uniforms");
1487 static void shader_glsl_clip_plane_uniform(const struct wined3d_context_gl *context_gl,
1488 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1490 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1491 struct wined3d_matrix matrix;
1492 struct wined3d_vec4 plane;
1494 plane = state->clip_planes[index];
1496 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1497 if (!use_vs(state))
1499 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1500 transpose_matrix(&matrix, &matrix);
1501 wined3d_vec4_transform(&plane, &plane, &matrix);
1504 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1507 /* Context activation is done by the caller (state handler). */
1508 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1509 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1511 struct wined3d_color float_key[2];
1512 const struct wined3d_texture *texture = state->textures[0];
1514 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1515 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1518 /* Context activation is done by the caller (state handler). */
1519 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1520 const struct wined3d_state *state)
1522 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1523 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1524 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1525 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1526 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1527 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1528 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1529 struct shader_glsl_priv *priv = shader_priv;
1530 unsigned int constant_version;
1531 DWORD update_mask;
1532 int i;
1534 /* No GLSL program set - nothing to do. */
1535 if (!prog)
1536 return;
1538 constant_version = prog->constant_version;
1539 update_mask = context->constant_update_mask & prog->constant_update_mask;
1541 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1542 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1543 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1545 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1546 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1547 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1549 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1550 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1551 vshader->reg_maps.boolean_constants);
1553 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1555 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1556 shader_glsl_clip_plane_uniform(context_gl, state, i, prog);
1559 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1560 shader_glsl_pointsize_uniform(context_gl, state, prog);
1562 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1564 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1565 max(state->viewport_count, 1) : 1;
1566 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1567 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1568 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1569 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1570 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1571 else
1572 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1573 checkGLcall("glUniform4fv");
1576 if (update_mask & WINED3D_SHADER_CONST_BASE_VERTEX_ID)
1578 GL_EXTCALL(glUniform1i(prog->vs.base_vertex_id_location, state->base_vertex_index));
1579 checkGLcall("base vertex id");
1582 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1584 struct wined3d_matrix mat;
1586 get_modelview_matrix(context, state, 0, &mat);
1587 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1588 checkGLcall("glUniformMatrix4fv");
1590 shader_glsl_ffp_vertex_normalmatrix_uniform(context_gl, state, prog);
1593 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1595 struct wined3d_matrix mat;
1597 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1599 if (prog->vs.modelview_matrix_location[i] == -1)
1600 break;
1602 get_modelview_matrix(context, state, i, &mat);
1603 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1604 checkGLcall("glUniformMatrix4fv");
1608 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1610 struct wined3d_matrix projection;
1612 get_projection_matrix(context, state, &projection);
1613 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1614 checkGLcall("glUniformMatrix4fv");
1617 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1619 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1620 shader_glsl_ffp_vertex_texmatrix_uniform(context_gl, state, i, prog);
1623 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1624 shader_glsl_ffp_vertex_material_uniform(context_gl, state, prog);
1626 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1628 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1629 DWORD point_count = 0;
1630 DWORD spot_count = 0;
1631 DWORD directional_count = 0;
1632 DWORD parallel_point_count = 0;
1634 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1636 if (!state->light_state.lights[i])
1637 continue;
1639 switch (state->light_state.lights[i]->OriginalParms.type)
1641 case WINED3D_LIGHT_POINT:
1642 ++point_count;
1643 break;
1644 case WINED3D_LIGHT_SPOT:
1645 ++spot_count;
1646 break;
1647 case WINED3D_LIGHT_DIRECTIONAL:
1648 ++directional_count;
1649 break;
1650 case WINED3D_LIGHT_PARALLELPOINT:
1651 ++parallel_point_count;
1652 break;
1653 default:
1654 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
1655 break;
1658 point_idx = 0;
1659 spot_idx = point_idx + point_count;
1660 directional_idx = spot_idx + spot_count;
1661 parallel_point_idx = directional_idx + directional_count;
1663 shader_glsl_ffp_vertex_lightambient_uniform(context_gl, state, prog);
1664 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1666 const struct wined3d_light_info *light_info = state->light_state.lights[i];
1667 unsigned int idx;
1669 if (!light_info)
1670 continue;
1672 switch (light_info->OriginalParms.type)
1674 case WINED3D_LIGHT_POINT:
1675 idx = point_idx++;
1676 break;
1677 case WINED3D_LIGHT_SPOT:
1678 idx = spot_idx++;
1679 break;
1680 case WINED3D_LIGHT_DIRECTIONAL:
1681 idx = directional_idx++;
1682 break;
1683 case WINED3D_LIGHT_PARALLELPOINT:
1684 idx = parallel_point_idx++;
1685 break;
1686 default:
1687 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1688 continue;
1690 shader_glsl_ffp_vertex_light_uniform(context_gl, state, idx, light_info, prog);
1694 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1695 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1696 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1698 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1699 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1700 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1702 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1703 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1704 pshader->reg_maps.boolean_constants);
1706 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1708 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1710 if (prog->ps.bumpenv_mat_location[i] == -1)
1711 continue;
1713 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1714 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1716 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1718 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1719 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1720 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1721 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1725 checkGLcall("bump env uniforms");
1728 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1730 const struct wined3d_vec4 correction_params =
1732 /* Position is relative to the framebuffer, not the viewport. */
1733 context->render_offscreen ? 0.0f : (float)state->fb.render_targets[0]->height,
1734 context->render_offscreen ? 1.0f : -1.0f,
1735 0.0f,
1736 0.0f,
1739 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1742 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1743 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1744 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1745 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1747 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1749 struct wined3d_color color;
1751 if (prog->ps.tex_factor_location != -1)
1753 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1754 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1757 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1758 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1759 else
1760 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1762 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1764 if (prog->ps.tss_constant_location[i] == -1)
1765 continue;
1767 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1768 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1771 checkGLcall("fixed function uniforms");
1774 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1775 shader_glsl_load_fog_uniform(context_gl, state, prog);
1777 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1779 float ref = wined3d_alpha_ref(state);
1781 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1782 checkGLcall("alpha test emulation uniform");
1785 if (priv->next_constant_version == UINT_MAX)
1787 TRACE("Max constant version reached, resetting to 0.\n");
1788 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1789 priv->next_constant_version = 1;
1791 else
1793 prog->constant_version = priv->next_constant_version++;
1797 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1799 struct constant_entry *entries = heap->entries;
1800 unsigned int *positions = heap->positions;
1801 unsigned int heap_idx, parent_idx;
1803 if (!heap->contained[idx])
1805 heap_idx = heap->size++;
1806 heap->contained[idx] = TRUE;
1808 else
1810 heap_idx = positions[idx];
1813 while (heap_idx > 1)
1815 parent_idx = heap_idx >> 1;
1817 if (new_version <= entries[parent_idx].version) break;
1819 entries[heap_idx] = entries[parent_idx];
1820 positions[entries[parent_idx].idx] = heap_idx;
1821 heap_idx = parent_idx;
1824 entries[heap_idx].version = new_version;
1825 entries[heap_idx].idx = idx;
1826 positions[idx] = heap_idx;
1829 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1831 struct shader_glsl_priv *priv = device->shader_priv;
1832 struct constant_heap *heap = &priv->vconst_heap;
1833 UINT i;
1835 for (i = start; i < count + start; ++i)
1837 update_heap_entry(heap, i, priv->next_constant_version);
1841 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1843 struct shader_glsl_priv *priv = device->shader_priv;
1844 struct constant_heap *heap = &priv->pconst_heap;
1845 UINT i;
1847 for (i = start; i < count + start; ++i)
1849 update_heap_entry(heap, i, priv->next_constant_version);
1853 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1855 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1856 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1857 if(shader_major > 3) return ret;
1859 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1860 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1861 return ret;
1864 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1866 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
1869 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1871 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
1872 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
1875 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
1877 return shader_glsl_get_version(gl_info) >= 150;
1880 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1882 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1885 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1886 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1888 va_list args;
1889 int ret;
1891 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1892 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1893 for (;;)
1895 va_start(args, format);
1896 ret = shader_vaddline(buffer, format, args);
1897 va_end(args);
1898 if (!ret)
1899 return;
1900 if (!string_buffer_resize(buffer, ret))
1901 return;
1905 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1906 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1908 va_list args;
1909 int ret;
1911 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1912 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1913 for (;;)
1915 va_start(args, format);
1916 ret = shader_vaddline(buffer, format, args);
1917 va_end(args);
1918 if (!ret)
1919 return;
1920 if (!string_buffer_resize(buffer, ret))
1921 return;
1925 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
1927 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
1930 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
1932 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
1935 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
1937 switch (mode)
1939 case WINED3DSIM_CONSTANT:
1940 return "flat ";
1941 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
1942 return "noperspective ";
1943 default:
1944 FIXME("Unhandled interpolation mode %#x.\n", mode);
1945 case WINED3DSIM_NONE:
1946 case WINED3DSIM_LINEAR:
1947 return "";
1951 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
1952 const DWORD *packed_interpolation_mode, unsigned int register_idx)
1954 return wined3d_extract_bits(packed_interpolation_mode,
1955 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
1958 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
1959 struct wined3d_string_buffer *buffer, unsigned int element_count,
1960 const DWORD *interpolation_mode, BOOL unroll)
1962 enum wined3d_shader_interpolation_mode mode;
1963 unsigned int i;
1965 if (shader_glsl_use_interface_blocks(gl_info))
1967 if (unroll)
1969 shader_addline(buffer, "in shader_in_out {\n");
1970 for (i = 0; i < element_count; ++i)
1972 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
1973 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
1975 shader_addline(buffer, "} shader_in;\n");
1977 else
1979 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
1982 else
1984 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
1988 static BOOL needs_interpolation_qualifiers_for_shader_outputs(const struct wined3d_gl_info *gl_info)
1990 /* In GLSL 4.40+ it is fine to specify interpolation qualifiers only in
1991 * fragment shaders. In older GLSL versions interpolation qualifiers must
1992 * match between shader stages. */
1993 return gl_info->glsl_version < MAKEDWORD_VERSION(4, 40);
1996 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
1997 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
1998 const DWORD *interpolation_mode)
2000 enum wined3d_shader_interpolation_mode mode;
2001 unsigned int i;
2003 if (shader_glsl_use_interface_blocks(gl_info))
2005 if (rasterizer_setup)
2007 shader_addline(buffer, "out shader_in_out {\n");
2008 for (i = 0; i < element_count; ++i)
2010 const char *interpolation_qualifiers = "";
2011 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2013 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2014 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2016 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2018 shader_addline(buffer, "} shader_out;\n");
2020 else
2022 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2025 else
2027 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2031 static BOOL use_legacy_fragment_output(const struct wined3d_gl_info *gl_info)
2033 /* Technically 1.30 does support user-defined fragment shader outputs but
2034 * we might not have glBindFragDataLocation() available (i.e. GL version
2035 * might be < 3.0). */
2036 return gl_info->glsl_version <= MAKEDWORD_VERSION(1, 30);
2039 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2041 return use_legacy_fragment_output(gl_info) ? "gl_FragData" : "ps_out";
2044 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2046 switch (primitive_type)
2048 case WINED3D_PT_POINTLIST:
2049 return "points";
2051 case WINED3D_PT_LINELIST:
2052 return "lines";
2054 case WINED3D_PT_LINESTRIP:
2055 return "line_strip";
2057 case WINED3D_PT_TRIANGLELIST:
2058 return "triangles";
2060 case WINED3D_PT_TRIANGLESTRIP:
2061 return "triangle_strip";
2063 case WINED3D_PT_LINELIST_ADJ:
2064 return "lines_adjacency";
2066 case WINED3D_PT_TRIANGLELIST_ADJ:
2067 return "triangles_adjacency";
2069 default:
2070 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2071 return "";
2075 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2077 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2078 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2079 DWORD input_reg_used = shader->u.ps.input_reg_used;
2080 unsigned int i;
2082 if (reg_maps->shader_version.major < 3)
2083 return input_reg_used & (1u << idx);
2085 for (i = 0; i < input_signature->element_count; ++i)
2087 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2089 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2090 continue;
2092 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2093 && input->semantic_idx == idx)
2094 return input_reg_used & (1u << input->register_idx);
2096 return FALSE;
2099 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2100 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2102 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2104 if (version->major >= 4)
2105 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2106 else
2107 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2110 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2111 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2112 unsigned int index)
2114 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2115 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2116 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2117 index, scalar_type, scalar_type, index);
2120 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2121 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2123 unsigned int index = e->register_idx;
2124 enum wined3d_component_type type;
2126 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2128 shader_addline(buffer, "uniform int base_vertex_id;\n");
2129 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID - base_vertex_id), 0.0, 0.0, 0.0);\n", index);
2130 return;
2132 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2134 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2135 index);
2136 return;
2138 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2139 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2141 if (shader_glsl_use_explicit_attrib_location(gl_info))
2142 shader_addline(buffer, "layout(location = %u) ", index);
2144 type = e->component_type;
2145 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2147 FIXME("Unhandled type %#x.\n", type);
2148 type = WINED3D_TYPE_FLOAT;
2150 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2151 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2152 else
2153 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2154 component_type_info[type].glsl_vector_type,
2155 component_type_info[type].glsl_scalar_type, index);
2158 /** Generate the variable & register declarations for the GLSL output target */
2159 static void shader_generate_glsl_declarations(const struct wined3d_context_gl *context_gl,
2160 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2161 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2163 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2164 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2165 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2166 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2167 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2168 unsigned int uniform_block_base, uniform_block_count;
2169 enum wined3d_shader_resource_type resource_type;
2170 const struct wined3d_shader_lconst *lconst;
2171 const char *prefix;
2172 unsigned int i;
2173 DWORD map;
2175 if (wined3d_settings.strict_shader_math)
2176 shader_addline(buffer, "#pragma optionNV(fastmath off)\n");
2178 prefix = shader_glsl_get_prefix(version->type);
2180 /* Prototype the subroutines */
2181 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
2183 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
2186 if (version->type != WINED3D_SHADER_TYPE_PIXEL && version->type != WINED3D_SHADER_TYPE_COMPUTE)
2188 if (version->type == WINED3D_SHADER_TYPE_HULL)
2189 shader_addline(buffer, "out gl_PerVertex { invariant vec4 gl_Position; } gl_out[];\n");
2190 else
2191 shader_addline(buffer, "invariant gl_Position;\n");
2194 /* Declare the constants (aka uniforms) */
2195 if (shader->limits->constant_float > 0)
2197 unsigned max_constantsF;
2199 /* Unless the shader uses indirect addressing, always declare the
2200 * maximum array size and ignore that we need some uniforms privately.
2201 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2202 * and immediate values, still declare VC[256]. If the shader needs
2203 * more uniforms than we have it won't work in any case. If it uses
2204 * less, the compiler will figure out which uniforms are really used
2205 * and strip them out. This allows a shader to use c255 on a dx9 card,
2206 * as long as it doesn't also use all the other constants.
2208 * If the shader uses indirect addressing the compiler must assume
2209 * that all declared uniforms are used. In this case, declare only the
2210 * amount that we're assured to have.
2212 * Thus we run into problems in these two cases:
2213 * 1) The shader really uses more uniforms than supported.
2214 * 2) The shader uses indirect addressing, less constants than
2215 * supported, but uses a constant index > #supported consts. */
2216 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2218 /* No indirect addressing here. */
2219 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2221 else
2223 if (reg_maps->usesrelconstF)
2225 /* Subtract the other potential uniforms from the max
2226 * available (bools, ints, and 1 row of projection matrix).
2227 * Subtract another uniform for immediate values, which have
2228 * to be loaded via uniform by the driver as well. The shader
2229 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2230 * shader code, so one vec4 should be enough. (Unfortunately
2231 * the Nvidia driver doesn't store 128 and -128 in one float).
2233 * Writing gl_ClipVertex requires one uniform for each
2234 * clipplane as well. */
2235 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2236 if (vs_args->clip_enabled)
2237 max_constantsF -= gl_info->limits.user_clip_distances;
2238 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2239 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2240 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2241 * for now take this into account when calculating the number of available constants
2243 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2244 /* Set by driver quirks in directx.c */
2245 max_constantsF -= gl_info->reserved_glsl_constants;
2247 if (max_constantsF < shader->limits->constant_float)
2249 static unsigned int once;
2251 if (!once++)
2252 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2253 " it may not render correctly.\n");
2254 else
2255 WARN("The hardware does not support enough uniform components to run this shader.\n");
2258 else
2260 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2263 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2264 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2267 /* Always declare the full set of constants, the compiler can remove the
2268 * unused ones because d3d doesn't (yet) support indirect int and bool
2269 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2270 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2271 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2273 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2274 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2276 /* Declare immediate constant buffer */
2277 if (reg_maps->icb)
2278 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2280 /* Declare constant buffers */
2281 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2282 &uniform_block_base, &uniform_block_count);
2283 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2285 if (reg_maps->cb_sizes[i])
2287 shader_addline(buffer, "layout(std140");
2288 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2289 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2290 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2291 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2295 /* Declare texture samplers */
2296 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2298 struct wined3d_shader_sampler_map_entry *entry;
2299 const char *sampler_type_prefix, *sampler_type;
2300 BOOL shadow_sampler, tex_rect;
2302 entry = &reg_maps->sampler_map.entries[i];
2304 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2306 ERR("Invalid resource index %u.\n", entry->resource_idx);
2307 continue;
2310 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2312 case WINED3D_DATA_FLOAT:
2313 case WINED3D_DATA_UNORM:
2314 case WINED3D_DATA_SNORM:
2315 sampler_type_prefix = "";
2316 break;
2318 case WINED3D_DATA_INT:
2319 sampler_type_prefix = "i";
2320 break;
2322 case WINED3D_DATA_UINT:
2323 sampler_type_prefix = "u";
2324 break;
2326 default:
2327 sampler_type_prefix = "";
2328 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2329 break;
2332 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2333 resource_type = version->type == WINED3D_SHADER_TYPE_PIXEL
2334 ? pixelshader_get_resource_type(reg_maps, entry->resource_idx, ps_args->tex_types)
2335 : reg_maps->resource_info[entry->resource_idx].type;
2337 switch (resource_type)
2339 case WINED3D_SHADER_RESOURCE_BUFFER:
2340 sampler_type = "samplerBuffer";
2341 break;
2343 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2344 if (shadow_sampler)
2345 sampler_type = "sampler1DShadow";
2346 else
2347 sampler_type = "sampler1D";
2348 break;
2350 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2351 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2352 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2353 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2354 if (shadow_sampler)
2356 if (tex_rect)
2357 sampler_type = "sampler2DRectShadow";
2358 else
2359 sampler_type = "sampler2DShadow";
2361 else
2363 if (tex_rect)
2364 sampler_type = "sampler2DRect";
2365 else
2366 sampler_type = "sampler2D";
2368 break;
2370 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2371 if (shadow_sampler)
2372 FIXME("Unsupported 3D shadow sampler.\n");
2373 sampler_type = "sampler3D";
2374 break;
2376 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2377 if (shadow_sampler)
2378 sampler_type = "samplerCubeShadow";
2379 else
2380 sampler_type = "samplerCube";
2381 break;
2383 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2384 if (shadow_sampler)
2385 sampler_type = "sampler1DArrayShadow";
2386 else
2387 sampler_type = "sampler1DArray";
2388 break;
2390 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2391 if (shadow_sampler)
2392 sampler_type = "sampler2DArrayShadow";
2393 else
2394 sampler_type = "sampler2DArray";
2395 break;
2397 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2398 if (shadow_sampler)
2399 sampler_type = "samplerCubeArrayShadow";
2400 else
2401 sampler_type = "samplerCubeArray";
2402 break;
2404 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2405 sampler_type = "sampler2DMS";
2406 break;
2408 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2409 sampler_type = "sampler2DMSArray";
2410 break;
2412 default:
2413 sampler_type = "unsupported_sampler";
2414 FIXME("Unhandled resource type %#x.\n", resource_type);
2415 break;
2418 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2419 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, version, entry->bind_idx);
2420 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2421 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2424 /* Declare images */
2425 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2427 const char *image_type_prefix, *image_type, *read_format;
2429 if (!reg_maps->uav_resource_info[i].type)
2430 continue;
2432 switch (reg_maps->uav_resource_info[i].data_type)
2434 case WINED3D_DATA_FLOAT:
2435 case WINED3D_DATA_UNORM:
2436 case WINED3D_DATA_SNORM:
2437 image_type_prefix = "";
2438 read_format = "r32f";
2439 break;
2441 case WINED3D_DATA_INT:
2442 image_type_prefix = "i";
2443 read_format = "r32i";
2444 break;
2446 case WINED3D_DATA_UINT:
2447 image_type_prefix = "u";
2448 read_format = "r32ui";
2449 break;
2451 default:
2452 image_type_prefix = "";
2453 read_format = "";
2454 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2455 break;
2458 switch (reg_maps->uav_resource_info[i].type)
2460 case WINED3D_SHADER_RESOURCE_BUFFER:
2461 image_type = "imageBuffer";
2462 break;
2464 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2465 image_type = "image1D";
2466 break;
2468 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2469 image_type = "image2D";
2470 break;
2472 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2473 image_type = "image3D";
2474 break;
2476 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2477 image_type = "image1DArray";
2478 break;
2480 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2481 image_type = "image2DArray";
2482 break;
2484 default:
2485 image_type = "unsupported_image";
2486 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2487 break;
2490 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2491 shader_addline(buffer, "layout(binding = %u)\n", i);
2492 if (reg_maps->uav_read_mask & (1u << i))
2493 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2494 read_format, image_type_prefix, image_type, prefix, i);
2495 else
2496 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2497 image_type_prefix, image_type, prefix, i);
2499 if (reg_maps->uav_counter_mask & (1u << i))
2500 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2501 i, prefix, i);
2504 /* Declare address variables */
2505 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2507 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2510 /* Declare output register temporaries */
2511 if (shader->limits->packed_output)
2512 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2514 /* Declare temporary variables */
2515 if (reg_maps->temporary_count)
2517 for (i = 0; i < reg_maps->temporary_count; ++i)
2518 shader_addline(buffer, "vec4 R%u;\n", i);
2520 else if (version->major < 4)
2522 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2524 if (map & 1)
2525 shader_addline(buffer, "vec4 R%u;\n", i);
2529 /* Declare indexable temporary variables */
2530 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2532 if (idx_temp_reg->component_count != 4)
2533 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2534 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2537 /* Declare loop registers aLx */
2538 if (version->major < 4)
2540 for (i = 0; i < reg_maps->loop_depth; ++i)
2542 shader_addline(buffer, "int aL%u;\n", i);
2543 shader_addline(buffer, "int tmpInt%u;\n", i);
2547 /* Temporary variables for matrix operations */
2548 shader_addline(buffer, "vec4 tmp0;\n");
2549 shader_addline(buffer, "vec4 tmp1;\n");
2550 if (gl_info->supported[ARB_GPU_SHADER5])
2551 shader_addline(buffer, "precise vec4 tmp_precise[2];\n");
2552 else
2553 shader_addline(buffer, "/* precise */ vec4 tmp_precise[2];\n");
2555 if (!shader->load_local_constsF)
2557 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2559 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2560 shader_glsl_append_imm_vec(buffer, (const float *)lconst->value, ARRAY_SIZE(lconst->value), gl_info);
2561 shader_addline(buffer, ";\n");
2566 /* Prototypes */
2567 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
2568 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2569 enum wined3d_data_type data_type);
2571 /** Used for opcode modifiers - They multiply the result by the specified amount */
2572 static const char * const shift_glsl_tab[] = {
2573 "", /* 0 (none) */
2574 "2.0 * ", /* 1 (x2) */
2575 "4.0 * ", /* 2 (x4) */
2576 "8.0 * ", /* 3 (x8) */
2577 "16.0 * ", /* 4 (x16) */
2578 "32.0 * ", /* 5 (x32) */
2579 "", /* 6 (x64) */
2580 "", /* 7 (x128) */
2581 "", /* 8 (d256) */
2582 "", /* 9 (d128) */
2583 "", /* 10 (d64) */
2584 "", /* 11 (d32) */
2585 "0.0625 * ", /* 12 (d16) */
2586 "0.125 * ", /* 13 (d8) */
2587 "0.25 * ", /* 14 (d4) */
2588 "0.5 * " /* 15 (d2) */
2591 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2592 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2593 const char *in_reg, const char *in_regswizzle, char *out_str)
2595 switch (src_modifier)
2597 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2598 case WINED3DSPSM_DW:
2599 case WINED3DSPSM_NONE:
2600 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2601 break;
2602 case WINED3DSPSM_NEG:
2603 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2604 break;
2605 case WINED3DSPSM_NOT:
2606 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2607 break;
2608 case WINED3DSPSM_BIAS:
2609 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2610 break;
2611 case WINED3DSPSM_BIASNEG:
2612 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2613 break;
2614 case WINED3DSPSM_SIGN:
2615 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2616 break;
2617 case WINED3DSPSM_SIGNNEG:
2618 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2619 break;
2620 case WINED3DSPSM_COMP:
2621 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2622 break;
2623 case WINED3DSPSM_X2:
2624 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2625 break;
2626 case WINED3DSPSM_X2NEG:
2627 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2628 break;
2629 case WINED3DSPSM_ABS:
2630 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2631 break;
2632 case WINED3DSPSM_ABSNEG:
2633 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2634 break;
2635 default:
2636 FIXME("Unhandled modifier %u\n", src_modifier);
2637 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2641 static void shader_glsl_fixup_scalar_register_variable(struct wined3d_string_buffer *register_name,
2642 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2644 /* The ARB_shading_language_420pack extension allows swizzle operations on
2645 * scalars. */
2646 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2647 string_buffer_sprintf(register_name, "%s", glsl_variable);
2648 else
2649 string_buffer_sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2652 /** Writes the GLSL variable name that corresponds to the register that the
2653 * DX opcode parameter is trying to access */
2654 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2655 enum wined3d_data_type data_type, struct wined3d_string_buffer *register_name,
2656 BOOL *is_swizzled, const struct wined3d_shader_context *ctx)
2658 /* oPos, oFog and oPts in D3D */
2659 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2661 const struct wined3d_shader *shader = ctx->shader;
2662 const struct wined3d_shader_reg_maps *reg_maps = ctx->reg_maps;
2663 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2664 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2665 const char *prefix = shader_glsl_get_prefix(version->type);
2666 const struct wined3d_gl_info *gl_info = priv->gl_info;
2667 struct glsl_src_param rel_param0, rel_param1;
2669 if (reg->idx[0].offset != ~0u && reg->idx[0].rel_addr)
2670 shader_glsl_add_src_param_ext(ctx, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0,
2671 &rel_param0, reg->idx[0].rel_addr->reg.data_type);
2672 if (reg->idx[1].offset != ~0u && reg->idx[1].rel_addr)
2673 shader_glsl_add_src_param_ext(ctx, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0,
2674 &rel_param1, reg->idx[1].rel_addr->reg.data_type);
2675 if (is_swizzled)
2676 *is_swizzled = FALSE;
2678 switch (reg->type)
2680 case WINED3DSPR_TEMP:
2681 string_buffer_sprintf(register_name, "R%u", reg->idx[0].offset);
2682 break;
2684 case WINED3DSPR_INPUT:
2685 case WINED3DSPR_INCONTROLPOINT:
2686 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2688 if (reg->idx[0].rel_addr)
2689 FIXME("VS3 input registers relative addressing.\n");
2690 if (is_swizzled && priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2691 *is_swizzled = TRUE;
2692 if (reg->idx[0].rel_addr)
2694 string_buffer_sprintf(register_name, "%s_in[%s + %u]",
2695 prefix, rel_param0.param_str, reg->idx[0].offset);
2697 else
2699 string_buffer_sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2701 break;
2704 if (version->type == WINED3D_SHADER_TYPE_HULL
2705 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2706 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2708 if (reg->idx[0].rel_addr)
2710 if (reg->idx[1].rel_addr)
2711 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2712 rel_param0.param_str, reg->idx[0].offset,
2713 rel_param1.param_str, reg->idx[1].offset);
2714 else
2715 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2716 rel_param0.param_str, reg->idx[0].offset,
2717 reg->idx[1].offset);
2719 else if (reg->idx[1].rel_addr)
2720 string_buffer_sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2721 rel_param1.param_str, reg->idx[1].offset);
2722 else
2723 string_buffer_sprintf(register_name, "shader_in[%u].reg[%u]",
2724 reg->idx[0].offset, reg->idx[1].offset);
2725 break;
2728 /* pixel shaders >= 3.0 */
2729 if (version->major >= 3)
2731 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2732 unsigned int in_count = vec4_varyings(version->major, gl_info);
2734 if (reg->idx[0].rel_addr)
2736 /* Removing a + 0 would be an obvious optimization, but
2737 * OS X doesn't see the NOP operation there. */
2738 if (idx)
2740 if (needs_legacy_glsl_syntax(gl_info)
2741 && shader->u.ps.declared_in_count > in_count)
2743 string_buffer_sprintf(register_name,
2744 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2745 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2746 prefix, rel_param0.param_str, idx);
2748 else
2750 string_buffer_sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2753 else
2755 if (needs_legacy_glsl_syntax(gl_info)
2756 && shader->u.ps.declared_in_count > in_count)
2758 string_buffer_sprintf(register_name,
2759 "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2760 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2761 prefix, rel_param0.param_str);
2763 else
2765 string_buffer_sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2769 else
2771 if (idx == in_count)
2772 string_buffer_sprintf(register_name, "gl_Color");
2773 else if (idx == in_count + 1)
2774 string_buffer_sprintf(register_name, "gl_SecondaryColor");
2775 else
2776 string_buffer_sprintf(register_name, "%s_in[%u]", prefix, idx);
2779 else
2781 if (!reg->idx[0].offset)
2782 string_buffer_sprintf(register_name, "ffp_varying_diffuse");
2783 else
2784 string_buffer_sprintf(register_name, "ffp_varying_specular");
2785 break;
2787 break;
2789 case WINED3DSPR_CONST:
2791 /* Relative addressing */
2792 if (reg->idx[0].rel_addr)
2794 if (wined3d_settings.check_float_constants)
2795 string_buffer_sprintf(register_name,
2796 "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2797 rel_param0.param_str, reg->idx[0].offset,
2798 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2799 prefix, rel_param0.param_str, reg->idx[0].offset);
2800 else if (reg->idx[0].offset)
2801 string_buffer_sprintf(register_name, "%s_c[%s + %u]",
2802 prefix, rel_param0.param_str, reg->idx[0].offset);
2803 else
2804 string_buffer_sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2806 else
2808 if (shader_constant_is_local(shader, reg->idx[0].offset))
2809 string_buffer_sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2810 else
2811 string_buffer_sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2814 break;
2816 case WINED3DSPR_CONSTINT:
2817 string_buffer_sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2818 break;
2820 case WINED3DSPR_CONSTBOOL:
2821 string_buffer_sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2822 break;
2824 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2825 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2826 string_buffer_sprintf(register_name, "T%u", reg->idx[0].offset);
2827 else
2828 string_buffer_sprintf(register_name, "A%u", reg->idx[0].offset);
2829 break;
2831 case WINED3DSPR_LOOP:
2832 string_buffer_sprintf(register_name, "aL%u", ctx->state->current_loop_reg - 1);
2833 break;
2835 case WINED3DSPR_SAMPLER:
2836 string_buffer_sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2837 break;
2839 case WINED3DSPR_COLOROUT:
2840 if (reg->idx[0].offset >= gl_info->limits.buffers)
2841 WARN("Write to render target %u, only %d supported.\n",
2842 reg->idx[0].offset, gl_info->limits.buffers);
2844 string_buffer_sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2845 break;
2847 case WINED3DSPR_RASTOUT:
2848 string_buffer_sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2849 break;
2851 case WINED3DSPR_DEPTHOUT:
2852 case WINED3DSPR_DEPTHOUTGE:
2853 case WINED3DSPR_DEPTHOUTLE:
2854 string_buffer_sprintf(register_name, "gl_FragDepth");
2855 break;
2857 case WINED3DSPR_ATTROUT:
2858 if (!reg->idx[0].offset)
2859 string_buffer_sprintf(register_name, "%s_out[8]", prefix);
2860 else
2861 string_buffer_sprintf(register_name, "%s_out[9]", prefix);
2862 break;
2864 case WINED3DSPR_TEXCRDOUT:
2865 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2866 if (reg->idx[0].rel_addr)
2867 string_buffer_sprintf(register_name, "%s_out[%s + %u]",
2868 prefix, rel_param0.param_str, reg->idx[0].offset);
2869 else
2870 string_buffer_sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2871 break;
2873 case WINED3DSPR_MISCTYPE:
2874 if (!reg->idx[0].offset)
2876 /* vPos */
2877 string_buffer_sprintf(register_name, "vpos");
2879 else if (reg->idx[0].offset == 1)
2881 /* Note that gl_FrontFacing is a bool, while vFace is
2882 * a float for which the sign determines front/back */
2883 string_buffer_sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2885 else
2887 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2888 string_buffer_sprintf(register_name, "unrecognized_register");
2890 break;
2892 case WINED3DSPR_IMMCONST:
2893 switch (reg->immconst_type)
2895 case WINED3D_IMMCONST_SCALAR:
2896 switch (data_type)
2898 case WINED3D_DATA_UNORM:
2899 case WINED3D_DATA_SNORM:
2900 case WINED3D_DATA_FLOAT:
2901 string_buffer_clear(register_name);
2902 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 1, gl_info);
2903 break;
2904 case WINED3D_DATA_INT:
2905 string_buffer_sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2906 break;
2907 case WINED3D_DATA_RESOURCE:
2908 case WINED3D_DATA_SAMPLER:
2909 case WINED3D_DATA_UINT:
2910 string_buffer_sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2911 break;
2912 default:
2913 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2914 break;
2916 break;
2918 case WINED3D_IMMCONST_VEC4:
2919 switch (data_type)
2921 case WINED3D_DATA_UNORM:
2922 case WINED3D_DATA_SNORM:
2923 case WINED3D_DATA_FLOAT:
2924 string_buffer_clear(register_name);
2925 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 4, gl_info);
2926 break;
2927 case WINED3D_DATA_INT:
2928 string_buffer_sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2929 reg->u.immconst_data[0], reg->u.immconst_data[1],
2930 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2931 break;
2932 case WINED3D_DATA_RESOURCE:
2933 case WINED3D_DATA_SAMPLER:
2934 case WINED3D_DATA_UINT:
2935 string_buffer_sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2936 reg->u.immconst_data[0], reg->u.immconst_data[1],
2937 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2938 break;
2939 default:
2940 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2941 break;
2943 break;
2945 default:
2946 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2947 string_buffer_sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2949 break;
2951 case WINED3DSPR_CONSTBUFFER:
2952 if (reg->idx[1].rel_addr)
2953 string_buffer_sprintf(register_name, "%s_cb%u[%s + %u]",
2954 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2955 else
2956 string_buffer_sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2957 break;
2959 case WINED3DSPR_IMMCONSTBUFFER:
2960 if (reg->idx[0].rel_addr)
2961 string_buffer_sprintf(register_name, "%s_icb[%s + %u]",
2962 prefix, rel_param0.param_str, reg->idx[0].offset);
2963 else
2964 string_buffer_sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2965 break;
2967 case WINED3DSPR_PRIMID:
2968 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2969 string_buffer_sprintf(register_name, "gl_PrimitiveIDIn");
2970 else
2971 string_buffer_sprintf(register_name, "gl_PrimitiveID");
2972 break;
2974 case WINED3DSPR_IDXTEMP:
2975 if (reg->idx[1].rel_addr)
2976 string_buffer_sprintf(register_name, "X%u[%s + %u]",
2977 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2978 else
2979 string_buffer_sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2980 break;
2982 case WINED3DSPR_LOCALTHREADINDEX:
2983 shader_glsl_fixup_scalar_register_variable(register_name,
2984 "int(gl_LocalInvocationIndex)", gl_info);
2985 break;
2987 case WINED3DSPR_GSINSTID:
2988 case WINED3DSPR_OUTPOINTID:
2989 shader_glsl_fixup_scalar_register_variable(register_name,
2990 "gl_InvocationID", gl_info);
2991 break;
2993 case WINED3DSPR_THREADID:
2994 string_buffer_sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2995 break;
2997 case WINED3DSPR_THREADGROUPID:
2998 string_buffer_sprintf(register_name, "ivec3(gl_WorkGroupID)");
2999 break;
3001 case WINED3DSPR_LOCALTHREADID:
3002 string_buffer_sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3003 break;
3005 case WINED3DSPR_FORKINSTID:
3006 case WINED3DSPR_JOININSTID:
3007 shader_glsl_fixup_scalar_register_variable(register_name,
3008 "phase_instance_id", gl_info);
3009 break;
3011 case WINED3DSPR_TESSCOORD:
3012 string_buffer_sprintf(register_name, "gl_TessCoord");
3013 break;
3015 case WINED3DSPR_OUTCONTROLPOINT:
3016 if (reg->idx[0].rel_addr)
3018 if (reg->idx[1].rel_addr)
3019 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3020 rel_param0.param_str, reg->idx[0].offset,
3021 rel_param1.param_str, reg->idx[1].offset);
3022 else
3023 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3024 rel_param0.param_str, reg->idx[0].offset,
3025 reg->idx[1].offset);
3027 else if (reg->idx[1].rel_addr)
3029 string_buffer_sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3030 reg->idx[0].offset, rel_param1.param_str,
3031 reg->idx[1].offset);
3033 else
3035 string_buffer_sprintf(register_name, "shader_out[%u].reg[%u]",
3036 reg->idx[0].offset, reg->idx[1].offset);
3038 break;
3040 case WINED3DSPR_PATCHCONST:
3041 if (version->type == WINED3D_SHADER_TYPE_HULL)
3042 string_buffer_sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3043 else
3044 string_buffer_sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3045 break;
3047 case WINED3DSPR_COVERAGE:
3048 string_buffer_sprintf(register_name, "gl_SampleMaskIn[0]");
3049 break;
3051 case WINED3DSPR_SAMPLEMASK:
3052 string_buffer_sprintf(register_name, "sample_mask");
3053 break;
3055 default:
3056 FIXME("Unhandled register type %#x.\n", reg->type);
3057 string_buffer_sprintf(register_name, "unrecognised_register");
3058 break;
3062 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3064 *str++ = '.';
3065 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3066 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3067 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3068 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3069 *str = '\0';
3072 /* Get the GLSL write mask for the destination register */
3073 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3075 DWORD mask = param->write_mask;
3077 if (shader_is_scalar(&param->reg))
3079 mask = WINED3DSP_WRITEMASK_0;
3080 *write_mask = '\0';
3082 else
3084 shader_glsl_write_mask_to_str(mask, write_mask);
3087 return mask;
3090 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3092 unsigned int size = 0;
3094 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3095 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3096 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3097 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3099 return size;
3102 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3103 unsigned int component_idx)
3105 /* swizzle bits fields: wwzzyyxx */
3106 return (swizzle >> (2 * component_idx)) & 0x3;
3109 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3111 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3112 * but addressed as "rgba". To fix this we need to swap the register's x
3113 * and z components. */
3114 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3115 unsigned int i;
3117 *str++ = '.';
3118 for (i = 0; i < 4; ++i)
3120 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3121 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3123 *str = '\0';
3126 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3127 BOOL fixup, DWORD mask, char *swizzle_str)
3129 if (shader_is_scalar(&param->reg))
3130 *swizzle_str = '\0';
3131 else
3132 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3135 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3136 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type, unsigned int size)
3138 if (dst_data_type == WINED3D_DATA_UNORM || dst_data_type == WINED3D_DATA_SNORM)
3139 dst_data_type = WINED3D_DATA_FLOAT;
3140 if (src_data_type == WINED3D_DATA_UNORM || src_data_type == WINED3D_DATA_SNORM)
3141 src_data_type = WINED3D_DATA_FLOAT;
3143 if (dst_data_type == src_data_type)
3145 string_buffer_sprintf(dst_param, "%s", src_param);
3146 return;
3149 if (src_data_type == WINED3D_DATA_FLOAT)
3151 switch (dst_data_type)
3153 case WINED3D_DATA_INT:
3154 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3155 return;
3156 case WINED3D_DATA_RESOURCE:
3157 case WINED3D_DATA_SAMPLER:
3158 case WINED3D_DATA_UINT:
3159 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3160 return;
3161 default:
3162 break;
3166 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3168 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3169 return;
3172 if (src_data_type == WINED3D_DATA_INT)
3174 switch (dst_data_type)
3176 case WINED3D_DATA_FLOAT:
3177 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3178 return;
3179 case WINED3D_DATA_UINT:
3180 if (size == 1)
3181 string_buffer_sprintf(dst_param, "uint(%s)", src_param);
3182 else
3183 string_buffer_sprintf(dst_param, "uvec%u(%s)", size, src_param);
3184 return;
3185 default:
3186 break;
3190 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3191 string_buffer_sprintf(dst_param, "%s", src_param);
3194 /* From a given parameter token, generate the corresponding GLSL string.
3195 * Also, return the actual register name and swizzle in case the
3196 * caller needs this information as well. */
3197 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
3198 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3199 enum wined3d_data_type data_type)
3201 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3202 struct wined3d_string_buffer *param_str = string_buffer_get(priv->string_buffers);
3203 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3204 enum wined3d_data_type param_data_type;
3205 BOOL is_color = FALSE;
3206 char swizzle_str[6];
3207 unsigned int size;
3209 glsl_src->param_str[0] = '\0';
3210 swizzle_str[0] = '\0';
3212 shader_glsl_get_register_name(&wined3d_src->reg, data_type, reg_name, &is_color, ctx);
3213 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3215 switch (wined3d_src->reg.type)
3217 case WINED3DSPR_IMMCONST:
3218 param_data_type = data_type;
3219 size = wined3d_src->reg.immconst_type == WINED3D_IMMCONST_SCALAR ? 1 : 4;
3220 break;
3221 case WINED3DSPR_FORKINSTID:
3222 case WINED3DSPR_GSINSTID:
3223 case WINED3DSPR_JOININSTID:
3224 case WINED3DSPR_LOCALTHREADINDEX:
3225 case WINED3DSPR_OUTPOINTID:
3226 case WINED3DSPR_PRIMID:
3227 param_data_type = WINED3D_DATA_INT;
3228 size = 1;
3229 break;
3230 case WINED3DSPR_LOCALTHREADID:
3231 case WINED3DSPR_THREADGROUPID:
3232 case WINED3DSPR_THREADID:
3233 param_data_type = WINED3D_DATA_INT;
3234 size = 3;
3235 break;
3236 default:
3237 param_data_type = WINED3D_DATA_FLOAT;
3238 size = 4;
3239 break;
3242 shader_glsl_sprintf_cast(param_str, reg_name->buffer, data_type, param_data_type, size);
3243 shader_glsl_gen_modifier(wined3d_src->modifiers, param_str->buffer, swizzle_str, glsl_src->param_str);
3245 string_buffer_release(priv->string_buffers, reg_name);
3246 string_buffer_release(priv->string_buffers, param_str);
3249 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3250 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3252 shader_glsl_add_src_param_ext(ins->ctx, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3255 /* From a given parameter token, generate the corresponding GLSL string.
3256 * Also, return the actual register name and swizzle in case the
3257 * caller needs this information as well. */
3258 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3259 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3261 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3262 struct wined3d_string_buffer *reg_name;
3263 size_t len;
3265 glsl_dst->mask_str[0] = '\0';
3267 reg_name = string_buffer_get(priv->string_buffers);
3268 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type, reg_name, NULL, ins->ctx);
3269 len = min(reg_name->content_size, ARRAY_SIZE(glsl_dst->reg_name) - 1);
3270 memcpy(glsl_dst->reg_name, reg_name->buffer, len);
3271 glsl_dst->reg_name[len] = '\0';
3272 string_buffer_release(priv->string_buffers, reg_name);
3274 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3277 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3278 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3279 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3280 unsigned int dst_idx, enum wined3d_data_type data_type)
3282 struct glsl_dst_param glsl_dst;
3283 DWORD mask;
3285 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3287 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3288 sprintf(glsl_dst.reg_name, "tmp_precise[%u]", dst_idx);
3290 switch (data_type)
3292 case WINED3D_DATA_FLOAT:
3293 case WINED3D_DATA_UNORM:
3294 case WINED3D_DATA_SNORM:
3295 shader_addline(buffer, "%s%s = %s(",
3296 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3297 break;
3298 case WINED3D_DATA_INT:
3299 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3300 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3301 break;
3302 case WINED3D_DATA_RESOURCE:
3303 case WINED3D_DATA_SAMPLER:
3304 case WINED3D_DATA_UINT:
3305 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3306 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3307 break;
3308 default:
3309 FIXME("Unhandled data type %#x.\n", data_type);
3310 shader_addline(buffer, "%s%s = %s(",
3311 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3312 break;
3316 return mask;
3319 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3320 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3322 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3325 /** Process GLSL instruction modifiers */
3326 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3328 const struct wined3d_shader_dst_param *dst;
3329 struct glsl_dst_param dst_param;
3330 DWORD modifiers;
3331 unsigned int i;
3333 for (i = 0; i < ins->dst_count; ++i)
3335 if ((dst = &ins->dst[i])->reg.type == WINED3DSPR_NULL)
3336 continue;
3338 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3340 shader_glsl_add_dst_param(ins, dst, &dst_param);
3341 shader_addline(ins->ctx->buffer, "%s%s = tmp_precise[%u]%s;\n",
3342 dst_param.reg_name, dst_param.mask_str, i, dst_param.mask_str);
3345 if (!(modifiers = dst->modifiers))
3346 continue;
3348 shader_glsl_add_dst_param(ins, dst, &dst_param);
3350 if (modifiers & WINED3DSPDM_SATURATE)
3352 /* _SAT means to clamp the value of the register to between 0 and 1 */
3353 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3354 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3357 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3359 FIXME("_centroid modifier not handled\n");
3362 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3364 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3369 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3371 switch (op)
3373 case WINED3D_SHADER_REL_OP_GT: return ">";
3374 case WINED3D_SHADER_REL_OP_EQ: return "==";
3375 case WINED3D_SHADER_REL_OP_GE: return ">=";
3376 case WINED3D_SHADER_REL_OP_LT: return "<";
3377 case WINED3D_SHADER_REL_OP_NE: return "!=";
3378 case WINED3D_SHADER_REL_OP_LE: return "<=";
3379 default:
3380 FIXME("Unrecognized operator %#x.\n", op);
3381 return "(\?\?)";
3385 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3387 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3390 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3391 unsigned int *coord_size, unsigned int *deriv_size)
3393 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3394 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3396 *coord_size = resource_type_info[resource_type].coord_size;
3397 *deriv_size = *coord_size;
3398 if (is_array)
3399 --(*deriv_size);
3402 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3403 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3405 enum wined3d_shader_resource_type resource_type;
3406 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3407 const struct wined3d_gl_info *gl_info = priv->gl_info;
3408 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3409 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3410 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3411 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3412 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3413 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3414 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3415 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3416 const char *base = "texture", *type_part = "", *suffix = "";
3417 unsigned int coord_size, deriv_size;
3419 resource_type = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3420 ? pixelshader_get_resource_type(ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
3421 : ctx->reg_maps->resource_info[resource_idx].type;
3423 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3425 if (resource_type >= ARRAY_SIZE(resource_type_info))
3427 ERR("Unexpected resource type %#x.\n", resource_type);
3428 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3431 /* Note that there's no such thing as a projected cube texture. */
3432 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3433 projected = FALSE;
3435 if (needs_legacy_glsl_syntax(gl_info))
3437 if (shadow)
3438 base = "shadow";
3440 type_part = resource_type_info[resource_type].type_part;
3441 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3442 type_part = "2DRect";
3443 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3444 FIXME("Unhandled resource type %#x.\n", resource_type);
3446 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3448 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3449 suffix = "ARB";
3450 else
3451 FIXME("Unsupported grad function.\n");
3455 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3457 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3458 if (flags & ~texel_fetch_flags)
3459 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3461 base = "texelFetch";
3462 type_part = "";
3465 sample_function->name = string_buffer_get(priv->string_buffers);
3466 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3467 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3469 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3470 if (shadow)
3471 ++coord_size;
3472 sample_function->offset_size = offset ? deriv_size : 0;
3473 sample_function->coord_mask = wined3d_mask_from_size(coord_size);
3474 sample_function->deriv_mask = wined3d_mask_from_size(deriv_size);
3475 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3478 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3479 struct glsl_sample_function *sample_function)
3481 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3483 string_buffer_release(priv->string_buffers, sample_function->name);
3486 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3487 BOOL sign_fixup, enum fixup_channel_source channel_source)
3489 switch(channel_source)
3491 case CHANNEL_SOURCE_ZERO:
3492 strcat(arguments, "0.0");
3493 break;
3495 case CHANNEL_SOURCE_ONE:
3496 strcat(arguments, "1.0");
3497 break;
3499 case CHANNEL_SOURCE_X:
3500 strcat(arguments, reg_name);
3501 strcat(arguments, ".x");
3502 break;
3504 case CHANNEL_SOURCE_Y:
3505 strcat(arguments, reg_name);
3506 strcat(arguments, ".y");
3507 break;
3509 case CHANNEL_SOURCE_Z:
3510 strcat(arguments, reg_name);
3511 strcat(arguments, ".z");
3512 break;
3514 case CHANNEL_SOURCE_W:
3515 strcat(arguments, reg_name);
3516 strcat(arguments, ".w");
3517 break;
3519 default:
3520 FIXME("Unhandled channel source %#x\n", channel_source);
3521 strcat(arguments, "undefined");
3522 break;
3525 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3528 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3529 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3531 unsigned int mask_size, remaining;
3532 DWORD fixup_mask = 0;
3533 char arguments[256];
3534 char mask_str[6];
3536 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3537 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3538 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3539 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3540 if (!(mask &= fixup_mask))
3541 return;
3543 if (is_complex_fixup(fixup))
3545 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3546 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3547 return;
3550 shader_glsl_write_mask_to_str(mask, mask_str);
3551 mask_size = shader_glsl_get_write_mask_size(mask);
3553 arguments[0] = '\0';
3554 remaining = mask_size;
3555 if (mask & WINED3DSP_WRITEMASK_0)
3557 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3558 if (--remaining) strcat(arguments, ", ");
3560 if (mask & WINED3DSP_WRITEMASK_1)
3562 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3563 if (--remaining) strcat(arguments, ", ");
3565 if (mask & WINED3DSP_WRITEMASK_2)
3567 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3568 if (--remaining) strcat(arguments, ", ");
3570 if (mask & WINED3DSP_WRITEMASK_3)
3572 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3573 if (--remaining) strcat(arguments, ", ");
3576 if (mask_size > 1)
3577 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3578 else
3579 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3582 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3584 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3585 struct wined3d_string_buffer *reg_name;
3587 reg_name = string_buffer_get(priv->string_buffers);
3588 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, NULL, ins->ctx);
3589 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name->buffer, ins->dst[0].write_mask, fixup);
3590 string_buffer_release(priv->string_buffers, reg_name);
3593 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3594 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3595 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3596 const char *coord_reg_fmt, ...)
3598 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3599 char dst_swizzle[6];
3600 struct color_fixup_desc fixup;
3601 BOOL np2_fixup = FALSE;
3602 va_list args;
3603 int ret;
3605 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3607 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3608 * We actually rely on it for vertex shaders and SM4+. */
3609 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3611 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3612 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3614 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3615 np2_fixup = TRUE;
3617 else
3619 fixup = COLOR_FIXUP_IDENTITY;
3622 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, sample_function->data_type);
3624 if (sample_function->output_single_component)
3625 shader_addline(ins->ctx->buffer, "vec4(");
3627 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3628 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3630 for (;;)
3632 va_start(args, coord_reg_fmt);
3633 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3634 va_end(args);
3635 if (!ret)
3636 break;
3637 if (!string_buffer_resize(ins->ctx->buffer, ret))
3638 break;
3641 if (np2_fixup)
3643 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3644 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3646 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3648 case 1:
3649 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3650 idx >> 1, (idx % 2) ? "z" : "x");
3651 break;
3652 case 2:
3653 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3654 idx >> 1, (idx % 2) ? "zw" : "xy");
3655 break;
3656 case 3:
3657 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3658 idx >> 1, (idx % 2) ? "zw" : "xy");
3659 break;
3660 case 4:
3661 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3662 idx >> 1, (idx % 2) ? "zw" : "xy");
3663 break;
3666 if (dx && dy)
3667 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3668 else if (bias)
3669 shader_addline(ins->ctx->buffer, ", %s", bias);
3670 if (sample_function->offset_size)
3672 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3673 shader_addline(ins->ctx->buffer, ", ");
3674 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3676 shader_addline(ins->ctx->buffer, ")");
3678 if (sample_function->output_single_component)
3679 shader_addline(ins->ctx->buffer, ")");
3681 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3683 if (!is_identity_fixup(fixup))
3684 shader_glsl_color_correction(ins, fixup);
3687 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3689 /* Write the final position.
3691 * OpenGL coordinates specify the center of the pixel while D3D coords
3692 * specify the corner. The offsets are stored in z and w in
3693 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3694 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3695 * a MAD. */
3696 if (use_viewport_index)
3698 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3699 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3701 else
3703 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3704 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3707 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3708 * in utils.c
3710 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3711 * shaders are run before the homogeneous divide, so we have to take the w
3712 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3713 * z = z * 2 - w. */
3714 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3717 /*****************************************************************************
3718 * Begin processing individual instruction opcodes
3719 ****************************************************************************/
3721 static void shader_glsl_binop(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 DWORD write_mask;
3727 const char *op;
3729 /* Determine the GLSL operator to use based on the opcode */
3730 switch (ins->handler_idx)
3732 case WINED3DSIH_ADD: op = "+"; break;
3733 case WINED3DSIH_AND: op = "&"; break;
3734 case WINED3DSIH_DIV: op = "/"; break;
3735 case WINED3DSIH_IADD: op = "+"; break;
3736 case WINED3DSIH_ISHL: op = "<<"; break;
3737 case WINED3DSIH_ISHR: op = ">>"; break;
3738 case WINED3DSIH_MUL: op = "*"; break;
3739 case WINED3DSIH_OR: op = "|"; break;
3740 case WINED3DSIH_SUB: op = "-"; break;
3741 case WINED3DSIH_USHR: op = ">>"; break;
3742 case WINED3DSIH_XOR: op = "^"; break;
3743 default:
3744 op = "<unhandled operator>";
3745 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3746 break;
3749 write_mask = shader_glsl_append_dst(buffer, ins);
3750 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3751 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3752 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3755 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3757 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3758 struct glsl_src_param src0_param;
3759 struct glsl_src_param src1_param;
3760 unsigned int mask_size;
3761 DWORD write_mask;
3762 const char *op;
3764 write_mask = shader_glsl_append_dst(buffer, ins);
3765 mask_size = shader_glsl_get_write_mask_size(write_mask);
3766 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3767 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3769 if (mask_size > 1)
3771 switch (ins->handler_idx)
3773 case WINED3DSIH_EQ: op = "equal"; break;
3774 case WINED3DSIH_IEQ: op = "equal"; break;
3775 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3776 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3777 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3778 case WINED3DSIH_LT: op = "lessThan"; break;
3779 case WINED3DSIH_ILT: op = "lessThan"; break;
3780 case WINED3DSIH_ULT: op = "lessThan"; break;
3781 case WINED3DSIH_NE: op = "notEqual"; break;
3782 case WINED3DSIH_INE: op = "notEqual"; break;
3783 default:
3784 op = "<unhandled operator>";
3785 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3786 break;
3789 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3790 mask_size, op, src0_param.param_str, src1_param.param_str);
3792 else
3794 switch (ins->handler_idx)
3796 case WINED3DSIH_EQ: op = "=="; break;
3797 case WINED3DSIH_IEQ: op = "=="; break;
3798 case WINED3DSIH_GE: op = ">="; break;
3799 case WINED3DSIH_IGE: op = ">="; break;
3800 case WINED3DSIH_UGE: op = ">="; break;
3801 case WINED3DSIH_LT: op = "<"; break;
3802 case WINED3DSIH_ILT: op = "<"; break;
3803 case WINED3DSIH_ULT: op = "<"; break;
3804 case WINED3DSIH_NE: op = "!="; break;
3805 case WINED3DSIH_INE: op = "!="; break;
3806 default:
3807 op = "<unhandled operator>";
3808 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3809 break;
3812 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3813 src0_param.param_str, op, src1_param.param_str);
3817 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3819 struct glsl_src_param src_param;
3820 DWORD write_mask;
3821 const char *op;
3823 switch (ins->handler_idx)
3825 case WINED3DSIH_INEG: op = "-"; break;
3826 case WINED3DSIH_NOT: op = "~"; break;
3827 default:
3828 op = "<unhandled operator>";
3829 ERR("Unhandled opcode %s.\n",
3830 debug_d3dshaderinstructionhandler(ins->handler_idx));
3831 break;
3834 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3835 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3836 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3839 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3841 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3842 struct glsl_src_param src0_param;
3843 struct glsl_src_param src1_param;
3844 DWORD write_mask;
3846 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3847 * If not, we can emulate it. */
3848 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3849 FIXME("64-bit integer multiplies not implemented.\n");
3851 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3853 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3854 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3855 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3857 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3858 src0_param.param_str, src1_param.param_str);
3862 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3864 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3865 struct glsl_src_param src0_param, src1_param;
3866 DWORD write_mask;
3868 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3870 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3872 char dst_mask[6];
3874 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3875 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3876 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3877 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3878 dst_mask, src0_param.param_str, src1_param.param_str);
3880 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3881 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3882 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3883 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3885 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
3886 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3888 else
3890 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3891 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3892 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3893 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3896 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3898 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3899 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3900 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3901 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3905 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3906 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3908 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3909 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3910 const struct wined3d_gl_info *gl_info = priv->gl_info;
3911 struct glsl_src_param src0_param;
3912 DWORD write_mask;
3914 write_mask = shader_glsl_append_dst(buffer, ins);
3915 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3917 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3918 * shader versions WINED3DSIO_MOVA is used for this. */
3919 if (ins->ctx->reg_maps->shader_version.major == 1
3920 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3921 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3923 /* This is a simple floor() */
3924 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3925 if (mask_size > 1) {
3926 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3927 } else {
3928 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3931 else if (ins->handler_idx == WINED3DSIH_MOVA)
3933 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3935 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3937 if (mask_size > 1)
3938 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3939 else
3940 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3942 else
3944 if (mask_size > 1)
3945 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3946 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3947 else
3948 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3949 src0_param.param_str, src0_param.param_str);
3952 else
3954 shader_addline(buffer, "%s);\n", src0_param.param_str);
3958 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3959 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3961 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3962 struct glsl_src_param src0_param;
3963 struct glsl_src_param src1_param;
3964 DWORD dst_write_mask, src_write_mask;
3965 unsigned int dst_size;
3967 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3968 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3970 /* dp4 works on vec4, dp3 on vec3, etc. */
3971 if (ins->handler_idx == WINED3DSIH_DP4)
3972 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3973 else if (ins->handler_idx == WINED3DSIH_DP3)
3974 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3975 else
3976 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3978 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3979 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3981 if (dst_size > 1) {
3982 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3983 } else {
3984 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3988 /* Note that this instruction has some restrictions. The destination write mask
3989 * can't contain the w component, and the source swizzles have to be .xyzw */
3990 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3992 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3993 struct glsl_src_param src0_param;
3994 struct glsl_src_param src1_param;
3995 char dst_mask[6];
3997 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3998 shader_glsl_append_dst(ins->ctx->buffer, ins);
3999 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4000 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4001 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4004 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4006 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4008 if (!stream)
4009 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4010 else
4011 FIXME("Unhandled primitive stream %u.\n", stream);
4014 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4015 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4016 * GLSL uses the value as-is. */
4017 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4019 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4020 struct glsl_src_param src0_param;
4021 struct glsl_src_param src1_param;
4022 DWORD dst_write_mask;
4023 unsigned int dst_size;
4025 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4026 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4028 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4029 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4031 if (dst_size > 1)
4033 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4034 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4036 else
4038 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4039 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4043 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4044 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4046 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4047 bool y_correction = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4048 ? priv->cur_ps_args->y_correction : false;
4049 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4050 struct glsl_src_param src_param;
4051 const char *instruction;
4052 DWORD write_mask;
4053 unsigned i;
4055 /* Determine the GLSL function to use based on the opcode */
4056 /* TODO: Possibly make this a table for faster lookups */
4057 switch (ins->handler_idx)
4059 case WINED3DSIH_ABS: instruction = "abs"; break;
4060 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4061 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4062 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4063 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4064 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4065 case WINED3DSIH_DSY: instruction = y_correction ? "ycorrection.y * dFdy" : "dFdy"; break;
4066 case WINED3DSIH_DSY_COARSE: instruction = y_correction ? "ycorrection.y * dFdyCoarse" : "dFdyCoarse"; break;
4067 case WINED3DSIH_DSY_FINE: instruction = y_correction ? "ycorrection.y * dFdyFine" : "dFdyFine"; break;
4068 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4069 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4070 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4071 case WINED3DSIH_FRC: instruction = "fract"; break;
4072 case WINED3DSIH_IMAX: instruction = "max"; break;
4073 case WINED3DSIH_IMIN: instruction = "min"; break;
4074 case WINED3DSIH_MAX: instruction = "max"; break;
4075 case WINED3DSIH_MIN: instruction = "min"; break;
4076 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4077 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4078 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4079 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4080 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4081 case WINED3DSIH_UMAX: instruction = "max"; break;
4082 case WINED3DSIH_UMIN: instruction = "min"; break;
4083 default: instruction = "";
4084 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4085 break;
4088 write_mask = shader_glsl_append_dst(buffer, ins);
4090 /* In D3D bits are numbered from the most significant bit. */
4091 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4092 shader_addline(buffer, "31 - ");
4093 shader_addline(buffer, "%s(", instruction);
4095 if (ins->src_count)
4097 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4098 shader_addline(buffer, "%s", src_param.param_str);
4099 for (i = 1; i < ins->src_count; ++i)
4101 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4102 shader_addline(buffer, ", %s", src_param.param_str);
4106 shader_addline(buffer, "));\n");
4109 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4111 struct wined3d_shader_dst_param dst;
4112 struct glsl_src_param src;
4113 DWORD write_mask;
4114 const char *fmt;
4115 unsigned int i;
4117 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4118 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4120 dst = ins->dst[0];
4121 for (i = 0; i < 4; ++i)
4123 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4124 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4125 &dst, 0, dst.reg.data_type)))
4126 continue;
4128 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4129 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4133 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4135 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4136 struct wined3d_shader_dst_param dst;
4137 struct glsl_src_param src[4];
4138 const char *instruction;
4139 BOOL tmp_dst = FALSE;
4140 char mask_char[6];
4141 unsigned int i, j;
4142 DWORD write_mask;
4144 switch (ins->handler_idx)
4146 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4147 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4148 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4149 default:
4150 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4151 return;
4154 for (i = 0; i < ins->src_count; ++i)
4156 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4157 && ins->dst[0].reg.type == ins->src[i].reg.type)
4158 tmp_dst = TRUE;
4161 dst = ins->dst[0];
4162 for (i = 0; i < 4; ++i)
4164 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4165 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4166 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4167 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4168 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, 0, dst.reg.data_type)))
4169 continue;
4171 for (j = 0; j < ins->src_count; ++j)
4172 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4173 shader_addline(buffer, "%s(", instruction);
4174 for (j = 0; j < ins->src_count - 2; ++j)
4175 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4176 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4179 if (tmp_dst)
4181 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
4182 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4183 shader_addline(buffer, "tmp0%s);\n", mask_char);
4187 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4189 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4191 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4192 struct glsl_src_param src_param;
4193 unsigned int mask_size;
4194 DWORD write_mask;
4195 char dst_mask[6];
4197 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4198 mask_size = shader_glsl_get_write_mask_size(write_mask);
4199 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4201 if (mask_size > 3)
4202 shader_addline(buffer, "tmp0.x = dot(vec3(%s), vec3(%s));\n",
4203 src_param.param_str, src_param.param_str);
4204 else
4205 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4206 src_param.param_str, src_param.param_str);
4207 shader_glsl_append_dst(buffer, ins);
4209 shader_addline(buffer, "tmp0.x == 0.0 ? %s : (%s * inversesqrt(tmp0.x)));\n",
4210 src_param.param_str, src_param.param_str);
4213 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4215 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4216 ins->ctx->reg_maps->shader_version.minor);
4217 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4218 struct glsl_src_param src0_param;
4219 const char *prefix, *suffix;
4220 unsigned int dst_size;
4221 DWORD dst_write_mask;
4223 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4224 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4226 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4227 dst_write_mask = WINED3DSP_WRITEMASK_3;
4229 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4231 switch (ins->handler_idx)
4233 case WINED3DSIH_EXP:
4234 case WINED3DSIH_EXPP:
4235 prefix = "exp2(";
4236 suffix = ")";
4237 break;
4239 case WINED3DSIH_LOG:
4240 case WINED3DSIH_LOGP:
4241 prefix = "log2(abs(";
4242 suffix = "))";
4243 break;
4245 case WINED3DSIH_RCP:
4246 prefix = "1.0 / ";
4247 suffix = "";
4248 break;
4250 case WINED3DSIH_RSQ:
4251 prefix = "inversesqrt(abs(";
4252 suffix = "))";
4253 break;
4255 default:
4256 prefix = "";
4257 suffix = "";
4258 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4259 break;
4262 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4263 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4264 else
4265 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4268 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4269 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4270 * dst.x = 2^(floor(src))
4271 * dst.y = src - floor(src)
4272 * dst.z = 2^src (partial precision is allowed, but optional)
4273 * dst.w = 1.0;
4274 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4275 * dst = 2^src; (partial precision is allowed, but optional)
4277 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4279 if (ins->ctx->reg_maps->shader_version.major < 2)
4281 struct glsl_src_param src_param;
4282 char dst_mask[6];
4284 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4286 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4287 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4288 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4289 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4291 shader_glsl_append_dst(ins->ctx->buffer, ins);
4292 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4293 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4294 return;
4297 shader_glsl_scalar_op(ins);
4300 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4301 const char *vector_constructor, const char *scalar_constructor)
4303 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4304 struct glsl_src_param src_param;
4305 unsigned int mask_size;
4306 DWORD write_mask;
4308 write_mask = shader_glsl_append_dst(buffer, ins);
4309 mask_size = shader_glsl_get_write_mask_size(write_mask);
4310 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4312 if (mask_size > 1)
4313 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4314 else
4315 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4318 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4320 shader_glsl_cast(ins, "ivec", "int");
4323 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4325 shader_glsl_cast(ins, "uvec", "uint");
4328 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4330 shader_glsl_cast(ins, "vec", "float");
4333 /** Process signed comparison opcodes in GLSL. */
4334 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4336 struct glsl_src_param src0_param;
4337 struct glsl_src_param src1_param;
4338 DWORD write_mask;
4339 unsigned int mask_size;
4341 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4342 mask_size = shader_glsl_get_write_mask_size(write_mask);
4343 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4344 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4346 if (mask_size > 1) {
4347 const char *compare;
4349 switch(ins->handler_idx)
4351 case WINED3DSIH_SLT: compare = "lessThan"; break;
4352 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4353 default: compare = "";
4354 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4357 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4358 src0_param.param_str, src1_param.param_str);
4359 } else {
4360 switch(ins->handler_idx)
4362 case WINED3DSIH_SLT:
4363 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4364 * to return 0.0 but step returns 1.0 because step is not < x
4365 * An alternative is a bvec compare padded with an unused second component.
4366 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4367 * issue. Playing with not() is not possible either because not() does not accept
4368 * a scalar.
4370 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4371 src0_param.param_str, src1_param.param_str);
4372 break;
4373 case WINED3DSIH_SGE:
4374 /* Here we can use the step() function and safe a conditional */
4375 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4376 break;
4377 default:
4378 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4384 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4386 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4387 struct wined3d_shader_dst_param dst[2];
4388 struct glsl_src_param src[3];
4389 unsigned int i, j, k;
4390 char mask_char[6];
4391 DWORD write_mask;
4392 BOOL tmp_dst[2];
4394 for (i = 0; i < ins->dst_count; ++i)
4396 tmp_dst[i] = FALSE;
4397 for (j = 0; j < ins->src_count; ++j)
4399 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4400 && ins->dst[i].reg.type == ins->src[j].reg.type)
4401 tmp_dst[i] = TRUE;
4405 dst[0] = ins->dst[0];
4406 dst[1] = ins->dst[1];
4407 for (i = 0; i < 4; ++i)
4409 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4411 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4412 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4413 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4414 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], j, dst[j].reg.data_type)))
4415 continue;
4417 for (k = 0; k < ARRAY_SIZE(src); ++k)
4418 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4420 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4421 src[0].param_str, src[1].param_str, src[2].param_str);
4425 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4427 if (tmp_dst[i])
4429 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4430 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], i, ins->dst[i].reg.data_type);
4431 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4436 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4438 const char *condition_prefix, *condition_suffix;
4439 struct wined3d_shader_dst_param dst;
4440 struct glsl_src_param src0_param;
4441 struct glsl_src_param src1_param;
4442 struct glsl_src_param src2_param;
4443 BOOL temp_destination = FALSE;
4444 DWORD cmp_channel = 0;
4445 unsigned int i, j;
4446 char mask_char[6];
4447 DWORD write_mask;
4449 switch (ins->handler_idx)
4451 case WINED3DSIH_CMP:
4452 condition_prefix = "";
4453 condition_suffix = " >= 0.0";
4454 break;
4456 case WINED3DSIH_CND:
4457 condition_prefix = "";
4458 condition_suffix = " > 0.5";
4459 break;
4461 case WINED3DSIH_MOVC:
4462 condition_prefix = "bool(";
4463 condition_suffix = ")";
4464 break;
4466 default:
4467 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4468 condition_prefix = "<unhandled prefix>";
4469 condition_suffix = "<unhandled suffix>";
4470 break;
4473 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4475 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4476 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4477 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4478 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4480 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4481 condition_prefix, src0_param.param_str, condition_suffix,
4482 src1_param.param_str, src2_param.param_str);
4483 return;
4486 dst = ins->dst[0];
4488 /* Splitting the instruction up in multiple lines imposes a problem:
4489 * The first lines may overwrite source parameters of the following lines.
4490 * Deal with that by using a temporary destination register if needed. */
4491 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4492 && ins->src[0].reg.type == dst.reg.type)
4493 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4494 && ins->src[1].reg.type == dst.reg.type)
4495 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4496 && ins->src[2].reg.type == dst.reg.type))
4497 temp_destination = TRUE;
4499 /* Cycle through all source0 channels. */
4500 for (i = 0; i < 4; ++i)
4502 write_mask = 0;
4503 /* Find the destination channels which use the current source0 channel. */
4504 for (j = 0; j < 4; ++j)
4506 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4508 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4509 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4512 dst.write_mask = ins->dst[0].write_mask & write_mask;
4514 if (temp_destination)
4516 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4517 continue;
4518 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4520 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type)))
4521 continue;
4523 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4524 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4525 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4527 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4528 condition_prefix, src0_param.param_str, condition_suffix,
4529 src1_param.param_str, src2_param.param_str);
4532 if (temp_destination)
4534 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4535 shader_glsl_append_dst(ins->ctx->buffer, ins);
4536 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4540 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4541 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4542 * the compare is done per component of src0. */
4543 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4545 struct glsl_src_param src0_param;
4546 struct glsl_src_param src1_param;
4547 struct glsl_src_param src2_param;
4548 DWORD write_mask;
4549 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4550 ins->ctx->reg_maps->shader_version.minor);
4552 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4554 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4555 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4556 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4557 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4559 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4560 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4561 else
4562 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4563 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4564 return;
4567 shader_glsl_conditional_move(ins);
4570 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4571 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4573 struct glsl_src_param src0_param;
4574 struct glsl_src_param src1_param;
4575 struct glsl_src_param src2_param;
4576 DWORD write_mask;
4578 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4579 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4580 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4581 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4582 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4583 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4586 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4587 Vertex shaders to GLSL codes */
4588 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4590 int i;
4591 int nComponents = 0;
4592 struct wined3d_shader_dst_param tmp_dst = {{0}};
4593 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4594 struct wined3d_shader_instruction tmp_ins;
4596 memset(&tmp_ins, 0, sizeof(tmp_ins));
4598 /* Set constants for the temporary argument */
4599 tmp_ins.ctx = ins->ctx;
4600 tmp_ins.dst_count = 1;
4601 tmp_ins.dst = &tmp_dst;
4602 tmp_ins.src_count = 2;
4603 tmp_ins.src = tmp_src;
4605 switch(ins->handler_idx)
4607 case WINED3DSIH_M4x4:
4608 nComponents = 4;
4609 tmp_ins.handler_idx = WINED3DSIH_DP4;
4610 break;
4611 case WINED3DSIH_M4x3:
4612 nComponents = 3;
4613 tmp_ins.handler_idx = WINED3DSIH_DP4;
4614 break;
4615 case WINED3DSIH_M3x4:
4616 nComponents = 4;
4617 tmp_ins.handler_idx = WINED3DSIH_DP3;
4618 break;
4619 case WINED3DSIH_M3x3:
4620 nComponents = 3;
4621 tmp_ins.handler_idx = WINED3DSIH_DP3;
4622 break;
4623 case WINED3DSIH_M3x2:
4624 nComponents = 2;
4625 tmp_ins.handler_idx = WINED3DSIH_DP3;
4626 break;
4627 default:
4628 break;
4631 tmp_dst = ins->dst[0];
4632 tmp_src[0] = ins->src[0];
4633 tmp_src[1] = ins->src[1];
4634 for (i = 0; i < nComponents; ++i)
4636 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4637 shader_glsl_dot(&tmp_ins);
4638 ++tmp_src[1].reg.idx[0].offset;
4643 The LRP instruction performs a component-wise linear interpolation
4644 between the second and third operands using the first operand as the
4645 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4646 This is equivalent to mix(src2, src1, src0);
4648 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4650 struct glsl_src_param src0_param;
4651 struct glsl_src_param src1_param;
4652 struct glsl_src_param src2_param;
4653 DWORD write_mask;
4655 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4657 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4658 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4659 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4661 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4662 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4665 /** Process the WINED3DSIO_LIT instruction in GLSL:
4666 * dst.x = dst.w = 1.0
4667 * dst.y = (src0.x > 0) ? src0.x
4668 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4669 * where src.w is clamped at +- 128
4671 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4673 struct glsl_src_param src0_param;
4674 struct glsl_src_param src1_param;
4675 struct glsl_src_param src3_param;
4676 char dst_mask[6];
4678 shader_glsl_append_dst(ins->ctx->buffer, ins);
4679 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4681 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4682 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4683 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4685 /* The sdk specifies the instruction like this
4686 * dst.x = 1.0;
4687 * if(src.x > 0.0) dst.y = src.x
4688 * else dst.y = 0.0.
4689 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4690 * else dst.z = 0.0;
4691 * dst.w = 1.0;
4692 * (where power = src.w clamped between -128 and 128)
4694 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4695 * dst.x = 1.0 ... No further explanation needed
4696 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4697 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4698 * dst.w = 1.0. ... Nothing fancy.
4700 * So we still have one conditional in there. So do this:
4701 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4703 * 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),
4704 * 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.
4705 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4707 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4708 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4709 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4711 shader_addline(ins->ctx->buffer,
4712 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4713 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4714 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4715 src0_param.param_str, src3_param.param_str, dst_mask);
4718 /** Process the WINED3DSIO_DST instruction in GLSL:
4719 * dst.x = 1.0
4720 * dst.y = src0.x * src0.y
4721 * dst.z = src0.z
4722 * dst.w = src1.w
4724 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4726 struct glsl_src_param src0y_param;
4727 struct glsl_src_param src0z_param;
4728 struct glsl_src_param src1y_param;
4729 struct glsl_src_param src1w_param;
4730 char dst_mask[6];
4732 shader_glsl_append_dst(ins->ctx->buffer, ins);
4733 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4735 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4736 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4737 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4738 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4740 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4741 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4744 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4745 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4746 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4748 * dst.x = cos(src0.?)
4749 * dst.y = sin(src0.?)
4750 * dst.z = dst.z
4751 * dst.w = dst.w
4753 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4755 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4756 struct glsl_src_param src0_param;
4757 DWORD write_mask;
4759 if (ins->ctx->reg_maps->shader_version.major < 4)
4761 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4763 write_mask = shader_glsl_append_dst(buffer, ins);
4764 switch (write_mask)
4766 case WINED3DSP_WRITEMASK_0:
4767 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4768 break;
4770 case WINED3DSP_WRITEMASK_1:
4771 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4772 break;
4774 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4775 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4776 src0_param.param_str, src0_param.param_str);
4777 break;
4779 default:
4780 ERR("Write mask should be .x, .y or .xy\n");
4781 break;
4784 return;
4787 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4790 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4792 char dst_mask[6];
4794 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4795 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4796 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4798 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4799 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4800 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4802 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4803 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4805 else
4807 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4808 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4809 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4812 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4814 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4815 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4816 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4820 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4821 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4822 * generate invalid code
4824 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4826 struct glsl_src_param src0_param;
4827 DWORD write_mask;
4829 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4830 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4832 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4835 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4836 * Start a for() loop where src1.y is the initial value of aL,
4837 * increment aL by src1.z for a total of src1.x iterations.
4838 * Need to use a temporary variable for this operation.
4840 /* FIXME: I don't think nested loops will work correctly this way. */
4841 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4843 struct wined3d_shader_parser_state *state = ins->ctx->state;
4844 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4845 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4846 const struct wined3d_shader *shader = ins->ctx->shader;
4847 const struct wined3d_shader_lconst *constant;
4848 struct wined3d_string_buffer *reg_name;
4849 const DWORD *control_values = NULL;
4851 if (ins->ctx->reg_maps->shader_version.major < 4)
4853 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4854 * class hardware doesn't support real varying indexing, but Microsoft
4855 * designed this feature for Shader model 2.x+. If the loop control is
4856 * known at compile time, the GLSL compiler can unroll the loop, and
4857 * replace indirect addressing with direct addressing. */
4858 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4860 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4862 if (constant->idx == ins->src[1].reg.idx[0].offset)
4864 control_values = constant->value;
4865 break;
4870 if (control_values)
4872 struct wined3d_shader_loop_control loop_control;
4873 loop_control.count = control_values[0];
4874 loop_control.start = control_values[1];
4875 loop_control.step = (int)control_values[2];
4877 if (loop_control.step > 0)
4879 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4880 state->current_loop_depth, loop_control.start,
4881 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4882 state->current_loop_depth, loop_control.step);
4884 else if (loop_control.step < 0)
4886 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4887 state->current_loop_depth, loop_control.start,
4888 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4889 state->current_loop_depth, loop_control.step);
4891 else
4893 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4894 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4895 state->current_loop_depth, loop_control.count,
4896 state->current_loop_depth);
4899 else
4901 reg_name = string_buffer_get(priv->string_buffers);
4902 shader_glsl_get_register_name(&ins->src[1].reg, ins->src[1].reg.data_type, reg_name, NULL, ins->ctx);
4904 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4905 state->current_loop_depth, state->current_loop_reg, reg_name->buffer,
4906 state->current_loop_depth, reg_name->buffer,
4907 state->current_loop_depth, state->current_loop_reg, reg_name->buffer);
4909 string_buffer_release(priv->string_buffers, reg_name);
4913 ++state->current_loop_reg;
4915 else
4917 shader_addline(buffer, "for (;;)\n{\n");
4920 ++state->current_loop_depth;
4923 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4925 struct wined3d_shader_parser_state *state = ins->ctx->state;
4927 shader_addline(ins->ctx->buffer, "}\n");
4929 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4931 --state->current_loop_depth;
4932 --state->current_loop_reg;
4935 if (ins->handler_idx == WINED3DSIH_ENDREP)
4937 --state->current_loop_depth;
4941 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4943 struct wined3d_shader_parser_state *state = ins->ctx->state;
4944 const struct wined3d_shader *shader = ins->ctx->shader;
4945 const struct wined3d_shader_lconst *constant;
4946 struct glsl_src_param src0_param;
4947 const DWORD *control_values = NULL;
4949 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4950 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4952 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4954 if (constant->idx == ins->src[0].reg.idx[0].offset)
4956 control_values = constant->value;
4957 break;
4962 if (control_values)
4964 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4965 state->current_loop_depth, state->current_loop_depth,
4966 control_values[0], state->current_loop_depth);
4968 else
4970 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4971 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4972 state->current_loop_depth, state->current_loop_depth,
4973 src0_param.param_str, state->current_loop_depth);
4976 ++state->current_loop_depth;
4979 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4981 struct glsl_src_param src0_param;
4983 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4984 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4987 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4989 struct glsl_src_param src0_param;
4991 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4992 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4995 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4997 shader_addline(ins->ctx->buffer, "default:\n");
5000 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
5002 struct glsl_src_param src_param;
5003 const char *condition;
5005 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5006 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5007 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
5010 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5012 shader_glsl_generate_condition(ins);
5013 shader_addline(ins->ctx->buffer, "{\n");
5016 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5018 struct glsl_src_param src0_param;
5019 struct glsl_src_param src1_param;
5021 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5022 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5024 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5025 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5028 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5030 shader_addline(ins->ctx->buffer, "} else {\n");
5033 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5035 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5036 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5037 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5039 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5040 if (!priv->gl_info->supported[ARB_CLIP_CONTROL])
5041 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5043 if (!stream)
5044 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5045 else
5046 FIXME("Unhandled primitive stream %u.\n", stream);
5049 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5051 shader_addline(ins->ctx->buffer, "break;\n");
5054 /* FIXME: According to MSDN the compare is done per component. */
5055 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5057 struct glsl_src_param src0_param;
5058 struct glsl_src_param src1_param;
5060 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5061 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5063 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5064 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5067 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5069 const char *op;
5071 switch (ins->handler_idx)
5073 case WINED3DSIH_BREAKP:
5074 op = "break;";
5075 break;
5076 case WINED3DSIH_CONTINUEP:
5077 op = "continue;";
5078 break;
5079 case WINED3DSIH_RETP:
5080 op = "return;";
5081 break;
5082 default:
5083 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5084 return;
5087 shader_glsl_generate_condition(ins);
5088 if (ins->handler_idx == WINED3DSIH_RETP)
5090 shader_addline(ins->ctx->buffer, "{\n");
5091 shader_glsl_generate_shader_epilogue(ins->ctx);
5093 shader_addline(ins->ctx->buffer, " %s\n", op);
5094 if (ins->handler_idx == WINED3DSIH_RETP)
5095 shader_addline(ins->ctx->buffer, "}\n");
5098 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5100 shader_addline(ins->ctx->buffer, "continue;\n");
5103 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5105 shader_addline(ins->ctx->buffer, "}\n");
5106 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5108 /* Subroutines appear at the end of the shader. */
5109 ins->ctx->state->in_subroutine = TRUE;
5112 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5114 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5117 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5119 struct glsl_src_param src1_param;
5121 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5122 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5123 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5126 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5128 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5130 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5132 shader_glsl_generate_shader_epilogue(ins->ctx);
5133 shader_addline(ins->ctx->buffer, "return;\n");
5137 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5139 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5140 ins->ctx->reg_maps->shader_version.minor);
5141 struct glsl_sample_function sample_function;
5142 DWORD sample_flags = 0;
5143 DWORD resource_idx;
5144 DWORD mask = 0, swizzle;
5145 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5146 enum wined3d_shader_resource_type resource_type;
5148 /* 1.0-1.4: Use destination register as sampler source.
5149 * 2.0+: Use provided sampler source. */
5150 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5151 resource_idx = ins->dst[0].reg.idx[0].offset;
5152 else
5153 resource_idx = ins->src[1].reg.idx[0].offset;
5155 resource_type = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
5156 ? pixelshader_get_resource_type(ins->ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
5157 : ins->ctx->reg_maps->resource_info[resource_idx].type;
5159 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5161 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5162 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5164 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5165 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5167 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5168 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5170 case WINED3D_TTFF_COUNT1:
5171 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5172 break;
5173 case WINED3D_TTFF_COUNT2:
5174 mask = WINED3DSP_WRITEMASK_1;
5175 break;
5176 case WINED3D_TTFF_COUNT3:
5177 mask = WINED3DSP_WRITEMASK_2;
5178 break;
5179 case WINED3D_TTFF_COUNT4:
5180 case WINED3D_TTFF_DISABLE:
5181 mask = WINED3DSP_WRITEMASK_3;
5182 break;
5186 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5188 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5190 if (src_mod == WINED3DSPSM_DZ) {
5191 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5192 mask = WINED3DSP_WRITEMASK_2;
5193 } else if (src_mod == WINED3DSPSM_DW) {
5194 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5195 mask = WINED3DSP_WRITEMASK_3;
5198 else
5200 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5201 && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5203 /* ps 2.0 texldp instruction always divides by the fourth component. */
5204 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5205 mask = WINED3DSP_WRITEMASK_3;
5209 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5210 mask |= sample_function.coord_mask;
5211 sample_function.coord_mask = mask;
5213 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5214 else swizzle = ins->src[1].swizzle;
5216 /* 1.0-1.3: Use destination register as coordinate source.
5217 1.4+: Use provided coordinate source register. */
5218 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5220 char coord_mask[6];
5221 shader_glsl_write_mask_to_str(mask, coord_mask);
5222 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5223 "T%u%s", resource_idx, coord_mask);
5225 else
5227 struct glsl_src_param coord_param;
5228 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5229 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5231 struct glsl_src_param bias;
5232 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5233 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5234 NULL, "%s", coord_param.param_str);
5235 } else {
5236 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5237 "%s", coord_param.param_str);
5240 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5243 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5245 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5246 const struct wined3d_gl_info *gl_info = priv->gl_info;
5247 struct glsl_src_param coord_param, dx_param, dy_param;
5248 struct glsl_sample_function sample_function;
5249 DWORD sampler_idx;
5250 DWORD swizzle = ins->src[1].swizzle;
5252 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5254 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5255 shader_glsl_tex(ins);
5256 return;
5259 sampler_idx = ins->src[1].reg.idx[0].offset;
5261 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5262 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5263 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5264 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5266 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5267 NULL, NULL, "%s", coord_param.param_str);
5268 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5271 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5273 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5274 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5275 const struct wined3d_gl_info *gl_info = priv->gl_info;
5276 struct glsl_src_param coord_param, lod_param;
5277 struct glsl_sample_function sample_function;
5278 DWORD swizzle = ins->src[1].swizzle;
5279 DWORD sampler_idx;
5281 sampler_idx = ins->src[1].reg.idx[0].offset;
5283 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5284 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5286 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5288 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5289 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5291 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5292 * However, the NVIDIA drivers allow them in fragment shaders as well,
5293 * even without the appropriate extension. */
5294 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5296 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5297 "%s", coord_param.param_str);
5298 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5301 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5302 unsigned int resource_idx, unsigned int sampler_idx)
5304 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5305 unsigned int i;
5307 for (i = 0; i < sampler_map->count; ++i)
5309 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5310 return entries[i].bind_idx;
5313 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5315 return ~0u;
5318 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5320 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5321 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5322 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5323 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5324 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5325 struct glsl_src_param structure_idx, offset, data, data2;
5326 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5327 enum wined3d_shader_resource_type resource_type;
5328 struct wined3d_string_buffer *address;
5329 enum wined3d_data_type data_type;
5330 unsigned int resource_idx, stride;
5331 const char *op, *resource;
5332 DWORD coord_mask;
5333 BOOL is_tgsm;
5335 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5336 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5337 if (is_tgsm)
5339 if (resource_idx >= reg_maps->tgsm_count)
5341 ERR("Invalid TGSM index %u.\n", resource_idx);
5342 return;
5344 resource = "g";
5345 data_type = WINED3D_DATA_UINT;
5346 coord_mask = 1;
5347 stride = reg_maps->tgsm[resource_idx].stride;
5349 else
5351 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5353 ERR("Invalid UAV index %u.\n", resource_idx);
5354 return;
5356 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5357 if (resource_type >= ARRAY_SIZE(resource_type_info))
5359 ERR("Unexpected resource type %#x.\n", resource_type);
5360 return;
5362 resource = "image";
5363 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5364 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5365 stride = reg_maps->uav_resource_info[resource_idx].stride;
5368 switch (ins->handler_idx)
5370 case WINED3DSIH_ATOMIC_AND:
5371 case WINED3DSIH_IMM_ATOMIC_AND:
5372 if (is_tgsm)
5373 op = "atomicAnd";
5374 else
5375 op = "imageAtomicAnd";
5376 break;
5377 case WINED3DSIH_ATOMIC_CMP_STORE:
5378 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5379 if (is_tgsm)
5380 op = "atomicCompSwap";
5381 else
5382 op = "imageAtomicCompSwap";
5383 break;
5384 case WINED3DSIH_ATOMIC_IADD:
5385 case WINED3DSIH_IMM_ATOMIC_IADD:
5386 if (is_tgsm)
5387 op = "atomicAdd";
5388 else
5389 op = "imageAtomicAdd";
5390 break;
5391 case WINED3DSIH_ATOMIC_IMAX:
5392 case WINED3DSIH_IMM_ATOMIC_IMAX:
5393 if (is_tgsm)
5394 op = "atomicMax";
5395 else
5396 op = "imageAtomicMax";
5397 if (data_type != WINED3D_DATA_INT)
5399 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5400 return;
5402 break;
5403 case WINED3DSIH_ATOMIC_IMIN:
5404 case WINED3DSIH_IMM_ATOMIC_IMIN:
5405 if (is_tgsm)
5406 op = "atomicMin";
5407 else
5408 op = "imageAtomicMin";
5409 if (data_type != WINED3D_DATA_INT)
5411 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5412 return;
5414 break;
5415 case WINED3DSIH_ATOMIC_OR:
5416 case WINED3DSIH_IMM_ATOMIC_OR:
5417 if (is_tgsm)
5418 op = "atomicOr";
5419 else
5420 op = "imageAtomicOr";
5421 break;
5422 case WINED3DSIH_ATOMIC_UMAX:
5423 case WINED3DSIH_IMM_ATOMIC_UMAX:
5424 if (is_tgsm)
5425 op = "atomicMax";
5426 else
5427 op = "imageAtomicMax";
5428 if (data_type != WINED3D_DATA_UINT)
5430 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5431 return;
5433 break;
5434 case WINED3DSIH_ATOMIC_UMIN:
5435 case WINED3DSIH_IMM_ATOMIC_UMIN:
5436 if (is_tgsm)
5437 op = "atomicMin";
5438 else
5439 op = "imageAtomicMin";
5440 if (data_type != WINED3D_DATA_UINT)
5442 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5443 return;
5445 break;
5446 case WINED3DSIH_ATOMIC_XOR:
5447 case WINED3DSIH_IMM_ATOMIC_XOR:
5448 if (is_tgsm)
5449 op = "atomicXor";
5450 else
5451 op = "imageAtomicXor";
5452 break;
5453 case WINED3DSIH_IMM_ATOMIC_EXCH:
5454 if (is_tgsm)
5455 op = "atomicExchange";
5456 else
5457 op = "imageAtomicExchange";
5458 break;
5459 default:
5460 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5461 return;
5464 address = string_buffer_get(priv->string_buffers);
5465 if (stride)
5467 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5468 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5469 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5471 else
5473 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5474 string_buffer_sprintf(address, "%s", offset.param_str);
5475 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5476 shader_addline(address, "/ 4");
5479 if (is_imm_instruction)
5480 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5482 if (is_tgsm)
5483 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5484 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5485 else
5486 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5487 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5489 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5490 shader_addline(buffer, "%s", data.param_str);
5491 if (ins->src_count >= 3)
5493 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5494 shader_addline(buffer, ", %s", data2.param_str);
5497 if (is_imm_instruction)
5498 shader_addline(buffer, ")");
5499 shader_addline(buffer, ");\n");
5501 string_buffer_release(priv->string_buffers, address);
5504 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5506 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5507 const char *op;
5509 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5510 op = "atomicCounterIncrement";
5511 else
5512 op = "atomicCounterDecrement";
5514 shader_glsl_append_dst(ins->ctx->buffer, ins);
5515 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5518 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5520 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5521 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5522 enum wined3d_shader_resource_type resource_type;
5523 struct glsl_src_param image_coord_param;
5524 enum wined3d_data_type data_type;
5525 DWORD coord_mask, write_mask;
5526 unsigned int uav_idx;
5527 char dst_swizzle[6];
5529 uav_idx = ins->src[1].reg.idx[0].offset;
5530 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5532 ERR("Invalid UAV index %u.\n", uav_idx);
5533 return;
5535 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5536 if (resource_type >= ARRAY_SIZE(resource_type_info))
5538 ERR("Unexpected resource type %#x.\n", resource_type);
5539 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5541 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5542 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5544 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5545 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5547 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5548 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5549 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5552 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5554 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5555 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5556 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5557 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5558 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5559 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5560 struct glsl_src_param structure_idx, offset;
5561 struct wined3d_string_buffer *address;
5562 struct wined3d_shader_dst_param dst;
5563 const char *function, *resource;
5565 resource_idx = src->reg.idx[0].offset;
5566 if (src->reg.type == WINED3DSPR_RESOURCE)
5568 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5570 ERR("Invalid resource index %u.\n", resource_idx);
5571 return;
5573 stride = reg_maps->resource_info[resource_idx].stride;
5574 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5575 function = "texelFetch";
5576 resource = "sampler";
5578 else if (src->reg.type == WINED3DSPR_UAV)
5580 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5582 ERR("Invalid UAV index %u.\n", resource_idx);
5583 return;
5585 stride = reg_maps->uav_resource_info[resource_idx].stride;
5586 bind_idx = resource_idx;
5587 function = "imageLoad";
5588 resource = "image";
5590 else
5592 if (resource_idx >= reg_maps->tgsm_count)
5594 ERR("Invalid TGSM index %u.\n", resource_idx);
5595 return;
5597 stride = reg_maps->tgsm[resource_idx].stride;
5598 bind_idx = resource_idx;
5599 function = NULL;
5600 resource = "g";
5603 address = string_buffer_get(priv->string_buffers);
5604 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5606 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5607 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5609 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5610 shader_addline(address, "%s / 4", offset.param_str);
5612 dst = ins->dst[0];
5613 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5615 /* The instruction is split into multiple lines. The first lines may
5616 * overwrite source parameters of the following lines. */
5617 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5618 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5621 for (i = 0; i < 4; ++i)
5623 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5624 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type))
5625 continue;
5627 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5628 if (function)
5629 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5630 function, prefix, resource, bind_idx, address->buffer, swizzle);
5631 else
5632 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5633 prefix, resource, bind_idx, address->buffer, swizzle);
5636 string_buffer_release(priv->string_buffers, address);
5639 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5641 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5642 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5643 struct glsl_src_param image_coord_param, image_data_param;
5644 enum wined3d_shader_resource_type resource_type;
5645 enum wined3d_data_type data_type;
5646 unsigned int uav_idx;
5647 DWORD coord_mask;
5649 uav_idx = ins->dst[0].reg.idx[0].offset;
5650 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5652 ERR("Invalid UAV index %u.\n", uav_idx);
5653 return;
5655 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5656 if (resource_type >= ARRAY_SIZE(resource_type_info))
5658 ERR("Unexpected resource type %#x.\n", resource_type);
5659 return;
5661 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5662 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5664 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5665 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5666 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5667 shader_glsl_get_prefix(version->type), uav_idx,
5668 image_coord_param.param_str, image_data_param.param_str);
5671 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5673 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5674 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5675 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5676 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5677 struct glsl_src_param structure_idx, offset, data;
5678 unsigned int i, resource_idx, stride, src_idx = 0;
5679 struct wined3d_string_buffer *address;
5680 DWORD write_mask;
5681 BOOL is_tgsm;
5683 resource_idx = ins->dst[0].reg.idx[0].offset;
5684 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5685 if (is_tgsm)
5687 if (resource_idx >= reg_maps->tgsm_count)
5689 ERR("Invalid TGSM index %u.\n", resource_idx);
5690 return;
5692 stride = reg_maps->tgsm[resource_idx].stride;
5694 else
5696 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5698 ERR("Invalid UAV index %u.\n", resource_idx);
5699 return;
5701 stride = reg_maps->uav_resource_info[resource_idx].stride;
5704 address = string_buffer_get(priv->string_buffers);
5705 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5707 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5708 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5710 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5711 shader_addline(address, "%s / 4", offset.param_str);
5713 for (i = 0; i < 4; ++i)
5715 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5716 continue;
5718 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5720 if (is_tgsm)
5721 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5722 prefix, resource_idx, address->buffer, i, data.param_str);
5723 else
5724 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5725 prefix, resource_idx, address->buffer, i, data.param_str);
5728 string_buffer_release(priv->string_buffers, address);
5731 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5733 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5734 unsigned int sync_flags = ins->flags;
5736 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5738 shader_addline(buffer, "barrier();\n");
5739 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5742 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5744 shader_addline(buffer, "memoryBarrierShared();\n");
5745 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5748 if (sync_flags & WINED3DSSF_GLOBAL_UAV)
5750 shader_addline(buffer, "memoryBarrier();\n");
5751 sync_flags &= ~WINED3DSSF_GLOBAL_UAV;
5754 if (sync_flags)
5755 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5758 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5759 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5761 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5762 unsigned int idx = reg->idx[0].offset;
5764 if (reg->type == WINED3DSPR_RESOURCE)
5766 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5768 ERR("Invalid resource index %u.\n", idx);
5769 return NULL;
5771 return &reg_maps->resource_info[idx];
5774 if (reg->type == WINED3DSPR_UAV)
5776 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5778 ERR("Invalid UAV index %u.\n", idx);
5779 return NULL;
5781 return &reg_maps->uav_resource_info[idx];
5784 FIXME("Unhandled register type %#x.\n", reg->type);
5785 return NULL;
5788 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5790 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5791 const struct wined3d_shader_resource_info *resource_info;
5792 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5793 unsigned int resource_idx;
5794 char dst_swizzle[6];
5795 DWORD write_mask;
5797 write_mask = shader_glsl_append_dst(buffer, ins);
5798 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5800 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5801 return;
5802 resource_idx = ins->src[0].reg.idx[0].offset;
5804 shader_addline(buffer, "ivec2(");
5805 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5807 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5808 resource_idx, WINED3D_SAMPLER_DEFAULT);
5809 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5811 else
5813 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5815 if (resource_info->stride)
5816 shader_addline(buffer, " / %u", resource_info->stride);
5817 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5818 shader_addline(buffer, " * 4");
5819 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5822 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5824 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5825 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5828 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5830 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5833 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5835 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5836 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5837 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5838 const struct wined3d_gl_info *gl_info = priv->gl_info;
5839 enum wined3d_shader_resource_type resource_type;
5840 enum wined3d_shader_register_type reg_type;
5841 unsigned int resource_idx, bind_idx, i;
5842 enum wined3d_data_type dst_data_type;
5843 struct glsl_src_param lod_param;
5844 BOOL supports_mipmaps;
5845 char dst_swizzle[6];
5846 DWORD write_mask;
5848 dst_data_type = ins->dst[0].reg.data_type;
5849 if (ins->flags == WINED3DSI_RESINFO_UINT)
5850 dst_data_type = WINED3D_DATA_UINT;
5851 else if (ins->flags)
5852 FIXME("Unhandled flags %#x.\n", ins->flags);
5854 reg_type = ins->src[1].reg.type;
5855 resource_idx = ins->src[1].reg.idx[0].offset;
5856 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5857 if (reg_type == WINED3DSPR_RESOURCE)
5859 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5860 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5861 resource_idx, WINED3D_SAMPLER_DEFAULT);
5863 else
5865 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5866 bind_idx = resource_idx;
5869 if (resource_type >= ARRAY_SIZE(resource_type_info))
5871 ERR("Unexpected resource type %#x.\n", resource_type);
5872 return;
5875 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, dst_data_type);
5876 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5878 if (dst_data_type == WINED3D_DATA_UINT)
5879 shader_addline(buffer, "uvec4(");
5880 else
5881 shader_addline(buffer, "vec4(");
5883 if (reg_type == WINED3DSPR_RESOURCE)
5885 shader_addline(buffer, "textureSize(%s_sampler%u",
5886 shader_glsl_get_prefix(version->type), bind_idx);
5888 else
5890 shader_addline(buffer, "imageSize(%s_image%u",
5891 shader_glsl_get_prefix(version->type), bind_idx);
5894 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5895 if (supports_mipmaps)
5896 shader_addline(buffer, ", %s", lod_param.param_str);
5897 shader_addline(buffer, "), ");
5899 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5900 shader_addline(buffer, "0, ");
5902 if (supports_mipmaps)
5904 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5906 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5907 shader_glsl_get_prefix(version->type), bind_idx);
5909 else
5911 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
5912 shader_addline(buffer, "1");
5915 else
5917 shader_addline(buffer, "1");
5920 shader_addline(buffer, ")%s);\n", dst_swizzle);
5923 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
5925 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5926 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5927 const struct wined3d_gl_info *gl_info = priv->gl_info;
5928 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5929 const struct wined3d_shader_dst_param *dst = ins->dst;
5930 const struct wined3d_shader_src_param *src = ins->src;
5931 enum wined3d_shader_resource_type resource_type;
5932 enum wined3d_data_type dst_data_type;
5933 unsigned int resource_idx, bind_idx;
5934 char dst_swizzle[6];
5935 DWORD write_mask;
5937 dst_data_type = dst->reg.data_type;
5938 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
5939 dst_data_type = WINED3D_DATA_UINT;
5940 else if (ins->flags)
5941 FIXME("Unhandled flags %#x.\n", ins->flags);
5943 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, 0, dst_data_type);
5944 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
5946 if (dst_data_type == WINED3D_DATA_UINT)
5947 shader_addline(buffer, "uvec4(");
5948 else
5949 shader_addline(buffer, "vec4(");
5951 if (src->reg.type == WINED3DSPR_RASTERIZER)
5953 if (gl_info->supported[ARB_SAMPLE_SHADING])
5955 shader_addline(buffer, "gl_NumSamples");
5957 else
5959 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
5960 shader_addline(buffer, "1");
5963 else
5965 resource_idx = src->reg.idx[0].offset;
5966 resource_type = reg_maps->resource_info[resource_idx].type;
5967 if (resource_type >= ARRAY_SIZE(resource_type_info))
5969 ERR("Unexpected resource type %#x.\n", resource_type);
5970 return;
5972 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5974 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
5976 shader_addline(buffer, "textureSamples(%s_sampler%u)",
5977 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
5979 else
5981 FIXME("textureSamples() is not supported.\n");
5982 shader_addline(buffer, "1");
5986 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
5989 static void shader_glsl_interpolate(const struct wined3d_shader_instruction *ins)
5991 const struct wined3d_shader_src_param *input = &ins->src[0];
5992 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5993 struct glsl_src_param sample_param;
5994 char dst_swizzle[6];
5995 DWORD write_mask;
5997 write_mask = shader_glsl_append_dst(buffer, ins);
5998 shader_glsl_get_swizzle(input, FALSE, write_mask, dst_swizzle);
6000 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &sample_param);
6001 shader_addline(buffer, "interpolateAtSample(shader_in.reg%u, %s)%s);\n",
6002 input->reg.idx[0].offset, sample_param.param_str, dst_swizzle);
6005 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6007 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6008 struct glsl_src_param coord_param, lod_param, sample_param;
6009 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6010 struct glsl_sample_function sample_function;
6011 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6012 BOOL has_lod_param;
6014 if (wined3d_shader_instruction_has_texel_offset(ins))
6015 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6017 resource_idx = ins->src[1].reg.idx[0].offset;
6018 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6020 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6022 ERR("Invalid resource index %u.\n", resource_idx);
6023 return;
6025 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6027 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6028 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6029 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6030 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6031 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6033 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6034 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6035 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6037 else
6039 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6040 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6041 "%s", coord_param.param_str);
6043 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6046 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6048 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6049 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6050 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6051 struct glsl_sample_function sample_function;
6052 DWORD flags = 0;
6054 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6055 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6056 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6057 flags |= WINED3D_GLSL_SAMPLE_LOD;
6058 if (wined3d_shader_instruction_has_texel_offset(ins))
6059 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6061 resource_idx = ins->src[1].reg.idx[0].offset;
6062 sampler_idx = ins->src[2].reg.idx[0].offset;
6064 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6065 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6067 switch (ins->handler_idx)
6069 case WINED3DSIH_SAMPLE:
6070 break;
6071 case WINED3DSIH_SAMPLE_B:
6072 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6073 lod_param_str = lod_param.param_str;
6074 break;
6075 case WINED3DSIH_SAMPLE_GRAD:
6076 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6077 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6078 dx_param_str = dx_param.param_str;
6079 dy_param_str = dy_param.param_str;
6080 break;
6081 case WINED3DSIH_SAMPLE_LOD:
6082 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6083 lod_param_str = lod_param.param_str;
6084 break;
6085 default:
6086 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6087 break;
6090 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6091 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6092 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6093 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6096 /* Unless EXT_texture_shadow_lod is available, GLSL doesn't provide a function
6097 * to sample from level zero with depth comparison for array textures and cube
6098 * textures. We use textureGrad*() to implement sample_c_lz in that case. */
6099 static void shader_glsl_gen_sample_c_lz_emulation(const struct wined3d_shader_instruction *ins,
6100 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6101 unsigned int coord_size, const char *coord_param, const char *ref_param)
6103 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6104 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6105 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6106 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6107 char dst_swizzle[6];
6109 WARN("Emitting textureGrad() for sample_c_lz.\n");
6111 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6112 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, sample_function->data_type);
6113 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6114 sample_function->offset_size ? "Offset" : "",
6115 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6116 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6117 if (sample_function->offset_size)
6119 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6120 shader_addline(buffer, ", ");
6121 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6123 shader_addline(buffer, "))%s);\n", dst_swizzle);
6126 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6128 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6129 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6130 const struct wined3d_shader_resource_info *resource_info;
6131 const struct wined3d_gl_info *gl_info = priv->gl_info;
6132 struct glsl_src_param coord_param, compare_param;
6133 struct glsl_sample_function sample_function;
6134 const char *lod_param = NULL;
6135 unsigned int coord_size;
6136 DWORD flags = 0;
6138 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6140 lod_param = "0";
6141 flags |= WINED3D_GLSL_SAMPLE_LOD;
6144 if (wined3d_shader_instruction_has_texel_offset(ins))
6145 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6147 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6148 return;
6149 resource_idx = ins->src[1].reg.idx[0].offset;
6150 sampler_idx = ins->src[2].reg.idx[0].offset;
6152 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6153 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6154 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6155 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6156 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6157 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6158 && !gl_info->supported[EXT_TEXTURE_SHADOW_LOD]
6159 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6160 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6162 shader_glsl_gen_sample_c_lz_emulation(ins, sampler_bind_idx, &sample_function,
6163 coord_size, coord_param.param_str, compare_param.param_str);
6165 else
6167 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6168 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6169 coord_size, coord_param.param_str, compare_param.param_str);
6171 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6174 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6176 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6177 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6178 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6179 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6180 struct glsl_src_param coord_param, compare_param, offset_param;
6181 const struct wined3d_gl_info *gl_info = priv->gl_info;
6182 const struct wined3d_shader_resource_info *resource_info;
6183 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6184 unsigned int coord_size, offset_size;
6185 char dst_swizzle[6];
6186 BOOL has_offset;
6188 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6190 FIXME("OpenGL implementation does not support textureGather.\n");
6191 return;
6194 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6195 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6196 || wined3d_shader_instruction_has_texel_offset(ins);
6198 resource_param_idx =
6199 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6200 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6201 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6202 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6203 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6205 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6206 return;
6208 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6210 ERR("Unexpected resource type %#x.\n", resource_info->type);
6211 return;
6213 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6215 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6216 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, resource_info->data_type);
6218 shader_glsl_add_src_param(ins, &ins->src[0], wined3d_mask_from_size(coord_size), &coord_param);
6220 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6221 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6222 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6224 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6225 shader_addline(buffer, ", %s", compare_param.param_str);
6227 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6229 shader_glsl_add_src_param(ins, &ins->src[1], wined3d_mask_from_size(offset_size), &offset_param);
6230 shader_addline(buffer, ", %s", offset_param.param_str);
6232 else if (has_offset)
6234 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6235 shader_addline(buffer, ", ");
6236 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6238 if (component_idx)
6239 shader_addline(buffer, ", %u", component_idx);
6241 shader_addline(buffer, ")%s);\n", dst_swizzle);
6244 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6246 /* FIXME: Make this work for more than just 2D textures */
6247 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6248 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6250 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6252 char dst_mask[6];
6254 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6255 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6256 ins->dst[0].reg.idx[0].offset, dst_mask);
6258 else
6260 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6261 DWORD reg = ins->src[0].reg.idx[0].offset;
6262 char dst_swizzle[6];
6264 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6266 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6268 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6269 struct glsl_src_param div_param;
6270 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6272 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6274 if (mask_size > 1)
6275 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6276 else
6277 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6279 else
6281 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6286 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6287 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6288 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6289 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6291 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6292 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6293 struct glsl_sample_function sample_function;
6294 struct glsl_src_param src0_param;
6295 UINT mask_size;
6297 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6299 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6300 * scalar, and projected sampling would require 4.
6302 * It is a dependent read - not valid with conditional NP2 textures
6304 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6305 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6307 switch(mask_size)
6309 case 1:
6310 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6311 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6312 break;
6314 case 2:
6315 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6316 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6317 break;
6319 case 3:
6320 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6321 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6322 break;
6324 default:
6325 FIXME("Unexpected mask size %u\n", mask_size);
6326 break;
6328 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6331 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6332 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6333 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6335 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6336 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6337 struct glsl_src_param src0_param;
6338 DWORD dst_mask;
6339 unsigned int mask_size;
6341 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6342 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6343 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6345 if (mask_size > 1) {
6346 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6347 } else {
6348 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6352 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6353 * Calculate the depth as dst.x / dst.y */
6354 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6356 struct glsl_dst_param dst_param;
6358 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6360 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6361 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6362 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6363 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6364 * >= 1.0 or < 0.0
6366 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6367 dst_param.reg_name, dst_param.reg_name);
6370 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6371 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6372 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6373 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6375 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6377 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6378 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
6379 struct glsl_src_param src0_param;
6381 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6383 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6384 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6387 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6388 * Calculate the 1st of a 2-row matrix multiplication. */
6389 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6391 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6392 DWORD reg = ins->dst[0].reg.idx[0].offset;
6393 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6394 struct glsl_src_param src0_param;
6396 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6397 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6400 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6401 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6402 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6404 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6405 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6406 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6407 DWORD reg = ins->dst[0].reg.idx[0].offset;
6408 struct glsl_src_param src0_param;
6410 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6411 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6412 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6415 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6417 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6418 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6419 struct glsl_sample_function sample_function;
6420 DWORD reg = ins->dst[0].reg.idx[0].offset;
6421 struct glsl_src_param src0_param;
6423 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6424 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6426 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6428 /* Sample the texture using the calculated coordinates */
6429 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6430 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6433 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6434 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6435 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6437 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6438 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6439 struct glsl_sample_function sample_function;
6440 DWORD reg = ins->dst[0].reg.idx[0].offset;
6441 struct glsl_src_param src0_param;
6443 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6444 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6446 /* Dependent read, not valid with conditional NP2 */
6447 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6449 /* Sample the texture using the calculated coordinates */
6450 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6451 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6453 tex_mx->current_row = 0;
6456 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6457 * Perform the 3rd row of a 3x3 matrix multiply */
6458 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6460 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6461 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6462 DWORD reg = ins->dst[0].reg.idx[0].offset;
6463 struct glsl_src_param src0_param;
6464 char dst_mask[6];
6466 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6468 shader_glsl_append_dst(ins->ctx->buffer, ins);
6469 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6470 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6472 tex_mx->current_row = 0;
6475 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6476 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6477 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6479 struct glsl_src_param src0_param;
6480 struct glsl_src_param src1_param;
6481 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6482 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6483 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6484 struct glsl_sample_function sample_function;
6485 DWORD reg = ins->dst[0].reg.idx[0].offset;
6486 char coord_mask[6];
6488 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6489 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6491 /* Perform the last matrix multiply operation */
6492 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6493 /* Reflection calculation */
6494 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6496 /* Dependent read, not valid with conditional NP2 */
6497 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6498 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6500 /* Sample the texture */
6501 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6502 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6503 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6505 tex_mx->current_row = 0;
6508 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6509 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6510 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6512 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6513 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6514 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6515 struct glsl_sample_function sample_function;
6516 DWORD reg = ins->dst[0].reg.idx[0].offset;
6517 struct glsl_src_param src0_param;
6518 char coord_mask[6];
6520 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6522 /* Perform the last matrix multiply operation */
6523 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6525 /* Construct the eye-ray vector from w coordinates */
6526 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6527 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6528 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6530 /* Dependent read, not valid with conditional NP2 */
6531 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6532 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6534 /* Sample the texture using the calculated coordinates */
6535 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6536 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6537 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6539 tex_mx->current_row = 0;
6542 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6543 * Apply a fake bump map transform.
6544 * texbem is pshader <= 1.3 only, this saves a few version checks
6546 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6548 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6549 struct glsl_sample_function sample_function;
6550 struct glsl_src_param coord_param;
6551 DWORD sampler_idx;
6552 DWORD mask;
6553 DWORD flags;
6554 char coord_mask[6];
6556 sampler_idx = ins->dst[0].reg.idx[0].offset;
6557 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6558 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6560 /* Dependent read, not valid with conditional NP2 */
6561 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6562 mask = sample_function.coord_mask;
6564 shader_glsl_write_mask_to_str(mask, coord_mask);
6566 /* With projected textures, texbem only divides the static texture coord,
6567 * not the displacement, so we can't let GL handle this. */
6568 if (flags & WINED3D_PSARGS_PROJECTED)
6570 DWORD div_mask=0;
6571 char coord_div_mask[3];
6572 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6574 case WINED3D_TTFF_COUNT1:
6575 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6576 break;
6577 case WINED3D_TTFF_COUNT2:
6578 div_mask = WINED3DSP_WRITEMASK_1;
6579 break;
6580 case WINED3D_TTFF_COUNT3:
6581 div_mask = WINED3DSP_WRITEMASK_2;
6582 break;
6583 case WINED3D_TTFF_COUNT4:
6584 case WINED3D_TTFF_DISABLE:
6585 div_mask = WINED3DSP_WRITEMASK_3;
6586 break;
6588 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6589 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6592 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6594 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6595 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6596 coord_param.param_str, coord_mask);
6598 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6600 struct glsl_src_param luminance_param;
6601 struct glsl_dst_param dst_param;
6603 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6604 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6606 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6607 dst_param.reg_name, dst_param.mask_str,
6608 luminance_param.param_str, sampler_idx, sampler_idx);
6610 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6613 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6615 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6616 struct glsl_src_param src0_param, src1_param;
6618 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6619 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6621 shader_glsl_append_dst(ins->ctx->buffer, ins);
6622 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6623 src0_param.param_str, sampler_idx, src1_param.param_str);
6626 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6627 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6628 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6630 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6631 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6632 struct glsl_sample_function sample_function;
6633 struct wined3d_string_buffer *reg_name;
6635 reg_name = string_buffer_get(priv->string_buffers);
6636 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6638 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6639 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6640 "%s.wx", reg_name->buffer);
6641 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6643 string_buffer_release(priv->string_buffers, reg_name);
6646 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6647 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6648 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6650 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6651 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6652 struct glsl_sample_function sample_function;
6653 struct wined3d_string_buffer *reg_name;
6655 reg_name = string_buffer_get(priv->string_buffers);
6656 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6658 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6659 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6660 "%s.yz", reg_name->buffer);
6661 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6663 string_buffer_release(priv->string_buffers, reg_name);
6666 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6667 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6668 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6670 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6671 struct glsl_sample_function sample_function;
6672 struct glsl_src_param src0_param;
6674 /* Dependent read, not valid with conditional NP2 */
6675 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6676 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6678 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6679 "%s", src0_param.param_str);
6680 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6683 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6684 * If any of the first 3 components are < 0, discard this pixel */
6685 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6687 if (ins->ctx->reg_maps->shader_version.major >= 4)
6689 shader_glsl_generate_condition(ins);
6690 shader_addline(ins->ctx->buffer, " discard;\n");
6692 else
6694 struct glsl_dst_param dst_param;
6696 /* The argument is a destination parameter, and no writemasks are allowed */
6697 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6699 /* 2.0 shaders compare all 4 components in texkill. */
6700 if (ins->ctx->reg_maps->shader_version.major >= 2)
6701 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6702 /* 1.x shaders only compare the first 3 components, probably due to
6703 * the nature of the texkill instruction as a tex* instruction, and
6704 * phase, which kills all .w components. Even if all 4 components are
6705 * defined, only the first 3 are used. */
6706 else
6707 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6711 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6712 * dst = dot2(src0, src1) + src2 */
6713 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6715 struct glsl_src_param src0_param;
6716 struct glsl_src_param src1_param;
6717 struct glsl_src_param src2_param;
6718 DWORD write_mask;
6719 unsigned int mask_size;
6721 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6722 mask_size = shader_glsl_get_write_mask_size(write_mask);
6724 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6725 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6726 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6728 if (mask_size > 1) {
6729 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6730 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6731 } else {
6732 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6733 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6737 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6738 const struct wined3d_shader_signature *input_signature,
6739 const struct wined3d_shader_reg_maps *reg_maps,
6740 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6742 unsigned int i;
6744 for (i = 0; i < input_signature->element_count; ++i)
6746 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6747 const char *semantic_name;
6748 UINT semantic_idx;
6749 char reg_mask[6];
6751 /* Unused */
6752 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6753 continue;
6755 semantic_name = input->semantic_name;
6756 semantic_idx = input->semantic_idx;
6757 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6759 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6761 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6763 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6764 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6766 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6768 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6770 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6772 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6773 input->register_idx, reg_mask);
6775 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6777 if (gl_info->supported[ARB_SAMPLE_SHADING])
6778 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6779 input->register_idx, reg_mask);
6780 else
6781 FIXME("ARB_sample_shading is not supported.\n");
6783 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6785 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6786 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6787 input->register_idx, reg_mask);
6788 else
6789 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6791 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6793 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6794 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6795 input->register_idx, reg_mask);
6796 else
6797 FIXME("ARB_viewport_array is not supported.\n");
6799 else
6801 if (input->sysval_semantic)
6802 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6803 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6804 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6805 shader_glsl_shader_input_name(gl_info),
6806 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6809 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6811 if (args->pointsprite)
6812 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6813 shader->u.ps.input_reg_map[input->register_idx]);
6814 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6815 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6816 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6817 needs_legacy_glsl_syntax(gl_info)
6818 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6819 else
6820 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6821 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6823 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6825 if (!semantic_idx)
6826 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6827 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6828 else if (semantic_idx == 1)
6829 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6830 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6831 else
6832 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6833 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6835 else
6837 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6838 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6843 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6845 struct glsl_program_key key;
6847 key.vs_id = entry->vs.id;
6848 key.hs_id = entry->hs.id;
6849 key.ds_id = entry->ds.id;
6850 key.gs_id = entry->gs.id;
6851 key.ps_id = entry->ps.id;
6852 key.cs_id = entry->cs.id;
6854 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6856 ERR("Failed to insert program entry.\n");
6860 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6861 const struct glsl_program_key *key)
6863 struct wine_rb_entry *entry;
6865 entry = wine_rb_get(&priv->program_lookup, key);
6866 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6869 /* Context activation is done by the caller. */
6870 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6871 struct glsl_shader_prog_link *entry)
6873 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6875 GL_EXTCALL(glDeleteProgram(entry->id));
6876 if (entry->vs.id)
6877 list_remove(&entry->vs.shader_entry);
6878 if (entry->hs.id)
6879 list_remove(&entry->hs.shader_entry);
6880 if (entry->ds.id)
6881 list_remove(&entry->ds.shader_entry);
6882 if (entry->gs.id)
6883 list_remove(&entry->gs.shader_entry);
6884 if (entry->ps.id)
6885 list_remove(&entry->ps.shader_entry);
6886 if (entry->cs.id)
6887 list_remove(&entry->cs.shader_entry);
6888 heap_free(entry);
6891 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6892 const struct wined3d_gl_info *gl_info, const DWORD *map,
6893 const struct wined3d_shader_signature *input_signature,
6894 const struct wined3d_shader_reg_maps *reg_maps_in,
6895 const struct wined3d_shader_signature *output_signature,
6896 const struct wined3d_shader_reg_maps *reg_maps_out)
6898 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6899 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6900 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6901 unsigned int in_count = vec4_varyings(3, gl_info);
6902 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6903 DWORD in_idx, *set = NULL;
6904 unsigned int i, j;
6905 char reg_mask[6];
6907 set = heap_calloc(max_varyings, sizeof(*set));
6909 for (i = 0; i < input_signature->element_count; ++i)
6911 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6913 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6914 continue;
6916 in_idx = map[input->register_idx];
6917 /* Declared, but not read register */
6918 if (in_idx == ~0u)
6919 continue;
6920 if (in_idx >= max_varyings)
6922 FIXME("More input varyings declared than supported, expect issues.\n");
6923 continue;
6926 if (in_idx == in_count)
6927 string_buffer_sprintf(destination, "gl_FrontColor");
6928 else if (in_idx == in_count + 1)
6929 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6930 else
6931 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6933 if (!set[in_idx])
6934 set[in_idx] = ~0u;
6936 for (j = 0; j < output_signature->element_count; ++j)
6938 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6939 DWORD mask;
6941 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6942 || input->semantic_idx != output->semantic_idx
6943 || strcmp(input->semantic_name, output->semantic_name)
6944 || !(mask = input->mask & output->mask))
6945 continue;
6947 if (set[in_idx] == ~0u)
6948 set[in_idx] = 0;
6949 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6950 shader_glsl_write_mask_to_str(mask, reg_mask);
6952 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6953 destination->buffer, reg_mask, output->register_idx, reg_mask);
6957 for (i = 0; i < max_varyings; ++i)
6959 unsigned int size;
6961 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6962 continue;
6964 if (set[i] == ~0u)
6965 set[i] = 0;
6967 size = 0;
6968 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6969 reg_mask[size++] = 'x';
6970 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6971 reg_mask[size++] = 'y';
6972 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6973 reg_mask[size++] = 'z';
6974 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6975 reg_mask[size++] = 'w';
6976 reg_mask[size] = '\0';
6978 if (i == in_count)
6979 string_buffer_sprintf(destination, "gl_FrontColor");
6980 else if (i == in_count + 1)
6981 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6982 else
6983 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6985 if (size == 1)
6986 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6987 else
6988 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6991 heap_free(set);
6992 string_buffer_release(&priv->string_buffers, destination);
6995 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6996 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6997 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6998 BOOL rasterizer_setup)
7000 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7001 char reg_mask[6];
7002 unsigned int i;
7004 for (i = 0; i < output_signature->element_count; ++i)
7006 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7008 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7009 continue;
7011 if (output->stream_idx)
7012 continue;
7014 if (output->register_idx >= input_count)
7015 continue;
7017 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7019 shader_addline(buffer,
7020 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7021 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7025 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7026 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7028 unsigned int i, clip_or_cull_index;
7029 const char *name;
7030 char reg_mask[6];
7032 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7033 /* Assign consecutive indices starting from 0. */
7034 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7035 for (i = 0; i < 4; ++i)
7037 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7038 continue;
7040 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7041 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7042 name, clip_or_cull_index, element->register_idx, reg_mask);
7043 ++clip_or_cull_index;
7047 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7048 const struct wined3d_gl_info *gl_info, const DWORD *map,
7049 const struct wined3d_shader_signature *input_signature,
7050 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7051 const struct wined3d_shader_signature *output_signature,
7052 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7054 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7055 const char *semantic_name;
7056 unsigned int semantic_idx;
7057 char reg_mask[6];
7058 unsigned int i;
7060 /* First, sort out position and point size system values. */
7061 for (i = 0; i < output_signature->element_count; ++i)
7063 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7065 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7066 continue;
7068 if (output->stream_idx)
7069 continue;
7071 semantic_name = output->semantic_name;
7072 semantic_idx = output->semantic_idx;
7073 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7075 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7077 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7078 reg_mask, output->register_idx, reg_mask);
7080 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7082 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7083 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7085 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7087 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7088 output->register_idx, reg_mask);
7090 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7092 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7093 output->register_idx, reg_mask);
7095 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7097 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7099 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7101 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7103 else if (output->sysval_semantic)
7105 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7109 /* Then, setup the pixel shader input. */
7110 if (reg_maps_out->shader_version.major < 4)
7111 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7112 output_signature, reg_maps_out);
7113 else
7114 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7117 /* Context activation is done by the caller. */
7118 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7119 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7120 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7122 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7123 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7124 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7125 const char *semantic_name;
7126 UINT semantic_idx;
7127 char reg_mask[6];
7128 unsigned int i;
7129 GLuint ret;
7131 string_buffer_clear(buffer);
7133 shader_glsl_add_version_declaration(buffer, gl_info);
7135 shader_addline(buffer, "invariant gl_Position;\n");
7137 if (per_vertex_point_size)
7139 shader_addline(buffer, "uniform struct\n{\n");
7140 shader_addline(buffer, " float size_min;\n");
7141 shader_addline(buffer, " float size_max;\n");
7142 shader_addline(buffer, "} ffp_point;\n");
7145 if (ps_major < 3)
7147 DWORD colors_written_mask[2] = {0};
7148 DWORD texcoords_written_mask[WINED3D_MAX_TEXTURES] = {0};
7150 if (!legacy_syntax)
7152 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7153 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7154 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7155 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7158 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7160 for (i = 0; i < vs->output_signature.element_count; ++i)
7162 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7163 DWORD write_mask;
7165 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7166 continue;
7168 semantic_name = output->semantic_name;
7169 semantic_idx = output->semantic_idx;
7170 write_mask = output->mask;
7171 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7173 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7175 if (legacy_syntax)
7176 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7177 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7178 else
7179 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7180 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7182 colors_written_mask[semantic_idx] = write_mask;
7184 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7186 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7187 reg_mask, output->register_idx, reg_mask);
7189 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7191 if (semantic_idx < WINED3D_MAX_TEXTURES)
7193 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7194 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7195 semantic_idx, reg_mask, output->register_idx, reg_mask);
7196 texcoords_written_mask[semantic_idx] = write_mask;
7199 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7201 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7202 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7204 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7206 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7207 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7208 output->register_idx, reg_mask[1]);
7212 for (i = 0; i < 2; ++i)
7214 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7216 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7217 if (!i)
7218 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7219 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7220 reg_mask, reg_mask);
7221 else
7222 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7223 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7224 reg_mask, reg_mask);
7227 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
7229 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7230 continue;
7232 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7234 if (!shader_glsl_full_ffp_varyings(gl_info) && !texcoords_written_mask[i])
7235 continue;
7237 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7238 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7239 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7243 else
7245 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7247 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7248 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7249 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7250 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7253 shader_addline(buffer, "}\n");
7255 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7256 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7257 shader_glsl_compile(gl_info, ret, buffer->buffer);
7259 return ret;
7262 static void shader_glsl_generate_stream_output_setup(struct wined3d_string_buffer *buffer,
7263 const struct wined3d_shader *shader)
7265 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
7266 unsigned int i, register_idx, component_idx;
7268 shader_addline(buffer, "out shader_in_out\n{\n");
7269 for (i = 0; i < so_desc->element_count; ++i)
7271 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7273 if (e->stream_idx)
7275 FIXME("Unhandled stream %u.\n", e->stream_idx);
7276 continue;
7278 if (!e->semantic_name)
7279 continue;
7280 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7281 continue;
7283 if (component_idx || e->component_count != 4)
7285 if (e->component_count == 1)
7286 shader_addline(buffer, "float");
7287 else
7288 shader_addline(buffer, "vec%u", e->component_count);
7290 shader_addline(buffer, " reg%u_%u_%u;\n",
7291 register_idx, component_idx, component_idx + e->component_count - 1);
7293 else
7295 shader_addline(buffer, "vec4 reg%u;\n", register_idx);
7298 shader_addline(buffer, "} shader_out;\n");
7300 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7301 shader->limits->packed_output);
7302 for (i = 0; i < so_desc->element_count; ++i)
7304 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7306 if (e->stream_idx)
7308 FIXME("Unhandled stream %u.\n", e->stream_idx);
7309 continue;
7311 if (!e->semantic_name)
7312 continue;
7313 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7314 continue;
7316 if (component_idx || e->component_count != 4)
7318 DWORD write_mask;
7319 char str_mask[6];
7321 write_mask = wined3d_mask_from_size(e->component_count) << component_idx;
7322 shader_glsl_write_mask_to_str(write_mask, str_mask);
7323 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7324 register_idx, component_idx, component_idx + e->component_count - 1,
7325 register_idx, str_mask);
7327 else
7329 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n", register_idx, register_idx);
7332 shader_addline(buffer, "}\n");
7335 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7336 const struct wined3d_shader *shader, unsigned int input_count,
7337 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const DWORD *interpolation_mode)
7339 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7340 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7342 if (rasterizer_setup)
7343 input_count = min(vec4_varyings(4, gl_info), input_count);
7345 if (input_count)
7346 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7348 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7349 prefix, shader->limits->packed_output);
7351 if (rasterizer_setup)
7352 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7353 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7354 else
7355 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7356 &shader->reg_maps, "shader_out", rasterizer_setup);
7358 shader_addline(buffer, "}\n");
7361 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7362 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7363 const char *reg_mask)
7365 if (!constant->sysval_semantic)
7367 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7368 return;
7371 switch (constant->sysval_semantic)
7373 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7374 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7375 case WINED3D_SV_TESS_FACTOR_LINEDET:
7376 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7377 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7378 break;
7379 case WINED3D_SV_TESS_FACTOR_QUADINT:
7380 case WINED3D_SV_TESS_FACTOR_TRIINT:
7381 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7382 break;
7383 default:
7384 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7385 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7389 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7390 const struct wined3d_shader_signature *signature, BOOL input_setup)
7392 unsigned int i, register_count, user_constant_index, user_constant_count;
7394 register_count = user_constant_count = 0;
7395 for (i = 0; i < signature->element_count; ++i)
7397 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7398 register_count = max(constant->register_idx + 1, register_count);
7399 if (!constant->sysval_semantic)
7400 ++user_constant_count;
7403 if (user_constant_count)
7404 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7405 input_setup ? "in" : "out", user_constant_count);
7406 if (input_setup)
7407 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7409 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7410 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7412 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7413 char reg_mask[6];
7415 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7417 if (input_setup)
7418 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7419 else
7420 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7422 shader_addline(buffer, " = ");
7424 if (input_setup)
7425 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7426 else
7427 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7429 shader_addline(buffer, ";\n");
7431 shader_addline(buffer, "}\n");
7434 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7435 const struct wined3d_gl_info *gl_info)
7437 const char *output = get_fragment_output(gl_info);
7439 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7440 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7441 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7442 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7443 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7444 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7447 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7448 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7450 const char *output = get_fragment_output(gl_info);
7452 switch (mode)
7454 case WINED3D_FFP_PS_FOG_OFF:
7455 return;
7457 case WINED3D_FFP_PS_FOG_LINEAR:
7458 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7459 break;
7461 case WINED3D_FFP_PS_FOG_EXP:
7462 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7463 break;
7465 case WINED3D_FFP_PS_FOG_EXP2:
7466 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7467 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7468 break;
7470 default:
7471 ERR("Invalid fog mode %#x.\n", mode);
7472 return;
7475 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7476 output, output);
7479 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7480 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7482 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7483 * flipping all the operators here, just negate the comparison below. */
7484 static const char * const comparison_operator[] =
7486 "", /* WINED3D_CMP_NEVER */
7487 "<", /* WINED3D_CMP_LESS */
7488 "==", /* WINED3D_CMP_EQUAL */
7489 "<=", /* WINED3D_CMP_LESSEQUAL */
7490 ">", /* WINED3D_CMP_GREATER */
7491 "!=", /* WINED3D_CMP_NOTEQUAL */
7492 ">=", /* WINED3D_CMP_GREATEREQUAL */
7493 "" /* WINED3D_CMP_ALWAYS */
7496 if (alpha_func == WINED3D_CMP_ALWAYS)
7497 return;
7499 if (alpha_func != WINED3D_CMP_NEVER)
7500 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7501 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7502 shader_addline(buffer, " discard;\n");
7505 static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *buffer,
7506 const char *colour, const char *colour_key_low, const char *colour_key_high)
7508 shader_addline(buffer, "if (all(greaterThanEqual(%s, %s)) && all(lessThan(%s, %s)))\n",
7509 colour, colour_key_low, colour, colour_key_high);
7510 shader_addline(buffer, " discard;\n");
7513 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7514 const struct wined3d_gl_info *gl_info)
7516 if (gl_info->supported[ARB_CULL_DISTANCE])
7517 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7518 if (gl_info->supported[ARB_GPU_SHADER5])
7519 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7520 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7521 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7522 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7523 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7524 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7525 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7526 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7527 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7528 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7529 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7530 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7531 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7532 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7533 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7534 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7535 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7536 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7537 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7538 if (gl_info->supported[ARB_TEXTURE_GATHER])
7539 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7540 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7541 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7542 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7543 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7544 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7545 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7546 if (gl_info->supported[EXT_GPU_SHADER4])
7547 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7548 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7549 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7550 if (gl_info->supported[EXT_TEXTURE_SHADOW_LOD])
7551 shader_addline(buffer, "#extension GL_EXT_texture_shadow_lod : enable\n");
7554 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7555 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7556 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7558 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7559 struct wined3d_string_buffer *src, *assignment;
7560 enum wined3d_data_type dst_data_type;
7561 const char *swizzle;
7562 unsigned int i;
7564 if (output_signature->element_count)
7566 src = string_buffer_get(string_buffers);
7567 assignment = string_buffer_get(string_buffers);
7568 for (i = 0; i < output_signature->element_count; ++i)
7570 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7572 /* register_idx is set to ~0u for non-color outputs. */
7573 if (output->register_idx == ~0u)
7574 continue;
7575 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7577 FIXME("Unhandled component type %#x.\n", output->component_type);
7578 continue;
7580 dst_data_type = component_type_info[output->component_type].data_type;
7581 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7582 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7583 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT, 4);
7584 swizzle = args->rt_alpha_swizzle & (1u << output->semantic_idx) ? ".argb" : "";
7585 shader_addline(buffer, "%s%s;\n", assignment->buffer, swizzle);
7587 string_buffer_release(string_buffers, src);
7588 string_buffer_release(string_buffers, assignment);
7590 else
7592 DWORD mask = shader->reg_maps.rt_mask;
7594 while (mask)
7596 i = wined3d_bit_scan(&mask);
7597 swizzle = args->rt_alpha_swizzle & (1u << i) ? ".argb" : "";
7598 shader_addline(buffer, "color_out%u = ps_out[%u]%s;\n", i, i, swizzle);
7603 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7604 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7605 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7607 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7609 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7610 if (reg_maps->shader_version.major < 2)
7611 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7613 if (args->srgb_correction)
7614 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7616 /* SM < 3 does not replace the fog stage. */
7617 if (reg_maps->shader_version.major < 3)
7618 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7620 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7622 if (reg_maps->sample_mask)
7623 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7625 if (!use_legacy_fragment_output(gl_info))
7626 shader_glsl_generate_color_output(buffer, gl_info, shader, args, string_buffers);
7629 /* Context activation is done by the caller. */
7630 static GLuint shader_glsl_generate_fragment_shader(const struct wined3d_context_gl *context_gl,
7631 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7632 const struct wined3d_shader *shader, const struct ps_compile_args *args,
7633 struct ps_np2fixup_info *np2fixup_info)
7635 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7636 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7637 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7638 const char *prefix = shader_glsl_get_prefix(version->type);
7639 BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7640 unsigned int i, extra_constants_needed = 0;
7641 struct shader_glsl_ctx_priv priv_ctx;
7642 GLuint shader_id;
7643 DWORD map;
7645 memset(&priv_ctx, 0, sizeof(priv_ctx));
7646 priv_ctx.gl_info = gl_info;
7647 priv_ctx.cur_ps_args = args;
7648 priv_ctx.cur_np2fixup_info = np2fixup_info;
7649 priv_ctx.string_buffers = string_buffers;
7651 shader_glsl_add_version_declaration(buffer, gl_info);
7653 shader_glsl_enable_extensions(buffer, gl_info);
7654 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7655 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7656 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7657 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7658 if (shader_glsl_use_explicit_attrib_location(gl_info))
7659 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7660 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7661 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7662 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7663 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7664 if (gl_info->supported[ARB_SAMPLE_SHADING])
7665 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7666 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7667 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7668 /* The spec says that it doesn't have to be explicitly enabled, but the
7669 * nvidia drivers write a warning if we don't do so. */
7670 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7671 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7673 /* Base Declarations */
7674 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7676 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7678 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7679 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7680 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7681 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7684 /* Declare uniforms for NP2 texcoord fixup:
7685 * This is NOT done inside the loop that declares the texture samplers
7686 * since the NP2 fixup code is currently only used for the GeforceFX
7687 * series and when forcing the ARB_npot extension off. Modern cards just
7688 * skip the code anyway, so put it inside a separate loop. */
7689 if (args->np2_fixup)
7691 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7692 unsigned int cur = 0;
7694 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7695 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7696 * samplerNP2Fixup stores texture dimensions and is updated through
7697 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7699 for (i = 0; i < shader->limits->sampler; ++i)
7701 enum wined3d_shader_resource_type resource_type;
7703 resource_type = pixelshader_get_resource_type(reg_maps, i, args->tex_types);
7705 if (!resource_type || !(args->np2_fixup & (1u << i)))
7706 continue;
7708 if (resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7710 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7711 continue;
7714 fixup->idx[i] = cur++;
7717 fixup->num_consts = (cur + 1) >> 1;
7718 fixup->active = args->np2_fixup;
7719 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7722 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7724 shader_addline(buffer, "uniform struct\n{\n");
7725 shader_addline(buffer, " vec4 color;\n");
7726 shader_addline(buffer, " float density;\n");
7727 shader_addline(buffer, " float end;\n");
7728 shader_addline(buffer, " float scale;\n");
7729 shader_addline(buffer, "} ffp_fog;\n");
7731 if (legacy_syntax)
7733 if (glsl_is_color_reg_read(shader, 0))
7734 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7735 if (glsl_is_color_reg_read(shader, 1))
7736 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7737 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7738 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7740 else
7742 if (glsl_is_color_reg_read(shader, 0))
7743 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7744 if (glsl_is_color_reg_read(shader, 1))
7745 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7746 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7747 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7748 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7752 if (version->major >= 3)
7754 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7756 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7757 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7758 shader->u.ps.interpolation_mode, version->major >= 4);
7759 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7762 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
7764 if (!(map & 1))
7765 continue;
7767 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7769 if (reg_maps->luminanceparams & (1u << i))
7771 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7772 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7773 extra_constants_needed++;
7776 extra_constants_needed++;
7779 if (args->srgb_correction)
7781 shader_addline(buffer, "const vec4 srgb_const0 = ");
7782 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
7783 shader_addline(buffer, ";\n");
7784 shader_addline(buffer, "const vec4 srgb_const1 = ");
7785 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
7786 shader_addline(buffer, ";\n");
7788 if ((reg_maps->usesdsy && wined3d_settings.offscreen_rendering_mode != ORM_FBO)
7789 || (reg_maps->vpos && !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]))
7791 ++extra_constants_needed;
7792 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7794 if (reg_maps->vpos)
7796 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7798 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7799 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7800 args->y_correction ? "origin_upper_left, " : "");
7801 else if (args->y_correction)
7802 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7804 shader_addline(buffer, "vec4 vpos;\n");
7807 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7808 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7810 if (!use_legacy_fragment_output(gl_info))
7812 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7814 if (args->dual_source_blend)
7815 shader_addline(buffer, "vec4 ps_out[2];\n");
7816 else
7817 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7818 if (output_signature->element_count)
7820 for (i = 0; i < output_signature->element_count; ++i)
7822 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7824 if (output->register_idx == ~0u)
7825 continue;
7826 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7828 FIXME("Unhandled component type %#x.\n", output->component_type);
7829 continue;
7831 if (shader_glsl_use_explicit_attrib_location(gl_info))
7833 if (args->dual_source_blend)
7834 shader_addline(buffer, "layout(location = 0, index = %u) ", output->semantic_idx);
7835 else
7836 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7838 shader_addline(buffer, "out %s4 color_out%u;\n",
7839 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7842 else
7844 DWORD mask = reg_maps->rt_mask;
7846 while (mask)
7848 i = wined3d_bit_scan(&mask);
7849 if (shader_glsl_use_explicit_attrib_location(gl_info))
7851 if (args->dual_source_blend)
7852 shader_addline(buffer, "layout(location = 0, index = %u) ", i);
7853 else
7854 shader_addline(buffer, "layout(location = %u) ", i);
7856 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7861 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7862 FIXME("Insufficient uniforms to run this shader.\n");
7864 if (shader->u.ps.force_early_depth_stencil)
7865 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7867 shader_addline(buffer, "void main()\n{\n");
7869 if (reg_maps->sample_mask)
7870 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7872 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7873 * add approximately 0.5. This causes off-by-one problems as spotted by
7874 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7875 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7876 * causes precision troubles when we just subtract 0.5.
7878 * To deal with that, just floor() the position. This will eliminate the
7879 * fraction on all cards.
7881 * TODO: Test how this behaves with multisampling.
7883 * An advantage of floor is that it works even if the driver doesn't add
7884 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7885 * to return in gl_FragCoord, even though coordinates specify the pixel
7886 * centers instead of the pixel corners. This code will behave correctly
7887 * on drivers that returns integer values. */
7888 if (reg_maps->vpos)
7890 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7891 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7892 else if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7893 shader_addline(buffer,
7894 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7895 else
7896 shader_addline(buffer,
7897 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7900 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7902 unsigned int i;
7903 WORD map = reg_maps->texcoord;
7905 if (legacy_syntax)
7907 if (glsl_is_color_reg_read(shader, 0))
7908 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7909 if (glsl_is_color_reg_read(shader, 1))
7910 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7913 for (i = 0; map; map >>= 1, ++i)
7915 if (map & 1)
7917 if (args->pointsprite)
7918 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7919 else if (args->texcoords_initialized & (1u << i))
7920 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7921 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7922 else
7923 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7924 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7928 if (legacy_syntax)
7929 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7932 /* Pack 3.0 inputs */
7933 if (reg_maps->shader_version.major >= 3)
7934 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7935 reg_maps->shader_version.major >= 4);
7937 /* Base Shader Body */
7938 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7939 return 0;
7941 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7942 if (reg_maps->shader_version.major < 4)
7943 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
7945 shader_addline(buffer, "}\n");
7947 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7948 TRACE("Compiling shader object %u.\n", shader_id);
7949 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7951 return shader_id;
7954 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7955 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7956 const struct vs_compile_args *args)
7958 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7959 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7960 unsigned int i;
7962 /* Unpack outputs. */
7963 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7965 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7966 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7967 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7968 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7970 if (reg_maps->shader_version.major < 3)
7972 if (args->fog_src == VS_FOG_Z)
7973 shader_addline(buffer, "%s = gl_Position.z;\n",
7974 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7975 else if (!reg_maps->fog)
7976 shader_addline(buffer, "%s = 0.0;\n",
7977 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7980 /* We always store the clipplanes without y inversion. */
7981 if (args->clip_enabled)
7983 if (legacy_syntax)
7984 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7985 else
7986 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7987 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7990 if (args->point_size && !args->per_vertex_point_size)
7991 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7993 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7994 shader_glsl_fixup_position(buffer, FALSE);
7997 /* Context activation is done by the caller. */
7998 static GLuint shader_glsl_generate_vertex_shader(const struct wined3d_context_gl *context_gl,
7999 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
8001 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8002 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8003 const struct wined3d_shader_version *version = &reg_maps->shader_version;
8004 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8005 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8006 struct shader_glsl_ctx_priv priv_ctx;
8007 GLuint shader_id;
8008 unsigned int i;
8010 memset(&priv_ctx, 0, sizeof(priv_ctx));
8011 priv_ctx.gl_info = gl_info;
8012 priv_ctx.cur_vs_args = args;
8013 priv_ctx.string_buffers = string_buffers;
8015 shader_glsl_add_version_declaration(buffer, gl_info);
8017 shader_glsl_enable_extensions(buffer, gl_info);
8018 if (gl_info->supported[ARB_DRAW_INSTANCED])
8019 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8020 if (shader_glsl_use_explicit_attrib_location(gl_info))
8021 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8022 if (gl_info->supported[ARB_SHADER_VIEWPORT_LAYER_ARRAY])
8023 shader_addline(buffer, "#extension GL_ARB_shader_viewport_layer_array : enable\n");
8025 /* Base Declarations */
8026 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8028 for (i = 0; i < shader->input_signature.element_count; ++i)
8029 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8031 if (args->point_size && !args->per_vertex_point_size)
8033 shader_addline(buffer, "uniform struct\n{\n");
8034 shader_addline(buffer, " float size;\n");
8035 shader_addline(buffer, " float size_min;\n");
8036 shader_addline(buffer, " float size_max;\n");
8037 shader_addline(buffer, "} ffp_point;\n");
8040 if (!needs_legacy_glsl_syntax(gl_info))
8042 if (args->clip_enabled)
8043 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8045 if (version->major < 3)
8047 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8048 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8049 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
8050 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8054 if (version->major < 4)
8055 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8057 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8058 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8060 if (reg_maps->shader_version.major >= 4)
8061 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8062 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8064 shader_addline(buffer, "void main()\n{\n");
8066 if (reg_maps->input_rel_addressing)
8068 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8069 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8070 for (i = 0; i < shader->input_signature.element_count; ++i)
8072 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8073 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8077 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8078 return 0;
8080 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8081 if (reg_maps->shader_version.major < 4)
8082 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8084 shader_addline(buffer, "}\n");
8086 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8087 TRACE("Compiling shader object %u.\n", shader_id);
8088 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8090 return shader_id;
8093 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8094 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8096 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8097 char reg_mask[6];
8098 unsigned int i;
8100 for (i = 0; i < output_signature->element_count; ++i)
8102 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8104 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8105 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8106 output->register_idx, reg_mask, output->register_idx, reg_mask);
8110 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8111 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8112 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8113 const char *phase_name, unsigned phase_idx)
8115 unsigned int i;
8116 HRESULT hr;
8118 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8119 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8120 for (i = 0; i < phase->temporary_count; ++i)
8121 shader_addline(buffer, "vec4 R%u;\n", i);
8122 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8123 shader_addline(buffer, "}\n");
8124 return hr;
8127 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8128 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8130 if (phase->instance_count)
8132 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8133 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8134 shader_addline(buffer, "}\n");
8136 else
8138 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8142 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context_gl *context_gl,
8143 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8145 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8146 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8147 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8148 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8149 const struct wined3d_hull_shader *hs = &shader->u.hs;
8150 const struct wined3d_shader_phase *phase;
8151 struct shader_glsl_ctx_priv priv_ctx;
8152 GLuint shader_id;
8153 unsigned int i;
8155 memset(&priv_ctx, 0, sizeof(priv_ctx));
8156 priv_ctx.gl_info = gl_info;
8157 priv_ctx.string_buffers = string_buffers;
8159 shader_glsl_add_version_declaration(buffer, gl_info);
8161 shader_glsl_enable_extensions(buffer, gl_info);
8162 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8164 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8166 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8168 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8169 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8171 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8173 if (hs->phases.control_point)
8175 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8176 shader->limits->packed_output);
8177 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8178 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8179 shader_addline(buffer, "}\n");
8182 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8183 if ((phase = hs->phases.control_point))
8185 for (i = 0; i < phase->temporary_count; ++i)
8186 shader_addline(buffer, "vec4 R%u;\n", i);
8187 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8188 return 0;
8189 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8191 else
8193 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8195 shader_addline(buffer, "}\n");
8197 for (i = 0; i < hs->phases.fork_count; ++i)
8199 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8200 &hs->phases.fork[i], "fork", i)))
8201 return 0;
8204 for (i = 0; i < hs->phases.join_count; ++i)
8206 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8207 &hs->phases.join[i], "join", i)))
8208 return 0;
8211 shader_addline(buffer, "void main()\n{\n");
8212 shader_addline(buffer, "hs_control_point_phase();\n");
8213 if (reg_maps->vocp)
8214 shader_addline(buffer, "barrier();\n");
8215 for (i = 0; i < hs->phases.fork_count; ++i)
8216 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8217 for (i = 0; i < hs->phases.join_count; ++i)
8218 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8219 shader_addline(buffer, "setup_patch_constant_output();\n");
8220 shader_addline(buffer, "}\n");
8222 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8223 TRACE("Compiling shader object %u.\n", shader_id);
8224 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8226 return shader_id;
8229 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8230 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8231 const struct ds_compile_args *args)
8233 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8235 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8236 shader_glsl_fixup_position(buffer, FALSE);
8239 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context_gl *context_gl,
8240 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8242 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8243 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8244 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8245 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8246 struct shader_glsl_ctx_priv priv_ctx;
8247 GLuint shader_id;
8249 memset(&priv_ctx, 0, sizeof(priv_ctx));
8250 priv_ctx.gl_info = gl_info;
8251 priv_ctx.cur_ds_args = args;
8252 priv_ctx.string_buffers = string_buffers;
8254 shader_glsl_add_version_declaration(buffer, gl_info);
8256 shader_glsl_enable_extensions(buffer, gl_info);
8257 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8259 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8261 shader_addline(buffer, "layout(");
8262 switch (shader->u.ds.tessellator_domain)
8264 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8265 shader_addline(buffer, "isolines");
8266 break;
8267 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8268 shader_addline(buffer, "quads");
8269 break;
8270 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8271 shader_addline(buffer, "triangles");
8272 break;
8274 switch (args->tessellator_output_primitive)
8276 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8277 if (args->render_offscreen)
8278 shader_addline(buffer, ", ccw");
8279 else
8280 shader_addline(buffer, ", cw");
8281 break;
8282 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8283 if (args->render_offscreen)
8284 shader_addline(buffer, ", cw");
8285 else
8286 shader_addline(buffer, ", ccw");
8287 break;
8288 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8289 shader_addline(buffer, ", point_mode");
8290 break;
8291 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8292 break;
8294 switch (args->tessellator_partitioning)
8296 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8297 shader_addline(buffer, ", fractional_odd_spacing");
8298 break;
8299 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8300 shader_addline(buffer, ", fractional_even_spacing");
8301 break;
8302 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8303 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8304 shader_addline(buffer, ", equal_spacing");
8305 break;
8307 shader_addline(buffer, ") in;\n");
8309 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8311 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8312 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8314 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8315 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8316 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8318 shader_addline(buffer, "void main()\n{\n");
8319 shader_addline(buffer, "setup_patch_constant_input();\n");
8321 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8322 return 0;
8324 shader_addline(buffer, "}\n");
8326 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8327 TRACE("Compiling shader object %u.\n", shader_id);
8328 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8330 return shader_id;
8333 /* Context activation is done by the caller. */
8334 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context_gl *context_gl,
8335 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8337 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8338 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8339 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8340 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8341 const struct wined3d_shader_signature_element *output;
8342 enum wined3d_primitive_type primitive_type;
8343 struct shader_glsl_ctx_priv priv_ctx;
8344 unsigned int max_vertices;
8345 unsigned int i, j;
8346 GLuint shader_id;
8348 memset(&priv_ctx, 0, sizeof(priv_ctx));
8349 priv_ctx.gl_info = gl_info;
8350 priv_ctx.string_buffers = string_buffers;
8352 shader_glsl_add_version_declaration(buffer, gl_info);
8354 shader_glsl_enable_extensions(buffer, gl_info);
8356 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8358 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8359 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8360 if (shader->u.gs.instance_count > 1)
8361 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8362 shader_addline(buffer, ") in;\n");
8364 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8365 if (!(max_vertices = shader->u.gs.vertices_out))
8367 switch (args->primitive_type)
8369 case WINED3D_PT_POINTLIST:
8370 max_vertices = 1;
8371 break;
8372 case WINED3D_PT_LINELIST:
8373 max_vertices = 2;
8374 break;
8375 case WINED3D_PT_TRIANGLELIST:
8376 max_vertices = 3;
8377 break;
8378 default:
8379 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8380 break;
8383 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8384 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8385 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8387 if (!gl_info->supported[ARB_CLIP_CONTROL])
8389 shader_addline(buffer, "uniform vec4 pos_fixup");
8390 if (reg_maps->viewport_array)
8391 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8392 shader_addline(buffer, ";\n");
8395 if (is_rasterization_disabled(shader))
8397 shader_glsl_generate_stream_output_setup(buffer, shader);
8399 else
8401 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8402 gl_info, TRUE, args->interpolation_mode);
8405 shader_addline(buffer, "void main()\n{\n");
8406 if (shader->function)
8408 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8409 return 0;
8411 else
8413 for (i = 0; i < max_vertices; ++i)
8415 for (j = 0; j < shader->output_signature.element_count; ++j)
8417 output = &shader->output_signature.elements[j];
8418 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8419 output->register_idx, i, output->register_idx);
8421 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8422 if (!gl_info->supported[ARB_CLIP_CONTROL])
8423 shader_glsl_fixup_position(buffer, FALSE);
8424 shader_addline(buffer, "EmitVertex();\n");
8427 shader_addline(buffer, "}\n");
8429 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8430 TRACE("Compiling shader object %u.\n", shader_id);
8431 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8433 return shader_id;
8436 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8438 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8439 const struct wined3d_gl_info *gl_info = priv->gl_info;
8440 struct wined3d_string_buffer *buffer = ctx->buffer;
8441 const struct wined3d_shader *shader = ctx->shader;
8443 switch (shader->reg_maps.shader_version.type)
8445 case WINED3D_SHADER_TYPE_PIXEL:
8446 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8447 break;
8448 case WINED3D_SHADER_TYPE_VERTEX:
8449 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8450 break;
8451 case WINED3D_SHADER_TYPE_DOMAIN:
8452 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8453 break;
8454 case WINED3D_SHADER_TYPE_GEOMETRY:
8455 case WINED3D_SHADER_TYPE_COMPUTE:
8456 break;
8457 default:
8458 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8459 break;
8463 /* Context activation is done by the caller. */
8464 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context_gl *context_gl,
8465 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8466 const struct wined3d_shader *shader)
8468 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8469 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8470 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8471 struct shader_glsl_ctx_priv priv_ctx;
8472 GLuint shader_id;
8473 unsigned int i;
8475 memset(&priv_ctx, 0, sizeof(priv_ctx));
8476 priv_ctx.gl_info = gl_info;
8477 priv_ctx.string_buffers = string_buffers;
8479 shader_glsl_add_version_declaration(buffer, gl_info);
8481 shader_glsl_enable_extensions(buffer, gl_info);
8482 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8484 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8486 for (i = 0; i < reg_maps->tgsm_count; ++i)
8488 if (reg_maps->tgsm[i].size)
8489 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8492 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8493 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8495 shader_addline(buffer, "void main()\n{\n");
8496 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8497 shader_addline(buffer, "}\n");
8499 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8500 TRACE("Compiling shader object %u.\n", shader_id);
8501 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8503 return shader_id;
8506 static GLuint find_glsl_fragment_shader(const struct wined3d_context_gl *context_gl,
8507 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8508 struct wined3d_shader *shader,
8509 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8511 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8512 struct glsl_shader_private *shader_data;
8513 struct ps_np2fixup_info *np2fixup;
8514 UINT i;
8515 DWORD new_size;
8516 GLuint ret;
8518 if (!shader->backend_data)
8520 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8522 ERR("Failed to allocate backend data.\n");
8523 return 0;
8526 shader_data = shader->backend_data;
8527 gl_shaders = shader_data->gl_shaders.ps;
8529 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8530 * so a linear search is more performant than a hashmap or a binary search
8531 * (cache coherency etc)
8533 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8535 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8537 if (args->np2_fixup)
8538 *np2fixup_info = &gl_shaders[i].np2fixup;
8539 return gl_shaders[i].id;
8543 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8544 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8546 if (shader_data->num_gl_shaders)
8548 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8549 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8551 else
8553 new_array = heap_alloc(sizeof(*gl_shaders));
8554 new_size = 1;
8557 if(!new_array) {
8558 ERR("Out of memory\n");
8559 return 0;
8561 shader_data->gl_shaders.ps = new_array;
8562 shader_data->shader_array_size = new_size;
8563 gl_shaders = new_array;
8566 gl_shaders[shader_data->num_gl_shaders].args = *args;
8568 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8569 memset(np2fixup, 0, sizeof(*np2fixup));
8570 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8572 string_buffer_clear(buffer);
8573 ret = shader_glsl_generate_fragment_shader(context_gl, buffer, string_buffers, shader, args, np2fixup);
8574 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8576 return ret;
8579 static BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8580 const DWORD use_map)
8582 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8583 return FALSE;
8584 if ((stored->clip_enabled) != new->clip_enabled)
8585 return FALSE;
8586 if (stored->point_size != new->point_size)
8587 return FALSE;
8588 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8589 return FALSE;
8590 if (stored->flatshading != new->flatshading)
8591 return FALSE;
8592 if (stored->next_shader_type != new->next_shader_type)
8593 return FALSE;
8594 if (stored->next_shader_input_count != new->next_shader_input_count)
8595 return FALSE;
8596 if (stored->fog_src != new->fog_src)
8597 return FALSE;
8598 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8601 static GLuint find_glsl_vertex_shader(const struct wined3d_context_gl *context_gl,
8602 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct vs_compile_args *args)
8604 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8605 uint32_t use_map = context_gl->c.stream_info.use_map;
8606 struct glsl_shader_private *shader_data;
8607 unsigned int i, new_size;
8608 GLuint ret;
8610 if (!shader->backend_data)
8612 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8614 ERR("Failed to allocate backend data.\n");
8615 return 0;
8618 shader_data = shader->backend_data;
8619 gl_shaders = shader_data->gl_shaders.vs;
8621 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8622 * so a linear search is more performant than a hashmap or a binary search
8623 * (cache coherency etc)
8625 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8627 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8628 return gl_shaders[i].id;
8631 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8633 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8635 if (shader_data->num_gl_shaders)
8637 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8638 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8640 else
8642 new_array = heap_alloc(sizeof(*gl_shaders));
8643 new_size = 1;
8646 if(!new_array) {
8647 ERR("Out of memory\n");
8648 return 0;
8650 shader_data->gl_shaders.vs = new_array;
8651 shader_data->shader_array_size = new_size;
8652 gl_shaders = new_array;
8655 gl_shaders[shader_data->num_gl_shaders].args = *args;
8657 string_buffer_clear(&priv->shader_buffer);
8658 ret = shader_glsl_generate_vertex_shader(context_gl, priv, shader, args);
8659 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8661 return ret;
8664 static GLuint find_glsl_hull_shader(const struct wined3d_context_gl *context_gl,
8665 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8667 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8668 struct glsl_shader_private *shader_data;
8669 unsigned int new_size;
8670 GLuint ret;
8672 if (!shader->backend_data)
8674 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8676 ERR("Failed to allocate backend data.\n");
8677 return 0;
8680 shader_data = shader->backend_data;
8681 gl_shaders = shader_data->gl_shaders.hs;
8683 if (shader_data->num_gl_shaders > 0)
8685 assert(shader_data->num_gl_shaders == 1);
8686 return gl_shaders[0].id;
8689 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8691 assert(!shader_data->gl_shaders.hs);
8692 new_size = 1;
8693 if (!(new_array = heap_alloc(sizeof(*new_array))))
8695 ERR("Failed to allocate GL shaders array.\n");
8696 return 0;
8698 shader_data->gl_shaders.hs = new_array;
8699 shader_data->shader_array_size = new_size;
8700 gl_shaders = new_array;
8702 string_buffer_clear(&priv->shader_buffer);
8703 ret = shader_glsl_generate_hull_shader(context_gl, priv, shader);
8704 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8706 return ret;
8709 static GLuint find_glsl_domain_shader(const struct wined3d_context_gl *context_gl,
8710 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8712 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8713 struct glsl_shader_private *shader_data;
8714 unsigned int i, new_size;
8715 GLuint ret;
8717 if (!shader->backend_data)
8719 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8721 ERR("Failed to allocate backend data.\n");
8722 return 0;
8725 shader_data = shader->backend_data;
8726 gl_shaders = shader_data->gl_shaders.ds;
8728 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8730 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8731 return gl_shaders[i].id;
8734 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8736 if (shader_data->num_gl_shaders)
8738 new_size = shader_data->shader_array_size + 1;
8739 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8741 else
8743 new_array = heap_alloc(sizeof(*new_array));
8744 new_size = 1;
8747 if (!new_array)
8749 ERR("Failed to allocate GL shaders array.\n");
8750 return 0;
8752 shader_data->gl_shaders.ds = new_array;
8753 shader_data->shader_array_size = new_size;
8754 gl_shaders = new_array;
8756 string_buffer_clear(&priv->shader_buffer);
8757 ret = shader_glsl_generate_domain_shader(context_gl, priv, shader, args);
8758 gl_shaders[shader_data->num_gl_shaders].args = *args;
8759 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8761 return ret;
8764 static GLuint find_glsl_geometry_shader(const struct wined3d_context_gl *context_gl,
8765 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8767 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8768 struct glsl_shader_private *shader_data;
8769 unsigned int i, new_size;
8770 GLuint ret;
8772 if (!shader->backend_data)
8774 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8776 ERR("Failed to allocate backend data.\n");
8777 return 0;
8780 shader_data = shader->backend_data;
8781 gl_shaders = shader_data->gl_shaders.gs;
8783 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8785 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8786 return gl_shaders[i].id;
8789 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8791 if (shader_data->num_gl_shaders)
8793 new_size = shader_data->shader_array_size + 1;
8794 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8796 else
8798 new_array = heap_alloc(sizeof(*new_array));
8799 new_size = 1;
8802 if (!new_array)
8804 ERR("Failed to allocate GL shaders array.\n");
8805 return 0;
8807 shader_data->gl_shaders.gs = new_array;
8808 shader_data->shader_array_size = new_size;
8809 gl_shaders = new_array;
8811 string_buffer_clear(&priv->shader_buffer);
8812 ret = shader_glsl_generate_geometry_shader(context_gl, priv, shader, args);
8813 gl_shaders[shader_data->num_gl_shaders].args = *args;
8814 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8816 return ret;
8819 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8821 switch (mcs)
8823 case WINED3D_MCS_MATERIAL:
8824 return material;
8825 case WINED3D_MCS_COLOR1:
8826 return "ffp_attrib_diffuse";
8827 case WINED3D_MCS_COLOR2:
8828 return "ffp_attrib_specular";
8829 default:
8830 ERR("Invalid material color source %#x.\n", mcs);
8831 return "<invalid>";
8835 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8836 const struct wined3d_ffp_vs_settings *settings, unsigned int idx, BOOL legacy_lighting)
8838 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8839 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8840 if (settings->localviewer)
8841 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8842 else
8843 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8844 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0%s) specular +="
8845 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n",
8846 legacy_lighting ? " && ffp_material.shininess > 0.0" : "", idx);
8849 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8850 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8852 const char *diffuse, *specular, *emissive, *ambient;
8853 unsigned int i, idx;
8855 if (!settings->lighting)
8857 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8858 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8859 return;
8862 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8863 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8864 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8865 shader_addline(buffer, "vec3 dir, dst;\n");
8866 shader_addline(buffer, "float att, t;\n");
8868 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8869 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8870 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8871 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8873 idx = 0;
8874 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8876 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8877 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8878 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8879 shader_addline(buffer, "dst.x = 1.0;\n");
8880 if (legacy_lighting)
8882 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8883 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8884 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8886 else
8888 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
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", idx, idx, idx);
8892 if (!legacy_lighting)
8893 shader_addline(buffer, "att = 1.0 / att;\n");
8894 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8895 if (!settings->normal)
8897 shader_addline(buffer, "}\n");
8898 continue;
8900 shader_addline(buffer, "dir = normalize(dir);\n");
8901 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8902 shader_addline(buffer, "}\n");
8905 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8907 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8908 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8909 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8910 shader_addline(buffer, "dst.x = 1.0;\n");
8911 if (legacy_lighting)
8913 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8914 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8915 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8917 else
8919 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8921 shader_addline(buffer, "dir = normalize(dir);\n");
8922 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8923 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8924 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8925 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8926 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8927 idx, idx, idx, idx);
8928 if (legacy_lighting)
8929 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8930 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8931 idx, idx, idx);
8932 else
8933 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8934 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8935 idx, idx, idx);
8936 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8937 if (!settings->normal)
8939 shader_addline(buffer, "}\n");
8940 continue;
8942 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8943 shader_addline(buffer, "}\n");
8946 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8948 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8949 if (!settings->normal)
8950 continue;
8951 shader_addline(buffer, "att = 1.0;\n");
8952 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8953 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8956 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8958 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8959 if (!settings->normal)
8960 continue;
8961 shader_addline(buffer, "att = 1.0;\n");
8962 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8963 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8966 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8967 ambient, diffuse, emissive);
8968 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8969 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8972 /* Context activation is done by the caller. */
8973 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8974 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8976 static const struct attrib_info
8978 const char type[6];
8979 const char name[24];
8981 attrib_info[] =
8983 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8984 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8985 /* TODO: Indexed vertex blending */
8986 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8987 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8988 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8989 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8990 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8992 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8993 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8994 BOOL output_legacy_fogcoord = legacy_syntax;
8995 BOOL legacy_lighting = priv->legacy_lighting;
8996 GLuint shader_obj;
8997 unsigned int i;
8999 string_buffer_clear(buffer);
9001 shader_glsl_add_version_declaration(buffer, gl_info);
9003 if (shader_glsl_use_explicit_attrib_location(gl_info))
9004 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9006 shader_addline(buffer, "invariant gl_Position;\n");
9008 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
9010 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
9012 if (shader_glsl_use_explicit_attrib_location(gl_info))
9013 shader_addline(buffer, "layout(location = %u) ", i);
9014 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
9016 shader_addline(buffer, "\n");
9018 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9019 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9020 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9021 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", WINED3D_MAX_TEXTURES);
9023 shader_addline(buffer, "uniform struct\n{\n");
9024 shader_addline(buffer, " vec4 emissive;\n");
9025 shader_addline(buffer, " vec4 ambient;\n");
9026 shader_addline(buffer, " vec4 diffuse;\n");
9027 shader_addline(buffer, " vec4 specular;\n");
9028 shader_addline(buffer, " float shininess;\n");
9029 shader_addline(buffer, "} ffp_material;\n");
9031 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9032 shader_addline(buffer, "uniform struct\n{\n");
9033 shader_addline(buffer, " vec4 diffuse;\n");
9034 shader_addline(buffer, " vec4 specular;\n");
9035 shader_addline(buffer, " vec4 ambient;\n");
9036 shader_addline(buffer, " vec4 position;\n");
9037 shader_addline(buffer, " vec3 direction;\n");
9038 shader_addline(buffer, " float range;\n");
9039 shader_addline(buffer, " float falloff;\n");
9040 shader_addline(buffer, " float c_att;\n");
9041 shader_addline(buffer, " float l_att;\n");
9042 shader_addline(buffer, " float q_att;\n");
9043 shader_addline(buffer, " float cos_htheta;\n");
9044 shader_addline(buffer, " float cos_hphi;\n");
9045 shader_addline(buffer, "} ffp_light[%u];\n", WINED3D_MAX_ACTIVE_LIGHTS);
9047 if (settings->point_size)
9049 shader_addline(buffer, "uniform struct\n{\n");
9050 shader_addline(buffer, " float size;\n");
9051 shader_addline(buffer, " float size_min;\n");
9052 shader_addline(buffer, " float size_max;\n");
9053 shader_addline(buffer, " float c_att;\n");
9054 shader_addline(buffer, " float l_att;\n");
9055 shader_addline(buffer, " float q_att;\n");
9056 shader_addline(buffer, "} ffp_point;\n");
9059 if (legacy_syntax)
9061 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9062 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9063 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9064 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9066 else
9068 if (settings->clipping)
9069 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9071 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9072 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9073 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9074 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9077 shader_addline(buffer, "\nvoid main()\n{\n");
9078 shader_addline(buffer, "float m;\n");
9079 shader_addline(buffer, "vec3 r;\n");
9081 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9083 if (attrib_info[i].name[0])
9084 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9085 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9087 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9089 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9090 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9091 && settings->texcoords & (1u << i))
9092 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9095 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9097 if (settings->transformed)
9099 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9100 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9101 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9103 else
9105 for (i = 0; i < settings->vertexblends; ++i)
9106 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9108 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9109 for (i = 0; i < settings->vertexblends + 1; ++i)
9110 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9112 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9113 if (settings->clipping)
9115 if (legacy_syntax)
9116 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9117 else
9118 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9119 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9121 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9124 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9125 if (settings->normal)
9127 if (!settings->vertexblends)
9129 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9131 else
9133 for (i = 0; i < settings->vertexblends + 1; ++i)
9134 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9137 if (settings->normalize)
9138 shader_addline(buffer, "normal = normalize(normal);\n");
9141 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9142 if (legacy_syntax)
9144 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9145 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9147 else
9149 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9150 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9153 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9155 BOOL output_legacy_texcoord = legacy_syntax;
9157 switch (settings->texgen[i] & 0xffff0000)
9159 case WINED3DTSS_TCI_PASSTHRU:
9160 if (settings->texcoords & (1u << i))
9161 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9162 i, i, i);
9163 else if (shader_glsl_full_ffp_varyings(gl_info))
9164 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9165 else
9166 output_legacy_texcoord = FALSE;
9167 break;
9169 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9170 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9171 break;
9173 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9174 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9175 break;
9177 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9178 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9179 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9180 break;
9182 case WINED3DTSS_TCI_SPHEREMAP:
9183 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9184 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9185 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9186 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9187 break;
9189 default:
9190 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9191 break;
9193 if (output_legacy_texcoord)
9194 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9197 switch (settings->fog_mode)
9199 case WINED3D_FFP_VS_FOG_OFF:
9200 output_legacy_fogcoord = FALSE;
9201 break;
9203 case WINED3D_FFP_VS_FOG_FOGCOORD:
9204 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9205 break;
9207 case WINED3D_FFP_VS_FOG_RANGE:
9208 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9209 break;
9211 case WINED3D_FFP_VS_FOG_DEPTH:
9212 if (settings->ortho_fog)
9214 if (gl_info->supported[ARB_CLIP_CONTROL])
9215 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9216 else
9217 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9218 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9220 else if (settings->transformed)
9222 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9224 else
9226 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9228 break;
9230 default:
9231 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9232 break;
9234 if (output_legacy_fogcoord)
9235 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9237 if (settings->point_size)
9239 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9240 " + ffp_point.l_att * length(ec_pos.xyz)"
9241 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9242 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9243 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9246 shader_addline(buffer, "}\n");
9248 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9249 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9251 return shader_obj;
9254 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9255 DWORD argnum, unsigned int stage, DWORD arg)
9257 const char *ret;
9259 if (arg == ARG_UNUSED)
9260 return "<unused arg>";
9262 switch (arg & WINED3DTA_SELECTMASK)
9264 case WINED3DTA_DIFFUSE:
9265 ret = "ffp_varying_diffuse";
9266 break;
9268 case WINED3DTA_CURRENT:
9269 ret = "ret";
9270 break;
9272 case WINED3DTA_TEXTURE:
9273 switch (stage)
9275 case 0: ret = "tex0"; break;
9276 case 1: ret = "tex1"; break;
9277 case 2: ret = "tex2"; break;
9278 case 3: ret = "tex3"; break;
9279 case 4: ret = "tex4"; break;
9280 case 5: ret = "tex5"; break;
9281 case 6: ret = "tex6"; break;
9282 case 7: ret = "tex7"; break;
9283 default:
9284 ret = "<invalid texture>";
9285 break;
9287 break;
9289 case WINED3DTA_TFACTOR:
9290 ret = "tex_factor";
9291 break;
9293 case WINED3DTA_SPECULAR:
9294 ret = "ffp_varying_specular";
9295 break;
9297 case WINED3DTA_TEMP:
9298 ret = "temp_reg";
9299 break;
9301 case WINED3DTA_CONSTANT:
9302 switch (stage)
9304 case 0: ret = "tss_const0"; break;
9305 case 1: ret = "tss_const1"; break;
9306 case 2: ret = "tss_const2"; break;
9307 case 3: ret = "tss_const3"; break;
9308 case 4: ret = "tss_const4"; break;
9309 case 5: ret = "tss_const5"; break;
9310 case 6: ret = "tss_const6"; break;
9311 case 7: ret = "tss_const7"; break;
9312 default:
9313 ret = "<invalid constant>";
9314 break;
9316 break;
9318 default:
9319 return "<unhandled arg>";
9322 if (arg & WINED3DTA_COMPLEMENT)
9324 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9325 if (argnum == 0)
9326 ret = "arg0";
9327 else if (argnum == 1)
9328 ret = "arg1";
9329 else if (argnum == 2)
9330 ret = "arg2";
9333 if (arg & WINED3DTA_ALPHAREPLICATE)
9335 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9336 if (argnum == 0)
9337 ret = "arg0";
9338 else if (argnum == 1)
9339 ret = "arg1";
9340 else if (argnum == 2)
9341 ret = "arg2";
9344 return ret;
9347 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9348 BOOL alpha, BOOL tmp_dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9350 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9352 if (color && alpha)
9353 dstmask = "";
9354 else if (color)
9355 dstmask = ".xyz";
9356 else
9357 dstmask = ".w";
9359 dstreg = tmp_dst ? "temp_reg" : "ret";
9361 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9362 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9363 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9365 switch (op)
9367 case WINED3D_TOP_DISABLE:
9368 break;
9370 case WINED3D_TOP_SELECT_ARG1:
9371 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9372 break;
9374 case WINED3D_TOP_SELECT_ARG2:
9375 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9376 break;
9378 case WINED3D_TOP_MODULATE:
9379 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9380 break;
9382 case WINED3D_TOP_MODULATE_4X:
9383 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9384 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9385 break;
9387 case WINED3D_TOP_MODULATE_2X:
9388 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9389 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9390 break;
9392 case WINED3D_TOP_ADD:
9393 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9394 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9395 break;
9397 case WINED3D_TOP_ADD_SIGNED:
9398 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9399 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9400 break;
9402 case WINED3D_TOP_ADD_SIGNED_2X:
9403 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9404 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9405 break;
9407 case WINED3D_TOP_SUBTRACT:
9408 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9409 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9410 break;
9412 case WINED3D_TOP_ADD_SMOOTH:
9413 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9414 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9415 break;
9417 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9418 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9419 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9420 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9421 break;
9423 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9424 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9425 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9426 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9427 break;
9429 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9430 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9431 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9432 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9433 break;
9435 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9436 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9437 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9438 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9439 break;
9441 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9442 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9443 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9444 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9445 break;
9447 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9448 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9449 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9450 break;
9452 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9453 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9454 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9455 break;
9457 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9458 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9459 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9460 break;
9461 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9462 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9463 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9464 break;
9466 case WINED3D_TOP_BUMPENVMAP:
9467 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9468 /* These are handled in the first pass, nothing to do. */
9469 break;
9471 case WINED3D_TOP_DOTPRODUCT3:
9472 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9473 dstreg, dstmask, arg1, arg2, dstmask);
9474 break;
9476 case WINED3D_TOP_MULTIPLY_ADD:
9477 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9478 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9479 break;
9481 case WINED3D_TOP_LERP:
9482 /* MSDN isn't quite right here. */
9483 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9484 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9485 break;
9487 default:
9488 FIXME("Unhandled operation %#x.\n", op);
9489 break;
9493 /* Context activation is done by the caller. */
9494 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9495 const struct ffp_frag_settings *settings, const struct wined3d_context_gl *context_gl)
9497 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9498 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9499 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9500 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9501 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
9502 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9503 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9504 UINT lowest_disabled_stage;
9505 GLuint shader_id;
9506 DWORD arg0, arg1, arg2;
9507 unsigned int stage;
9509 string_buffer_clear(buffer);
9511 /* Find out which textures are read */
9512 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9514 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9515 break;
9517 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9518 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9519 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9521 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9522 || (stage == 0 && settings->color_key_enabled))
9523 tex_map |= 1u << stage;
9524 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9525 tfactor_used = TRUE;
9526 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9527 tempreg_used = TRUE;
9528 if (settings->op[stage].tmp_dst)
9529 tempreg_used = TRUE;
9530 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9531 tss_const_map |= 1u << stage;
9533 switch (settings->op[stage].cop)
9535 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9536 lum_map |= 1u << stage;
9537 /* fall through */
9538 case WINED3D_TOP_BUMPENVMAP:
9539 bump_map |= 1u << stage;
9540 /* fall through */
9541 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9542 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9543 tex_map |= 1u << stage;
9544 break;
9546 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9547 tfactor_used = TRUE;
9548 break;
9550 default:
9551 break;
9554 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9555 continue;
9557 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9558 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9559 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9561 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9562 tex_map |= 1u << stage;
9563 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9564 tfactor_used = TRUE;
9565 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9566 tempreg_used = TRUE;
9567 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9568 tss_const_map |= 1u << stage;
9570 lowest_disabled_stage = stage;
9572 shader_glsl_add_version_declaration(buffer, gl_info);
9574 if (shader_glsl_use_explicit_attrib_location(gl_info))
9575 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9576 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9577 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9578 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9579 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9581 if (!use_legacy_fragment_output(gl_info))
9583 shader_addline(buffer, "vec4 ps_out[1];\n");
9584 if (shader_glsl_use_explicit_attrib_location(gl_info))
9585 shader_addline(buffer, "layout(location = 0) ");
9586 shader_addline(buffer, "out vec4 color_out0;\n");
9589 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9590 shader_addline(buffer, "vec4 ret;\n");
9591 if (tempreg_used || settings->sRGB_write)
9592 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9593 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9595 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9597 const char *sampler_type;
9599 if (tss_const_map & (1u << stage))
9600 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9602 if (!(tex_map & (1u << stage)))
9603 continue;
9605 switch (settings->op[stage].tex_type)
9607 case WINED3D_GL_RES_TYPE_TEX_1D:
9608 sampler_type = "1D";
9609 break;
9610 case WINED3D_GL_RES_TYPE_TEX_2D:
9611 sampler_type = "2D";
9612 break;
9613 case WINED3D_GL_RES_TYPE_TEX_3D:
9614 sampler_type = "3D";
9615 break;
9616 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9617 sampler_type = "Cube";
9618 break;
9619 case WINED3D_GL_RES_TYPE_TEX_RECT:
9620 sampler_type = "2DRect";
9621 break;
9622 default:
9623 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9624 sampler_type = NULL;
9625 break;
9627 if (sampler_type)
9629 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9630 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
9631 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9634 shader_addline(buffer, "vec4 tex%u;\n", stage);
9636 if (!(bump_map & (1u << stage)))
9637 continue;
9638 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9640 if (!(lum_map & (1u << stage)))
9641 continue;
9642 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9643 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9645 if (tfactor_used)
9646 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9647 if (settings->color_key_enabled)
9648 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9649 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9651 if (settings->sRGB_write)
9653 shader_addline(buffer, "const vec4 srgb_const0 = ");
9654 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
9655 shader_addline(buffer, ";\n");
9656 shader_addline(buffer, "const vec4 srgb_const1 = ");
9657 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
9658 shader_addline(buffer, ";\n");
9661 shader_addline(buffer, "uniform struct\n{\n");
9662 shader_addline(buffer, " vec4 color;\n");
9663 shader_addline(buffer, " float density;\n");
9664 shader_addline(buffer, " float end;\n");
9665 shader_addline(buffer, " float scale;\n");
9666 shader_addline(buffer, "} ffp_fog;\n");
9668 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9669 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9671 if (legacy_syntax)
9673 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9674 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9675 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9676 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9677 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9679 else
9681 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9682 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9683 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9684 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9685 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9688 shader_addline(buffer, "void main()\n{\n");
9690 if (legacy_syntax)
9692 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9693 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9696 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9698 if (tex_map & (1u << stage))
9700 if (settings->pointsprite)
9701 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9702 else if (settings->texcoords_initialized & (1u << stage))
9703 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9704 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9705 else
9706 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9710 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9711 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9713 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9714 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9716 /* Generate texture sampling instructions */
9717 for (stage = 0; stage < WINED3D_MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9719 const char *texture_function, *coord_mask;
9720 BOOL proj;
9722 if (!(tex_map & (1u << stage)))
9723 continue;
9725 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9727 proj = FALSE;
9729 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9730 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9732 proj = TRUE;
9734 else
9736 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9737 proj = TRUE;
9740 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9741 proj = FALSE;
9743 switch (settings->op[stage].tex_type)
9745 case WINED3D_GL_RES_TYPE_TEX_1D:
9746 texture_function = "texture1D";
9747 coord_mask = "x";
9748 break;
9749 case WINED3D_GL_RES_TYPE_TEX_2D:
9750 texture_function = "texture2D";
9751 coord_mask = "xy";
9752 break;
9753 case WINED3D_GL_RES_TYPE_TEX_3D:
9754 texture_function = "texture3D";
9755 coord_mask = "xyz";
9756 break;
9757 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9758 texture_function = "textureCube";
9759 coord_mask = "xyz";
9760 break;
9761 case WINED3D_GL_RES_TYPE_TEX_RECT:
9762 texture_function = "texture2DRect";
9763 coord_mask = "xy";
9764 break;
9765 default:
9766 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9767 texture_function = "";
9768 coord_mask = "xyzw";
9769 proj = FALSE;
9770 break;
9772 if (!legacy_syntax)
9773 texture_function = "texture";
9775 if (stage > 0
9776 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9777 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9779 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9781 /* With projective textures, texbem only divides the static
9782 * texture coordinate, not the displacement, so multiply the
9783 * displacement with the dividing parameter before passing it to
9784 * TXP. */
9785 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9787 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9789 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9790 stage, stage);
9791 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9793 else
9795 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9796 stage, stage);
9797 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9800 else
9802 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9805 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ret.%s%s);\n",
9806 stage, texture_function, proj ? "Proj" : "", stage, coord_mask, proj ? "w" : "");
9808 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9809 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9810 stage, stage - 1, stage - 1, stage - 1);
9812 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9814 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9815 stage, texture_function, proj ? "Proj" : "", stage, stage);
9817 else
9819 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].%s%s);\n",
9820 stage, texture_function, proj ? "Proj" : "", stage, stage, coord_mask, proj ? "w" : "");
9823 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9824 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9825 settings->op[stage].color_fixup);
9828 if (settings->color_key_enabled)
9829 shader_glsl_generate_colour_key_test(buffer, "tex0", "color_key[0]", "color_key[1]");
9831 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9833 /* Generate the main shader */
9834 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9836 BOOL op_equal;
9838 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9839 break;
9841 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9842 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9843 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9844 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9845 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9846 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9847 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9848 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9849 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9850 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9851 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9852 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9853 else
9854 op_equal = settings->op[stage].aop == settings->op[stage].cop
9855 && settings->op[stage].carg0 == settings->op[stage].aarg0
9856 && settings->op[stage].carg1 == settings->op[stage].aarg1
9857 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9859 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9861 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9862 settings->op[stage].cop, settings->op[stage].carg0,
9863 settings->op[stage].carg1, settings->op[stage].carg2);
9865 else if (op_equal)
9867 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9868 settings->op[stage].cop, settings->op[stage].carg0,
9869 settings->op[stage].carg1, settings->op[stage].carg2);
9871 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9872 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9874 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9875 settings->op[stage].cop, settings->op[stage].carg0,
9876 settings->op[stage].carg1, settings->op[stage].carg2);
9877 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9878 settings->op[stage].aop, settings->op[stage].aarg0,
9879 settings->op[stage].aarg1, settings->op[stage].aarg2);
9883 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9884 get_fragment_output(gl_info));
9886 if (settings->sRGB_write)
9887 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9889 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9891 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9892 if (!use_legacy_fragment_output(gl_info))
9893 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9895 shader_addline(buffer, "}\n");
9897 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9898 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9900 string_buffer_release(&priv->string_buffers, tex_reg_name);
9901 return shader_id;
9904 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9905 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9907 struct glsl_ffp_vertex_shader *shader;
9908 const struct wine_rb_entry *entry;
9910 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9911 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9913 if (!(shader = heap_alloc(sizeof(*shader))))
9914 return NULL;
9916 shader->desc.settings = *settings;
9917 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9918 list_init(&shader->linked_programs);
9919 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9920 ERR("Failed to insert ffp vertex shader.\n");
9922 return shader;
9925 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9926 const struct ffp_frag_settings *args, const struct wined3d_context_gl *context_gl)
9928 struct glsl_ffp_fragment_shader *glsl_desc;
9929 const struct ffp_frag_desc *desc;
9931 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9932 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9934 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9935 return NULL;
9937 glsl_desc->entry.settings = *args;
9938 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context_gl);
9939 list_init(&glsl_desc->linked_programs);
9940 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9942 return glsl_desc;
9946 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9947 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9949 unsigned int i;
9950 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9952 for (i = 0; i < vs_c_count; ++i)
9954 string_buffer_sprintf(name, "vs_c[%u]", i);
9955 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9957 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9959 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9961 string_buffer_sprintf(name, "vs_i[%u]", i);
9962 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9965 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9967 string_buffer_sprintf(name, "vs_b[%u]", i);
9968 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9971 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9972 vs->base_vertex_id_location = GL_EXTCALL(glGetUniformLocation(program_id, "base_vertex_id"));
9974 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9976 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9977 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9979 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9980 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9981 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9983 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9984 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9986 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9987 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9988 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9989 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9990 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9991 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9992 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
9994 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9995 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9996 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9997 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9998 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9999 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10000 string_buffer_sprintf(name, "ffp_light[%u].position", i);
10001 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10002 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
10003 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10004 string_buffer_sprintf(name, "ffp_light[%u].range", i);
10005 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10006 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
10007 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10008 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
10009 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10010 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
10011 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10012 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
10013 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10014 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
10015 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10016 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
10017 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10019 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10020 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10021 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10022 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10023 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10024 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10025 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10027 string_buffer_release(&priv->string_buffers, name);
10030 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10031 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10033 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10036 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10037 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10039 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10042 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10043 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10045 unsigned int i;
10046 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10048 for (i = 0; i < ps_c_count; ++i)
10050 string_buffer_sprintf(name, "ps_c[%u]", i);
10051 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10053 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10055 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10057 string_buffer_sprintf(name, "ps_i[%u]", i);
10058 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10061 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10063 string_buffer_sprintf(name, "ps_b[%u]", i);
10064 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10067 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10069 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10070 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10071 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10072 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10073 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10074 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10075 string_buffer_sprintf(name, "tss_const%u", i);
10076 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10079 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10080 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10082 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10083 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10084 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10085 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10087 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10089 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10090 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10091 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10093 string_buffer_release(&priv->string_buffers, name);
10096 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10097 const struct wined3d_context_gl *context_gl, struct wined3d_shader *shader)
10099 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10100 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10101 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10102 struct glsl_cs_compiled_shader *gl_shaders;
10103 struct glsl_shader_private *shader_data;
10104 struct glsl_shader_prog_link *entry;
10105 GLuint shader_id, program_id;
10107 if (!(entry = heap_alloc(sizeof(*entry))))
10109 ERR("Out of memory.\n");
10110 return E_OUTOFMEMORY;
10113 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10115 ERR("Failed to allocate backend data.\n");
10116 heap_free(entry);
10117 return E_OUTOFMEMORY;
10119 shader_data = shader->backend_data;
10121 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10123 ERR("Failed to allocate GL shader array.\n");
10124 heap_free(entry);
10125 heap_free(shader->backend_data);
10126 shader->backend_data = NULL;
10127 return E_OUTOFMEMORY;
10129 shader_data->shader_array_size = 1;
10130 gl_shaders = shader_data->gl_shaders.cs;
10132 TRACE("Compiling compute shader %p.\n", shader);
10134 string_buffer_clear(buffer);
10135 shader_id = shader_glsl_generate_compute_shader(context_gl, buffer, &priv->string_buffers, shader);
10136 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10138 program_id = GL_EXTCALL(glCreateProgram());
10139 TRACE("Created new GLSL shader program %u.\n", program_id);
10141 entry->id = program_id;
10142 entry->vs.id = 0;
10143 entry->hs.id = 0;
10144 entry->ds.id = 0;
10145 entry->gs.id = 0;
10146 entry->ps.id = 0;
10147 entry->cs.id = shader_id;
10148 entry->constant_version = 0;
10149 entry->shader_controlled_clip_distances = 0;
10150 entry->ps.np2_fixup_info = NULL;
10151 add_glsl_program_entry(priv, entry);
10153 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10154 GL_EXTCALL(glAttachShader(program_id, shader_id));
10155 checkGLcall("glAttachShader");
10157 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10159 TRACE("Linking GLSL shader program %u.\n", program_id);
10160 GL_EXTCALL(glLinkProgram(program_id));
10161 shader_glsl_validate_link(gl_info, program_id);
10163 GL_EXTCALL(glUseProgram(program_id));
10164 checkGLcall("glUseProgram");
10165 shader_glsl_load_program_resources(context_gl, priv, program_id, shader);
10166 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10168 entry->constant_update_mask = 0;
10170 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10171 checkGLcall("glUseProgram");
10172 return WINED3D_OK;
10175 static GLuint find_glsl_compute_shader(const struct wined3d_context_gl *context_gl,
10176 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10178 struct glsl_shader_private *shader_data;
10180 if (!shader->backend_data)
10182 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10183 if (FAILED(shader_glsl_compile_compute_shader(priv, context_gl, shader)))
10185 ERR("Failed to compile compute shader %p.\n", shader);
10186 return 0;
10189 shader_data = shader->backend_data;
10190 return shader_data->gl_shaders.cs[0].id;
10193 /* Context activation is done by the caller. */
10194 static void set_glsl_compute_shader_program(const struct wined3d_context_gl *context_gl,
10195 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10197 struct glsl_shader_prog_link *entry;
10198 struct wined3d_shader *shader;
10199 struct glsl_program_key key;
10200 GLuint cs_id;
10202 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10203 return;
10205 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10207 WARN("Compute shader is NULL.\n");
10208 ctx_data->glsl_program = NULL;
10209 return;
10212 cs_id = find_glsl_compute_shader(context_gl, priv, shader);
10213 memset(&key, 0, sizeof(key));
10214 key.cs_id = cs_id;
10215 if (!(entry = get_glsl_program_entry(priv, &key)))
10216 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10217 ctx_data->glsl_program = entry;
10220 /* Context activation is done by the caller. */
10221 static void set_glsl_shader_program(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
10222 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10224 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
10225 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10226 const struct wined3d_shader *pre_rasterization_shader;
10227 const struct ps_np2fixup_info *np2fixup_info = NULL;
10228 struct wined3d_shader *hshader, *dshader, *gshader;
10229 struct glsl_shader_prog_link *entry = NULL;
10230 struct wined3d_shader *vshader = NULL;
10231 struct wined3d_shader *pshader = NULL;
10232 GLuint reorder_shader_id = 0;
10233 struct glsl_program_key key;
10234 GLuint program_id;
10235 unsigned int i;
10236 GLuint vs_id = 0;
10237 GLuint hs_id = 0;
10238 GLuint ds_id = 0;
10239 GLuint gs_id = 0;
10240 GLuint ps_id = 0;
10241 struct list *ps_list, *vs_list;
10242 WORD attribs_map;
10243 struct wined3d_string_buffer *tmp_name;
10245 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10247 vs_id = ctx_data->glsl_program->vs.id;
10248 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10250 if (use_vs(state))
10251 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10253 else if (use_vs(state))
10255 struct vs_compile_args vs_compile_args;
10257 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10259 find_vs_compile_args(state, vshader, &vs_compile_args, &context_gl->c);
10260 vs_id = find_glsl_vertex_shader(context_gl, priv, vshader, &vs_compile_args);
10261 vs_list = &vshader->linked_programs;
10263 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10265 struct glsl_ffp_vertex_shader *ffp_shader;
10266 struct wined3d_ffp_vs_settings settings;
10268 wined3d_ffp_get_vs_settings(&context_gl->c, state, &settings);
10269 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10270 vs_id = ffp_shader->id;
10271 vs_list = &ffp_shader->linked_programs;
10274 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10275 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10276 hs_id = ctx_data->glsl_program->hs.id;
10277 else if (hshader)
10278 hs_id = find_glsl_hull_shader(context_gl, priv, hshader);
10280 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10281 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10283 ds_id = ctx_data->glsl_program->ds.id;
10285 else if (dshader)
10287 struct ds_compile_args args;
10289 find_ds_compile_args(state, dshader, &args, &context_gl->c);
10290 ds_id = find_glsl_domain_shader(context_gl, priv, dshader, &args);
10293 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10294 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10296 gs_id = ctx_data->glsl_program->gs.id;
10298 else if (gshader)
10300 struct gs_compile_args args;
10302 find_gs_compile_args(state, gshader, &args, &context_gl->c);
10303 gs_id = find_glsl_geometry_shader(context_gl, priv, gshader, &args);
10306 /* A pixel shader is not used when rasterization is disabled. */
10307 if (is_rasterization_disabled(gshader))
10309 ps_id = 0;
10310 ps_list = NULL;
10312 else if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10314 ps_id = ctx_data->glsl_program->ps.id;
10315 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10317 if (use_ps(state))
10318 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10320 else if (use_ps(state))
10322 struct ps_compile_args ps_compile_args;
10323 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10324 find_ps_compile_args(state, pshader, context_gl->c.stream_info.position_transformed,
10325 &ps_compile_args, &context_gl->c);
10326 ps_id = find_glsl_fragment_shader(context_gl, &priv->shader_buffer, &priv->string_buffers,
10327 pshader, &ps_compile_args, &np2fixup_info);
10328 ps_list = &pshader->linked_programs;
10330 else if (priv->fragment_pipe == &glsl_fragment_pipe
10331 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10333 struct glsl_ffp_fragment_shader *ffp_shader;
10334 struct ffp_frag_settings settings;
10336 wined3d_ffp_get_fs_settings(&context_gl->c, state, &settings, FALSE);
10337 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context_gl);
10338 ps_id = ffp_shader->id;
10339 ps_list = &ffp_shader->linked_programs;
10342 key.vs_id = vs_id;
10343 key.hs_id = hs_id;
10344 key.ds_id = ds_id;
10345 key.gs_id = gs_id;
10346 key.ps_id = ps_id;
10347 key.cs_id = 0;
10348 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10350 ctx_data->glsl_program = entry;
10351 return;
10354 /* If we get to this point, then no matching program exists, so we create one */
10355 program_id = GL_EXTCALL(glCreateProgram());
10356 TRACE("Created new GLSL shader program %u.\n", program_id);
10358 /* Create the entry */
10359 entry = heap_alloc(sizeof(*entry));
10360 entry->id = program_id;
10361 entry->vs.id = vs_id;
10362 entry->hs.id = hs_id;
10363 entry->ds.id = ds_id;
10364 entry->gs.id = gs_id;
10365 entry->ps.id = ps_id;
10366 entry->cs.id = 0;
10367 entry->constant_version = 0;
10368 entry->shader_controlled_clip_distances = 0;
10369 entry->ps.np2_fixup_info = np2fixup_info;
10370 /* Add the hash table entry */
10371 add_glsl_program_entry(priv, entry);
10373 /* Set the current program */
10374 ctx_data->glsl_program = entry;
10376 /* Attach GLSL vshader */
10377 if (vs_id)
10379 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10380 GL_EXTCALL(glAttachShader(program_id, vs_id));
10381 checkGLcall("glAttachShader");
10383 list_add_head(vs_list, &entry->vs.shader_entry);
10386 if (vshader)
10388 attribs_map = vshader->reg_maps.input_registers;
10389 if (vshader->reg_maps.shader_version.major < 4)
10391 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10392 state->primitive_type == WINED3D_PT_POINTLIST && vshader->reg_maps.point_size,
10393 d3d_info->emulated_flatshading
10394 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10395 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10396 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10397 checkGLcall("glAttachShader");
10398 /* Flag the reorder function for deletion, it will be freed
10399 * automatically when the program is destroyed. */
10400 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10403 else
10405 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10408 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10410 /* Bind vertex attributes to a corresponding index number to match
10411 * the same index numbers as ARB_vertex_programs (makes loading
10412 * vertex attributes simpler). With this method, we can use the
10413 * exact same code to load the attributes later for both ARB and
10414 * GLSL shaders.
10416 * We have to do this here because we need to know the Program ID
10417 * in order to make the bindings work, and it has to be done prior
10418 * to linking the GLSL program. */
10419 tmp_name = string_buffer_get(&priv->string_buffers);
10420 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
10422 if (!(attribs_map & 1))
10423 continue;
10425 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10426 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10427 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10429 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10430 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10431 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10432 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10435 checkGLcall("glBindAttribLocation");
10437 if (!use_legacy_fragment_output(gl_info))
10439 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
10441 string_buffer_sprintf(tmp_name, "color_out%u", i);
10442 if (state->blend_state && state->blend_state->dual_source)
10443 GL_EXTCALL(glBindFragDataLocationIndexed(program_id, 0, i, tmp_name->buffer));
10444 else
10445 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10446 checkGLcall("glBindFragDataLocation");
10449 string_buffer_release(&priv->string_buffers, tmp_name);
10452 if (hshader)
10454 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10455 GL_EXTCALL(glAttachShader(program_id, hs_id));
10456 checkGLcall("glAttachShader");
10458 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10461 if (dshader)
10463 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10464 GL_EXTCALL(glAttachShader(program_id, ds_id));
10465 checkGLcall("glAttachShader");
10467 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10470 if (gshader)
10472 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10473 GL_EXTCALL(glAttachShader(program_id, gs_id));
10474 checkGLcall("glAttachShader");
10476 shader_glsl_init_transform_feedback(context_gl, priv, program_id, gshader);
10478 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10481 /* Attach GLSL pshader */
10482 if (ps_id)
10484 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10485 GL_EXTCALL(glAttachShader(program_id, ps_id));
10486 checkGLcall("glAttachShader");
10488 list_add_head(ps_list, &entry->ps.shader_entry);
10491 /* Link the program */
10492 TRACE("Linking GLSL shader program %u.\n", program_id);
10493 GL_EXTCALL(glLinkProgram(program_id));
10494 shader_glsl_validate_link(gl_info, program_id);
10496 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10497 vshader ? vshader->limits->constant_float : 0);
10498 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10499 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10500 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10501 pshader ? pshader->limits->constant_float : 0);
10502 checkGLcall("find glsl program uniform locations");
10504 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10505 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10507 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10508 entry->shader_controlled_clip_distances = 1;
10509 entry->clip_distance_mask = (1u << clip_distance_count) - 1;
10512 if (needs_legacy_glsl_syntax(gl_info))
10514 if (pshader && pshader->reg_maps.shader_version.major >= 3
10515 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10517 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10518 entry->vs.vertex_color_clamp = GL_FALSE;
10520 else
10522 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10525 else
10527 /* With core profile we never change vertex_color_clamp from
10528 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10529 * glClampColorARB(). */
10530 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10533 /* Set the shader to allow uniform loading on it */
10534 GL_EXTCALL(glUseProgram(program_id));
10535 checkGLcall("glUseProgram");
10537 entry->constant_update_mask = 0;
10538 if (vshader)
10540 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10541 if (vshader->reg_maps.integer_constants)
10542 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10543 if (vshader->reg_maps.boolean_constants)
10544 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10545 if (entry->vs.pos_fixup_location != -1)
10546 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10547 if (entry->vs.base_vertex_id_location != -1)
10548 entry->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
10550 shader_glsl_load_program_resources(context_gl, priv, program_id, vshader);
10552 else
10554 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10555 | WINED3D_SHADER_CONST_FFP_PROJ;
10557 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10559 if (entry->vs.modelview_matrix_location[i] != -1)
10561 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10562 break;
10566 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10568 if (entry->vs.texture_matrix_location[i] != -1)
10570 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10571 break;
10574 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10575 || entry->vs.material_specular_location != -1
10576 || entry->vs.material_emissive_location != -1
10577 || entry->vs.material_shininess_location != -1)
10578 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10579 if (entry->vs.light_ambient_location != -1)
10580 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10582 if (entry->vs.clip_planes_location != -1)
10583 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10584 if (entry->vs.pointsize_min_location != -1)
10585 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10587 if (hshader)
10588 shader_glsl_load_program_resources(context_gl, priv, program_id, hshader);
10590 if (dshader)
10592 if (entry->ds.pos_fixup_location != -1)
10593 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10595 shader_glsl_load_program_resources(context_gl, priv, program_id, dshader);
10598 if (gshader)
10600 if (entry->gs.pos_fixup_location != -1)
10601 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10603 shader_glsl_load_program_resources(context_gl, priv, program_id, gshader);
10606 if (ps_id)
10608 if (pshader)
10610 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10611 if (pshader->reg_maps.integer_constants)
10612 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10613 if (pshader->reg_maps.boolean_constants)
10614 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10615 if (entry->ps.ycorrection_location != -1)
10616 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10618 shader_glsl_load_program_resources(context_gl, priv, program_id, pshader);
10619 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10621 else
10623 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10625 shader_glsl_load_samplers(&context_gl->c, priv, program_id, NULL);
10628 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10630 if (entry->ps.bumpenv_mat_location[i] != -1)
10632 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10633 break;
10637 if (entry->ps.fog_color_location != -1)
10638 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10639 if (entry->ps.alpha_test_ref_location != -1)
10640 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10641 if (entry->ps.np2_fixup_location != -1)
10642 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10643 if (entry->ps.color_key_location != -1)
10644 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10648 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10650 struct wined3d_device *device = shader->device;
10651 struct wined3d_context *context;
10653 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10655 context = context_acquire(device, NULL, 0);
10656 shader_glsl_compile_compute_shader(shader_priv, wined3d_context_gl(context), shader);
10657 context_release(context);
10661 /* Context activation is done by the caller. */
10662 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10663 const struct wined3d_state *state)
10665 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10666 struct glsl_context_data *ctx_data = context->shader_backend_data;
10667 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10668 struct shader_glsl_priv *priv = shader_priv;
10669 struct glsl_shader_prog_link *glsl_program;
10670 GLenum current_vertex_color_clamp;
10671 GLuint program_id, prev_id;
10673 priv->vertex_pipe->vp_enable(context, !use_vs(state));
10674 priv->fragment_pipe->fp_enable(context, !use_ps(state));
10676 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10677 set_glsl_shader_program(context_gl, state, priv, ctx_data);
10678 glsl_program = ctx_data->glsl_program;
10680 if (glsl_program)
10682 program_id = glsl_program->id;
10683 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10684 if (glsl_program->shader_controlled_clip_distances)
10685 wined3d_context_gl_enable_clip_distances(context_gl, glsl_program->clip_distance_mask);
10687 else
10689 program_id = 0;
10690 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10693 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10695 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10696 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10698 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10699 checkGLcall("glClampColorARB");
10701 else
10703 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10707 TRACE("Using GLSL program %u.\n", program_id);
10709 if (prev_id != program_id)
10711 GL_EXTCALL(glUseProgram(program_id));
10712 checkGLcall("glUseProgram");
10714 if (glsl_program)
10715 context->constant_update_mask |= glsl_program->constant_update_mask;
10718 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10721 /* Context activation is done by the caller. */
10722 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10723 const struct wined3d_state *state)
10725 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10726 struct glsl_context_data *ctx_data = context->shader_backend_data;
10727 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10728 struct shader_glsl_priv *priv = shader_priv;
10729 GLuint program_id, prev_id;
10731 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10732 set_glsl_compute_shader_program(context_gl, state, priv, ctx_data);
10733 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10735 TRACE("Using GLSL program %u.\n", program_id);
10737 if (prev_id != program_id)
10739 GL_EXTCALL(glUseProgram(program_id));
10740 checkGLcall("glUseProgram");
10743 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10744 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10745 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10746 | (1u << WINED3D_SHADER_TYPE_HULL)
10747 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10750 /* "context" is not necessarily the currently active context. */
10751 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10753 struct glsl_context_data *ctx_data = context->shader_backend_data;
10755 ctx_data->glsl_program = NULL;
10756 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10757 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10758 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10759 | (1u << WINED3D_SHADER_TYPE_HULL)
10760 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10761 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10764 /* Context activation is done by the caller. */
10765 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10767 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10768 struct glsl_context_data *ctx_data = context->shader_backend_data;
10769 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10770 struct shader_glsl_priv *priv = shader_priv;
10772 shader_glsl_invalidate_current_program(context);
10773 GL_EXTCALL(glUseProgram(0));
10774 checkGLcall("glUseProgram");
10776 priv->vertex_pipe->vp_enable(context, FALSE);
10777 priv->fragment_pipe->fp_enable(context, FALSE);
10779 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10781 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10782 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10783 checkGLcall("glClampColorARB");
10787 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10788 const struct glsl_shader_prog_link *program)
10790 const struct glsl_context_data *ctx_data;
10791 struct wined3d_context *context;
10792 unsigned int i;
10794 for (i = 0; i < device->context_count; ++i)
10796 context = device->contexts[i];
10797 ctx_data = context->shader_backend_data;
10799 if (ctx_data->glsl_program == program)
10800 shader_glsl_invalidate_current_program(context);
10804 static void shader_glsl_destroy(struct wined3d_shader *shader)
10806 struct glsl_shader_private *shader_data = shader->backend_data;
10807 struct wined3d_device *device = shader->device;
10808 struct shader_glsl_priv *priv = device->shader_priv;
10809 const struct wined3d_gl_info *gl_info;
10810 const struct list *linked_programs;
10811 struct wined3d_context *context;
10813 if (!shader_data || !shader_data->num_gl_shaders)
10815 heap_free(shader_data);
10816 shader->backend_data = NULL;
10817 return;
10820 context = context_acquire(device, NULL, 0);
10821 gl_info = wined3d_context_gl(context)->gl_info;
10823 TRACE("Deleting linked programs.\n");
10824 linked_programs = &shader->linked_programs;
10825 if (linked_programs->next)
10827 struct glsl_shader_prog_link *entry, *entry2;
10828 UINT i;
10830 switch (shader->reg_maps.shader_version.type)
10832 case WINED3D_SHADER_TYPE_PIXEL:
10834 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10836 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10838 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10839 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10840 checkGLcall("glDeleteShader");
10842 heap_free(shader_data->gl_shaders.ps);
10844 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10845 struct glsl_shader_prog_link, ps.shader_entry)
10847 shader_glsl_invalidate_contexts_program(device, entry);
10848 delete_glsl_program_entry(priv, gl_info, entry);
10851 break;
10854 case WINED3D_SHADER_TYPE_VERTEX:
10856 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10858 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10860 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10861 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10862 checkGLcall("glDeleteShader");
10864 heap_free(shader_data->gl_shaders.vs);
10866 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10867 struct glsl_shader_prog_link, vs.shader_entry)
10869 shader_glsl_invalidate_contexts_program(device, entry);
10870 delete_glsl_program_entry(priv, gl_info, entry);
10873 break;
10876 case WINED3D_SHADER_TYPE_HULL:
10878 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10880 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10882 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10883 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10884 checkGLcall("glDeleteShader");
10886 heap_free(shader_data->gl_shaders.hs);
10888 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10889 struct glsl_shader_prog_link, hs.shader_entry)
10891 shader_glsl_invalidate_contexts_program(device, entry);
10892 delete_glsl_program_entry(priv, gl_info, entry);
10895 break;
10898 case WINED3D_SHADER_TYPE_DOMAIN:
10900 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10902 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10904 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10905 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10906 checkGLcall("glDeleteShader");
10908 heap_free(shader_data->gl_shaders.ds);
10910 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10911 struct glsl_shader_prog_link, ds.shader_entry)
10913 shader_glsl_invalidate_contexts_program(device, entry);
10914 delete_glsl_program_entry(priv, gl_info, entry);
10917 break;
10920 case WINED3D_SHADER_TYPE_GEOMETRY:
10922 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10924 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10926 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10927 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10928 checkGLcall("glDeleteShader");
10930 heap_free(shader_data->gl_shaders.gs);
10932 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10933 struct glsl_shader_prog_link, gs.shader_entry)
10935 shader_glsl_invalidate_contexts_program(device, entry);
10936 delete_glsl_program_entry(priv, gl_info, entry);
10939 break;
10942 case WINED3D_SHADER_TYPE_COMPUTE:
10944 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10946 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10948 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10949 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10950 checkGLcall("glDeleteShader");
10952 heap_free(shader_data->gl_shaders.cs);
10954 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10955 struct glsl_shader_prog_link, cs.shader_entry)
10957 shader_glsl_invalidate_contexts_program(device, entry);
10958 delete_glsl_program_entry(priv, gl_info, entry);
10961 break;
10964 default:
10965 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10966 break;
10970 heap_free(shader->backend_data);
10971 shader->backend_data = NULL;
10973 context_release(context);
10976 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10978 const struct glsl_program_key *k = key;
10979 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10980 const struct glsl_shader_prog_link, program_lookup_entry);
10982 if (k->vs_id > prog->vs.id) return 1;
10983 else if (k->vs_id < prog->vs.id) return -1;
10985 if (k->gs_id > prog->gs.id) return 1;
10986 else if (k->gs_id < prog->gs.id) return -1;
10988 if (k->ps_id > prog->ps.id) return 1;
10989 else if (k->ps_id < prog->ps.id) return -1;
10991 if (k->hs_id > prog->hs.id) return 1;
10992 else if (k->hs_id < prog->hs.id) return -1;
10994 if (k->ds_id > prog->ds.id) return 1;
10995 else if (k->ds_id < prog->ds.id) return -1;
10997 if (k->cs_id > prog->cs.id) return 1;
10998 else if (k->cs_id < prog->cs.id) return -1;
11000 return 0;
11003 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
11005 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
11006 + constant_count * sizeof(*heap->contained)
11007 + constant_count * sizeof(*heap->positions);
11008 void *mem;
11010 if (!(mem = heap_alloc(size)))
11012 ERR("Failed to allocate memory\n");
11013 return FALSE;
11016 heap->entries = mem;
11017 heap->entries[1].version = 0;
11018 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11019 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11020 heap->positions = (unsigned int *)(heap->contained + constant_count);
11021 heap->size = 1;
11023 return TRUE;
11026 static void constant_heap_free(struct constant_heap *heap)
11028 heap_free(heap->entries);
11031 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11032 const struct wined3d_fragment_pipe_ops *fragment_pipe)
11034 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11035 struct fragment_caps fragment_caps;
11036 void *vertex_priv, *fragment_priv;
11037 struct shader_glsl_priv *priv;
11039 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11040 return E_OUTOFMEMORY;
11042 string_buffer_list_init(&priv->string_buffers);
11044 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11046 ERR("Failed to initialize vertex pipe.\n");
11047 heap_free(priv);
11048 return E_FAIL;
11051 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11053 ERR("Failed to initialize fragment pipe.\n");
11054 vertex_pipe->vp_free(device, NULL);
11055 heap_free(priv);
11056 return E_FAIL;
11059 if (!string_buffer_init(&priv->shader_buffer))
11061 ERR("Failed to initialize shader buffer.\n");
11062 goto fail;
11065 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11067 ERR("Failed to allocate memory.\n");
11068 goto fail;
11071 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11073 ERR("Failed to initialize vertex shader constant heap\n");
11074 goto fail;
11077 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11079 ERR("Failed to initialize pixel shader constant heap\n");
11080 goto fail;
11083 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11085 priv->next_constant_version = 1;
11086 priv->vertex_pipe = vertex_pipe;
11087 priv->fragment_pipe = fragment_pipe;
11088 fragment_pipe->get_caps(device->adapter, &fragment_caps);
11089 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11090 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11092 device->vertex_priv = vertex_priv;
11093 device->fragment_priv = fragment_priv;
11094 device->shader_priv = priv;
11096 return WINED3D_OK;
11098 fail:
11099 constant_heap_free(&priv->pconst_heap);
11100 constant_heap_free(&priv->vconst_heap);
11101 heap_free(priv->stack);
11102 string_buffer_free(&priv->shader_buffer);
11103 fragment_pipe->free_private(device, NULL);
11104 vertex_pipe->vp_free(device, NULL);
11105 heap_free(priv);
11106 return E_OUTOFMEMORY;
11109 /* Context activation is done by the caller. */
11110 static void shader_glsl_free(struct wined3d_device *device, struct wined3d_context *context)
11112 struct shader_glsl_priv *priv = device->shader_priv;
11114 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11115 constant_heap_free(&priv->pconst_heap);
11116 constant_heap_free(&priv->vconst_heap);
11117 heap_free(priv->stack);
11118 string_buffer_list_cleanup(&priv->string_buffers);
11119 string_buffer_free(&priv->shader_buffer);
11120 priv->fragment_pipe->free_private(device, context);
11121 priv->vertex_pipe->vp_free(device, context);
11123 heap_free(device->shader_priv);
11124 device->shader_priv = NULL;
11127 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11129 struct glsl_context_data *ctx_data;
11131 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11132 return FALSE;
11133 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11134 context->shader_backend_data = ctx_data;
11135 return TRUE;
11138 static void shader_glsl_free_context_data(struct wined3d_context *context)
11140 heap_free(context->shader_backend_data);
11143 static void shader_glsl_init_context_state(struct wined3d_context *context)
11145 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11146 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11148 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11149 checkGLcall("GL_PROGRAM_POINT_SIZE");
11152 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11154 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11155 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11156 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11158 if (shader_model_4
11159 && gl_info->supported[ARB_COMPUTE_SHADER]
11160 && gl_info->supported[ARB_CULL_DISTANCE]
11161 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11162 && gl_info->supported[ARB_GPU_SHADER5]
11163 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11164 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11165 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11166 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11167 && gl_info->supported[ARB_TESSELLATION_SHADER]
11168 && gl_info->supported[ARB_TEXTURE_GATHER]
11169 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11170 return 5;
11172 if (shader_model_4)
11173 return 4;
11175 /* Support for texldd and texldl instructions in pixel shaders is required
11176 * for SM3. */
11177 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11178 return 3;
11180 return 2;
11183 static void shader_glsl_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
11185 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11186 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11188 TRACE("Shader model %u.\n", shader_model);
11190 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11191 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11192 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11193 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11194 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11195 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11197 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11198 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11200 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11201 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11202 caps->varying_count = gl_info->limits.glsl_varyings;
11204 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11205 * Direct3D minimum requirement.
11207 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11208 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11210 * The problem is that the refrast clamps temporary results in the shader to
11211 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11212 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11213 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11214 * offer a way to query this.
11216 if (shader_model >= 4)
11217 caps->ps_1x_max_value = FLT_MAX;
11218 else
11219 caps->ps_1x_max_value = 1024.0f;
11221 /* Ideally we'd only set caps like sRGB writes here if supported by both
11222 * the shader backend and the fragment pipe, but we can get called before
11223 * shader_glsl_alloc(). */
11224 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11225 | WINED3D_SHADER_CAP_SRGB_WRITE;
11226 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
11227 caps->wined3d_caps |= WINED3D_SHADER_CAP_OUTPUT_INTERPOLATION;
11228 if (shader_glsl_full_ffp_varyings(gl_info))
11229 caps->wined3d_caps |= WINED3D_SHADER_CAP_FULL_FFP_VARYINGS;
11232 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11234 /* We support everything except YUV conversions. */
11235 return !is_complex_fixup(fixup);
11238 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11240 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11241 /* WINED3DSIH_ADD */ shader_glsl_binop,
11242 /* WINED3DSIH_AND */ shader_glsl_binop,
11243 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11244 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11245 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11246 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11247 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11248 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11249 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11250 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11251 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11252 /* WINED3DSIH_BEM */ shader_glsl_bem,
11253 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11254 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11255 /* WINED3DSIH_BREAK */ shader_glsl_break,
11256 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11257 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11258 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11259 /* WINED3DSIH_CALL */ shader_glsl_call,
11260 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11261 /* WINED3DSIH_CASE */ shader_glsl_case,
11262 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11263 /* WINED3DSIH_CND */ shader_glsl_cnd,
11264 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11265 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11266 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11267 /* WINED3DSIH_CRS */ shader_glsl_cross,
11268 /* WINED3DSIH_CUT */ shader_glsl_cut,
11269 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11270 /* WINED3DSIH_DCL */ shader_glsl_nop,
11271 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11272 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11273 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11274 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11275 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11276 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11277 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11278 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11279 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11280 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11281 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11282 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11283 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11284 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11285 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11286 /* WINED3DSIH_DCL_INPUT_PS_SGV */ shader_glsl_nop,
11287 /* WINED3DSIH_DCL_INPUT_PS_SIV */ shader_glsl_nop,
11288 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11289 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11290 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11291 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11292 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11293 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11294 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11295 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11296 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11297 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11298 /* WINED3DSIH_DCL_STREAM */ NULL,
11299 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11300 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11301 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11302 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11303 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11304 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11305 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11306 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11307 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11308 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11309 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11310 /* WINED3DSIH_DEF */ shader_glsl_nop,
11311 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11312 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11313 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11314 /* WINED3DSIH_DIV */ shader_glsl_binop,
11315 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11316 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11317 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11318 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11319 /* WINED3DSIH_DST */ shader_glsl_dst,
11320 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11321 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11322 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11323 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11324 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11325 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11326 /* WINED3DSIH_ELSE */ shader_glsl_else,
11327 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11328 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11329 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11330 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11331 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11332 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11333 /* WINED3DSIH_EQ */ shader_glsl_relop,
11334 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ shader_glsl_interpolate,
11335 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11336 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11337 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11338 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11339 /* WINED3DSIH_FCALL */ NULL,
11340 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11341 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11342 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11343 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11344 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11345 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11346 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11347 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11348 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11349 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11350 /* WINED3DSIH_GE */ shader_glsl_relop,
11351 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11352 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11353 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11354 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11355 /* WINED3DSIH_IADD */ shader_glsl_binop,
11356 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11357 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11358 /* WINED3DSIH_IF */ shader_glsl_if,
11359 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11360 /* WINED3DSIH_IGE */ shader_glsl_relop,
11361 /* WINED3DSIH_ILT */ shader_glsl_relop,
11362 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11363 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11364 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11365 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11366 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11367 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11368 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11369 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11370 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11371 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11372 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11373 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11374 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11375 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11376 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11377 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11378 /* WINED3DSIH_INE */ shader_glsl_relop,
11379 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11380 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11381 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11382 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11383 /* WINED3DSIH_LABEL */ shader_glsl_label,
11384 /* WINED3DSIH_LD */ shader_glsl_ld,
11385 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11386 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11387 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11388 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11389 /* WINED3DSIH_LIT */ shader_glsl_lit,
11390 /* WINED3DSIH_LOD */ NULL,
11391 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11392 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11393 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11394 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11395 /* WINED3DSIH_LT */ shader_glsl_relop,
11396 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11397 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11398 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11399 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11400 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11401 /* WINED3DSIH_MAD */ shader_glsl_mad,
11402 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11403 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11404 /* WINED3DSIH_MOV */ shader_glsl_mov,
11405 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11406 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11407 /* WINED3DSIH_MUL */ shader_glsl_binop,
11408 /* WINED3DSIH_NE */ shader_glsl_relop,
11409 /* WINED3DSIH_NOP */ shader_glsl_nop,
11410 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11411 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11412 /* WINED3DSIH_OR */ shader_glsl_binop,
11413 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11414 /* WINED3DSIH_POW */ shader_glsl_pow,
11415 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11416 /* WINED3DSIH_REP */ shader_glsl_rep,
11417 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11418 /* WINED3DSIH_RET */ shader_glsl_ret,
11419 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11420 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11421 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11422 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11423 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11424 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11425 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11426 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11427 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11428 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11429 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11430 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11431 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11432 /* WINED3DSIH_SAMPLE_POS */ NULL,
11433 /* WINED3DSIH_SETP */ NULL,
11434 /* WINED3DSIH_SGE */ shader_glsl_compare,
11435 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11436 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11437 /* WINED3DSIH_SLT */ shader_glsl_compare,
11438 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11439 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11440 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11441 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11442 /* WINED3DSIH_SUB */ shader_glsl_binop,
11443 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11444 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11445 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11446 /* WINED3DSIH_TEX */ shader_glsl_tex,
11447 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11448 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11449 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11450 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11451 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11452 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11453 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11454 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11455 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11456 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11457 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11458 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11459 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11460 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11461 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11462 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11463 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11464 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11465 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11466 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11467 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11468 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11469 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11470 /* WINED3DSIH_UGE */ shader_glsl_relop,
11471 /* WINED3DSIH_ULT */ shader_glsl_relop,
11472 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11473 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11474 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11475 /* WINED3DSIH_USHR */ shader_glsl_binop,
11476 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11477 /* WINED3DSIH_XOR */ shader_glsl_binop,
11480 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11481 SHADER_HANDLER hw_fct;
11483 /* Select handler */
11484 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11486 /* Unhandled opcode */
11487 if (!hw_fct)
11489 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11490 return;
11492 hw_fct(ins);
11494 shader_glsl_add_instruction_modifiers(ins);
11497 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11499 struct shader_glsl_priv *priv = shader_priv;
11501 return priv->ffp_proj_control;
11504 static uint64_t shader_glsl_shader_compile(struct wined3d_context *context, const struct wined3d_shader_desc *shader_desc,
11505 enum wined3d_shader_type shader_type)
11507 ERR("Not implemented.\n");
11508 return 0;
11511 const struct wined3d_shader_backend_ops glsl_shader_backend =
11513 shader_glsl_handle_instruction,
11514 shader_glsl_precompile,
11515 shader_glsl_select,
11516 shader_glsl_select_compute,
11517 shader_glsl_disable,
11518 shader_glsl_update_float_vertex_constants,
11519 shader_glsl_update_float_pixel_constants,
11520 shader_glsl_load_constants,
11521 shader_glsl_destroy,
11522 shader_glsl_alloc,
11523 shader_glsl_free,
11524 shader_glsl_allocate_context_data,
11525 shader_glsl_free_context_data,
11526 shader_glsl_init_context_state,
11527 shader_glsl_get_caps,
11528 shader_glsl_color_fixup_supported,
11529 shader_glsl_has_ffp_proj_control,
11530 shader_glsl_shader_compile,
11533 static void glsl_vertex_pipe_vp_enable(const struct wined3d_context *context, BOOL enable) {}
11535 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_adapter *adapter, struct wined3d_vertex_caps *caps)
11537 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11539 caps->xyzrhw = TRUE;
11540 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11541 caps->ffp_generic_attributes = TRUE;
11542 caps->max_active_lights = WINED3D_MAX_ACTIVE_LIGHTS;
11543 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11544 caps->max_vertex_blend_matrix_index = 0;
11545 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11546 | WINED3DVTXPCAPS_MATERIALSOURCE7
11547 | WINED3DVTXPCAPS_VERTEXFOG
11548 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11549 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11550 | WINED3DVTXPCAPS_LOCALVIEWER
11551 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11552 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11553 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11554 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11557 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11559 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11560 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11561 return 0;
11564 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11566 struct shader_glsl_priv *priv;
11568 if (shader_backend == &glsl_shader_backend)
11570 priv = shader_priv;
11571 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11572 return priv;
11575 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11577 return NULL;
11580 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *param)
11582 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11583 struct glsl_ffp_vertex_shader, desc.entry);
11584 struct glsl_shader_prog_link *program, *program2;
11585 struct glsl_ffp_destroy_ctx *ctx = param;
11586 const struct wined3d_gl_info *gl_info;
11588 gl_info = ctx->context_gl->gl_info;
11589 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11590 struct glsl_shader_prog_link, vs.shader_entry)
11592 delete_glsl_program_entry(ctx->priv, gl_info, program);
11594 GL_EXTCALL(glDeleteShader(shader->id));
11595 heap_free(shader);
11598 /* Context activation is done by the caller. */
11599 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device, struct wined3d_context *context)
11601 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11602 struct shader_glsl_priv *priv = device->vertex_priv;
11603 struct glsl_ffp_destroy_ctx ctx;
11605 ctx.priv = priv;
11606 ctx.context_gl = context_gl;
11607 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11610 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11611 const struct wined3d_state *state, DWORD state_id) {}
11613 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11614 const struct wined3d_state *state, DWORD state_id)
11616 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11619 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11620 const struct wined3d_state *state, DWORD state_id)
11622 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11623 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11624 BOOL specular = !!(context->stream_info.use_map & (1u << WINED3D_FFP_SPECULAR));
11625 BOOL diffuse = !!(context->stream_info.use_map & (1u << WINED3D_FFP_DIFFUSE));
11626 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11627 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11628 BOOL transformed = context->stream_info.position_transformed;
11629 BOOL wasrhw = context->last_was_rhw;
11630 unsigned int i;
11632 context->last_was_rhw = transformed;
11634 /* If the vertex declaration contains a transformed position attribute,
11635 * the draw uses the fixed function vertex pipeline regardless of any
11636 * vertex shader set by the application. */
11637 if (transformed != wasrhw
11638 || context->stream_info.swizzle_map != context->last_swizzle_map)
11639 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11641 context->last_swizzle_map = context->stream_info.swizzle_map;
11643 if (!use_vs(state))
11645 if (context->last_was_vshader)
11647 if (legacy_clip_planes)
11648 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11649 clipplane(context, state, STATE_CLIPPLANE(i));
11650 else
11651 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11654 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11656 /* Because of settings->texcoords, we have to regenerate the vertex
11657 * shader on a vdecl change if there aren't enough varyings to just
11658 * always output all the texture coordinates.
11660 * Likewise, we have to invalidate the shader when using per-vertex
11661 * colours and diffuse/specular attribute presence changes, or when
11662 * normal presence changes. */
11663 if (!shader_glsl_full_ffp_varyings(gl_info) || (state->render_states[WINED3D_RS_COLORVERTEX]
11664 && (diffuse != context->last_was_diffuse || specular != context->last_was_specular))
11665 || normal != context->last_was_normal)
11666 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11668 if (use_ps(state)
11669 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11670 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11671 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11673 else
11675 if (!context->last_was_vshader)
11677 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11678 if (legacy_clip_planes)
11679 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11680 clipplane(context, state, STATE_CLIPPLANE(i));
11681 else
11682 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11686 context->last_was_vshader = use_vs(state);
11687 context->last_was_diffuse = diffuse;
11688 context->last_was_specular = specular;
11689 context->last_was_normal = normal;
11692 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11693 const struct wined3d_state *state, DWORD state_id)
11695 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11696 /* Different vertex shaders potentially require a different vertex attributes setup. */
11697 if (!isStateDirty(context, STATE_VDECL))
11698 context_apply_state(context, state, STATE_VDECL);
11701 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11702 const struct wined3d_state *state, DWORD state_id)
11704 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11705 * winding) are defined in Hull Shaders, while in GLSL those are
11706 * specified in Tessellation Evaluation Shaders. */
11707 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11709 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11710 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11713 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11714 const struct wined3d_state *state, DWORD state_id)
11716 struct glsl_context_data *ctx_data = context->shader_backend_data;
11717 BOOL rasterization_disabled;
11719 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11720 if (ctx_data->rasterization_disabled != rasterization_disabled)
11721 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11722 ctx_data->rasterization_disabled = rasterization_disabled;
11724 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11725 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11726 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11727 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11728 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11731 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11732 const struct wined3d_state *state, DWORD state_id)
11734 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11735 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11736 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11737 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11738 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11739 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11740 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11743 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11744 const struct wined3d_state *state, DWORD state_id)
11746 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11749 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11750 const struct wined3d_state *state, DWORD state_id)
11752 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11755 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11757 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11758 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11759 unsigned int k;
11761 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11762 | WINED3D_SHADER_CONST_FFP_LIGHTS
11763 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11765 if (needs_legacy_glsl_syntax(gl_info))
11767 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11769 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11770 clipplane(context, state, STATE_CLIPPLANE(k));
11773 else
11775 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11779 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11780 const struct wined3d_state *state, DWORD state_id)
11782 /* Table fog behavior depends on the projection matrix. */
11783 if (state->render_states[WINED3D_RS_FOGENABLE]
11784 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11785 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11786 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11789 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11790 const struct wined3d_state *state, DWORD state_id)
11792 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11793 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11794 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11795 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11796 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11797 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11800 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11801 const struct wined3d_state *state, DWORD state_id)
11803 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11806 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11807 const struct wined3d_state *state, DWORD state_id)
11809 DWORD sampler = state_id - STATE_SAMPLER(0);
11810 const struct wined3d_texture *texture = state->textures[sampler];
11811 BOOL np2;
11813 if (!texture)
11814 return;
11816 if (sampler >= WINED3D_MAX_TEXTURES)
11817 return;
11819 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11820 || context->lastWasPow2Texture & (1u << sampler))
11822 if (np2)
11823 context->lastWasPow2Texture |= 1u << sampler;
11824 else
11825 context->lastWasPow2Texture &= ~(1u << sampler);
11827 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11831 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11832 const struct wined3d_state *state, DWORD state_id)
11834 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11837 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11838 const struct wined3d_state *state, DWORD state_id)
11840 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11843 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11844 const struct wined3d_state *state, DWORD state_id)
11846 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11849 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11850 const struct wined3d_state *state, DWORD state_id)
11852 if (!use_vs(state))
11853 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11856 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11857 const struct wined3d_state *state, DWORD state_id)
11859 static unsigned int once;
11861 if (state->primitive_type == WINED3D_PT_POINTLIST
11862 && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11863 FIXME("Non-point sprite points not supported in core profile.\n");
11866 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11867 const struct wined3d_state *state, DWORD state_id)
11869 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11872 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11873 const struct wined3d_state *state, DWORD state_id)
11875 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11876 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11877 UINT index = state_id - STATE_CLIPPLANE(0);
11879 if (index >= gl_info->limits.user_clip_distances)
11880 return;
11882 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11885 static const struct wined3d_state_entry_template glsl_vertex_pipe_vp_states[] =
11887 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11888 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11889 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11890 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11891 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11892 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11893 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11894 /* Clip planes */
11895 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11896 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11897 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11898 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11899 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11900 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11901 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11902 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11903 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11904 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11905 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11906 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11907 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11908 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11909 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11910 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11911 /* Lights */
11912 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11913 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11914 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11915 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11916 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11917 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11918 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11919 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11920 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11921 /* Viewport */
11922 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11923 /* Transform states */
11924 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11925 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11926 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11927 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11928 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11929 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11930 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11931 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11932 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11933 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11934 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11935 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11936 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11937 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11938 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11939 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11940 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11941 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11942 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11943 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11944 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11945 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11946 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11947 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11948 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11949 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11950 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11951 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11952 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11953 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11954 /* Fog */
11955 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11956 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11957 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11958 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11959 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11960 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11961 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11962 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11963 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11964 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11965 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11966 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11967 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11968 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11969 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11970 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11971 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11972 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11973 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11974 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11975 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11976 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11977 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11978 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11979 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11980 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11981 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11982 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11983 /* NP2 texture matrix fixups. They are not needed if
11984 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11985 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11986 * matrix. */
11987 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11988 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11989 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11990 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11991 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11992 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11993 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11994 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11995 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11996 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11997 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11998 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11999 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12000 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12001 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12002 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12003 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12004 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12005 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12006 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12007 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12008 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12009 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12010 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12011 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12012 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
12013 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
12014 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
12017 /* TODO:
12018 * - Implement vertex tweening. */
12019 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
12021 glsl_vertex_pipe_vp_enable,
12022 glsl_vertex_pipe_vp_get_caps,
12023 glsl_vertex_pipe_vp_get_emul_mask,
12024 glsl_vertex_pipe_vp_alloc,
12025 glsl_vertex_pipe_vp_free,
12026 glsl_vertex_pipe_vp_states,
12029 static void glsl_fragment_pipe_enable(const struct wined3d_context *context, BOOL enable)
12031 /* Nothing to do. */
12034 static void glsl_fragment_pipe_get_caps(const struct wined3d_adapter *adapter, struct fragment_caps *caps)
12036 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
12038 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
12039 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
12040 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
12041 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12042 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12043 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12044 | WINED3DTEXOPCAPS_SELECTARG1
12045 | WINED3DTEXOPCAPS_SELECTARG2
12046 | WINED3DTEXOPCAPS_MODULATE4X
12047 | WINED3DTEXOPCAPS_MODULATE2X
12048 | WINED3DTEXOPCAPS_MODULATE
12049 | WINED3DTEXOPCAPS_ADDSIGNED2X
12050 | WINED3DTEXOPCAPS_ADDSIGNED
12051 | WINED3DTEXOPCAPS_ADD
12052 | WINED3DTEXOPCAPS_SUBTRACT
12053 | WINED3DTEXOPCAPS_ADDSMOOTH
12054 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12055 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12056 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12057 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12058 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12059 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12060 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12061 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12062 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12063 | WINED3DTEXOPCAPS_DOTPRODUCT3
12064 | WINED3DTEXOPCAPS_MULTIPLYADD
12065 | WINED3DTEXOPCAPS_LERP
12066 | WINED3DTEXOPCAPS_BUMPENVMAP
12067 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12068 caps->MaxTextureBlendStages = WINED3D_MAX_TEXTURES;
12069 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], WINED3D_MAX_TEXTURES);
12072 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
12074 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
12075 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12076 return 0;
12079 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12081 struct shader_glsl_priv *priv;
12083 if (shader_backend == &glsl_shader_backend)
12085 priv = shader_priv;
12086 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12087 return priv;
12090 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12092 return NULL;
12095 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *param)
12097 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12098 struct glsl_ffp_fragment_shader, entry.entry);
12099 struct glsl_shader_prog_link *program, *program2;
12100 struct glsl_ffp_destroy_ctx *ctx = param;
12101 const struct wined3d_gl_info *gl_info;
12103 gl_info = ctx->context_gl->gl_info;
12104 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12105 struct glsl_shader_prog_link, ps.shader_entry)
12107 delete_glsl_program_entry(ctx->priv, gl_info, program);
12109 GL_EXTCALL(glDeleteShader(shader->id));
12110 heap_free(shader);
12113 /* Context activation is done by the caller. */
12114 static void glsl_fragment_pipe_free(struct wined3d_device *device, struct wined3d_context *context)
12116 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12117 struct shader_glsl_priv *priv = device->fragment_priv;
12118 struct glsl_ffp_destroy_ctx ctx;
12120 ctx.priv = priv;
12121 ctx.context_gl = context_gl;
12122 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12125 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12126 const struct wined3d_state *state, DWORD state_id)
12128 context->last_was_pshader = use_ps(state);
12130 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12133 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12134 const struct wined3d_state *state, DWORD state_id)
12136 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12139 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12140 const struct wined3d_state *state, DWORD state_id)
12142 BOOL use_vshader = use_vs(state);
12143 enum fogsource new_source;
12144 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12145 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12147 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12149 if (!state->render_states[WINED3D_RS_FOGENABLE])
12150 return;
12152 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12154 if (use_vshader)
12155 new_source = FOGSOURCE_VS;
12156 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12157 new_source = FOGSOURCE_COORD;
12158 else
12159 new_source = FOGSOURCE_FFP;
12161 else
12163 new_source = FOGSOURCE_FFP;
12166 if (new_source != context->fog_source || fogstart == fogend)
12168 context->fog_source = new_source;
12169 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12173 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12174 const struct wined3d_state *state, DWORD state_id)
12176 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12178 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12179 if (!shader_glsl_full_ffp_varyings(gl_info))
12180 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12182 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12183 glsl_fragment_pipe_fog(context, state, state_id);
12186 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12187 const struct wined3d_state *state, DWORD state_id)
12189 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12191 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12192 if (!shader_glsl_full_ffp_varyings(gl_info))
12193 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12196 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12197 const struct wined3d_state *state, DWORD state_id)
12199 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12202 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12203 const struct wined3d_state *state, DWORD state_id)
12205 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12208 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12209 const struct wined3d_state *state, DWORD state_id)
12211 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12212 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12213 float ref = wined3d_alpha_ref(state);
12215 if (func)
12217 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12218 checkGLcall("glAlphaFunc");
12222 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12223 const struct wined3d_state *state, DWORD state_id)
12225 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12228 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12229 const struct wined3d_state *state, DWORD state_id)
12231 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12233 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12235 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12236 checkGLcall("glEnable(GL_ALPHA_TEST)");
12238 else
12240 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12241 checkGLcall("glDisable(GL_ALPHA_TEST)");
12245 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12246 const struct wined3d_state *state, DWORD state_id)
12248 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12251 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12252 const struct wined3d_state *state, DWORD state_id)
12254 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12257 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12258 const struct wined3d_state *state, DWORD state_id)
12260 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12263 static const struct wined3d_state_entry_template glsl_fragment_pipe_state_template[] =
12265 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12266 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12267 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12268 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12269 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12270 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12271 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12272 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12273 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12274 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12275 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12276 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12277 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12278 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12279 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12280 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12281 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12282 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12283 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12284 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12285 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12286 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12287 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12288 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12289 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12290 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12291 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12292 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12293 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12294 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12295 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12296 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12297 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12298 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12299 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12300 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12301 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12302 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12303 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12304 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12305 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12306 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12307 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12308 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12309 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12310 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12311 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12312 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12313 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12314 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12315 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12316 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12317 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12318 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12319 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12320 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12321 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12322 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12323 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12324 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12325 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12326 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12327 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12328 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12329 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12330 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12331 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12332 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12333 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12334 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12335 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12336 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12337 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12338 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12339 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12340 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12341 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12342 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12343 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12344 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12345 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12346 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12347 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12348 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12349 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12350 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12351 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12352 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12353 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12354 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12355 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12356 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12357 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12358 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12359 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12360 {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 },
12361 {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 },
12362 {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 },
12363 {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 },
12364 {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 },
12365 {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 },
12366 {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 },
12367 {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 },
12368 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12369 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12370 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12371 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12372 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12373 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12374 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12375 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12376 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12377 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12378 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12379 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12380 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12383 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12385 return TRUE;
12388 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12392 const struct wined3d_fragment_pipe_ops glsl_fragment_pipe =
12394 glsl_fragment_pipe_enable,
12395 glsl_fragment_pipe_get_caps,
12396 glsl_fragment_pipe_get_emul_mask,
12397 glsl_fragment_pipe_alloc,
12398 glsl_fragment_pipe_free,
12399 glsl_fragment_pipe_alloc_context_data,
12400 glsl_fragment_pipe_free_context_data,
12401 shader_glsl_color_fixup_supported,
12402 glsl_fragment_pipe_state_template,
12405 struct glsl_blitter_args
12407 GLenum texture_type;
12408 struct color_fixup_desc fixup;
12409 unsigned short use_colour_key;
12412 struct glsl_blitter_program
12414 struct wine_rb_entry entry;
12415 struct glsl_blitter_args args;
12416 GLuint id;
12419 struct wined3d_glsl_blitter
12421 struct wined3d_blitter blitter;
12422 struct wined3d_string_buffer_list string_buffers;
12423 struct wine_rb_tree programs;
12424 GLuint palette_texture;
12427 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12429 const struct glsl_blitter_args *a = key;
12430 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12432 return memcmp(a, b, sizeof(*a));
12435 /* Context activation is done by the caller. */
12436 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12438 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12439 struct wined3d_context_gl *context_gl = ctx;
12440 const struct wined3d_gl_info *gl_info;
12442 gl_info = context_gl->gl_info;
12443 GL_EXTCALL(glDeleteProgram(program->id));
12444 checkGLcall("glDeleteProgram()");
12445 heap_free(program);
12448 /* Context activation is done by the caller. */
12449 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12451 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12452 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12453 struct wined3d_glsl_blitter *glsl_blitter;
12454 struct wined3d_blitter *next;
12456 if ((next = blitter->next))
12457 next->ops->blitter_destroy(next, context);
12459 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12461 if (glsl_blitter->palette_texture)
12462 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12464 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context_gl);
12465 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12467 heap_free(glsl_blitter);
12470 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12471 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12472 const char *output, const char *tex_type, const char *swizzle)
12474 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12475 shader_addline(buffer, "\nvoid main()\n{\n");
12476 /* The alpha-component contains the palette index. */
12477 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12478 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12479 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12480 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12481 * the middle. */
12482 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12483 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12484 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12485 if (args->use_colour_key)
12486 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12487 shader_addline(buffer, "}\n");
12490 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12491 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12492 const char *tex_type)
12494 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12495 char chroma, luminance;
12496 const char *tex;
12498 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12499 * macropixel, giving effectively 16 bits per pixel. The color consists of
12500 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12501 * luminance values, one for each single pixel it contains, and one U and
12502 * one V value shared between both pixels.
12504 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12505 * component contains the luminance and alpha the chroma. With UYVY it is
12506 * vice versa. Thus take the format into account when generating the read
12507 * swizzles
12509 * Reading the Y value is straightforward - just sample the texture. The
12510 * hardware takes care of filtering in the horizontal and vertical
12511 * direction.
12513 * Reading the U and V values is harder. We have to avoid filtering
12514 * horizontally, because that would mix the U and V values of one pixel or
12515 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12516 * get an unfiltered read, regardless of the filtering setting. Vertical
12517 * filtering works automatically though - the U and V values of two rows
12518 * are mixed nicely.
12520 * Apart of avoiding filtering issues, the code has to know which value it
12521 * just read, and where it can find the other one. To determine this, it
12522 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12523 * accordingly.
12525 * Handling horizontal filtering of U and V values requires reading a 2nd
12526 * pair of pixels, extracting U and V and mixing them. This is not
12527 * implemented yet.
12529 * An alternative implementation idea is to load the texture as A8R8G8B8
12530 * texture, with width / 2. This way one read gives all 3 values, finding
12531 * U and V is easy in an unfiltered situation. Finding the luminance on
12532 * the other hand requires finding out if it is an odd or even pixel. The
12533 * real drawback of this approach is filtering. This would have to be
12534 * emulated completely in the shader, reading up two 2 packed pixels in up
12535 * to 2 rows and interpolating both horizontally and vertically. Beyond
12536 * that it would require adjustments to the texture handling code to deal
12537 * with the width scaling. */
12539 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12541 chroma = 'x';
12542 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12544 else
12546 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12547 luminance = 'x';
12550 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12552 /* First we have to read the chroma values. This means we need at least
12553 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12554 * unmodified chroma, we have to rid ourselves of the filtering when we
12555 * sample the texture. */
12556 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12557 /* We must not allow filtering between pixel x and x+1, this would mix U
12558 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12559 * center is at 0.5, so add 0.5. */
12560 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12561 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12563 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12564 * and 0.75 for the even and odd pixels respectively. */
12565 /* Put the value into either of the chroma values. */
12566 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12567 shader_addline(buffer, " if (even)\n");
12568 shader_addline(buffer, " chroma.y = luminance;\n");
12569 shader_addline(buffer, " else\n");
12570 shader_addline(buffer, " chroma.x = luminance;\n");
12572 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12573 * current one. Otherwise, sample the left pixel. */
12574 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12575 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12577 /* Put the value into the other chroma. */
12578 shader_addline(buffer, " if (even)\n");
12579 shader_addline(buffer, " chroma.x = luminance;\n");
12580 shader_addline(buffer, " else\n");
12581 shader_addline(buffer, " chroma.y = luminance;\n");
12583 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12584 * the current one and lerp the two U and V values. */
12586 /* This gives the correctly filtered luminance value. */
12587 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12590 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12591 const struct wined3d_gl_info *gl_info, const char *tex_type)
12593 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12594 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12596 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12597 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12598 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12599 * have only half the pitch of the luminance plane, the packing into the
12600 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12601 * luminance data it looks approximately like this:
12603 * +----------------------------------+----
12604 * | |
12605 * | |
12606 * | |
12607 * | |
12608 * | | 2
12609 * | LUMINANCE | -
12610 * | | 3
12611 * | |
12612 * | |
12613 * | |
12614 * | |
12615 * +----------------+-----------------+----
12616 * | | |
12617 * | V even rows | V odd rows |
12618 * | | | 1
12619 * +----------------+------------------ -
12620 * | | | 3
12621 * | U even rows | U odd rows |
12622 * | | |
12623 * +----------------+-----------------+----
12624 * | | |
12625 * | 0.5 | 0.5 |
12627 * So it appears as if there are 4 chroma images, but in fact the odd rows
12628 * in the chroma images are in the same row as the even ones. So it is
12629 * kinda tricky to read. */
12631 /* First sample the chroma values. */
12632 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12633 /* The chroma planes have only half the width. */
12634 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12636 /* The first value is between 2/3 and 5/6 of the texture's height, so
12637 * scale+bias the coordinate. Also read the right side of the image when
12638 * reading odd lines.
12640 * Don't forget to clamp the y values in into the range, otherwise we'll
12641 * get filtering bleeding. */
12643 /* Read odd lines from the right side (add 0.5 to the x coordinate). Keep
12644 * in mind that each line of the chroma plane corresponds to 2 lines of the
12645 * resulting image. */
12646 shader_addline(buffer, " if (fract(texcoord.y * size.y * 0.25) >= 0.5)\n");
12647 shader_addline(buffer, " texcoord.x += 0.5;\n");
12649 /* Clamp, keep the half pixel origin in mind. */
12650 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12651 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12653 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12655 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12656 * 6/6th No need to clamp because we're just reusing the already clamped
12657 * value from above. */
12658 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12659 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12661 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12662 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12663 * values from bleeding into the sampled luminance values due to
12664 * filtering. */
12665 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12666 /* Multiply the y coordinate by 2/3 and clamp it. */
12667 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12668 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12671 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12672 const struct wined3d_gl_info *gl_info, const char *tex_type)
12674 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12675 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12677 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12678 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12679 * effective bitdepth is 12 bits per pixel. If the whole texture is
12680 * interpreted as luminance data it looks approximately like this:
12682 * +----------------------------------+----
12683 * | |
12684 * | |
12685 * | |
12686 * | |
12687 * | | 2
12688 * | LUMINANCE | -
12689 * | | 3
12690 * | |
12691 * | |
12692 * | |
12693 * | |
12694 * +----------------------------------+----
12695 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12696 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12697 * | | 1
12698 * | | -
12699 * | | 3
12700 * | |
12701 * | |
12702 * +----------------------------------+---- */
12704 /* First sample the chroma values. */
12705 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12706 /* We only have half the number of chroma pixels. */
12707 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12708 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12710 /* We must not allow filtering horizontally, this would mix U and V.
12711 * Vertical filtering is ok. However, bear in mind that the pixel center
12712 * is at 0.5, so add 0.5. */
12714 /* Convert to non-normalised coordinates so we can find the individual
12715 * pixel. */
12716 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12717 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12718 * 0.5 to hit the center of the pixel. Then convert back to normalised
12719 * coordinates. */
12720 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12721 /* Clamp, keep the half pixel origin in mind. */
12722 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12724 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12725 /* Add 1.0 / size.x to sample the adjacent texel. */
12726 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12727 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12729 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12730 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12731 * values from bleeding into the sampled luminance values due to
12732 * filtering. */
12733 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12734 /* Multiply the y coordinate by 2/3 and clamp it. */
12735 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12736 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12739 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12740 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12741 const char *output, const char *tex_type, const char *swizzle)
12743 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12745 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12746 shader_addline(buffer, "float luminance;\n");
12747 shader_addline(buffer, "vec2 texcoord;\n");
12748 shader_addline(buffer, "vec2 chroma;\n");
12749 shader_addline(buffer, "uniform vec2 size;\n");
12751 shader_addline(buffer, "\nvoid main()\n{\n");
12753 switch (complex_fixup)
12755 case COMPLEX_FIXUP_UYVY:
12756 case COMPLEX_FIXUP_YUY2:
12757 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12758 break;
12760 case COMPLEX_FIXUP_YV12:
12761 gen_yv12_read(buffer, gl_info, tex_type);
12762 break;
12764 case COMPLEX_FIXUP_NV12:
12765 gen_nv12_read(buffer, gl_info, tex_type);
12766 break;
12768 case COMPLEX_FIXUP_YUV:
12769 /* With APPLE_rgb_422, things are much simpler. The only thing we
12770 * have to do here is Y'CbCr to RGB conversion. */
12771 shader_addline(buffer, " vec3 yuv = vec3(texture%s(sampler, out_texcoord.xy));\n",
12772 needs_legacy_glsl_syntax(gl_info) ? tex_type : "");
12773 shader_addline(buffer, " luminance = yuv.y;\n");
12774 shader_addline(buffer, " chroma = yuv.xz;\n");
12775 break;
12777 default:
12778 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12779 string_buffer_free(buffer);
12780 return;
12783 /* Calculate the final result. Formula is taken from
12784 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12785 * ranges from -0.5 to 0.5. */
12786 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12788 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12789 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12790 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12791 if (args->use_colour_key)
12792 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12794 shader_addline(buffer, "}\n");
12797 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12798 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12799 const char *output, const char *tex_type, const char *swizzle)
12801 shader_addline(buffer, "\nvoid main()\n{\n");
12802 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12803 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12804 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12805 if (args->use_colour_key)
12806 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12807 shader_addline(buffer, "}\n");
12810 /* Context activation is done by the caller. */
12811 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12812 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12814 static const struct
12816 GLenum texture_target;
12817 const char texture_type[7];
12818 const char texcoord_swizzle[4];
12820 texture_data[] =
12822 {GL_TEXTURE_2D, "2D", "xy"},
12823 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12824 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12826 static const char vshader_main[] =
12827 "\n"
12828 "void main()\n"
12829 "{\n"
12830 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12831 " out_texcoord = texcoord;\n"
12832 "}\n";
12833 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12834 struct wined3d_string_buffer *buffer, *output;
12835 GLuint program, vshader_id, fshader_id;
12836 const char *tex_type = NULL, *swizzle = NULL, *ptr;
12837 unsigned int i;
12838 GLint loc;
12840 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12842 if (args->texture_type == texture_data[i].texture_target)
12844 tex_type = texture_data[i].texture_type;
12845 swizzle = texture_data[i].texcoord_swizzle;
12846 break;
12849 if (i == ARRAY_SIZE(texture_data))
12851 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12852 return 0;
12855 program = GL_EXTCALL(glCreateProgram());
12857 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12859 buffer = string_buffer_get(&blitter->string_buffers);
12860 shader_glsl_add_version_declaration(buffer, gl_info);
12861 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12862 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12863 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12864 shader_addline(buffer, vshader_main);
12866 ptr = buffer->buffer;
12867 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12868 GL_EXTCALL(glAttachShader(program, vshader_id));
12869 GL_EXTCALL(glDeleteShader(vshader_id));
12871 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12873 string_buffer_clear(buffer);
12874 shader_glsl_add_version_declaration(buffer, gl_info);
12875 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12876 if (args->use_colour_key)
12878 shader_addline(buffer, "uniform struct\n{\n");
12879 shader_addline(buffer, " vec4 low, high;\n");
12880 shader_addline(buffer, "} colour_key;\n");
12882 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12883 /* TODO: Declare the out variable with the correct type (and put it in the
12884 * blitter args). */
12885 if (!use_legacy_fragment_output(gl_info))
12886 shader_addline(buffer, "out vec4 ps_out[1];\n");
12888 output = string_buffer_get(&blitter->string_buffers);
12889 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12891 switch (complex_fixup)
12893 case COMPLEX_FIXUP_P8:
12894 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12895 break;
12896 case COMPLEX_FIXUP_YUY2:
12897 case COMPLEX_FIXUP_UYVY:
12898 case COMPLEX_FIXUP_YV12:
12899 case COMPLEX_FIXUP_NV12:
12900 case COMPLEX_FIXUP_YUV:
12901 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12902 break;
12903 case COMPLEX_FIXUP_NONE:
12904 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12907 string_buffer_release(&blitter->string_buffers, output);
12909 ptr = buffer->buffer;
12910 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12911 string_buffer_release(&blitter->string_buffers, buffer);
12912 GL_EXTCALL(glAttachShader(program, fshader_id));
12913 GL_EXTCALL(glDeleteShader(fshader_id));
12915 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12916 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12918 if (!use_legacy_fragment_output(gl_info))
12919 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12921 GL_EXTCALL(glCompileShader(vshader_id));
12922 print_glsl_info_log(gl_info, vshader_id, FALSE);
12923 GL_EXTCALL(glCompileShader(fshader_id));
12924 print_glsl_info_log(gl_info, fshader_id, FALSE);
12925 GL_EXTCALL(glLinkProgram(program));
12926 shader_glsl_validate_link(gl_info, program);
12928 GL_EXTCALL(glUseProgram(program));
12929 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12930 GL_EXTCALL(glUniform1i(loc, 0));
12931 if (complex_fixup == COMPLEX_FIXUP_P8)
12933 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12934 GL_EXTCALL(glUniform1i(loc, 1));
12937 return program;
12940 /* Context activation is done by the caller. */
12941 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12942 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl)
12944 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12945 const struct wined3d_palette *palette;
12947 palette = texture_gl->t.swapchain ? texture_gl->t.swapchain->palette : NULL;
12949 if (!blitter->palette_texture)
12950 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12952 wined3d_context_gl_active_texture(context_gl, gl_info, 1);
12953 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12954 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12955 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12956 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12958 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
12959 checkGLcall("glBindBuffer");
12961 if (palette)
12963 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12964 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12966 else
12968 static const DWORD black;
12970 FIXME("P8 texture loaded without a palette.\n");
12971 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
12972 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
12975 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
12978 /* Context activation is done by the caller. */
12979 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
12980 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl, BOOL use_colour_key)
12982 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12983 struct glsl_blitter_program *program;
12984 struct glsl_blitter_args args;
12985 struct wine_rb_entry *entry;
12987 memset(&args, 0, sizeof(args));
12988 args.texture_type = texture_gl->target;
12989 args.fixup = texture_gl->t.resource.format->color_fixup;
12990 args.use_colour_key = use_colour_key;
12992 if ((entry = wine_rb_get(&blitter->programs, &args)))
12993 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12995 if (!(program = heap_alloc(sizeof(*program))))
12997 ERR("Failed to allocate blitter program memory.\n");
12998 return NULL;
13001 program->args = args;
13002 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
13004 WARN("Failed to generate blitter program.\n");
13005 heap_free(program);
13006 return NULL;
13009 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
13011 ERR("Failed to store blitter program.\n");
13012 GL_EXTCALL(glDeleteProgram(program->id));
13013 heap_free(program);
13014 return NULL;
13017 return program;
13020 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
13021 const struct wined3d_texture_gl *src_texture, DWORD src_location,
13022 const struct wined3d_texture_gl *dst_texture, DWORD dst_location)
13024 const struct wined3d_resource *src_resource = &src_texture->t.resource;
13025 const struct wined3d_resource *dst_resource = &dst_texture->t.resource;
13026 const struct wined3d_format *src_format = src_resource->format;
13027 const struct wined3d_format *dst_format = dst_resource->format;
13028 bool src_ds, dst_ds;
13029 BOOL decompress;
13031 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13033 src_ds = src_format->depth_size || src_format->stencil_size;
13034 dst_ds = dst_format->depth_size || dst_format->stencil_size;
13035 /* We could in principle support raw depth/stencil <-> colour blits as
13036 * well in some cases, but note that typeless formats like e.g.
13037 * R16_TYPELESS may use a normalised GL format for depth/stencil
13038 * resources, and a floating-point format for colour resources, */
13039 if (src_ds && dst_ds)
13040 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13041 else if (!src_ds && !dst_ds)
13042 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13045 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY
13046 && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13048 TRACE("Unsupported blit_op %#x.\n", blit_op);
13049 return FALSE;
13052 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13053 return FALSE;
13055 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13056 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13058 TRACE("Multi-sample source textures not supported.\n");
13059 return FALSE;
13062 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13064 TRACE("Destination resource does not have GPU access.\n");
13065 return FALSE;
13068 /* We don't necessarily want to blit from resources without
13069 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13070 * compressed textures. */
13071 decompress = (src_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
13072 && !(dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED);
13073 if (!decompress && !(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13075 TRACE("Source resource does not have GPU access.\n");
13076 return FALSE;
13079 if (!is_identity_fixup(dst_format->color_fixup)
13080 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13082 TRACE("Destination fixups are not supported.\n");
13083 return FALSE;
13086 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
13087 && !((dst_format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_FBO_ATTACHABLE)
13088 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
13090 TRACE("Destination texture is not FBO attachable.\n");
13091 return FALSE;
13094 TRACE("Returning supported.\n");
13095 return TRUE;
13098 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13099 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13100 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13101 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13102 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
13103 const struct wined3d_format *resolve_format)
13105 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
13106 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
13107 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
13108 struct wined3d_device *device = dst_texture->resource.device;
13109 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13110 struct wined3d_texture *staging_texture = NULL;
13111 struct wined3d_glsl_blitter *glsl_blitter;
13112 struct wined3d_color_key alpha_test_key;
13113 struct glsl_blitter_program *program;
13114 struct wined3d_blitter *next;
13115 unsigned int src_level;
13116 GLint location;
13117 RECT s, d;
13119 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
13120 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
13121 "colour_key %p, filter %s, resolve format %p.\n",
13122 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13123 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13124 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
13126 if (!glsl_blitter_supported(op, context, src_texture_gl, src_location, dst_texture_gl, dst_location))
13128 if (!(next = blitter->next))
13130 ERR("No blitter to handle blit op %#x.\n", op);
13131 return dst_location;
13134 TRACE("Forwarding to blitter %p.\n", next);
13135 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13136 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
13137 resolve_format);
13140 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13142 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13144 struct wined3d_resource_desc desc;
13145 struct wined3d_box upload_box;
13146 HRESULT hr;
13148 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13150 src_level = src_sub_resource_idx % src_texture->level_count;
13151 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13152 desc.format = src_texture->resource.format->id;
13153 desc.multisample_type = src_texture->resource.multisample_type;
13154 desc.multisample_quality = src_texture->resource.multisample_quality;
13155 desc.usage = WINED3DUSAGE_PRIVATE;
13156 desc.bind_flags = 0;
13157 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13158 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13159 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13160 desc.depth = 1;
13161 desc.size = 0;
13163 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13164 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13166 ERR("Failed to create staging texture, hr %#x.\n", hr);
13167 return dst_location;
13170 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13171 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13172 src_texture, src_sub_resource_idx, &upload_box);
13174 src_texture = staging_texture;
13175 src_texture_gl = wined3d_texture_gl(src_texture);
13176 src_sub_resource_idx = 0;
13178 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13179 && (src_texture->sub_resources[src_sub_resource_idx].locations
13180 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13181 && !wined3d_resource_is_offscreen(&src_texture->resource))
13184 /* Without FBO blits transferring from the drawable to the texture is
13185 * expensive, because we have to flip the data in sysmem. Since we can
13186 * flip in the blitter, we don't actually need that flip anyway. So we
13187 * use the surface's texture as scratch texture, and flip the source
13188 * rectangle instead. */
13189 texture2d_load_fb_texture(src_texture_gl, src_sub_resource_idx, FALSE, context);
13191 s = *src_rect;
13192 src_level = src_sub_resource_idx % src_texture->level_count;
13193 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13194 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13195 src_rect = &s;
13197 else
13199 wined3d_texture_load(src_texture, context, FALSE);
13202 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
13203 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13204 else
13205 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13207 wined3d_context_gl_apply_blit_state(context_gl, device);
13209 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13211 d = *dst_rect;
13212 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
13213 dst_rect = &d;
13216 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
13218 GLenum buffer;
13220 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13222 TRACE("Destination texture %p is onscreen.\n", dst_texture);
13223 buffer = wined3d_texture_get_gl_buffer(dst_texture);
13225 else
13227 TRACE("Destination texture %p is offscreen.\n", dst_texture);
13228 buffer = GL_COLOR_ATTACHMENT0;
13230 wined3d_context_gl_apply_fbo_state_blit(context_gl, GL_DRAW_FRAMEBUFFER,
13231 &dst_texture->resource, dst_sub_resource_idx, NULL, 0, dst_location);
13232 wined3d_context_gl_set_draw_buffer(context_gl, buffer);
13233 wined3d_context_gl_check_fbo_status(context_gl, GL_DRAW_FRAMEBUFFER);
13234 context_invalidate_state(context, STATE_FRAMEBUFFER);
13237 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13239 const struct wined3d_format *f = src_texture->resource.format;
13241 alpha_test_key.color_space_low_value = 0;
13242 alpha_test_key.color_space_high_value = ~(wined3d_mask_from_size(f->alpha_size) << f->alpha_offset);
13243 colour_key = &alpha_test_key;
13245 else if (op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY)
13247 colour_key = NULL;
13250 if (!(program = glsl_blitter_get_program(glsl_blitter, context_gl, src_texture_gl, !!colour_key)))
13252 ERR("Failed to get blitter program.\n");
13253 return dst_location;
13255 GL_EXTCALL(glUseProgram(program->id));
13256 switch (get_complex_fixup(program->args.fixup))
13258 case COMPLEX_FIXUP_P8:
13259 glsl_blitter_upload_palette(glsl_blitter, context_gl, src_texture_gl);
13260 break;
13262 case COMPLEX_FIXUP_YUY2:
13263 case COMPLEX_FIXUP_UYVY:
13264 case COMPLEX_FIXUP_YV12:
13265 case COMPLEX_FIXUP_NV12:
13266 case COMPLEX_FIXUP_YUV:
13267 src_level = src_sub_resource_idx % src_texture->level_count;
13268 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13269 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13270 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13271 break;
13273 default:
13274 break;
13276 if (colour_key)
13278 struct wined3d_color float_key[2];
13280 wined3d_format_get_float_color_key(src_texture->resource.format, colour_key, float_key);
13281 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.low"));
13282 GL_EXTCALL(glUniform4fv(location, 1, &float_key[0].r));
13283 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.high"));
13284 GL_EXTCALL(glUniform4fv(location, 1, &float_key[1].r));
13286 wined3d_context_gl_draw_shaded_quad(context_gl, src_texture_gl, src_sub_resource_idx, src_rect, dst_rect, filter);
13287 GL_EXTCALL(glUseProgram(0));
13289 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13290 gl_info->gl_ops.gl.p_glFlush();
13292 if (staging_texture)
13293 wined3d_texture_decref(staging_texture);
13295 return dst_location;
13298 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13299 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13300 const RECT *draw_rect, DWORD flags, const struct wined3d_color *color, float depth, DWORD stencil)
13302 struct wined3d_blitter *next;
13304 if ((next = blitter->next))
13305 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13306 clear_rects, draw_rect, flags, color, depth, stencil);
13309 static const struct wined3d_blitter_ops glsl_blitter_ops =
13311 glsl_blitter_destroy,
13312 glsl_blitter_clear,
13313 glsl_blitter_blit,
13316 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13317 const struct wined3d_device *device)
13319 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
13320 struct wined3d_glsl_blitter *blitter;
13322 if (device->shader_backend != &glsl_shader_backend)
13323 return NULL;
13325 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13326 return NULL;
13328 if (!(blitter = heap_alloc(sizeof(*blitter))))
13330 ERR("Failed to allocate blitter.\n");
13331 return NULL;
13334 TRACE("Created blitter %p.\n", blitter);
13336 blitter->blitter.ops = &glsl_blitter_ops;
13337 blitter->blitter.next = *next;
13338 string_buffer_list_init(&blitter->string_buffers);
13339 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13340 blitter->palette_texture = 0;
13341 *next = &blitter->blitter;
13343 return *next;