include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / wined3d / glsl_shader.c
blobc0d75897857a7f1529295b7dbba304b61f28d311
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 <limits.h>
33 #include <stdio.h>
35 #include "wined3d_private.h"
36 #include "wined3d_gl.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d);
40 WINE_DECLARE_DEBUG_CHANNEL(winediag);
42 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
43 #define WINED3D_GLSL_SAMPLE_LOD 0x02
44 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
45 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
46 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
48 static const struct
50 unsigned int coord_size;
51 unsigned int resinfo_size;
52 const char *type_part;
54 resource_type_info[] =
56 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
57 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
58 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
59 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
60 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
61 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
62 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
63 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
64 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
65 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
66 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
69 static const struct
71 enum wined3d_data_type data_type;
72 const char *glsl_scalar_type;
73 const char *glsl_vector_type;
75 component_type_info[] =
77 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_UNKNOWN */
78 {WINED3D_DATA_UINT, "uint", "uvec"}, /* WINED3D_TYPE_UINT */
79 {WINED3D_DATA_INT, "int", "ivec"}, /* WINED3D_TYPE_INT */
80 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_FLOAT */
83 struct glsl_dst_param
85 char reg_name[150];
86 char mask_str[6];
89 struct glsl_src_param
91 char param_str[200];
94 struct glsl_sample_function
96 struct wined3d_string_buffer *name;
97 unsigned int coord_mask;
98 unsigned int deriv_mask;
99 enum wined3d_data_type data_type;
100 BOOL output_single_component;
101 unsigned int offset_size;
104 enum heap_node_op
106 HEAP_NODE_TRAVERSE_LEFT,
107 HEAP_NODE_TRAVERSE_RIGHT,
108 HEAP_NODE_POP,
111 struct constant_entry
113 unsigned int idx;
114 unsigned int version;
117 struct constant_heap
119 struct constant_entry *entries;
120 BOOL *contained;
121 unsigned int *positions;
122 unsigned int size;
125 /* GLSL shader private data */
126 struct shader_glsl_priv
128 bool use_vkd3d;
130 struct wined3d_string_buffer shader_buffer;
131 struct wined3d_string_buffer_list string_buffers;
132 struct wine_rb_tree program_lookup;
133 struct constant_heap vconst_heap;
134 struct constant_heap pconst_heap;
135 unsigned char *stack;
136 UINT next_constant_version;
138 const struct wined3d_vertex_pipe_ops *vertex_pipe;
139 const struct wined3d_fragment_pipe_ops *fragment_pipe;
140 struct wine_rb_tree ffp_vertex_shaders;
141 struct wine_rb_tree ffp_fragment_shaders;
142 BOOL 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_FFP_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_FFP_TEXTURES];
220 GLint bumpenv_lum_scale_location[WINED3D_MAX_FFP_TEXTURES];
221 GLint bumpenv_lum_offset_location[WINED3D_MAX_FFP_TEXTURES];
222 GLint tss_constant_location[WINED3D_MAX_FFP_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 color_key_location;
233 struct glsl_cs_program
235 struct list shader_entry;
236 GLuint id;
239 /* Struct to maintain data about a linked GLSL program */
240 struct glsl_shader_prog_link
242 struct wine_rb_entry program_lookup_entry;
243 struct glsl_vs_program vs;
244 struct glsl_hs_program hs;
245 struct glsl_ds_program ds;
246 struct glsl_gs_program gs;
247 struct glsl_ps_program ps;
248 struct glsl_cs_program cs;
249 GLuint id;
250 DWORD constant_update_mask;
251 unsigned int constant_version;
252 DWORD shader_controlled_clip_distances : 1;
253 DWORD clip_distance_mask : 8; /* WINED3D_MAX_CLIP_DISTANCES, 8 */
254 DWORD padding : 23;
257 struct glsl_program_key
259 GLuint vs_id;
260 GLuint hs_id;
261 GLuint ds_id;
262 GLuint gs_id;
263 GLuint ps_id;
264 GLuint cs_id;
267 struct shader_glsl_ctx_priv
269 const struct wined3d_gl_info *gl_info;
270 const struct vs_compile_args *cur_vs_args;
271 const struct ds_compile_args *cur_ds_args;
272 const struct ps_compile_args *cur_ps_args;
273 struct wined3d_string_buffer_list *string_buffers;
276 struct glsl_context_data
278 struct glsl_shader_prog_link *glsl_program;
279 GLenum vertex_color_clamp;
280 BOOL rasterization_disabled;
283 struct glsl_ps_compiled_shader
285 struct ps_compile_args args;
286 GLuint id;
289 struct glsl_vs_compiled_shader
291 struct vs_compile_args args;
292 GLuint id;
295 struct glsl_hs_compiled_shader
297 GLuint id;
300 struct glsl_ds_compiled_shader
302 struct ds_compile_args args;
303 GLuint id;
306 struct glsl_gs_compiled_shader
308 struct gs_compile_args args;
309 GLuint id;
312 struct glsl_cs_compiled_shader
314 GLuint id;
317 struct glsl_shader_private
319 union
321 struct glsl_vs_compiled_shader *vs;
322 struct glsl_hs_compiled_shader *hs;
323 struct glsl_ds_compiled_shader *ds;
324 struct glsl_gs_compiled_shader *gs;
325 struct glsl_ps_compiled_shader *ps;
326 struct glsl_cs_compiled_shader *cs;
327 } gl_shaders;
328 unsigned int num_gl_shaders, shader_array_size;
331 struct glsl_ffp_vertex_shader
333 struct wined3d_ffp_vs_desc desc;
334 GLuint id;
335 struct list linked_programs;
338 struct glsl_ffp_fragment_shader
340 struct ffp_frag_desc entry;
341 GLuint id;
342 struct list linked_programs;
345 struct glsl_ffp_destroy_ctx
347 struct shader_glsl_priv *priv;
348 const struct wined3d_context_gl *context_gl;
351 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
353 static const char *debug_gl_shader_type(GLenum type)
355 switch (type)
357 #define WINED3D_TO_STR(u) case u: return #u
358 WINED3D_TO_STR(GL_VERTEX_SHADER);
359 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
360 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
361 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
362 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
363 WINED3D_TO_STR(GL_COMPUTE_SHADER);
364 #undef WINED3D_TO_STR
365 default:
366 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
370 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
372 switch (type)
374 case WINED3D_SHADER_TYPE_VERTEX:
375 return "vs";
377 case WINED3D_SHADER_TYPE_HULL:
378 return "hs";
380 case WINED3D_SHADER_TYPE_DOMAIN:
381 return "ds";
383 case WINED3D_SHADER_TYPE_GEOMETRY:
384 return "gs";
386 case WINED3D_SHADER_TYPE_PIXEL:
387 return "ps";
389 case WINED3D_SHADER_TYPE_COMPUTE:
390 return "cs";
392 default:
393 FIXME("Unhandled shader type %#x.\n", type);
394 return "unknown";
398 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
400 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
401 return 440;
402 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
403 return 150;
404 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
405 return 130;
406 else
407 return 120;
410 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
411 const struct wined3d_gl_info *gl_info)
413 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
416 static unsigned int shader_glsl_full_ffp_varyings(const struct wined3d_gl_info *gl_info)
418 /* On core profile we have to also count diffuse and specular colours and
419 * the fog coordinate. */
420 return gl_info->limits.glsl_varyings >= (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
421 ? WINED3D_MAX_FFP_TEXTURES * 4 : (WINED3D_MAX_FFP_TEXTURES + 2) * 4 + 1);
424 static void shader_glsl_append_imm_vec(struct wined3d_string_buffer *buffer,
425 const float *values, unsigned int size, const struct wined3d_gl_info *gl_info)
427 const int *int_values = (const int *)values;
428 unsigned int i;
429 char str[17];
431 if (!gl_info->supported[ARB_SHADER_BIT_ENCODING])
433 if (size > 1)
434 shader_addline(buffer, "vec%u(", size);
436 for (i = 0; i < size; ++i)
438 wined3d_ftoa(values[i], str);
439 shader_addline(buffer, i ? ", %s" : "%s", str);
442 if (size > 1)
443 shader_addline(buffer, ")");
445 return;
448 shader_addline(buffer, "intBitsToFloat(");
449 if (size > 1)
450 shader_addline(buffer, "ivec%u(", size);
452 for (i = 0; i < size; ++i)
454 wined3d_ftoa(values[i], str);
455 shader_addline(buffer, i ? ", %#x /* %s */" : "%#x /* %s */", int_values[i], str);
458 if (size > 1)
459 shader_addline(buffer, ")");
460 shader_addline(buffer, ")");
463 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
464 const int *values, unsigned int size)
466 int i;
468 if (!size || size > 4)
470 ERR("Invalid vector size %u.\n", size);
471 return;
474 if (size > 1)
475 shader_addline(buffer, "ivec%u(", size);
477 for (i = 0; i < size; ++i)
478 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
480 if (size > 1)
481 shader_addline(buffer, ")");
484 /* Context activation is done by the caller. */
485 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
487 int length = 0;
488 char *log;
490 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
491 return;
493 if (program)
494 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
495 else
496 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
498 /* A size of 1 is just a null-terminated string, so the log should be bigger than
499 * that if there are errors. */
500 if (length > 1)
502 const char *ptr, *end, *line;
504 log = malloc(length);
505 if (program)
506 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
507 else
508 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
510 ptr = log;
511 /* The info log is supposed to be zero-terminated. Note that at least
512 * some versions of fglrx don't terminate the string properly. The
513 * reported length does include the supposed terminator though, so we
514 * don't care here. */
515 end = &ptr[length - 1];
516 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
518 WARN("Info log received from GLSL shader #%u:\n", id);
519 while ((line = wined3d_get_line(&ptr, end)))
521 WARN(" %.*s", (int)(ptr - line), line);
524 else
526 FIXME("Info log received from GLSL shader #%u:\n", id);
527 while ((line = wined3d_get_line(&ptr, end)))
529 FIXME(" %.*s", (int)(ptr - line), line);
532 free(log);
536 /* Context activation is done by the caller. */
537 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
539 const char *ptr, *end, *line;
541 TRACE("Compiling shader object %u.\n", shader);
543 if (TRACE_ON(d3d_shader))
545 ptr = src;
546 end = ptr + strlen(ptr);
547 while ((line = wined3d_get_line(&ptr, end)))
549 TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
553 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
554 checkGLcall("glShaderSource");
555 GL_EXTCALL(glCompileShader(shader));
556 checkGLcall("glCompileShader");
557 print_glsl_info_log(gl_info, shader, FALSE);
560 /* Context activation is done by the caller. */
561 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
563 GLint i, shader_count, source_size = -1;
564 GLuint *shaders;
565 char *source = NULL;
567 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
568 if (!(shaders = calloc(shader_count, sizeof(*shaders))))
570 ERR("Failed to allocate shader array memory.\n");
571 return;
574 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
575 for (i = 0; i < shader_count; ++i)
577 const char *ptr, *end, *line;
578 GLint tmp;
580 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
582 if (source_size < tmp)
584 free(source);
586 if (!(source = malloc(tmp)))
588 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
589 free(shaders);
590 return;
592 source_size = tmp;
595 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
596 ptr = source;
597 end = &ptr[tmp - 1];
599 FIXME("Shader %u:\n", shaders[i]);
600 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
601 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
602 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
603 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
604 FIXME("\n");
605 while ((line = wined3d_get_line(&ptr, end)))
607 FIXME(" %.*s", (int)(ptr - line), line);
609 FIXME("\n");
612 free(source);
613 free(shaders);
616 /* Context activation is done by the caller. */
617 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
619 GLint tmp;
621 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
622 return;
624 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
625 if (!tmp)
627 FIXME("Program %u link status invalid.\n", program);
628 shader_glsl_dump_program_source(gl_info, program);
631 print_glsl_info_log(gl_info, program, TRUE);
634 static struct vkd3d_shader_resource_binding *create_resource_bindings(const struct wined3d_gl_info *gl_info,
635 enum wined3d_shader_type shader_type, unsigned int *count)
637 struct vkd3d_shader_resource_binding *bindings;
638 enum vkd3d_shader_visibility visibility;
639 unsigned int base, i;
641 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, shader_type, &base, count);
642 if (!(bindings = calloc(*count, sizeof(*bindings))))
643 return NULL;
645 visibility = vkd3d_shader_visibility_from_wined3d(shader_type);
646 for (i = 0; i < *count; ++i)
648 bindings[i].type = VKD3D_SHADER_DESCRIPTOR_TYPE_CBV;
649 bindings[i].register_space = 0;
650 bindings[i].register_index = i;
651 bindings[i].shader_visibility = visibility;
652 bindings[i].flags = VKD3D_SHADER_BINDING_FLAG_BUFFER;
653 bindings[i].binding.set = 0;
654 bindings[i].binding.binding = base + i;
655 bindings[i].binding.count = 1;
658 return bindings;
661 static struct vkd3d_shader_combined_resource_sampler *create_combined_resource_sampler_bindings(
662 const struct wined3d_context_gl *context_gl, const struct wined3d_shader *shader, unsigned int *count)
664 struct vkd3d_shader_combined_resource_sampler *bindings;
665 unsigned int bind_idx, shader_sampler_count, base, i;
666 struct wined3d_shader_sampler_map_entry *entry;
667 enum vkd3d_shader_visibility visibility;
668 const unsigned int *tex_unit_map;
670 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl,
671 &shader->reg_maps.shader_version, &base, count);
673 shader_sampler_count = shader->reg_maps.sampler_map.count;
674 if (shader_sampler_count > *count)
676 FIXME("Shader %p needs %u samplers, but only %u are supported.\n",
677 shader, shader_sampler_count, *count);
678 return NULL;
680 *count = min(shader_sampler_count, *count);
682 if (!(bindings = calloc(*count, sizeof(*bindings))))
683 return NULL;
685 visibility = vkd3d_shader_visibility_from_wined3d(shader->reg_maps.shader_version.type);
686 for (i = 0; i < *count; ++i)
688 entry = &shader->reg_maps.sampler_map.entries[i];
689 bind_idx = base + entry->bind_idx;
690 if (tex_unit_map)
691 bind_idx = tex_unit_map[bind_idx];
693 bindings[i].resource_space = 0;
694 bindings[i].resource_index = entry->resource_idx;
695 bindings[i].sampler_space = 0;
696 bindings[i].sampler_index = entry->sampler_idx;
697 bindings[i].shader_visibility = visibility;
698 bindings[i].flags = VKD3D_SHADER_BINDING_FLAG_IMAGE;
699 bindings[i].binding.set = 0;
700 bindings[i].binding.binding = bind_idx;
701 bindings[i].binding.count = 1;
704 return bindings;
707 static GLuint shader_glsl_generate_vkd3d_shader(const struct wined3d_context_gl *context_gl,
708 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, GLenum gl_shader_type)
710 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
711 struct vkd3d_shader_combined_resource_sampler *combined_samplers;
712 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
713 struct vkd3d_shader_transform_feedback_info xfb_info;
714 unsigned int binding_count, combined_sampler_count;
715 struct vkd3d_shader_interface_info interface_info;
716 const struct wined3d_stream_output_desc *so_desc;
717 struct vkd3d_shader_resource_binding *bindings;
718 struct vkd3d_shader_compile_info info;
719 struct vkd3d_shader_code glsl;
720 GLuint shader_id;
721 char *messages;
722 int ret;
724 static const struct vkd3d_shader_compile_option compile_options[] =
726 {VKD3D_SHADER_COMPILE_OPTION_API_VERSION, VKD3D_SHADER_API_VERSION_1_6},
729 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY && (so_desc = shader->u.gs.so_desc))
731 xfb_info.type = VKD3D_SHADER_STRUCTURE_TYPE_TRANSFORM_FEEDBACK_INFO;
732 xfb_info.next = NULL;
733 xfb_info.elements = (const struct vkd3d_shader_transform_feedback_element *)so_desc->elements;
734 xfb_info.element_count = so_desc->element_count;
735 xfb_info.buffer_strides = so_desc->buffer_strides;
736 xfb_info.buffer_stride_count = so_desc->buffer_stride_count;
738 interface_info.next = &xfb_info;
740 else
742 interface_info.next = NULL;
745 if (!(bindings = create_resource_bindings(gl_info, version->type, &binding_count)))
747 ERR("Failed to create resource bindings.\n");
748 return 0;
751 if (!(combined_samplers = create_combined_resource_sampler_bindings(context_gl, shader, &combined_sampler_count)))
753 ERR("Failed to create combined sampler bindings.\n");
754 free(bindings);
755 return 0;
758 interface_info.type = VKD3D_SHADER_STRUCTURE_TYPE_INTERFACE_INFO;
759 interface_info.bindings = bindings;
760 interface_info.binding_count = binding_count;
761 interface_info.push_constant_buffers = NULL;
762 interface_info.push_constant_buffer_count = 0;
763 interface_info.combined_samplers = combined_samplers;
764 interface_info.combined_sampler_count = combined_sampler_count;
765 interface_info.uav_counters = NULL;
766 interface_info.uav_counter_count = 0;
768 info.type = VKD3D_SHADER_STRUCTURE_TYPE_COMPILE_INFO;
769 info.next = &interface_info;
770 if (shader->source_type == VKD3D_SHADER_SOURCE_D3D_BYTECODE)
772 info.source.code = shader->function;
773 info.source.size = shader->functionLength;
775 else
777 info.source.code = shader->byte_code;
778 info.source.size = shader->byte_code_size;
780 info.source_type = shader->source_type;
781 info.target_type = VKD3D_SHADER_TARGET_GLSL;
782 info.options = compile_options;
783 info.option_count = ARRAY_SIZE(compile_options);
784 info.log_level = VKD3D_SHADER_LOG_WARNING;
785 info.source_name = NULL;
787 ret = vkd3d_shader_compile(&info, &glsl, &messages);
788 if (messages && *messages && FIXME_ON(d3d_shader))
790 const char *ptr, *end, *line;
792 FIXME("Shader log:\n");
793 ptr = messages;
794 end = ptr + strlen(ptr);
795 while ((line = wined3d_get_line(&ptr, end)))
797 FIXME(" %.*s", (int)(ptr - line), line);
799 FIXME("\n");
801 vkd3d_shader_free_messages(messages);
802 free(combined_samplers);
803 free(bindings);
805 if (ret < 0)
807 ERR("Failed to compile shader, ret %d.\n", ret);
808 return 0;
811 if (!(shader_id = GL_EXTCALL(glCreateShader(gl_shader_type))))
813 ERR("Failed to create shader.\n");
814 vkd3d_shader_free_shader_code(&glsl);
815 return 0;
818 TRACE("Compiling shader object %u.\n", shader_id);
819 GL_EXTCALL(glShaderSource(shader_id, 1, (const GLchar **)&glsl.code, (const GLint *)&glsl.size));
820 checkGLcall("glShaderSource");
821 GL_EXTCALL(glCompileShader(shader_id));
822 checkGLcall("glCompileShader");
823 print_glsl_info_log(gl_info, shader_id, FALSE);
825 vkd3d_shader_free_shader_code(&glsl);
827 return shader_id;
830 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
832 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
833 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
834 * versions. */
835 return shader_glsl_get_version(gl_info) >= 140;
838 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
840 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
843 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
844 struct shader_glsl_priv *priv, GLuint program_id,
845 const struct wined3d_shader_reg_maps *reg_maps)
847 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
848 struct wined3d_string_buffer *name;
849 unsigned int i, base, count;
850 GLuint block_idx;
852 if (shader_glsl_use_layout_binding_qualifier(gl_info))
853 return;
855 name = string_buffer_get(&priv->string_buffers);
856 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
857 for (i = 0; i < count; ++i)
859 if (!reg_maps->cb_sizes[i])
860 continue;
862 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
863 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
864 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
866 checkGLcall("glUniformBlockBinding");
867 string_buffer_release(&priv->string_buffers, name);
870 /* Context activation is done by the caller. */
871 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
872 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
873 unsigned int base, unsigned int count, const unsigned int *tex_unit_map)
875 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
876 unsigned int i, mapped_unit;
877 GLint name_loc;
879 for (i = 0; i < count; ++i)
881 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
882 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
883 if (name_loc == -1)
884 continue;
886 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
887 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
889 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
890 continue;
893 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
894 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
896 checkGLcall("Load sampler bindings");
897 string_buffer_release(&priv->string_buffers, sampler_name);
900 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
901 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
903 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
904 const unsigned int *tex_unit_map;
905 unsigned int base, count;
907 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
908 if (sampler_idx >= count)
909 return WINED3D_UNMAPPED_STAGE;
910 if (!tex_unit_map)
911 return base + sampler_idx;
912 return tex_unit_map[base + sampler_idx];
915 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
916 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
917 unsigned int sampler_idx)
919 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
920 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
921 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
922 else
923 ERR("Unmapped sampler %u.\n", sampler_idx);
926 /* Context activation is done by the caller. */
927 static void shader_glsl_load_samplers(const struct wined3d_context *context,
928 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
930 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
931 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
932 const struct wined3d_shader_version *shader_version;
933 const unsigned int *tex_unit_map;
934 unsigned int base, count;
935 const char *prefix;
937 if (shader_glsl_use_layout_binding_qualifier(gl_info))
938 return;
940 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
941 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
942 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
943 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
946 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
947 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
949 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
951 if (icb)
953 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
954 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
955 GLint icb_location;
957 string_buffer_sprintf(icb_name, "%s_icb", prefix);
958 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
959 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
960 checkGLcall("Load immediate constant buffer");
962 string_buffer_release(&priv->string_buffers, icb_name);
966 /* Context activation is done by the caller. */
967 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
968 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
970 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
971 struct wined3d_string_buffer *name;
972 GLint location;
973 unsigned int i;
975 if (shader_glsl_use_layout_binding_qualifier(gl_info))
976 return;
978 name = string_buffer_get(&priv->string_buffers);
979 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
981 if (!reg_maps->uav_resource_info[i].type)
982 continue;
984 string_buffer_sprintf(name, "%s_image%u", prefix, i);
985 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
986 if (location == -1)
987 continue;
989 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
990 GL_EXTCALL(glUniform1i(location, i));
992 checkGLcall("Load image bindings");
993 string_buffer_release(&priv->string_buffers, name);
996 /* Context activation is done by the caller. */
997 static void shader_glsl_load_program_resources(const struct wined3d_context_gl *context_gl,
998 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
1000 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1002 shader_glsl_init_uniform_block_bindings(context_gl->gl_info, priv, program_id, reg_maps);
1003 shader_glsl_load_icb(context_gl->gl_info, priv, program_id, reg_maps);
1004 /* Texture unit mapping is set up to be the same each time the shader
1005 * program is used so we can hardcode the sampler uniform values. */
1006 shader_glsl_load_samplers(&context_gl->c, priv, program_id, reg_maps);
1009 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
1010 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
1012 if (varyings && *strings)
1014 char *ptr = *strings;
1016 varyings[*varying_count] = ptr;
1018 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
1019 ptr += buffer->content_size + 1;
1021 *strings = ptr;
1024 *strings_length += buffer->content_size + 1;
1025 ++(*varying_count);
1028 static void append_transform_feedback_skip_components(const char **varyings,
1029 unsigned int *varying_count, char **strings, unsigned int *strings_length,
1030 struct wined3d_string_buffer *buffer, unsigned int component_count)
1032 unsigned int j;
1034 for (j = 0; j < component_count / 4; ++j)
1036 string_buffer_sprintf(buffer, "gl_SkipComponents4");
1037 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
1039 if (component_count % 4)
1041 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
1042 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
1046 static BOOL shader_glsl_generate_transform_feedback_varyings(struct wined3d_string_buffer *buffer,
1047 const char **varyings, unsigned int *varying_count, char *strings, unsigned int *strings_length,
1048 GLenum buffer_mode, struct wined3d_shader *shader)
1050 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
1051 unsigned int buffer_idx, count, length, highest_output_slot, stride;
1052 unsigned int i, register_idx, component_idx;
1053 BOOL have_varyings_to_record = FALSE;
1055 count = length = 0;
1056 highest_output_slot = 0;
1057 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
1059 stride = 0;
1061 for (i = 0; i < so_desc->element_count; ++i)
1063 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
1065 highest_output_slot = max(highest_output_slot, e->output_slot);
1066 if (e->output_slot != buffer_idx)
1067 continue;
1069 if (e->stream_idx)
1071 FIXME("Unhandled stream %u.\n", e->stream_idx);
1072 continue;
1075 stride += e->component_count;
1077 if (!e->semantic_name)
1079 append_transform_feedback_skip_components(varyings, &count,
1080 &strings, &length, buffer, e->component_count);
1081 continue;
1084 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
1085 continue;
1087 if (component_idx || e->component_count != 4)
1089 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
1091 FIXME("Unsupported component range %u-%u.\n", component_idx, e->component_count);
1092 append_transform_feedback_skip_components(varyings, &count,
1093 &strings, &length, buffer, e->component_count);
1094 continue;
1097 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
1098 register_idx, component_idx, component_idx + e->component_count - 1);
1099 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
1101 else
1103 string_buffer_sprintf(buffer, "shader_in_out.reg%u", register_idx);
1104 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
1107 have_varyings_to_record = TRUE;
1110 if (buffer_idx < so_desc->buffer_stride_count
1111 && stride < so_desc->buffer_strides[buffer_idx] / 4)
1113 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
1114 append_transform_feedback_skip_components(varyings, &count,
1115 &strings, &length, buffer, component_count);
1118 if (highest_output_slot <= buffer_idx)
1119 break;
1121 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
1123 string_buffer_sprintf(buffer, "gl_NextBuffer");
1124 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
1128 if (varying_count)
1129 *varying_count = count;
1130 if (strings_length)
1131 *strings_length = length;
1133 return have_varyings_to_record;
1136 static void shader_glsl_init_transform_feedback(const struct wined3d_context_gl *context_gl,
1137 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
1139 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
1140 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1141 struct wined3d_string_buffer *buffer;
1142 unsigned int i, count, length;
1143 const char **varyings;
1144 char *strings;
1145 GLenum mode;
1147 if (!so_desc)
1148 return;
1150 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
1152 mode = GL_INTERLEAVED_ATTRIBS;
1154 else
1156 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
1158 for (i = 0; i < so_desc->element_count; ++i)
1160 if (!so_desc->elements[i].semantic_name)
1162 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
1163 return;
1165 ++element_count[so_desc->elements[i].output_slot];
1168 if (element_count[0] == so_desc->element_count)
1170 mode = GL_INTERLEAVED_ATTRIBS;
1172 else
1174 mode = GL_SEPARATE_ATTRIBS;
1175 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
1177 if (element_count[i] != 1)
1178 break;
1180 for (; i < ARRAY_SIZE(element_count); ++i)
1182 if (element_count[i])
1184 FIXME("Only single element per buffer is allowed in separate mode.\n");
1185 return;
1191 buffer = string_buffer_get(&priv->string_buffers);
1193 if (!shader_glsl_generate_transform_feedback_varyings(buffer, NULL, &count, NULL, &length, mode, shader))
1195 FIXME("No varyings to record, disabling transform feedback.\n");
1196 shader->u.gs.so_desc = NULL;
1197 string_buffer_release(&priv->string_buffers, buffer);
1198 return;
1201 if (!(varyings = calloc(count, sizeof(*varyings))))
1203 ERR("Out of memory.\n");
1204 string_buffer_release(&priv->string_buffers, buffer);
1205 return;
1207 if (!(strings = calloc(length, sizeof(*strings))))
1209 ERR("Out of memory.\n");
1210 free(varyings);
1211 string_buffer_release(&priv->string_buffers, buffer);
1212 return;
1215 shader_glsl_generate_transform_feedback_varyings(buffer, varyings, NULL, strings, NULL, mode, shader);
1216 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
1217 checkGLcall("glTransformFeedbackVaryings");
1219 free(varyings);
1220 free(strings);
1221 string_buffer_release(&priv->string_buffers, buffer);
1224 /* Context activation is done by the caller. */
1225 static void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
1226 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
1228 unsigned int start = ~0U, end = 0;
1229 int stack_idx = 0;
1230 unsigned int heap_idx = 1;
1231 unsigned int idx;
1233 if (heap->entries[heap_idx].version <= version) return;
1235 idx = heap->entries[heap_idx].idx;
1236 if (constant_locations[idx] != -1)
1237 start = end = idx;
1238 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1240 while (stack_idx >= 0)
1242 /* Note that we fall through to the next case statement. */
1243 switch(stack[stack_idx])
1245 case HEAP_NODE_TRAVERSE_LEFT:
1247 unsigned int left_idx = heap_idx << 1;
1248 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1250 heap_idx = left_idx;
1251 idx = heap->entries[heap_idx].idx;
1252 if (constant_locations[idx] != -1)
1254 if (start > idx)
1255 start = idx;
1256 if (end < idx)
1257 end = idx;
1260 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1261 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1262 break;
1266 case HEAP_NODE_TRAVERSE_RIGHT:
1268 unsigned int right_idx = (heap_idx << 1) + 1;
1269 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1271 heap_idx = right_idx;
1272 idx = heap->entries[heap_idx].idx;
1273 if (constant_locations[idx] != -1)
1275 if (start > idx)
1276 start = idx;
1277 if (end < idx)
1278 end = idx;
1281 stack[stack_idx++] = HEAP_NODE_POP;
1282 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1283 break;
1287 case HEAP_NODE_POP:
1288 heap_idx >>= 1;
1289 --stack_idx;
1290 break;
1293 if (start <= end)
1294 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1295 checkGLcall("walk_constant_heap()");
1298 /* Context activation is done by the caller. */
1299 static void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1300 GLint location, const struct wined3d_vec4 *data)
1302 GLfloat clamped_constant[4];
1304 if (location == -1) return;
1306 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1307 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1308 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1309 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1311 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1314 /* Context activation is done by the caller. */
1315 static void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1316 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1317 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1319 int stack_idx = 0;
1320 unsigned int heap_idx = 1;
1321 unsigned int idx;
1323 if (heap->entries[heap_idx].version <= version) return;
1325 idx = heap->entries[heap_idx].idx;
1326 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1327 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1329 while (stack_idx >= 0)
1331 /* Note that we fall through to the next case statement. */
1332 switch(stack[stack_idx])
1334 case HEAP_NODE_TRAVERSE_LEFT:
1336 unsigned int left_idx = heap_idx << 1;
1337 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1339 heap_idx = left_idx;
1340 idx = heap->entries[heap_idx].idx;
1341 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1343 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1344 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1345 break;
1349 case HEAP_NODE_TRAVERSE_RIGHT:
1351 unsigned int right_idx = (heap_idx << 1) + 1;
1352 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1354 heap_idx = right_idx;
1355 idx = heap->entries[heap_idx].idx;
1356 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1358 stack[stack_idx++] = HEAP_NODE_POP;
1359 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1360 break;
1364 case HEAP_NODE_POP:
1365 heap_idx >>= 1;
1366 --stack_idx;
1367 break;
1370 checkGLcall("walk_constant_heap_clamped()");
1373 /* Context activation is done by the caller. */
1374 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, struct wined3d_context_gl *context_gl,
1375 struct wined3d_buffer *constant_buffer, const GLint *constant_locations, const struct constant_heap *heap,
1376 unsigned char *stack, unsigned int version)
1378 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1379 const struct wined3d_shader_lconst *lconst;
1381 if (constant_buffer)
1383 const struct wined3d_vec4 *constants = wined3d_buffer_load_sysmem(constant_buffer, &context_gl->c);
1385 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1386 if (shader->reg_maps.shader_version.major == 1
1387 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1388 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1389 else
1390 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1393 if (!shader->load_local_constsF)
1395 TRACE("No need to load local float constants for this shader.\n");
1396 return;
1399 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1400 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1402 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1404 checkGLcall("glUniform4fv()");
1407 /* Context activation is done by the caller. */
1408 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, struct wined3d_context_gl *context_gl,
1409 struct wined3d_buffer *constant_buffer, const GLint locations[WINED3D_MAX_CONSTS_I], uint32_t constants_set)
1411 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1412 unsigned int i;
1413 struct list* ptr;
1415 if (constant_buffer)
1417 const struct wined3d_ivec4 *constants = wined3d_buffer_load_sysmem(constant_buffer, &context_gl->c);
1419 while (constants_set)
1421 /* We found this uniform name in the program - go ahead and send the data */
1422 i = wined3d_bit_scan(&constants_set);
1423 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1427 /* Load immediate constants */
1428 ptr = list_head(&shader->constantsI);
1429 while (ptr)
1431 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1432 unsigned int idx = lconst->idx;
1433 const GLint *values = (const GLint *)lconst->value;
1435 /* We found this uniform name in the program - go ahead and send the data */
1436 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1437 ptr = list_next(&shader->constantsI, ptr);
1439 checkGLcall("glUniform4iv()");
1442 /* Context activation is done by the caller. */
1443 static void shader_glsl_load_constants_b(const struct wined3d_shader *shader, struct wined3d_context_gl *context_gl,
1444 struct wined3d_buffer *constant_buffer, const GLint locations[WINED3D_MAX_CONSTS_B], uint32_t constants_set)
1446 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1447 unsigned int i;
1448 struct list* ptr;
1450 if (constant_buffer)
1452 const BOOL *constants = wined3d_buffer_load_sysmem(constant_buffer, &context_gl->c);
1454 while (constants_set)
1456 i = wined3d_bit_scan(&constants_set);
1457 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1461 /* Load immediate constants */
1462 ptr = list_head(&shader->constantsB);
1463 while (ptr)
1465 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1466 unsigned int idx = lconst->idx;
1467 const GLint *values = (const GLint *)lconst->value;
1469 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1470 ptr = list_next(&shader->constantsB, ptr);
1472 checkGLcall("glUniform1iv()");
1475 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1477 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1480 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1482 struct wined3d_matrix temp;
1483 unsigned int i, j;
1485 for (i = 0; i < 4; ++i)
1486 for (j = 0; j < 4; ++j)
1487 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1489 *out = temp;
1492 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context_gl *context_gl,
1493 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1495 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1496 struct wined3d_matrix mv;
1497 float mat[3 * 3];
1499 if (prog->vs.normal_matrix_location == -1)
1500 return;
1502 get_modelview_matrix(&context_gl->c, state, 0, &mv);
1503 compute_normal_matrix(mat, context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING, &mv);
1505 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1506 checkGLcall("glUniformMatrix3fv");
1509 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context_gl *context_gl,
1510 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1512 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1513 struct wined3d_matrix mat;
1515 if (tex >= WINED3D_MAX_FFP_TEXTURES)
1516 return;
1517 if (prog->vs.texture_matrix_location[tex] == -1)
1518 return;
1520 get_texture_matrix(&context_gl->c, state, tex, &mat);
1521 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1522 checkGLcall("glUniformMatrix4fv");
1525 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context_gl *context_gl,
1526 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1528 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1530 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1531 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1532 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1533 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1534 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1535 checkGLcall("setting FFP material uniforms");
1538 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context_gl *context_gl,
1539 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1540 struct glsl_shader_prog_link *prog)
1542 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1543 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1544 struct wined3d_vec4 vec4;
1546 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1547 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1548 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1550 switch (light_info->OriginalParms.type)
1552 case WINED3D_LIGHT_POINT:
1553 wined3d_vec4_transform(&vec4, &light_info->position, view);
1554 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1555 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1556 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1557 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1558 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1559 break;
1561 case WINED3D_LIGHT_SPOT:
1562 wined3d_vec4_transform(&vec4, &light_info->position, view);
1563 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1565 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1566 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1568 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1569 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1570 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1571 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1572 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1573 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1574 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1575 break;
1577 case WINED3D_LIGHT_DIRECTIONAL:
1578 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1579 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1580 break;
1582 case WINED3D_LIGHT_PARALLELPOINT:
1583 wined3d_vec4_transform(&vec4, &light_info->position, view);
1584 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1585 break;
1587 default:
1588 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1590 checkGLcall("setting FFP lights uniforms");
1593 static void shader_glsl_pointsize_uniform(const struct wined3d_context_gl *context_gl,
1594 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1596 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1597 float size, att[3];
1598 float min, max;
1600 get_pointsize_minmax(&context_gl->c, state, &min, &max);
1602 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1603 checkGLcall("glUniform1f");
1604 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1605 checkGLcall("glUniform1f");
1607 get_pointsize(&context_gl->c, state, &size, att);
1609 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1610 checkGLcall("glUniform1f");
1611 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1612 checkGLcall("glUniform1f");
1613 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1614 checkGLcall("glUniform1f");
1615 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1616 checkGLcall("glUniform1f");
1619 static void shader_glsl_load_fog_uniform(const struct wined3d_context_gl *context_gl,
1620 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1622 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1623 struct wined3d_color color;
1624 float start, end, scale;
1625 union
1627 DWORD d;
1628 float f;
1629 } tmpvalue;
1631 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1632 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1633 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1634 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1635 get_fog_start_end(&context_gl->c, state, &start, &end);
1636 scale = 1.0f / (end - start);
1637 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1638 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1639 checkGLcall("fog emulation uniforms");
1642 static void shader_glsl_clip_plane_uniform(const struct wined3d_context_gl *context_gl,
1643 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1645 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1646 struct wined3d_matrix matrix;
1647 struct wined3d_vec4 plane;
1649 plane = state->clip_planes[index];
1651 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1652 if (!use_vs(state))
1654 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1655 transpose_matrix(&matrix, &matrix);
1656 wined3d_vec4_transform(&plane, &plane, &matrix);
1659 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1662 static void shader_glsl_load_constants(struct shader_glsl_priv *priv,
1663 struct wined3d_context *context, const struct wined3d_state *state)
1665 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1666 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1667 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1668 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1669 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1670 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1671 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1672 struct wined3d_device *device = context->device;
1673 unsigned int constant_version;
1674 DWORD update_mask;
1675 int i;
1677 /* No GLSL program set - nothing to do. */
1678 if (!prog)
1679 return;
1681 constant_version = prog->constant_version;
1682 update_mask = context->constant_update_mask & prog->constant_update_mask;
1684 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1685 shader_glsl_load_constants_f(vshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_VS_F],
1686 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1688 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1689 shader_glsl_load_constants_i(vshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_VS_I],
1690 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1692 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1693 shader_glsl_load_constants_b(vshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_VS_B],
1694 prog->vs.uniform_b_locations, vshader->reg_maps.boolean_constants);
1696 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1698 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1699 shader_glsl_clip_plane_uniform(context_gl, state, i, prog);
1702 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1703 shader_glsl_pointsize_uniform(context_gl, state, prog);
1705 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1707 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1708 max(state->viewport_count, 1) : 1;
1709 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1710 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1711 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1712 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1713 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1714 else
1715 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1716 checkGLcall("glUniform4fv");
1719 if (update_mask & WINED3D_SHADER_CONST_BASE_VERTEX_ID)
1721 GL_EXTCALL(glUniform1i(prog->vs.base_vertex_id_location, state->base_vertex_index));
1722 checkGLcall("base vertex id");
1725 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1727 struct wined3d_matrix mat;
1729 get_modelview_matrix(context, state, 0, &mat);
1730 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1731 checkGLcall("glUniformMatrix4fv");
1733 shader_glsl_ffp_vertex_normalmatrix_uniform(context_gl, state, prog);
1736 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1738 struct wined3d_matrix mat;
1740 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1742 if (prog->vs.modelview_matrix_location[i] == -1)
1743 break;
1745 get_modelview_matrix(context, state, i, &mat);
1746 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1747 checkGLcall("glUniformMatrix4fv");
1751 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1753 struct wined3d_matrix projection;
1755 get_projection_matrix(context, state, &projection);
1756 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1757 checkGLcall("glUniformMatrix4fv");
1760 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1762 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
1763 shader_glsl_ffp_vertex_texmatrix_uniform(context_gl, state, i, prog);
1766 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1767 shader_glsl_ffp_vertex_material_uniform(context_gl, state, prog);
1769 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1771 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1772 const struct wined3d_ffp_vs_constants *constants;
1773 DWORD point_count = 0;
1774 DWORD spot_count = 0;
1775 DWORD directional_count = 0;
1777 constants = wined3d_buffer_load_sysmem(context->device->push_constants[WINED3D_PUSH_CONSTANTS_VS_FFP], context);
1779 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1781 if (!state->light_state.lights[i])
1782 continue;
1784 switch (state->light_state.lights[i]->OriginalParms.type)
1786 case WINED3D_LIGHT_POINT:
1787 ++point_count;
1788 break;
1789 case WINED3D_LIGHT_SPOT:
1790 ++spot_count;
1791 break;
1792 case WINED3D_LIGHT_DIRECTIONAL:
1793 ++directional_count;
1794 break;
1795 case WINED3D_LIGHT_PARALLELPOINT:
1796 break;
1797 default:
1798 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
1799 break;
1802 point_idx = 0;
1803 spot_idx = point_idx + point_count;
1804 directional_idx = spot_idx + spot_count;
1805 parallel_point_idx = directional_idx + directional_count;
1807 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &constants->light.ambient.r));
1808 checkGLcall("glUniform3fv");
1810 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1812 const struct wined3d_light_info *light_info = state->light_state.lights[i];
1813 unsigned int idx;
1815 if (!light_info)
1816 continue;
1818 switch (light_info->OriginalParms.type)
1820 case WINED3D_LIGHT_POINT:
1821 idx = point_idx++;
1822 break;
1823 case WINED3D_LIGHT_SPOT:
1824 idx = spot_idx++;
1825 break;
1826 case WINED3D_LIGHT_DIRECTIONAL:
1827 idx = directional_idx++;
1828 break;
1829 case WINED3D_LIGHT_PARALLELPOINT:
1830 idx = parallel_point_idx++;
1831 break;
1832 default:
1833 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1834 continue;
1836 shader_glsl_ffp_vertex_light_uniform(context_gl, state, idx, light_info, prog);
1840 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1841 shader_glsl_load_constants_f(pshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_PS_F],
1842 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1844 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1845 shader_glsl_load_constants_i(pshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_PS_I],
1846 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1848 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1849 shader_glsl_load_constants_b(pshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_PS_B],
1850 prog->ps.uniform_b_locations, pshader->reg_maps.boolean_constants);
1852 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1854 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
1856 if (prog->ps.bumpenv_mat_location[i] == -1)
1857 continue;
1859 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1860 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1862 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1864 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1865 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1866 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1867 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1871 checkGLcall("bump env uniforms");
1874 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1876 const struct wined3d_ffp_ps_constants *constants = wined3d_buffer_load_sysmem(
1877 context_gl->c.device->push_constants[WINED3D_PUSH_CONSTANTS_PS_FFP], &context_gl->c);
1879 GL_EXTCALL(glUniform4fv(prog->ps.color_key_location, 2, &constants->color_key[0].r));
1882 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1884 const struct wined3d_ffp_ps_constants *constants;
1886 constants = wined3d_buffer_load_sysmem(
1887 context_gl->c.device->push_constants[WINED3D_PUSH_CONSTANTS_PS_FFP], &context_gl->c);
1889 if (prog->ps.tex_factor_location != -1)
1890 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &constants->texture_factor.r));
1892 GL_EXTCALL(glUniform4fv(prog->ps.specular_enable_location, 1, &constants->specular_enable.r));
1894 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
1896 if (prog->ps.tss_constant_location[i] == -1)
1897 continue;
1899 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &constants->texture_constants[i].r));
1902 checkGLcall("fixed function uniforms");
1905 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1906 shader_glsl_load_fog_uniform(context_gl, state, prog);
1908 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1910 float ref = wined3d_alpha_ref(state);
1912 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1913 checkGLcall("alpha test emulation uniform");
1916 if (priv->next_constant_version == UINT_MAX)
1918 TRACE("Max constant version reached, resetting to 0.\n");
1919 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1920 priv->next_constant_version = 1;
1922 else
1924 prog->constant_version = priv->next_constant_version++;
1928 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1930 struct constant_entry *entries = heap->entries;
1931 unsigned int *positions = heap->positions;
1932 unsigned int heap_idx, parent_idx;
1934 if (!heap->contained[idx])
1936 heap_idx = heap->size++;
1937 heap->contained[idx] = TRUE;
1939 else
1941 heap_idx = positions[idx];
1944 while (heap_idx > 1)
1946 parent_idx = heap_idx >> 1;
1948 if (new_version <= entries[parent_idx].version) break;
1950 entries[heap_idx] = entries[parent_idx];
1951 positions[entries[parent_idx].idx] = heap_idx;
1952 heap_idx = parent_idx;
1955 entries[heap_idx].version = new_version;
1956 entries[heap_idx].idx = idx;
1957 positions[idx] = heap_idx;
1960 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1962 struct shader_glsl_priv *priv = device->shader_priv;
1963 struct constant_heap *heap = &priv->vconst_heap;
1964 UINT i;
1966 for (i = start; i < count + start; ++i)
1968 update_heap_entry(heap, i, priv->next_constant_version);
1972 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1974 struct shader_glsl_priv *priv = device->shader_priv;
1975 struct constant_heap *heap = &priv->pconst_heap;
1976 UINT i;
1978 for (i = start; i < count + start; ++i)
1980 update_heap_entry(heap, i, priv->next_constant_version);
1984 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1986 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1987 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1988 if(shader_major > 3) return ret;
1990 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1991 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1992 return ret;
1995 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1997 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
2000 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
2002 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
2003 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
2006 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
2008 return shader_glsl_get_version(gl_info) >= 150;
2011 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
2013 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
2016 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
2017 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2019 va_list args;
2020 int ret;
2022 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2023 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
2024 for (;;)
2026 va_start(args, format);
2027 ret = shader_vaddline(buffer, format, args);
2028 va_end(args);
2029 if (!ret)
2030 return;
2031 if (!string_buffer_resize(buffer, ret))
2032 return;
2036 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
2037 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
2039 va_list args;
2040 int ret;
2042 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
2043 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
2044 for (;;)
2046 va_start(args, format);
2047 ret = shader_vaddline(buffer, format, args);
2048 va_end(args);
2049 if (!ret)
2050 return;
2051 if (!string_buffer_resize(buffer, ret))
2052 return;
2056 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
2058 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
2061 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
2063 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
2066 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
2068 switch (mode)
2070 case WINED3DSIM_CONSTANT:
2071 return "flat ";
2072 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
2073 return "noperspective ";
2074 default:
2075 FIXME("Unhandled interpolation mode %#x.\n", mode);
2076 case WINED3DSIM_NONE:
2077 case WINED3DSIM_LINEAR:
2078 return "";
2082 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
2083 const uint32_t *packed_interpolation_mode, unsigned int register_idx)
2085 return wined3d_extract_bits(packed_interpolation_mode,
2086 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
2089 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
2090 struct wined3d_string_buffer *buffer, unsigned int element_count,
2091 const uint32_t *interpolation_mode, BOOL unroll)
2093 enum wined3d_shader_interpolation_mode mode;
2094 unsigned int i;
2096 if (shader_glsl_use_interface_blocks(gl_info))
2098 if (unroll)
2100 shader_addline(buffer, "in shader_in_out {\n");
2101 for (i = 0; i < element_count; ++i)
2103 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2104 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
2106 shader_addline(buffer, "} shader_in;\n");
2108 else
2110 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
2113 else
2115 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2119 static BOOL needs_interpolation_qualifiers_for_shader_outputs(const struct wined3d_gl_info *gl_info)
2121 /* In GLSL 4.40+ it is fine to specify interpolation qualifiers only in
2122 * fragment shaders. In older GLSL versions interpolation qualifiers must
2123 * match between shader stages. */
2124 return gl_info->glsl_version < MAKEDWORD_VERSION(4, 40);
2127 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2128 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2129 const uint32_t *interpolation_mode)
2131 enum wined3d_shader_interpolation_mode mode;
2132 unsigned int i;
2134 if (shader_glsl_use_interface_blocks(gl_info))
2136 if (rasterizer_setup)
2138 shader_addline(buffer, "out shader_in_out {\n");
2139 for (i = 0; i < element_count; ++i)
2141 const char *interpolation_qualifiers = "";
2142 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2144 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2145 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2147 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2149 shader_addline(buffer, "} shader_out;\n");
2151 else
2153 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2156 else
2158 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2162 static BOOL use_legacy_fragment_output(const struct wined3d_gl_info *gl_info)
2164 /* Technically 1.30 does support user-defined fragment shader outputs but
2165 * we might not have glBindFragDataLocation() available (i.e. GL version
2166 * might be < 3.0). */
2167 return gl_info->glsl_version <= MAKEDWORD_VERSION(1, 30);
2170 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2172 return use_legacy_fragment_output(gl_info) ? "gl_FragData" : "ps_out";
2175 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2177 switch (primitive_type)
2179 case WINED3D_PT_POINTLIST:
2180 return "points";
2182 case WINED3D_PT_LINELIST:
2183 return "lines";
2185 case WINED3D_PT_LINESTRIP:
2186 return "line_strip";
2188 case WINED3D_PT_TRIANGLELIST:
2189 return "triangles";
2191 case WINED3D_PT_TRIANGLESTRIP:
2192 return "triangle_strip";
2194 case WINED3D_PT_LINELIST_ADJ:
2195 return "lines_adjacency";
2197 case WINED3D_PT_TRIANGLELIST_ADJ:
2198 return "triangles_adjacency";
2200 default:
2201 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2202 return "";
2206 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2208 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2209 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2210 DWORD input_reg_used = shader->u.ps.input_reg_used;
2211 unsigned int i;
2213 if (reg_maps->shader_version.major < 3)
2214 return input_reg_used & (1u << idx);
2216 for (i = 0; i < input_signature->element_count; ++i)
2218 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2220 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2221 continue;
2223 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2224 && input->semantic_idx == idx)
2225 return input_reg_used & (1u << input->register_idx);
2227 return FALSE;
2230 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2231 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2232 unsigned int index)
2234 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2235 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2236 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2237 index, scalar_type, scalar_type, index);
2240 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2241 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2243 unsigned int index = e->register_idx;
2244 enum wined3d_component_type type;
2246 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2248 shader_addline(buffer, "uniform int base_vertex_id;\n");
2249 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID - base_vertex_id), 0.0, 0.0, 0.0);\n", index);
2250 return;
2252 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2254 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2255 index);
2256 return;
2258 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2259 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2261 if (shader_glsl_use_explicit_attrib_location(gl_info))
2262 shader_addline(buffer, "layout(location = %u) ", index);
2264 type = e->component_type;
2265 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2267 FIXME("Unhandled type %#x.\n", type);
2268 type = WINED3D_TYPE_FLOAT;
2270 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2271 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2272 else
2273 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2274 component_type_info[type].glsl_vector_type,
2275 component_type_info[type].glsl_scalar_type, index);
2278 /** Generate the variable & register declarations for the GLSL output target */
2279 static void shader_generate_glsl_declarations(const struct wined3d_context_gl *context_gl,
2280 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2281 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2283 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2284 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2285 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2286 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2287 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2288 unsigned int uniform_block_base, uniform_block_count;
2289 enum wined3d_shader_resource_type resource_type;
2290 const struct wined3d_shader_lconst *lconst;
2291 const char *prefix;
2292 unsigned int i;
2293 uint32_t map;
2295 if (wined3d_settings.strict_shader_math)
2296 shader_addline(buffer, "#pragma optionNV(fastmath off)\n");
2298 prefix = shader_glsl_get_prefix(version->type);
2300 /* Prototype the subroutines */
2301 map = reg_maps->labels;
2302 while (map)
2304 i = wined3d_bit_scan(&map);
2305 shader_addline(buffer, "void subroutine%u();\n", i);
2308 if (version->type != WINED3D_SHADER_TYPE_PIXEL && version->type != WINED3D_SHADER_TYPE_COMPUTE)
2310 if (version->type == WINED3D_SHADER_TYPE_HULL)
2311 shader_addline(buffer, "out gl_PerVertex { invariant vec4 gl_Position; } gl_out[];\n");
2312 else
2313 shader_addline(buffer, "invariant gl_Position;\n");
2316 /* Declare the constants (aka uniforms) */
2317 if (shader->limits->constant_float > 0)
2319 unsigned max_constantsF;
2321 /* Unless the shader uses indirect addressing, always declare the
2322 * maximum array size and ignore that we need some uniforms privately.
2323 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2324 * and immediate values, still declare VC[256]. If the shader needs
2325 * more uniforms than we have it won't work in any case. If it uses
2326 * less, the compiler will figure out which uniforms are really used
2327 * and strip them out. This allows a shader to use c255 on a dx9 card,
2328 * as long as it doesn't also use all the other constants.
2330 * If the shader uses indirect addressing the compiler must assume
2331 * that all declared uniforms are used. In this case, declare only the
2332 * amount that we're assured to have.
2334 * Thus we run into problems in these two cases:
2335 * 1) The shader really uses more uniforms than supported.
2336 * 2) The shader uses indirect addressing, less constants than
2337 * supported, but uses a constant index > #supported consts. */
2338 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2340 /* No indirect addressing here. */
2341 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2343 else
2345 if (reg_maps->usesrelconstF)
2347 /* Subtract the other potential uniforms from the max
2348 * available (bools, ints, and 1 row of projection matrix).
2349 * Subtract another uniform for immediate values, which have
2350 * to be loaded via uniform by the driver as well. The shader
2351 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2352 * shader code, so one vec4 should be enough. (Unfortunately
2353 * the Nvidia driver doesn't store 128 and -128 in one float).
2355 * Writing gl_ClipVertex requires one uniform for each
2356 * clipplane as well. */
2357 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2358 if (vs_args->clip_enabled)
2359 max_constantsF -= gl_info->limits.user_clip_distances;
2360 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2361 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2362 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2363 * for now take this into account when calculating the number of available constants
2365 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2366 /* Set by driver quirks in directx.c */
2367 max_constantsF -= gl_info->reserved_glsl_constants;
2369 if (max_constantsF < shader->limits->constant_float)
2371 static unsigned int once;
2373 if (!once++)
2374 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2375 " it may not render correctly.\n");
2376 else
2377 WARN("The hardware does not support enough uniform components to run this shader.\n");
2380 else
2382 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2385 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2386 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2389 /* Always declare the full set of constants, the compiler can remove the
2390 * unused ones because d3d doesn't (yet) support indirect int and bool
2391 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2392 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2393 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2395 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2396 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2398 /* Declare immediate constant buffer */
2399 if (reg_maps->icb)
2400 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2402 /* Declare constant buffers */
2403 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2404 &uniform_block_base, &uniform_block_count);
2405 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2407 if (reg_maps->cb_sizes[i])
2409 shader_addline(buffer, "layout(std140");
2410 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2411 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2412 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2413 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2417 /* Declare texture samplers */
2418 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2420 struct wined3d_shader_sampler_map_entry *entry;
2421 const char *sampler_type_prefix, *sampler_type;
2422 BOOL shadow_sampler;
2424 entry = &reg_maps->sampler_map.entries[i];
2426 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2428 ERR("Invalid resource index %u.\n", entry->resource_idx);
2429 continue;
2432 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2434 case WINED3D_DATA_FLOAT:
2435 case WINED3D_DATA_UNORM:
2436 case WINED3D_DATA_SNORM:
2437 sampler_type_prefix = "";
2438 break;
2440 case WINED3D_DATA_INT:
2441 sampler_type_prefix = "i";
2442 break;
2444 case WINED3D_DATA_UINT:
2445 sampler_type_prefix = "u";
2446 break;
2448 default:
2449 sampler_type_prefix = "";
2450 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2451 break;
2454 shadow_sampler = shader_sampler_is_shadow(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2455 resource_type = version->type == WINED3D_SHADER_TYPE_PIXEL
2456 ? pixelshader_get_resource_type(reg_maps, entry->resource_idx, ps_args->tex_types)
2457 : reg_maps->resource_info[entry->resource_idx].type;
2459 switch (resource_type)
2461 case WINED3D_SHADER_RESOURCE_BUFFER:
2462 sampler_type = "samplerBuffer";
2463 break;
2465 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2466 if (shadow_sampler)
2467 sampler_type = "sampler1DShadow";
2468 else
2469 sampler_type = "sampler1D";
2470 break;
2472 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2473 if (shadow_sampler)
2474 sampler_type = "sampler2DShadow";
2475 else
2476 sampler_type = "sampler2D";
2477 break;
2479 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2480 if (shadow_sampler)
2481 FIXME("Unsupported 3D shadow sampler.\n");
2482 sampler_type = "sampler3D";
2483 break;
2485 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2486 if (shadow_sampler)
2487 sampler_type = "samplerCubeShadow";
2488 else
2489 sampler_type = "samplerCube";
2490 break;
2492 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2493 if (shadow_sampler)
2494 sampler_type = "sampler1DArrayShadow";
2495 else
2496 sampler_type = "sampler1DArray";
2497 break;
2499 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2500 if (shadow_sampler)
2501 sampler_type = "sampler2DArrayShadow";
2502 else
2503 sampler_type = "sampler2DArray";
2504 break;
2506 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2507 if (shadow_sampler)
2508 sampler_type = "samplerCubeArrayShadow";
2509 else
2510 sampler_type = "samplerCubeArray";
2511 break;
2513 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2514 sampler_type = "sampler2DMS";
2515 break;
2517 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2518 sampler_type = "sampler2DMSArray";
2519 break;
2521 default:
2522 sampler_type = "unsupported_sampler";
2523 FIXME("Unhandled resource type %#x.\n", resource_type);
2524 break;
2527 if (gl_info->supported[ARB_BINDLESS_TEXTURE])
2528 shader_addline(buffer, "layout(bindless_sampler) ");
2529 else if (shader_glsl_use_layout_binding_qualifier(gl_info))
2530 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, version, entry->bind_idx);
2531 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2532 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2535 /* Declare images */
2536 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2538 const char *image_type_prefix, *image_type, *read_format;
2540 if (!reg_maps->uav_resource_info[i].type)
2541 continue;
2543 switch (reg_maps->uav_resource_info[i].data_type)
2545 case WINED3D_DATA_FLOAT:
2546 case WINED3D_DATA_UNORM:
2547 case WINED3D_DATA_SNORM:
2548 image_type_prefix = "";
2549 read_format = "r32f";
2550 break;
2552 case WINED3D_DATA_INT:
2553 image_type_prefix = "i";
2554 read_format = "r32i";
2555 break;
2557 case WINED3D_DATA_UINT:
2558 image_type_prefix = "u";
2559 read_format = "r32ui";
2560 break;
2562 default:
2563 image_type_prefix = "";
2564 read_format = "";
2565 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2566 break;
2569 switch (reg_maps->uav_resource_info[i].type)
2571 case WINED3D_SHADER_RESOURCE_BUFFER:
2572 image_type = "imageBuffer";
2573 break;
2575 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2576 image_type = "image1D";
2577 break;
2579 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2580 image_type = "image2D";
2581 break;
2583 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2584 image_type = "image3D";
2585 break;
2587 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2588 image_type = "image1DArray";
2589 break;
2591 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2592 image_type = "image2DArray";
2593 break;
2595 default:
2596 image_type = "unsupported_image";
2597 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2598 break;
2601 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2602 shader_addline(buffer, "layout(binding = %u)\n", i);
2603 if (reg_maps->uav_read_mask & (1u << i))
2604 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2605 read_format, image_type_prefix, image_type, prefix, i);
2606 else
2607 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2608 image_type_prefix, image_type, prefix, i);
2610 if (reg_maps->uav_counter_mask & (1u << i))
2611 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2612 i, prefix, i);
2615 /* Declare address variables */
2616 map = reg_maps->address;
2617 while (map)
2619 i = wined3d_bit_scan(&map);
2620 shader_addline(buffer, "ivec4 A%u;\n", i);
2623 /* Declare output register temporaries */
2624 if (shader->limits->packed_output)
2625 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2627 /* Declare temporary variables */
2628 if (reg_maps->temporary_count)
2630 for (i = 0; i < reg_maps->temporary_count; ++i)
2631 shader_addline(buffer, "vec4 R%u;\n", i);
2633 else if (version->major < 4)
2635 map = reg_maps->temporary;
2636 while (map)
2638 i = wined3d_bit_scan(&map);
2639 shader_addline(buffer, "vec4 R%u;\n", i);
2643 /* Declare indexable temporary variables */
2644 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2646 if (idx_temp_reg->component_count != 4)
2647 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2648 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2651 /* Declare loop registers aLx */
2652 if (version->major < 4)
2654 for (i = 0; i < reg_maps->loop_depth; ++i)
2656 shader_addline(buffer, "int aL%u;\n", i);
2657 shader_addline(buffer, "int tmpInt%u;\n", i);
2661 /* Temporary variables for matrix operations */
2662 shader_addline(buffer, "vec4 tmp0;\n");
2663 shader_addline(buffer, "vec4 tmp1;\n");
2664 if (gl_info->supported[ARB_GPU_SHADER5])
2665 shader_addline(buffer, "precise vec4 tmp_precise[2];\n");
2666 else
2667 shader_addline(buffer, "/* precise */ vec4 tmp_precise[2];\n");
2669 if (!shader->load_local_constsF)
2671 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2673 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2674 shader_glsl_append_imm_vec(buffer, (const float *)lconst->value, ARRAY_SIZE(lconst->value), gl_info);
2675 shader_addline(buffer, ";\n");
2680 /* Prototypes */
2681 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
2682 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2683 enum wined3d_data_type data_type);
2685 /** Used for opcode modifiers - They multiply the result by the specified amount */
2686 static const char * const shift_glsl_tab[] = {
2687 "", /* 0 (none) */
2688 "2.0 * ", /* 1 (x2) */
2689 "4.0 * ", /* 2 (x4) */
2690 "8.0 * ", /* 3 (x8) */
2691 "16.0 * ", /* 4 (x16) */
2692 "32.0 * ", /* 5 (x32) */
2693 "", /* 6 (x64) */
2694 "", /* 7 (x128) */
2695 "", /* 8 (d256) */
2696 "", /* 9 (d128) */
2697 "", /* 10 (d64) */
2698 "", /* 11 (d32) */
2699 "0.0625 * ", /* 12 (d16) */
2700 "0.125 * ", /* 13 (d8) */
2701 "0.25 * ", /* 14 (d4) */
2702 "0.5 * " /* 15 (d2) */
2705 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2706 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2707 const char *in_reg, const char *in_regswizzle, char *out_str)
2709 switch (src_modifier)
2711 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2712 case WINED3DSPSM_DW:
2713 case WINED3DSPSM_NONE:
2714 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2715 break;
2716 case WINED3DSPSM_NEG:
2717 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2718 break;
2719 case WINED3DSPSM_NOT:
2720 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2721 break;
2722 case WINED3DSPSM_BIAS:
2723 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2724 break;
2725 case WINED3DSPSM_BIASNEG:
2726 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2727 break;
2728 case WINED3DSPSM_SIGN:
2729 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2730 break;
2731 case WINED3DSPSM_SIGNNEG:
2732 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2733 break;
2734 case WINED3DSPSM_COMP:
2735 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2736 break;
2737 case WINED3DSPSM_X2:
2738 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2739 break;
2740 case WINED3DSPSM_X2NEG:
2741 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2742 break;
2743 case WINED3DSPSM_ABS:
2744 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2745 break;
2746 case WINED3DSPSM_ABSNEG:
2747 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2748 break;
2749 default:
2750 FIXME("Unhandled modifier %u\n", src_modifier);
2751 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2755 static void shader_glsl_fixup_scalar_register_variable(struct wined3d_string_buffer *register_name,
2756 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2758 /* The ARB_shading_language_420pack extension allows swizzle operations on
2759 * scalars. */
2760 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2761 string_buffer_sprintf(register_name, "%s", glsl_variable);
2762 else
2763 string_buffer_sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2766 /** Writes the GLSL variable name that corresponds to the register that the
2767 * DX opcode parameter is trying to access */
2768 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2769 enum wined3d_data_type data_type, struct wined3d_string_buffer *register_name,
2770 BOOL *is_swizzled, const struct wined3d_shader_context *ctx)
2772 /* oPos, oFog and oPts in D3D */
2773 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2775 const struct wined3d_shader *shader = ctx->shader;
2776 const struct wined3d_shader_reg_maps *reg_maps = ctx->reg_maps;
2777 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2778 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2779 const char *prefix = shader_glsl_get_prefix(version->type);
2780 const struct wined3d_gl_info *gl_info = priv->gl_info;
2781 struct glsl_src_param rel_param0, rel_param1;
2783 if (reg->idx[0].offset != ~0u && reg->idx[0].rel_addr)
2784 shader_glsl_add_src_param_ext(ctx, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0,
2785 &rel_param0, reg->idx[0].rel_addr->reg.data_type);
2786 if (reg->idx[1].offset != ~0u && reg->idx[1].rel_addr)
2787 shader_glsl_add_src_param_ext(ctx, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0,
2788 &rel_param1, reg->idx[1].rel_addr->reg.data_type);
2789 if (is_swizzled)
2790 *is_swizzled = FALSE;
2792 switch (reg->type)
2794 case WINED3DSPR_TEMP:
2795 string_buffer_sprintf(register_name, "R%u", reg->idx[0].offset);
2796 break;
2798 case WINED3DSPR_INPUT:
2799 case WINED3DSPR_INCONTROLPOINT:
2800 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2802 if (reg->idx[0].rel_addr)
2803 FIXME("VS3 input registers relative addressing.\n");
2804 if (is_swizzled && priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2805 *is_swizzled = TRUE;
2806 if (reg->idx[0].rel_addr)
2808 string_buffer_sprintf(register_name, "%s_in[%s + %u]",
2809 prefix, rel_param0.param_str, reg->idx[0].offset);
2811 else
2813 string_buffer_sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2815 break;
2818 if (version->type == WINED3D_SHADER_TYPE_HULL
2819 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2820 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2822 if (reg->idx[0].rel_addr)
2824 if (reg->idx[1].rel_addr)
2825 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2826 rel_param0.param_str, reg->idx[0].offset,
2827 rel_param1.param_str, reg->idx[1].offset);
2828 else
2829 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2830 rel_param0.param_str, reg->idx[0].offset,
2831 reg->idx[1].offset);
2833 else if (reg->idx[1].rel_addr)
2834 string_buffer_sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2835 rel_param1.param_str, reg->idx[1].offset);
2836 else
2837 string_buffer_sprintf(register_name, "shader_in[%u].reg[%u]",
2838 reg->idx[0].offset, reg->idx[1].offset);
2839 break;
2842 /* pixel shaders >= 3.0 */
2843 if (version->major >= 3)
2845 unsigned int idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2846 unsigned int in_count = vec4_varyings(version->major, gl_info);
2848 if (reg->idx[0].rel_addr)
2850 /* Removing a + 0 would be an obvious optimization, but
2851 * OS X doesn't see the NOP operation there. */
2852 if (idx)
2854 if (needs_legacy_glsl_syntax(gl_info)
2855 && shader->u.ps.declared_in_count > in_count)
2857 string_buffer_sprintf(register_name,
2858 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2859 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2860 prefix, rel_param0.param_str, idx);
2862 else
2864 string_buffer_sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2867 else
2869 if (needs_legacy_glsl_syntax(gl_info)
2870 && shader->u.ps.declared_in_count > in_count)
2872 string_buffer_sprintf(register_name,
2873 "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2874 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2875 prefix, rel_param0.param_str);
2877 else
2879 string_buffer_sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2883 else
2885 if (idx == in_count)
2886 string_buffer_sprintf(register_name, "gl_Color");
2887 else if (idx == in_count + 1)
2888 string_buffer_sprintf(register_name, "gl_SecondaryColor");
2889 else
2890 string_buffer_sprintf(register_name, "%s_in[%u]", prefix, idx);
2893 else
2895 if (!reg->idx[0].offset)
2896 string_buffer_sprintf(register_name, "ffp_varying_diffuse");
2897 else
2898 string_buffer_sprintf(register_name, "ffp_varying_specular");
2899 break;
2901 break;
2903 case WINED3DSPR_CONST:
2905 /* Relative addressing */
2906 if (reg->idx[0].rel_addr)
2908 if (wined3d_settings.check_float_constants)
2909 string_buffer_sprintf(register_name,
2910 "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2911 rel_param0.param_str, reg->idx[0].offset,
2912 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2913 prefix, rel_param0.param_str, reg->idx[0].offset);
2914 else if (reg->idx[0].offset)
2915 string_buffer_sprintf(register_name, "%s_c[%s + %u]",
2916 prefix, rel_param0.param_str, reg->idx[0].offset);
2917 else
2918 string_buffer_sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2920 else
2922 if (shader_constant_is_local(shader, reg->idx[0].offset))
2923 string_buffer_sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2924 else
2925 string_buffer_sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2928 break;
2930 case WINED3DSPR_CONSTINT:
2931 string_buffer_sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2932 break;
2934 case WINED3DSPR_CONSTBOOL:
2935 string_buffer_sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2936 break;
2938 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2939 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2940 string_buffer_sprintf(register_name, "T%u", reg->idx[0].offset);
2941 else
2942 string_buffer_sprintf(register_name, "A%u", reg->idx[0].offset);
2943 break;
2945 case WINED3DSPR_LOOP:
2946 string_buffer_sprintf(register_name, "aL%u", ctx->state->current_loop_reg - 1);
2947 break;
2949 case WINED3DSPR_SAMPLER:
2950 string_buffer_sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2951 break;
2953 case WINED3DSPR_COLOROUT:
2954 if (reg->idx[0].offset >= gl_info->limits.buffers)
2955 WARN("Write to render target %u, only %d supported.\n",
2956 reg->idx[0].offset, gl_info->limits.buffers);
2958 string_buffer_sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2959 break;
2961 case WINED3DSPR_RASTOUT:
2962 string_buffer_sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2963 break;
2965 case WINED3DSPR_DEPTHOUT:
2966 case WINED3DSPR_DEPTHOUTGE:
2967 case WINED3DSPR_DEPTHOUTLE:
2968 string_buffer_sprintf(register_name, "gl_FragDepth");
2969 break;
2971 case WINED3DSPR_ATTROUT:
2972 if (!reg->idx[0].offset)
2973 string_buffer_sprintf(register_name, "%s_out[8]", prefix);
2974 else
2975 string_buffer_sprintf(register_name, "%s_out[9]", prefix);
2976 break;
2978 case WINED3DSPR_TEXCRDOUT:
2979 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2980 if (reg->idx[0].rel_addr)
2981 string_buffer_sprintf(register_name, "%s_out[%s + %u]",
2982 prefix, rel_param0.param_str, reg->idx[0].offset);
2983 else
2984 string_buffer_sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2985 break;
2987 case WINED3DSPR_MISCTYPE:
2988 if (!reg->idx[0].offset)
2990 /* vPos */
2991 string_buffer_sprintf(register_name, "vpos");
2993 else if (reg->idx[0].offset == 1)
2995 /* Note that gl_FrontFacing is a bool, while vFace is
2996 * a float for which the sign determines front/back */
2997 string_buffer_sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2999 else
3001 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
3002 string_buffer_sprintf(register_name, "unrecognized_register");
3004 break;
3006 case WINED3DSPR_IMMCONST:
3007 switch (reg->immconst_type)
3009 case WINED3D_IMMCONST_SCALAR:
3010 switch (data_type)
3012 case WINED3D_DATA_UNORM:
3013 case WINED3D_DATA_SNORM:
3014 case WINED3D_DATA_FLOAT:
3015 string_buffer_clear(register_name);
3016 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 1, gl_info);
3017 break;
3018 case WINED3D_DATA_INT:
3019 string_buffer_sprintf(register_name, "%#x", reg->u.immconst_data[0]);
3020 break;
3021 case WINED3D_DATA_RESOURCE:
3022 case WINED3D_DATA_SAMPLER:
3023 case WINED3D_DATA_UINT:
3024 string_buffer_sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
3025 break;
3026 default:
3027 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
3028 break;
3030 break;
3032 case WINED3D_IMMCONST_VEC4:
3033 switch (data_type)
3035 case WINED3D_DATA_UNORM:
3036 case WINED3D_DATA_SNORM:
3037 case WINED3D_DATA_FLOAT:
3038 string_buffer_clear(register_name);
3039 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 4, gl_info);
3040 break;
3041 case WINED3D_DATA_INT:
3042 string_buffer_sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
3043 reg->u.immconst_data[0], reg->u.immconst_data[1],
3044 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3045 break;
3046 case WINED3D_DATA_RESOURCE:
3047 case WINED3D_DATA_SAMPLER:
3048 case WINED3D_DATA_UINT:
3049 string_buffer_sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
3050 reg->u.immconst_data[0], reg->u.immconst_data[1],
3051 reg->u.immconst_data[2], reg->u.immconst_data[3]);
3052 break;
3053 default:
3054 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
3055 break;
3057 break;
3059 default:
3060 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
3061 string_buffer_sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
3063 break;
3065 case WINED3DSPR_CONSTBUFFER:
3066 if (reg->idx[1].rel_addr)
3067 string_buffer_sprintf(register_name, "%s_cb%u[%s + %u]",
3068 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3069 else
3070 string_buffer_sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
3071 break;
3073 case WINED3DSPR_IMMCONSTBUFFER:
3074 if (reg->idx[0].rel_addr)
3075 string_buffer_sprintf(register_name, "%s_icb[%s + %u]",
3076 prefix, rel_param0.param_str, reg->idx[0].offset);
3077 else
3078 string_buffer_sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
3079 break;
3081 case WINED3DSPR_PRIMID:
3082 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
3083 string_buffer_sprintf(register_name, "gl_PrimitiveIDIn");
3084 else
3085 string_buffer_sprintf(register_name, "gl_PrimitiveID");
3086 break;
3088 case WINED3DSPR_IDXTEMP:
3089 if (reg->idx[1].rel_addr)
3090 string_buffer_sprintf(register_name, "X%u[%s + %u]",
3091 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
3092 else
3093 string_buffer_sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
3094 break;
3096 case WINED3DSPR_LOCALTHREADINDEX:
3097 shader_glsl_fixup_scalar_register_variable(register_name,
3098 "int(gl_LocalInvocationIndex)", gl_info);
3099 break;
3101 case WINED3DSPR_GSINSTID:
3102 case WINED3DSPR_OUTPOINTID:
3103 shader_glsl_fixup_scalar_register_variable(register_name,
3104 "gl_InvocationID", gl_info);
3105 break;
3107 case WINED3DSPR_THREADID:
3108 string_buffer_sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3109 break;
3111 case WINED3DSPR_THREADGROUPID:
3112 string_buffer_sprintf(register_name, "ivec3(gl_WorkGroupID)");
3113 break;
3115 case WINED3DSPR_LOCALTHREADID:
3116 string_buffer_sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3117 break;
3119 case WINED3DSPR_FORKINSTID:
3120 case WINED3DSPR_JOININSTID:
3121 shader_glsl_fixup_scalar_register_variable(register_name,
3122 "phase_instance_id", gl_info);
3123 break;
3125 case WINED3DSPR_TESSCOORD:
3126 string_buffer_sprintf(register_name, "gl_TessCoord");
3127 break;
3129 case WINED3DSPR_OUTCONTROLPOINT:
3130 if (reg->idx[0].rel_addr)
3132 if (reg->idx[1].rel_addr)
3133 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3134 rel_param0.param_str, reg->idx[0].offset,
3135 rel_param1.param_str, reg->idx[1].offset);
3136 else
3137 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3138 rel_param0.param_str, reg->idx[0].offset,
3139 reg->idx[1].offset);
3141 else if (reg->idx[1].rel_addr)
3143 string_buffer_sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3144 reg->idx[0].offset, rel_param1.param_str,
3145 reg->idx[1].offset);
3147 else
3149 string_buffer_sprintf(register_name, "shader_out[%u].reg[%u]",
3150 reg->idx[0].offset, reg->idx[1].offset);
3152 break;
3154 case WINED3DSPR_PATCHCONST:
3155 if (version->type == WINED3D_SHADER_TYPE_HULL)
3156 string_buffer_sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3157 else
3158 string_buffer_sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3159 break;
3161 case WINED3DSPR_COVERAGE:
3162 string_buffer_sprintf(register_name, "gl_SampleMaskIn[0]");
3163 break;
3165 case WINED3DSPR_SAMPLEMASK:
3166 string_buffer_sprintf(register_name, "sample_mask");
3167 break;
3169 case WINED3DSPR_STENCILREF:
3170 string_buffer_sprintf(register_name, "stencil_ref");
3171 break;
3173 default:
3174 FIXME("Unhandled register type %#x.\n", reg->type);
3175 string_buffer_sprintf(register_name, "unrecognised_register");
3176 break;
3180 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3182 *str++ = '.';
3183 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3184 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3185 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3186 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3187 *str = '\0';
3190 /* Get the GLSL write mask for the destination register */
3191 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3193 DWORD mask = param->write_mask;
3195 if (shader_is_scalar(&param->reg))
3197 mask = WINED3DSP_WRITEMASK_0;
3198 *write_mask = '\0';
3200 else
3202 shader_glsl_write_mask_to_str(mask, write_mask);
3205 return mask;
3208 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3210 unsigned int size = 0;
3212 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3213 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3214 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3215 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3217 return size;
3220 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3221 unsigned int component_idx)
3223 /* swizzle bits fields: wwzzyyxx */
3224 return (swizzle >> (2 * component_idx)) & 0x3;
3227 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3229 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3230 * but addressed as "rgba". To fix this we need to swap the register's x
3231 * and z components. */
3232 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3233 unsigned int i;
3235 *str++ = '.';
3236 for (i = 0; i < 4; ++i)
3238 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3239 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3241 *str = '\0';
3244 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3245 BOOL fixup, DWORD mask, char *swizzle_str)
3247 if (shader_is_scalar(&param->reg))
3248 *swizzle_str = '\0';
3249 else
3250 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3253 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3254 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type, unsigned int size)
3256 if (dst_data_type == WINED3D_DATA_UNORM || dst_data_type == WINED3D_DATA_SNORM)
3257 dst_data_type = WINED3D_DATA_FLOAT;
3258 if (src_data_type == WINED3D_DATA_UNORM || src_data_type == WINED3D_DATA_SNORM)
3259 src_data_type = WINED3D_DATA_FLOAT;
3261 if (dst_data_type == src_data_type)
3263 string_buffer_sprintf(dst_param, "%s", src_param);
3264 return;
3267 if (src_data_type == WINED3D_DATA_FLOAT)
3269 switch (dst_data_type)
3271 case WINED3D_DATA_INT:
3272 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3273 return;
3274 case WINED3D_DATA_RESOURCE:
3275 case WINED3D_DATA_SAMPLER:
3276 case WINED3D_DATA_UINT:
3277 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3278 return;
3279 default:
3280 break;
3284 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3286 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3287 return;
3290 if (src_data_type == WINED3D_DATA_INT)
3292 switch (dst_data_type)
3294 case WINED3D_DATA_FLOAT:
3295 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3296 return;
3297 case WINED3D_DATA_UINT:
3298 if (size == 1)
3299 string_buffer_sprintf(dst_param, "uint(%s)", src_param);
3300 else
3301 string_buffer_sprintf(dst_param, "uvec%u(%s)", size, src_param);
3302 return;
3303 default:
3304 break;
3308 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3309 string_buffer_sprintf(dst_param, "%s", src_param);
3312 /* From a given parameter token, generate the corresponding GLSL string.
3313 * Also, return the actual register name and swizzle in case the
3314 * caller needs this information as well. */
3315 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
3316 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3317 enum wined3d_data_type data_type)
3319 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3320 struct wined3d_string_buffer *param_str = string_buffer_get(priv->string_buffers);
3321 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3322 enum wined3d_data_type param_data_type;
3323 BOOL is_color = FALSE;
3324 char swizzle_str[6];
3325 unsigned int size;
3327 glsl_src->param_str[0] = '\0';
3328 swizzle_str[0] = '\0';
3330 shader_glsl_get_register_name(&wined3d_src->reg, data_type, reg_name, &is_color, ctx);
3331 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3333 switch (wined3d_src->reg.type)
3335 case WINED3DSPR_IMMCONST:
3336 param_data_type = data_type;
3337 size = wined3d_src->reg.immconst_type == WINED3D_IMMCONST_SCALAR ? 1 : 4;
3338 break;
3339 case WINED3DSPR_FORKINSTID:
3340 case WINED3DSPR_GSINSTID:
3341 case WINED3DSPR_JOININSTID:
3342 case WINED3DSPR_LOCALTHREADINDEX:
3343 case WINED3DSPR_OUTPOINTID:
3344 case WINED3DSPR_PRIMID:
3345 param_data_type = WINED3D_DATA_INT;
3346 size = 1;
3347 break;
3348 case WINED3DSPR_LOCALTHREADID:
3349 case WINED3DSPR_THREADGROUPID:
3350 case WINED3DSPR_THREADID:
3351 param_data_type = WINED3D_DATA_INT;
3352 size = 3;
3353 break;
3354 default:
3355 param_data_type = WINED3D_DATA_FLOAT;
3356 size = 4;
3357 break;
3360 shader_glsl_sprintf_cast(param_str, reg_name->buffer, data_type, param_data_type, size);
3361 shader_glsl_gen_modifier(wined3d_src->modifiers, param_str->buffer, swizzle_str, glsl_src->param_str);
3363 string_buffer_release(priv->string_buffers, reg_name);
3364 string_buffer_release(priv->string_buffers, param_str);
3367 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3368 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3370 shader_glsl_add_src_param_ext(ins->ctx, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3373 /* From a given parameter token, generate the corresponding GLSL string.
3374 * Also, return the actual register name and swizzle in case the
3375 * caller needs this information as well. */
3376 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3377 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3379 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3380 struct wined3d_string_buffer *reg_name;
3381 size_t len;
3383 glsl_dst->mask_str[0] = '\0';
3385 reg_name = string_buffer_get(priv->string_buffers);
3386 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type, reg_name, NULL, ins->ctx);
3387 len = min(reg_name->content_size, ARRAY_SIZE(glsl_dst->reg_name) - 1);
3388 memcpy(glsl_dst->reg_name, reg_name->buffer, len);
3389 glsl_dst->reg_name[len] = '\0';
3390 string_buffer_release(priv->string_buffers, reg_name);
3392 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3395 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3396 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3397 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3398 unsigned int dst_idx, enum wined3d_data_type data_type)
3400 struct glsl_dst_param glsl_dst;
3401 DWORD mask;
3403 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3405 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3406 sprintf(glsl_dst.reg_name, "tmp_precise[%u]", dst_idx);
3408 switch (data_type)
3410 case WINED3D_DATA_FLOAT:
3411 case WINED3D_DATA_UNORM:
3412 case WINED3D_DATA_SNORM:
3413 shader_addline(buffer, "%s%s = %s(",
3414 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3415 break;
3416 case WINED3D_DATA_INT:
3417 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3418 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3419 break;
3420 case WINED3D_DATA_RESOURCE:
3421 case WINED3D_DATA_SAMPLER:
3422 case WINED3D_DATA_UINT:
3423 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3424 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3425 break;
3426 default:
3427 FIXME("Unhandled data type %#x.\n", data_type);
3428 shader_addline(buffer, "%s%s = %s(",
3429 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3430 break;
3434 return mask;
3437 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3438 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3440 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3443 /** Process GLSL instruction modifiers */
3444 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3446 const struct wined3d_shader_dst_param *dst;
3447 struct glsl_dst_param dst_param;
3448 DWORD modifiers;
3449 unsigned int i;
3451 for (i = 0; i < ins->dst_count; ++i)
3453 if ((dst = &ins->dst[i])->reg.type == WINED3DSPR_NULL)
3454 continue;
3456 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3458 shader_glsl_add_dst_param(ins, dst, &dst_param);
3459 shader_addline(ins->ctx->buffer, "%s%s = tmp_precise[%u]%s;\n",
3460 dst_param.reg_name, dst_param.mask_str, i, dst_param.mask_str);
3463 if (!(modifiers = dst->modifiers))
3464 continue;
3466 shader_glsl_add_dst_param(ins, dst, &dst_param);
3468 if (modifiers & WINED3DSPDM_SATURATE)
3470 /* _SAT means to clamp the value of the register to between 0 and 1 */
3471 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3472 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3475 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3477 FIXME("_centroid modifier not handled\n");
3480 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3482 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3487 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3489 switch (op)
3491 case WINED3D_SHADER_REL_OP_GT: return ">";
3492 case WINED3D_SHADER_REL_OP_EQ: return "==";
3493 case WINED3D_SHADER_REL_OP_GE: return ">=";
3494 case WINED3D_SHADER_REL_OP_LT: return "<";
3495 case WINED3D_SHADER_REL_OP_NE: return "!=";
3496 case WINED3D_SHADER_REL_OP_LE: return "<=";
3497 default:
3498 FIXME("Unrecognized operator %#x.\n", op);
3499 return "(\?\?)";
3503 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3505 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3508 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3509 unsigned int *coord_size, unsigned int *deriv_size)
3511 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3512 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3514 *coord_size = resource_type_info[resource_type].coord_size;
3515 *deriv_size = *coord_size;
3516 if (is_array)
3517 --(*deriv_size);
3520 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3521 DWORD resource_idx, DWORD sampler_idx, uint32_t flags, struct glsl_sample_function *sample_function)
3523 enum wined3d_shader_resource_type resource_type;
3524 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3525 const struct wined3d_gl_info *gl_info = priv->gl_info;
3526 BOOL shadow = shader_sampler_is_shadow(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3527 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3528 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3529 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3530 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3531 const char *base = "texture", *type_part = "", *suffix = "";
3532 unsigned int coord_size, deriv_size;
3534 resource_type = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3535 ? pixelshader_get_resource_type(ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
3536 : ctx->reg_maps->resource_info[resource_idx].type;
3538 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3540 if (resource_type >= ARRAY_SIZE(resource_type_info))
3542 ERR("Unexpected resource type %#x.\n", resource_type);
3543 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3546 /* Note that there's no such thing as a projected cube texture. */
3547 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3548 projected = FALSE;
3550 if (needs_legacy_glsl_syntax(gl_info))
3552 if (shadow)
3553 base = "shadow";
3555 type_part = resource_type_info[resource_type].type_part;
3556 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3557 FIXME("Unhandled resource type %#x.\n", resource_type);
3559 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3561 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3562 suffix = "ARB";
3563 else
3564 FIXME("Unsupported grad function.\n");
3568 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3570 static const uint32_t texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3571 if (flags & ~texel_fetch_flags)
3572 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3574 base = "texelFetch";
3575 type_part = "";
3578 sample_function->name = string_buffer_get(priv->string_buffers);
3579 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3580 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3582 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3583 if (shadow)
3584 ++coord_size;
3585 sample_function->offset_size = offset ? deriv_size : 0;
3586 sample_function->coord_mask = wined3d_mask_from_size(coord_size);
3587 sample_function->deriv_mask = wined3d_mask_from_size(deriv_size);
3588 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3591 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3592 struct glsl_sample_function *sample_function)
3594 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3596 string_buffer_release(priv->string_buffers, sample_function->name);
3599 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3600 BOOL sign_fixup, enum fixup_channel_source channel_source)
3602 switch(channel_source)
3604 case CHANNEL_SOURCE_ZERO:
3605 strcat(arguments, "0.0");
3606 break;
3608 case CHANNEL_SOURCE_ONE:
3609 strcat(arguments, "1.0");
3610 break;
3612 case CHANNEL_SOURCE_X:
3613 strcat(arguments, reg_name);
3614 strcat(arguments, ".x");
3615 break;
3617 case CHANNEL_SOURCE_Y:
3618 strcat(arguments, reg_name);
3619 strcat(arguments, ".y");
3620 break;
3622 case CHANNEL_SOURCE_Z:
3623 strcat(arguments, reg_name);
3624 strcat(arguments, ".z");
3625 break;
3627 case CHANNEL_SOURCE_W:
3628 strcat(arguments, reg_name);
3629 strcat(arguments, ".w");
3630 break;
3632 default:
3633 FIXME("Unhandled channel source %#x\n", channel_source);
3634 strcat(arguments, "undefined");
3635 break;
3638 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3641 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3642 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3644 unsigned int mask_size, remaining;
3645 DWORD fixup_mask = 0;
3646 char arguments[256];
3647 char mask_str[6];
3649 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3650 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3651 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3652 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3653 if (!(mask &= fixup_mask))
3654 return;
3656 if (is_complex_fixup(fixup))
3658 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3659 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3660 return;
3663 shader_glsl_write_mask_to_str(mask, mask_str);
3664 mask_size = shader_glsl_get_write_mask_size(mask);
3666 arguments[0] = '\0';
3667 remaining = mask_size;
3668 if (mask & WINED3DSP_WRITEMASK_0)
3670 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3671 if (--remaining) strcat(arguments, ", ");
3673 if (mask & WINED3DSP_WRITEMASK_1)
3675 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3676 if (--remaining) strcat(arguments, ", ");
3678 if (mask & WINED3DSP_WRITEMASK_2)
3680 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3681 if (--remaining) strcat(arguments, ", ");
3683 if (mask & WINED3DSP_WRITEMASK_3)
3685 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3686 if (--remaining) strcat(arguments, ", ");
3689 if (mask_size > 1)
3690 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3691 else
3692 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3695 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3697 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3698 struct wined3d_string_buffer *reg_name;
3700 reg_name = string_buffer_get(priv->string_buffers);
3701 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, NULL, ins->ctx);
3702 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name->buffer, ins->dst[0].write_mask, fixup);
3703 string_buffer_release(priv->string_buffers, reg_name);
3706 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3707 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3708 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3709 const char *coord_reg_fmt, ...)
3711 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3712 char dst_swizzle[6];
3713 struct color_fixup_desc fixup;
3714 va_list args;
3715 int ret;
3717 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3719 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3720 * We actually rely on it for vertex shaders and SM4+. */
3721 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3723 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3724 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3726 else
3728 fixup = COLOR_FIXUP_IDENTITY;
3731 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, sample_function->data_type);
3733 if (sample_function->output_single_component)
3734 shader_addline(ins->ctx->buffer, "vec4(");
3736 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3737 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3739 for (;;)
3741 va_start(args, coord_reg_fmt);
3742 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3743 va_end(args);
3744 if (!ret)
3745 break;
3746 if (!string_buffer_resize(ins->ctx->buffer, ret))
3747 break;
3750 if (dx && dy)
3751 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3752 else if (bias)
3753 shader_addline(ins->ctx->buffer, ", %s", bias);
3754 if (sample_function->offset_size)
3756 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3757 shader_addline(ins->ctx->buffer, ", ");
3758 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3760 shader_addline(ins->ctx->buffer, ")");
3762 if (sample_function->output_single_component)
3763 shader_addline(ins->ctx->buffer, ")");
3765 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3767 if (!is_identity_fixup(fixup))
3768 shader_glsl_color_correction(ins, fixup);
3771 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3773 /* Write the final position.
3775 * OpenGL coordinates specify the center of the pixel while D3D coords
3776 * specify the corner. The offsets are stored in z and w in
3777 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3778 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3779 * a MAD. */
3780 if (use_viewport_index)
3782 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3783 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3785 else
3787 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3788 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3791 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3792 * in utils.c
3794 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3795 * shaders are run before the homogeneous divide, so we have to take the w
3796 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3797 * z = z * 2 - w. */
3798 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3801 /*****************************************************************************
3802 * Begin processing individual instruction opcodes
3803 ****************************************************************************/
3805 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3807 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3808 struct glsl_src_param src0_param;
3809 struct glsl_src_param src1_param;
3810 DWORD write_mask;
3811 const char *op;
3813 /* Determine the GLSL operator to use based on the opcode */
3814 switch (ins->handler_idx)
3816 case WINED3DSIH_ADD: op = "+"; break;
3817 case WINED3DSIH_AND: op = "&"; break;
3818 case WINED3DSIH_DIV: op = "/"; break;
3819 case WINED3DSIH_IADD: op = "+"; break;
3820 case WINED3DSIH_ISHL: op = "<<"; break;
3821 case WINED3DSIH_ISHR: op = ">>"; break;
3822 case WINED3DSIH_MUL: op = "*"; break;
3823 case WINED3DSIH_OR: op = "|"; break;
3824 case WINED3DSIH_SUB: op = "-"; break;
3825 case WINED3DSIH_USHR: op = ">>"; break;
3826 case WINED3DSIH_XOR: op = "^"; break;
3827 default:
3828 op = "<unhandled operator>";
3829 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3830 break;
3833 write_mask = shader_glsl_append_dst(buffer, ins);
3834 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3835 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3836 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3839 static void shader_glsl_relop(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 unsigned int mask_size;
3845 DWORD write_mask;
3846 const char *op;
3848 write_mask = shader_glsl_append_dst(buffer, ins);
3849 mask_size = shader_glsl_get_write_mask_size(write_mask);
3850 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3851 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3853 if (mask_size > 1)
3855 switch (ins->handler_idx)
3857 case WINED3DSIH_EQ: op = "equal"; break;
3858 case WINED3DSIH_IEQ: op = "equal"; break;
3859 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3860 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3861 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3862 case WINED3DSIH_LT: op = "lessThan"; break;
3863 case WINED3DSIH_ILT: op = "lessThan"; break;
3864 case WINED3DSIH_ULT: op = "lessThan"; break;
3865 case WINED3DSIH_NE: op = "notEqual"; break;
3866 case WINED3DSIH_INE: op = "notEqual"; break;
3867 default:
3868 op = "<unhandled operator>";
3869 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3870 break;
3873 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3874 mask_size, op, src0_param.param_str, src1_param.param_str);
3876 else
3878 switch (ins->handler_idx)
3880 case WINED3DSIH_EQ: op = "=="; break;
3881 case WINED3DSIH_IEQ: op = "=="; break;
3882 case WINED3DSIH_GE: op = ">="; break;
3883 case WINED3DSIH_IGE: op = ">="; break;
3884 case WINED3DSIH_UGE: op = ">="; break;
3885 case WINED3DSIH_LT: op = "<"; break;
3886 case WINED3DSIH_ILT: op = "<"; break;
3887 case WINED3DSIH_ULT: op = "<"; break;
3888 case WINED3DSIH_NE: op = "!="; break;
3889 case WINED3DSIH_INE: op = "!="; break;
3890 default:
3891 op = "<unhandled operator>";
3892 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3893 break;
3896 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3897 src0_param.param_str, op, src1_param.param_str);
3901 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3903 struct glsl_src_param src_param;
3904 DWORD write_mask;
3905 const char *op;
3907 switch (ins->handler_idx)
3909 case WINED3DSIH_INEG: op = "-"; break;
3910 case WINED3DSIH_NOT: op = "~"; break;
3911 default:
3912 op = "<unhandled operator>";
3913 ERR("Unhandled opcode %s.\n",
3914 debug_d3dshaderinstructionhandler(ins->handler_idx));
3915 break;
3918 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3919 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3920 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3923 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3925 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3926 struct glsl_src_param src0_param;
3927 struct glsl_src_param src1_param;
3928 DWORD write_mask;
3930 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3931 * If not, we can emulate it. */
3932 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3933 FIXME("64-bit integer multiplies not implemented.\n");
3935 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3937 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3938 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3939 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3941 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3942 src0_param.param_str, src1_param.param_str);
3946 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3948 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3949 struct glsl_src_param src0_param, src1_param;
3950 DWORD write_mask;
3952 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3954 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3956 char dst_mask[6];
3958 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3959 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3960 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3961 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3962 dst_mask, src0_param.param_str, src1_param.param_str);
3964 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3965 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3966 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3967 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3969 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
3970 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3972 else
3974 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3975 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3976 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3977 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3980 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3982 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3983 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3984 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3985 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3989 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3990 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3992 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3993 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3994 const struct wined3d_gl_info *gl_info = priv->gl_info;
3995 struct glsl_src_param src0_param;
3996 DWORD write_mask;
3998 write_mask = shader_glsl_append_dst(buffer, ins);
3999 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4001 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
4002 * shader versions WINED3DSIO_MOVA is used for this. */
4003 if (ins->ctx->reg_maps->shader_version.major == 1
4004 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
4005 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
4007 /* This is a simple floor() */
4008 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4009 if (mask_size > 1) {
4010 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
4011 } else {
4012 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
4015 else if (ins->handler_idx == WINED3DSIH_MOVA)
4017 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4019 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
4021 if (mask_size > 1)
4022 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
4023 else
4024 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
4026 else
4028 if (mask_size > 1)
4029 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
4030 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
4031 else
4032 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
4033 src0_param.param_str, src0_param.param_str);
4036 else
4038 shader_addline(buffer, "%s);\n", src0_param.param_str);
4042 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
4043 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
4045 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4046 struct glsl_src_param src0_param;
4047 struct glsl_src_param src1_param;
4048 DWORD dst_write_mask, src_write_mask;
4049 unsigned int dst_size;
4051 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4052 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4054 /* dp4 works on vec4, dp3 on vec3, etc. */
4055 if (ins->handler_idx == WINED3DSIH_DP4)
4056 src_write_mask = WINED3DSP_WRITEMASK_ALL;
4057 else if (ins->handler_idx == WINED3DSIH_DP3)
4058 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4059 else
4060 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
4062 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
4063 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4065 if (dst_size > 1) {
4066 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4067 } else {
4068 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4072 /* Note that this instruction has some restrictions. The destination write mask
4073 * can't contain the w component, and the source swizzles have to be .xyzw */
4074 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4076 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4077 struct glsl_src_param src0_param;
4078 struct glsl_src_param src1_param;
4079 char dst_mask[6];
4081 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4082 shader_glsl_append_dst(ins->ctx->buffer, ins);
4083 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4084 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4085 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4088 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4090 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4092 if (!stream)
4093 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4094 else
4095 FIXME("Unhandled primitive stream %u.\n", stream);
4098 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4099 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4100 * GLSL uses the value as-is. */
4101 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4103 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4104 struct glsl_src_param src0_param;
4105 struct glsl_src_param src1_param;
4106 DWORD dst_write_mask;
4107 unsigned int dst_size;
4109 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4110 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4112 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4113 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4115 if (dst_size > 1)
4117 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4118 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4120 else
4122 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4123 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4127 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4128 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4130 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4131 struct glsl_src_param src_param;
4132 const char *instruction;
4133 DWORD write_mask;
4134 unsigned i;
4136 /* Determine the GLSL function to use based on the opcode */
4137 /* TODO: Possibly make this a table for faster lookups */
4138 switch (ins->handler_idx)
4140 case WINED3DSIH_ABS: instruction = "abs"; break;
4141 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4142 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4143 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4144 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4145 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4146 case WINED3DSIH_DSY: instruction = "dFdy"; break;
4147 case WINED3DSIH_DSY_COARSE: instruction = "dFdyCoarse"; break;
4148 case WINED3DSIH_DSY_FINE: instruction = "dFdyFine"; break;
4149 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4150 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4151 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4152 case WINED3DSIH_FRC: instruction = "fract"; break;
4153 case WINED3DSIH_IMAX: instruction = "max"; break;
4154 case WINED3DSIH_IMIN: instruction = "min"; break;
4155 case WINED3DSIH_MAX: instruction = "max"; break;
4156 case WINED3DSIH_MIN: instruction = "min"; break;
4157 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4158 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4159 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4160 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4161 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4162 case WINED3DSIH_UMAX: instruction = "max"; break;
4163 case WINED3DSIH_UMIN: instruction = "min"; break;
4164 default: instruction = "";
4165 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4166 break;
4169 write_mask = shader_glsl_append_dst(buffer, ins);
4171 /* In D3D bits are numbered from the most significant bit. */
4172 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4173 shader_addline(buffer, "31 - ");
4174 shader_addline(buffer, "%s(", instruction);
4176 if (ins->src_count)
4178 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4179 shader_addline(buffer, "%s", src_param.param_str);
4180 for (i = 1; i < ins->src_count; ++i)
4182 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4183 shader_addline(buffer, ", %s", src_param.param_str);
4187 shader_addline(buffer, "));\n");
4190 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4192 struct wined3d_shader_dst_param dst;
4193 struct glsl_src_param src;
4194 DWORD write_mask;
4195 const char *fmt;
4196 unsigned int i;
4198 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4199 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4201 dst = ins->dst[0];
4202 for (i = 0; i < 4; ++i)
4204 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4205 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4206 &dst, 0, dst.reg.data_type)))
4207 continue;
4209 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4210 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4214 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4216 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4217 struct wined3d_shader_dst_param dst;
4218 struct glsl_src_param src[4];
4219 const char *instruction;
4220 BOOL tmp_dst = FALSE;
4221 char mask_char[6];
4222 unsigned int i, j;
4223 DWORD write_mask;
4225 switch (ins->handler_idx)
4227 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4228 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4229 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4230 default:
4231 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4232 return;
4235 for (i = 0; i < ins->src_count; ++i)
4237 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4238 && ins->dst[0].reg.type == ins->src[i].reg.type)
4239 tmp_dst = TRUE;
4242 dst = ins->dst[0];
4243 for (i = 0; i < 4; ++i)
4245 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4246 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4247 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4248 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4249 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, 0, dst.reg.data_type)))
4250 continue;
4252 for (j = 0; j < ins->src_count; ++j)
4253 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4254 shader_addline(buffer, "%s(", instruction);
4255 for (j = 0; j < ins->src_count - 2; ++j)
4256 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4257 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4260 if (tmp_dst)
4262 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
4263 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4264 shader_addline(buffer, "tmp0%s);\n", mask_char);
4268 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4270 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4272 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4273 struct glsl_src_param src_param;
4274 unsigned int mask_size;
4275 DWORD write_mask;
4276 char dst_mask[6];
4278 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4279 mask_size = shader_glsl_get_write_mask_size(write_mask);
4280 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4282 if (mask_size > 3)
4283 shader_addline(buffer, "tmp0.x = dot(vec3(%s), vec3(%s));\n",
4284 src_param.param_str, src_param.param_str);
4285 else
4286 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4287 src_param.param_str, src_param.param_str);
4288 shader_glsl_append_dst(buffer, ins);
4290 shader_addline(buffer, "tmp0.x == 0.0 ? %s : (%s * inversesqrt(tmp0.x)));\n",
4291 src_param.param_str, src_param.param_str);
4294 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4296 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4297 ins->ctx->reg_maps->shader_version.minor);
4298 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4299 struct glsl_src_param src0_param;
4300 const char *prefix, *suffix;
4301 unsigned int dst_size;
4302 DWORD dst_write_mask;
4304 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4305 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4307 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4308 dst_write_mask = WINED3DSP_WRITEMASK_3;
4310 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4312 switch (ins->handler_idx)
4314 case WINED3DSIH_EXP:
4315 case WINED3DSIH_EXPP:
4316 prefix = "exp2(";
4317 suffix = ")";
4318 break;
4320 case WINED3DSIH_LOG:
4321 case WINED3DSIH_LOGP:
4322 prefix = "log2(abs(";
4323 suffix = "))";
4324 break;
4326 case WINED3DSIH_RCP:
4327 prefix = "1.0 / ";
4328 suffix = "";
4329 break;
4331 case WINED3DSIH_RSQ:
4332 prefix = "inversesqrt(abs(";
4333 suffix = "))";
4334 break;
4336 default:
4337 prefix = "";
4338 suffix = "";
4339 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4340 break;
4343 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4344 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4345 else
4346 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4349 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4350 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4351 * dst.x = 2^(floor(src))
4352 * dst.y = src - floor(src)
4353 * dst.z = 2^src (partial precision is allowed, but optional)
4354 * dst.w = 1.0;
4355 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4356 * dst = 2^src; (partial precision is allowed, but optional)
4358 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4360 if (ins->ctx->reg_maps->shader_version.major < 2)
4362 struct glsl_src_param src_param;
4363 char dst_mask[6];
4365 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4367 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4368 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4369 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4370 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4372 shader_glsl_append_dst(ins->ctx->buffer, ins);
4373 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4374 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4375 return;
4378 shader_glsl_scalar_op(ins);
4381 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4382 const char *vector_constructor, const char *scalar_constructor)
4384 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4385 struct glsl_src_param src_param;
4386 unsigned int mask_size;
4387 DWORD write_mask;
4389 write_mask = shader_glsl_append_dst(buffer, ins);
4390 mask_size = shader_glsl_get_write_mask_size(write_mask);
4391 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4393 if (mask_size > 1)
4394 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4395 else
4396 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4399 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4401 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4402 struct glsl_src_param src_param;
4403 unsigned int mask_size;
4404 DWORD write_mask;
4406 write_mask = shader_glsl_append_dst(buffer, ins);
4407 mask_size = shader_glsl_get_write_mask_size(write_mask);
4408 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4410 if (mask_size > 1)
4411 shader_addline(buffer, "mix(mix(ivec%u(max(%s, vec%u(-2147483648.0))), ivec%u(0x7fffffff), greaterThanEqual(%s, vec%u(2147483648.0))), ivec%u(0), isnan(%s)));\n",
4412 mask_size, src_param.param_str, mask_size, mask_size, src_param.param_str, mask_size, mask_size, src_param.param_str);
4413 else
4414 shader_addline(buffer, "mix(mix(int(max(%s, -2147483648.0)), 0x7fffffff, %s >= 2147483648.0), 0, isnan(%s)));\n",
4415 src_param.param_str, src_param.param_str, src_param.param_str);
4418 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4420 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4421 struct glsl_src_param src_param;
4422 unsigned int mask_size;
4423 DWORD write_mask;
4425 write_mask = shader_glsl_append_dst(buffer, ins);
4426 mask_size = shader_glsl_get_write_mask_size(write_mask);
4427 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4429 if (mask_size > 1)
4430 shader_addline(buffer, "mix(uvec%u(max(%s, vec%u(0.0))), uvec%u(0xffffffffu), greaterThanEqual(%s, vec%u(4294967296.0))));\n",
4431 mask_size, src_param.param_str, mask_size, mask_size, src_param.param_str, mask_size);
4432 else
4433 shader_addline(buffer, "mix(uint(max(%s, 0.0)), 0xffffffffu, %s >= 4294967296.0));\n",
4434 src_param.param_str, src_param.param_str);
4437 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4439 shader_glsl_cast(ins, "vec", "float");
4442 /** Process signed comparison opcodes in GLSL. */
4443 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4445 struct glsl_src_param src0_param;
4446 struct glsl_src_param src1_param;
4447 DWORD write_mask;
4448 unsigned int mask_size;
4450 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4451 mask_size = shader_glsl_get_write_mask_size(write_mask);
4452 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4453 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4455 if (mask_size > 1) {
4456 const char *compare;
4458 switch(ins->handler_idx)
4460 case WINED3DSIH_SLT: compare = "lessThan"; break;
4461 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4462 default: compare = "";
4463 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4466 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4467 src0_param.param_str, src1_param.param_str);
4468 } else {
4469 switch(ins->handler_idx)
4471 case WINED3DSIH_SLT:
4472 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4473 * to return 0.0 but step returns 1.0 because step is not < x
4474 * An alternative is a bvec compare padded with an unused second component.
4475 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4476 * issue. Playing with not() is not possible either because not() does not accept
4477 * a scalar.
4479 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4480 src0_param.param_str, src1_param.param_str);
4481 break;
4482 case WINED3DSIH_SGE:
4483 /* Here we can use the step() function and safe a conditional */
4484 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4485 break;
4486 default:
4487 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4493 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4495 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4496 struct wined3d_shader_dst_param dst[2];
4497 struct glsl_src_param src[3];
4498 unsigned int i, j, k;
4499 char mask_char[6];
4500 DWORD write_mask;
4501 BOOL tmp_dst[2];
4503 for (i = 0; i < ins->dst_count; ++i)
4505 tmp_dst[i] = FALSE;
4506 for (j = 0; j < ins->src_count; ++j)
4508 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4509 && ins->dst[i].reg.type == ins->src[j].reg.type)
4510 tmp_dst[i] = TRUE;
4514 dst[0] = ins->dst[0];
4515 dst[1] = ins->dst[1];
4516 for (i = 0; i < 4; ++i)
4518 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4520 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4521 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4522 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4523 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], j, dst[j].reg.data_type)))
4524 continue;
4526 for (k = 0; k < ARRAY_SIZE(src); ++k)
4527 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4529 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4530 src[0].param_str, src[1].param_str, src[2].param_str);
4534 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4536 if (tmp_dst[i])
4538 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4539 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], i, ins->dst[i].reg.data_type);
4540 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4545 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4547 const char *condition_prefix, *condition_suffix;
4548 struct wined3d_shader_dst_param dst;
4549 struct glsl_src_param src0_param;
4550 struct glsl_src_param src1_param;
4551 struct glsl_src_param src2_param;
4552 BOOL temp_destination = FALSE;
4553 DWORD cmp_channel = 0;
4554 unsigned int i, j;
4555 char mask_char[6];
4556 DWORD write_mask;
4558 switch (ins->handler_idx)
4560 case WINED3DSIH_CMP:
4561 condition_prefix = "";
4562 condition_suffix = " >= 0.0";
4563 break;
4565 case WINED3DSIH_CND:
4566 condition_prefix = "";
4567 condition_suffix = " > 0.5";
4568 break;
4570 case WINED3DSIH_MOVC:
4571 condition_prefix = "bool(";
4572 condition_suffix = ")";
4573 break;
4575 default:
4576 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4577 condition_prefix = "<unhandled prefix>";
4578 condition_suffix = "<unhandled suffix>";
4579 break;
4582 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4584 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4585 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4586 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4587 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4589 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4590 condition_prefix, src0_param.param_str, condition_suffix,
4591 src1_param.param_str, src2_param.param_str);
4592 return;
4595 dst = ins->dst[0];
4597 /* Splitting the instruction up in multiple lines imposes a problem:
4598 * The first lines may overwrite source parameters of the following lines.
4599 * Deal with that by using a temporary destination register if needed. */
4600 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4601 && ins->src[0].reg.type == dst.reg.type)
4602 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4603 && ins->src[1].reg.type == dst.reg.type)
4604 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4605 && ins->src[2].reg.type == dst.reg.type))
4606 temp_destination = TRUE;
4608 /* Cycle through all source0 channels. */
4609 for (i = 0; i < 4; ++i)
4611 write_mask = 0;
4612 /* Find the destination channels which use the current source0 channel. */
4613 for (j = 0; j < 4; ++j)
4615 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4617 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4618 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4621 dst.write_mask = ins->dst[0].write_mask & write_mask;
4623 if (temp_destination)
4625 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4626 continue;
4627 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4629 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type)))
4630 continue;
4632 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4633 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4634 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4636 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4637 condition_prefix, src0_param.param_str, condition_suffix,
4638 src1_param.param_str, src2_param.param_str);
4641 if (temp_destination)
4643 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4644 shader_glsl_append_dst(ins->ctx->buffer, ins);
4645 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4649 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4650 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4651 * the compare is done per component of src0. */
4652 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4654 struct glsl_src_param src0_param;
4655 struct glsl_src_param src1_param;
4656 struct glsl_src_param src2_param;
4657 DWORD write_mask;
4658 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4659 ins->ctx->reg_maps->shader_version.minor);
4661 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4663 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4664 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4665 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4666 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4668 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4669 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4670 else
4671 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4672 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4673 return;
4676 shader_glsl_conditional_move(ins);
4679 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4680 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4682 struct glsl_src_param src0_param;
4683 struct glsl_src_param src1_param;
4684 struct glsl_src_param src2_param;
4685 DWORD write_mask;
4687 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4688 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4689 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4690 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4691 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4692 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4695 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4696 Vertex shaders to GLSL codes */
4697 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4699 int i;
4700 int nComponents = 0;
4701 struct wined3d_shader_dst_param tmp_dst = {{0}};
4702 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4703 struct wined3d_shader_instruction tmp_ins;
4705 memset(&tmp_ins, 0, sizeof(tmp_ins));
4707 /* Set constants for the temporary argument */
4708 tmp_ins.ctx = ins->ctx;
4709 tmp_ins.dst_count = 1;
4710 tmp_ins.dst = &tmp_dst;
4711 tmp_ins.src_count = 2;
4712 tmp_ins.src = tmp_src;
4714 switch(ins->handler_idx)
4716 case WINED3DSIH_M4x4:
4717 nComponents = 4;
4718 tmp_ins.handler_idx = WINED3DSIH_DP4;
4719 break;
4720 case WINED3DSIH_M4x3:
4721 nComponents = 3;
4722 tmp_ins.handler_idx = WINED3DSIH_DP4;
4723 break;
4724 case WINED3DSIH_M3x4:
4725 nComponents = 4;
4726 tmp_ins.handler_idx = WINED3DSIH_DP3;
4727 break;
4728 case WINED3DSIH_M3x3:
4729 nComponents = 3;
4730 tmp_ins.handler_idx = WINED3DSIH_DP3;
4731 break;
4732 case WINED3DSIH_M3x2:
4733 nComponents = 2;
4734 tmp_ins.handler_idx = WINED3DSIH_DP3;
4735 break;
4736 default:
4737 break;
4740 tmp_dst = ins->dst[0];
4741 tmp_src[0] = ins->src[0];
4742 tmp_src[1] = ins->src[1];
4743 for (i = 0; i < nComponents; ++i)
4745 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4746 shader_glsl_dot(&tmp_ins);
4747 ++tmp_src[1].reg.idx[0].offset;
4752 The LRP instruction performs a component-wise linear interpolation
4753 between the second and third operands using the first operand as the
4754 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4755 This is equivalent to mix(src2, src1, src0);
4757 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4759 struct glsl_src_param src0_param;
4760 struct glsl_src_param src1_param;
4761 struct glsl_src_param src2_param;
4762 DWORD write_mask;
4764 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4766 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4767 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4768 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4770 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4771 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4774 /** Process the WINED3DSIO_LIT instruction in GLSL:
4775 * dst.x = dst.w = 1.0
4776 * dst.y = (src0.x > 0) ? src0.x
4777 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4778 * where src.w is clamped at +- 128
4780 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4782 struct glsl_src_param src0_param;
4783 struct glsl_src_param src1_param;
4784 struct glsl_src_param src3_param;
4785 char dst_mask[6];
4787 shader_glsl_append_dst(ins->ctx->buffer, ins);
4788 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4790 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4791 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4792 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4794 /* The sdk specifies the instruction like this
4795 * dst.x = 1.0;
4796 * if(src.x > 0.0) dst.y = src.x
4797 * else dst.y = 0.0.
4798 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4799 * else dst.z = 0.0;
4800 * dst.w = 1.0;
4801 * (where power = src.w clamped between -128 and 128)
4803 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4804 * dst.x = 1.0 ... No further explanation needed
4805 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4806 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4807 * dst.w = 1.0. ... Nothing fancy.
4809 * So we still have one conditional in there. So do this:
4810 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4812 * 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),
4813 * 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.
4814 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4816 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4817 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4818 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4820 shader_addline(ins->ctx->buffer,
4821 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4822 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4823 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4824 src0_param.param_str, src3_param.param_str, dst_mask);
4827 /** Process the WINED3DSIO_DST instruction in GLSL:
4828 * dst.x = 1.0
4829 * dst.y = src0.x * src0.y
4830 * dst.z = src0.z
4831 * dst.w = src1.w
4833 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4835 struct glsl_src_param src0y_param;
4836 struct glsl_src_param src0z_param;
4837 struct glsl_src_param src1y_param;
4838 struct glsl_src_param src1w_param;
4839 char dst_mask[6];
4841 shader_glsl_append_dst(ins->ctx->buffer, ins);
4842 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4844 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4845 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4846 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4847 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4849 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4850 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4853 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4854 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4855 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4857 * dst.x = cos(src0.?)
4858 * dst.y = sin(src0.?)
4859 * dst.z = dst.z
4860 * dst.w = dst.w
4862 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4864 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4865 struct glsl_src_param src0_param;
4866 DWORD write_mask;
4868 if (ins->ctx->reg_maps->shader_version.major < 4)
4870 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4872 write_mask = shader_glsl_append_dst(buffer, ins);
4873 switch (write_mask)
4875 case WINED3DSP_WRITEMASK_0:
4876 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4877 break;
4879 case WINED3DSP_WRITEMASK_1:
4880 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4881 break;
4883 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4884 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4885 src0_param.param_str, src0_param.param_str);
4886 break;
4888 default:
4889 ERR("Write mask should be .x, .y or .xy\n");
4890 break;
4893 return;
4896 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4899 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4901 char dst_mask[6];
4903 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4904 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4905 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4907 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4908 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4909 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4911 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4912 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4914 else
4916 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4917 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4918 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4921 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4923 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4924 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4925 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4929 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4930 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4931 * generate invalid code
4933 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4935 struct glsl_src_param src0_param;
4936 DWORD write_mask;
4938 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4939 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4941 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4944 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4945 * Start a for() loop where src1.y is the initial value of aL,
4946 * increment aL by src1.z for a total of src1.x iterations.
4947 * Need to use a temporary variable for this operation.
4949 /* FIXME: I don't think nested loops will work correctly this way. */
4950 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4952 struct wined3d_shader_parser_state *state = ins->ctx->state;
4953 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4954 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4955 const struct wined3d_shader *shader = ins->ctx->shader;
4956 const struct wined3d_shader_lconst *constant;
4957 struct wined3d_string_buffer *reg_name;
4958 const unsigned int *control_values = NULL;
4960 if (ins->ctx->reg_maps->shader_version.major < 4)
4962 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4963 * class hardware doesn't support real varying indexing, but Microsoft
4964 * designed this feature for Shader model 2.x+. If the loop control is
4965 * known at compile time, the GLSL compiler can unroll the loop, and
4966 * replace indirect addressing with direct addressing. */
4967 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4969 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4971 if (constant->idx == ins->src[1].reg.idx[0].offset)
4973 control_values = constant->value;
4974 break;
4979 if (control_values)
4981 struct wined3d_shader_loop_control loop_control;
4982 loop_control.count = control_values[0];
4983 loop_control.start = control_values[1];
4984 loop_control.step = (int)control_values[2];
4986 if (loop_control.step > 0)
4988 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4989 state->current_loop_depth, loop_control.start,
4990 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4991 state->current_loop_depth, loop_control.step);
4993 else if (loop_control.step < 0)
4995 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4996 state->current_loop_depth, loop_control.start,
4997 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4998 state->current_loop_depth, loop_control.step);
5000 else
5002 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
5003 state->current_loop_depth, loop_control.start, state->current_loop_depth,
5004 state->current_loop_depth, loop_control.count,
5005 state->current_loop_depth);
5008 else
5010 reg_name = string_buffer_get(priv->string_buffers);
5011 shader_glsl_get_register_name(&ins->src[1].reg, ins->src[1].reg.data_type, reg_name, NULL, ins->ctx);
5013 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
5014 state->current_loop_depth, state->current_loop_reg, reg_name->buffer,
5015 state->current_loop_depth, reg_name->buffer,
5016 state->current_loop_depth, state->current_loop_reg, reg_name->buffer);
5018 string_buffer_release(priv->string_buffers, reg_name);
5022 ++state->current_loop_reg;
5024 else
5026 shader_addline(buffer, "for (;;)\n{\n");
5029 ++state->current_loop_depth;
5032 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
5034 struct wined3d_shader_parser_state *state = ins->ctx->state;
5036 shader_addline(ins->ctx->buffer, "}\n");
5038 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
5040 --state->current_loop_depth;
5041 --state->current_loop_reg;
5044 if (ins->handler_idx == WINED3DSIH_ENDREP)
5046 --state->current_loop_depth;
5050 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
5052 struct wined3d_shader_parser_state *state = ins->ctx->state;
5053 const struct wined3d_shader *shader = ins->ctx->shader;
5054 const struct wined3d_shader_lconst *constant;
5055 struct glsl_src_param src0_param;
5056 const unsigned int *control_values = NULL;
5058 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
5059 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
5061 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
5063 if (constant->idx == ins->src[0].reg.idx[0].offset)
5065 control_values = constant->value;
5066 break;
5071 if (control_values)
5073 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
5074 state->current_loop_depth, state->current_loop_depth,
5075 control_values[0], state->current_loop_depth);
5077 else
5079 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5080 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5081 state->current_loop_depth, state->current_loop_depth,
5082 src0_param.param_str, state->current_loop_depth);
5085 ++state->current_loop_depth;
5088 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5090 struct glsl_src_param src0_param;
5092 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5093 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5096 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5098 struct glsl_src_param src0_param;
5100 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5101 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5104 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5106 shader_addline(ins->ctx->buffer, "default:\n");
5109 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
5111 struct glsl_src_param src_param;
5112 const char *condition;
5114 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5115 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5116 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
5119 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5121 shader_glsl_generate_condition(ins);
5122 shader_addline(ins->ctx->buffer, "{\n");
5125 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5127 struct glsl_src_param src0_param;
5128 struct glsl_src_param src1_param;
5130 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5131 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5133 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5134 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5137 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5139 shader_addline(ins->ctx->buffer, "} else {\n");
5142 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5144 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5145 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5146 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5148 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5149 if (!priv->gl_info->supported[ARB_CLIP_CONTROL])
5150 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5152 if (!stream)
5153 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5154 else
5155 FIXME("Unhandled primitive stream %u.\n", stream);
5158 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5160 shader_addline(ins->ctx->buffer, "break;\n");
5163 /* FIXME: According to MSDN the compare is done per component. */
5164 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5166 struct glsl_src_param src0_param;
5167 struct glsl_src_param src1_param;
5169 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5170 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5172 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5173 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5176 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5178 const char *op;
5180 switch (ins->handler_idx)
5182 case WINED3DSIH_BREAKP:
5183 op = "break;";
5184 break;
5185 case WINED3DSIH_CONTINUEP:
5186 op = "continue;";
5187 break;
5188 case WINED3DSIH_RETP:
5189 op = "return;";
5190 break;
5191 default:
5192 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5193 return;
5196 shader_glsl_generate_condition(ins);
5197 if (ins->handler_idx == WINED3DSIH_RETP)
5199 shader_addline(ins->ctx->buffer, "{\n");
5200 shader_glsl_generate_shader_epilogue(ins->ctx);
5202 shader_addline(ins->ctx->buffer, " %s\n", op);
5203 if (ins->handler_idx == WINED3DSIH_RETP)
5204 shader_addline(ins->ctx->buffer, "}\n");
5207 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5209 shader_addline(ins->ctx->buffer, "continue;\n");
5212 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5214 shader_addline(ins->ctx->buffer, "}\n");
5215 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5217 /* Subroutines appear at the end of the shader. */
5218 ins->ctx->state->in_subroutine = TRUE;
5221 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5223 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5226 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5228 struct glsl_src_param src1_param;
5230 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5231 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5232 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5235 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5237 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5239 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5241 shader_glsl_generate_shader_epilogue(ins->ctx);
5242 shader_addline(ins->ctx->buffer, "return;\n");
5246 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5248 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5249 ins->ctx->reg_maps->shader_version.minor);
5250 struct glsl_sample_function sample_function;
5251 DWORD sample_flags = 0;
5252 unsigned int resource_idx;
5253 DWORD mask = 0, swizzle;
5254 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5255 enum wined3d_shader_resource_type resource_type;
5257 /* 1.0-1.4: Use destination register as sampler source.
5258 * 2.0+: Use provided sampler source. */
5259 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5260 resource_idx = ins->dst[0].reg.idx[0].offset;
5261 else
5262 resource_idx = ins->src[1].reg.idx[0].offset;
5264 resource_type = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
5265 ? pixelshader_get_resource_type(ins->ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
5266 : ins->ctx->reg_maps->resource_info[resource_idx].type;
5268 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5270 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5271 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5273 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5274 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5276 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5277 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5279 case WINED3D_TTFF_COUNT1:
5280 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5281 break;
5282 case WINED3D_TTFF_COUNT2:
5283 mask = WINED3DSP_WRITEMASK_1;
5284 break;
5285 case WINED3D_TTFF_COUNT3:
5286 mask = WINED3DSP_WRITEMASK_2;
5287 break;
5288 case WINED3D_TTFF_COUNT4:
5289 case WINED3D_TTFF_DISABLE:
5290 mask = WINED3DSP_WRITEMASK_3;
5291 break;
5295 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5297 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5299 if (src_mod == WINED3DSPSM_DZ) {
5300 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5301 mask = WINED3DSP_WRITEMASK_2;
5302 } else if (src_mod == WINED3DSPSM_DW) {
5303 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5304 mask = WINED3DSP_WRITEMASK_3;
5307 else
5309 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5310 && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5312 /* ps 2.0 texldp instruction always divides by the fourth component. */
5313 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5314 mask = WINED3DSP_WRITEMASK_3;
5318 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5319 mask |= sample_function.coord_mask;
5320 sample_function.coord_mask = mask;
5322 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5323 else swizzle = ins->src[1].swizzle;
5325 /* 1.0-1.3: Use destination register as coordinate source.
5326 1.4+: Use provided coordinate source register. */
5327 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5329 char coord_mask[6];
5330 shader_glsl_write_mask_to_str(mask, coord_mask);
5331 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5332 "T%u%s", resource_idx, coord_mask);
5334 else
5336 struct glsl_src_param coord_param;
5337 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5338 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5340 struct glsl_src_param bias;
5341 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5342 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5343 NULL, "%s", coord_param.param_str);
5344 } else {
5345 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5346 "%s", coord_param.param_str);
5349 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5352 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5354 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5355 const struct wined3d_gl_info *gl_info = priv->gl_info;
5356 struct glsl_src_param coord_param, dx_param, dy_param;
5357 struct glsl_sample_function sample_function;
5358 DWORD sampler_idx;
5359 DWORD swizzle = ins->src[1].swizzle;
5361 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5363 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5364 shader_glsl_tex(ins);
5365 return;
5368 sampler_idx = ins->src[1].reg.idx[0].offset;
5370 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5371 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5372 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5373 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5375 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5376 NULL, NULL, "%s", coord_param.param_str);
5377 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5380 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5382 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5383 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5384 const struct wined3d_gl_info *gl_info = priv->gl_info;
5385 struct glsl_src_param coord_param, lod_param;
5386 struct glsl_sample_function sample_function;
5387 DWORD swizzle = ins->src[1].swizzle;
5388 DWORD sampler_idx;
5390 sampler_idx = ins->src[1].reg.idx[0].offset;
5392 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5393 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5395 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5397 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5398 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5400 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5401 * However, the NVIDIA drivers allow them in fragment shaders as well,
5402 * even without the appropriate extension. */
5403 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5405 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5406 "%s", coord_param.param_str);
5407 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5410 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5411 unsigned int resource_idx, unsigned int sampler_idx)
5413 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5414 unsigned int i;
5416 for (i = 0; i < sampler_map->count; ++i)
5418 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5419 return entries[i].bind_idx;
5422 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5424 return ~0u;
5427 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5429 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5430 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5431 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5432 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5433 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5434 struct glsl_src_param structure_idx, offset, data, data2;
5435 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5436 enum wined3d_shader_resource_type resource_type;
5437 struct wined3d_string_buffer *address;
5438 enum wined3d_data_type data_type;
5439 unsigned int resource_idx, stride;
5440 const char *op, *resource;
5441 DWORD coord_mask;
5442 BOOL is_tgsm;
5444 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5445 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5446 if (is_tgsm)
5448 if (resource_idx >= reg_maps->tgsm_count)
5450 ERR("Invalid TGSM index %u.\n", resource_idx);
5451 return;
5453 resource = "g";
5454 data_type = WINED3D_DATA_UINT;
5455 coord_mask = 1;
5456 stride = reg_maps->tgsm[resource_idx].stride;
5458 else
5460 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5462 ERR("Invalid UAV index %u.\n", resource_idx);
5463 return;
5465 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5466 if (resource_type >= ARRAY_SIZE(resource_type_info))
5468 ERR("Unexpected resource type %#x.\n", resource_type);
5469 return;
5471 resource = "image";
5472 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5473 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5474 stride = reg_maps->uav_resource_info[resource_idx].stride;
5477 switch (ins->handler_idx)
5479 case WINED3DSIH_ATOMIC_AND:
5480 case WINED3DSIH_IMM_ATOMIC_AND:
5481 if (is_tgsm)
5482 op = "atomicAnd";
5483 else
5484 op = "imageAtomicAnd";
5485 break;
5486 case WINED3DSIH_ATOMIC_CMP_STORE:
5487 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5488 if (is_tgsm)
5489 op = "atomicCompSwap";
5490 else
5491 op = "imageAtomicCompSwap";
5492 break;
5493 case WINED3DSIH_ATOMIC_IADD:
5494 case WINED3DSIH_IMM_ATOMIC_IADD:
5495 if (is_tgsm)
5496 op = "atomicAdd";
5497 else
5498 op = "imageAtomicAdd";
5499 break;
5500 case WINED3DSIH_ATOMIC_IMAX:
5501 case WINED3DSIH_IMM_ATOMIC_IMAX:
5502 if (is_tgsm)
5503 op = "atomicMax";
5504 else
5505 op = "imageAtomicMax";
5506 if (data_type != WINED3D_DATA_INT)
5508 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5509 return;
5511 break;
5512 case WINED3DSIH_ATOMIC_IMIN:
5513 case WINED3DSIH_IMM_ATOMIC_IMIN:
5514 if (is_tgsm)
5515 op = "atomicMin";
5516 else
5517 op = "imageAtomicMin";
5518 if (data_type != WINED3D_DATA_INT)
5520 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5521 return;
5523 break;
5524 case WINED3DSIH_ATOMIC_OR:
5525 case WINED3DSIH_IMM_ATOMIC_OR:
5526 if (is_tgsm)
5527 op = "atomicOr";
5528 else
5529 op = "imageAtomicOr";
5530 break;
5531 case WINED3DSIH_ATOMIC_UMAX:
5532 case WINED3DSIH_IMM_ATOMIC_UMAX:
5533 if (is_tgsm)
5534 op = "atomicMax";
5535 else
5536 op = "imageAtomicMax";
5537 if (data_type != WINED3D_DATA_UINT)
5539 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5540 return;
5542 break;
5543 case WINED3DSIH_ATOMIC_UMIN:
5544 case WINED3DSIH_IMM_ATOMIC_UMIN:
5545 if (is_tgsm)
5546 op = "atomicMin";
5547 else
5548 op = "imageAtomicMin";
5549 if (data_type != WINED3D_DATA_UINT)
5551 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5552 return;
5554 break;
5555 case WINED3DSIH_ATOMIC_XOR:
5556 case WINED3DSIH_IMM_ATOMIC_XOR:
5557 if (is_tgsm)
5558 op = "atomicXor";
5559 else
5560 op = "imageAtomicXor";
5561 break;
5562 case WINED3DSIH_IMM_ATOMIC_EXCH:
5563 if (is_tgsm)
5564 op = "atomicExchange";
5565 else
5566 op = "imageAtomicExchange";
5567 break;
5568 default:
5569 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5570 return;
5573 address = string_buffer_get(priv->string_buffers);
5574 if (stride)
5576 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5577 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5578 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5580 else
5582 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5583 string_buffer_sprintf(address, "%s", offset.param_str);
5584 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5585 shader_addline(address, "/ 4");
5588 if (is_imm_instruction)
5589 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5591 if (is_tgsm)
5592 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5593 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5594 else
5595 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5596 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5598 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5599 shader_addline(buffer, "%s", data.param_str);
5600 if (ins->src_count >= 3)
5602 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5603 shader_addline(buffer, ", %s", data2.param_str);
5606 if (is_imm_instruction)
5607 shader_addline(buffer, ")");
5608 shader_addline(buffer, ");\n");
5610 string_buffer_release(priv->string_buffers, address);
5613 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5615 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5616 const char *op;
5618 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5619 op = "atomicCounterIncrement";
5620 else
5621 op = "atomicCounterDecrement";
5623 shader_glsl_append_dst(ins->ctx->buffer, ins);
5624 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5627 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5629 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5630 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5631 enum wined3d_shader_resource_type resource_type;
5632 struct glsl_src_param image_coord_param;
5633 enum wined3d_data_type data_type;
5634 DWORD coord_mask, write_mask;
5635 unsigned int uav_idx;
5636 char dst_swizzle[6];
5638 uav_idx = ins->src[1].reg.idx[0].offset;
5639 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5641 ERR("Invalid UAV index %u.\n", uav_idx);
5642 return;
5644 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5645 if (resource_type >= ARRAY_SIZE(resource_type_info))
5647 ERR("Unexpected resource type %#x.\n", resource_type);
5648 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5650 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5651 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5653 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5654 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5656 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5657 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5658 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5661 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5663 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5664 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5665 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5666 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5667 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5668 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5669 struct glsl_src_param structure_idx, offset;
5670 struct wined3d_string_buffer *address;
5671 struct wined3d_shader_dst_param dst;
5672 const char *function, *resource;
5674 resource_idx = src->reg.idx[0].offset;
5675 if (src->reg.type == WINED3DSPR_RESOURCE)
5677 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5679 ERR("Invalid resource index %u.\n", resource_idx);
5680 return;
5682 stride = reg_maps->resource_info[resource_idx].stride;
5683 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5684 function = "texelFetch";
5685 resource = "sampler";
5687 else if (src->reg.type == WINED3DSPR_UAV)
5689 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5691 ERR("Invalid UAV index %u.\n", resource_idx);
5692 return;
5694 stride = reg_maps->uav_resource_info[resource_idx].stride;
5695 bind_idx = resource_idx;
5696 function = "imageLoad";
5697 resource = "image";
5699 else
5701 if (resource_idx >= reg_maps->tgsm_count)
5703 ERR("Invalid TGSM index %u.\n", resource_idx);
5704 return;
5706 stride = reg_maps->tgsm[resource_idx].stride;
5707 bind_idx = resource_idx;
5708 function = NULL;
5709 resource = "g";
5712 address = string_buffer_get(priv->string_buffers);
5713 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5715 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5716 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5718 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5719 shader_addline(address, "%s / 4", offset.param_str);
5721 dst = ins->dst[0];
5722 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5724 /* The instruction is split into multiple lines. The first lines may
5725 * overwrite source parameters of the following lines. */
5726 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5727 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5730 for (i = 0; i < 4; ++i)
5732 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5733 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type))
5734 continue;
5736 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5737 if (function)
5738 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5739 function, prefix, resource, bind_idx, address->buffer, swizzle);
5740 else
5741 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5742 prefix, resource, bind_idx, address->buffer, swizzle);
5745 string_buffer_release(priv->string_buffers, address);
5748 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5750 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5751 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5752 struct glsl_src_param image_coord_param, image_data_param;
5753 enum wined3d_shader_resource_type resource_type;
5754 enum wined3d_data_type data_type;
5755 unsigned int uav_idx;
5756 DWORD coord_mask;
5758 uav_idx = ins->dst[0].reg.idx[0].offset;
5759 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5761 ERR("Invalid UAV index %u.\n", uav_idx);
5762 return;
5764 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5765 if (resource_type >= ARRAY_SIZE(resource_type_info))
5767 ERR("Unexpected resource type %#x.\n", resource_type);
5768 return;
5770 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5771 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5773 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5774 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5775 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5776 shader_glsl_get_prefix(version->type), uav_idx,
5777 image_coord_param.param_str, image_data_param.param_str);
5780 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5782 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5783 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5784 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5785 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5786 struct glsl_src_param structure_idx, offset, data;
5787 unsigned int i, resource_idx, stride, src_idx = 0;
5788 struct wined3d_string_buffer *address;
5789 DWORD write_mask;
5790 BOOL is_tgsm;
5792 resource_idx = ins->dst[0].reg.idx[0].offset;
5793 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5794 if (is_tgsm)
5796 if (resource_idx >= reg_maps->tgsm_count)
5798 ERR("Invalid TGSM index %u.\n", resource_idx);
5799 return;
5801 stride = reg_maps->tgsm[resource_idx].stride;
5803 else
5805 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5807 ERR("Invalid UAV index %u.\n", resource_idx);
5808 return;
5810 stride = reg_maps->uav_resource_info[resource_idx].stride;
5813 address = string_buffer_get(priv->string_buffers);
5814 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5816 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5817 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5819 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5820 shader_addline(address, "%s / 4", offset.param_str);
5822 for (i = 0; i < 4; ++i)
5824 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5825 continue;
5827 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5829 if (is_tgsm)
5830 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5831 prefix, resource_idx, address->buffer, i, data.param_str);
5832 else
5833 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5834 prefix, resource_idx, address->buffer, i, data.param_str);
5837 string_buffer_release(priv->string_buffers, address);
5840 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5842 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5843 unsigned int sync_flags = ins->flags;
5845 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5847 shader_addline(buffer, "barrier();\n");
5848 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5851 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5853 shader_addline(buffer, "memoryBarrierShared();\n");
5854 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5857 if (sync_flags & WINED3DSSF_GLOBAL_UAV)
5859 shader_addline(buffer, "memoryBarrier();\n");
5860 sync_flags &= ~WINED3DSSF_GLOBAL_UAV;
5863 if (sync_flags)
5864 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5867 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5868 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5870 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5871 unsigned int idx = reg->idx[0].offset;
5873 if (reg->type == WINED3DSPR_RESOURCE)
5875 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5877 ERR("Invalid resource index %u.\n", idx);
5878 return NULL;
5880 return &reg_maps->resource_info[idx];
5883 if (reg->type == WINED3DSPR_UAV)
5885 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5887 ERR("Invalid UAV index %u.\n", idx);
5888 return NULL;
5890 return &reg_maps->uav_resource_info[idx];
5893 FIXME("Unhandled register type %#x.\n", reg->type);
5894 return NULL;
5897 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5899 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5900 const struct wined3d_shader_resource_info *resource_info;
5901 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5902 unsigned int resource_idx;
5903 char dst_swizzle[6];
5904 DWORD write_mask;
5906 write_mask = shader_glsl_append_dst(buffer, ins);
5907 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5909 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5910 return;
5911 resource_idx = ins->src[0].reg.idx[0].offset;
5913 shader_addline(buffer, "ivec2(");
5914 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5916 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5917 resource_idx, WINED3D_SAMPLER_DEFAULT);
5918 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5920 else
5922 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5924 if (resource_info->stride)
5925 shader_addline(buffer, " / %u", resource_info->stride);
5926 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5927 shader_addline(buffer, " * 4");
5928 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5931 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5933 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5934 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5937 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5939 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5942 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5944 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5945 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5946 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5947 const struct wined3d_gl_info *gl_info = priv->gl_info;
5948 enum wined3d_shader_resource_type resource_type;
5949 enum wined3d_shader_register_type reg_type;
5950 unsigned int resource_idx, bind_idx, i;
5951 enum wined3d_data_type dst_data_type;
5952 struct glsl_src_param lod_param;
5953 BOOL supports_mipmaps;
5954 char dst_swizzle[6];
5955 DWORD write_mask;
5957 dst_data_type = ins->dst[0].reg.data_type;
5958 if (ins->flags == WINED3DSI_RESINFO_UINT)
5959 dst_data_type = WINED3D_DATA_UINT;
5960 else if (ins->flags)
5961 FIXME("Unhandled flags %#x.\n", ins->flags);
5963 reg_type = ins->src[1].reg.type;
5964 resource_idx = ins->src[1].reg.idx[0].offset;
5965 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5966 if (reg_type == WINED3DSPR_RESOURCE)
5968 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5969 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5970 resource_idx, WINED3D_SAMPLER_DEFAULT);
5972 else
5974 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5975 bind_idx = resource_idx;
5978 if (resource_type >= ARRAY_SIZE(resource_type_info))
5980 ERR("Unexpected resource type %#x.\n", resource_type);
5981 return;
5984 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, dst_data_type);
5985 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5987 if (dst_data_type == WINED3D_DATA_UINT)
5988 shader_addline(buffer, "uvec4(");
5989 else
5990 shader_addline(buffer, "vec4(");
5992 if (reg_type == WINED3DSPR_RESOURCE)
5994 shader_addline(buffer, "textureSize(%s_sampler%u",
5995 shader_glsl_get_prefix(version->type), bind_idx);
5997 else
5999 shader_addline(buffer, "imageSize(%s_image%u",
6000 shader_glsl_get_prefix(version->type), bind_idx);
6003 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
6004 if (supports_mipmaps)
6005 shader_addline(buffer, ", %s", lod_param.param_str);
6006 shader_addline(buffer, "), ");
6008 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
6009 shader_addline(buffer, "0, ");
6011 if (supports_mipmaps)
6013 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6015 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
6016 shader_glsl_get_prefix(version->type), bind_idx);
6018 else
6020 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
6021 shader_addline(buffer, "1");
6024 else
6026 shader_addline(buffer, "1");
6029 shader_addline(buffer, ")%s);\n", dst_swizzle);
6032 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
6034 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6035 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6036 const struct wined3d_gl_info *gl_info = priv->gl_info;
6037 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6038 const struct wined3d_shader_dst_param *dst = ins->dst;
6039 const struct wined3d_shader_src_param *src = ins->src;
6040 enum wined3d_shader_resource_type resource_type;
6041 enum wined3d_data_type dst_data_type;
6042 unsigned int resource_idx, bind_idx;
6043 char dst_swizzle[6];
6044 DWORD write_mask;
6046 dst_data_type = dst->reg.data_type;
6047 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
6048 dst_data_type = WINED3D_DATA_UINT;
6049 else if (ins->flags)
6050 FIXME("Unhandled flags %#x.\n", ins->flags);
6052 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, 0, dst_data_type);
6053 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
6055 if (dst_data_type == WINED3D_DATA_UINT)
6056 shader_addline(buffer, "uvec4(");
6057 else
6058 shader_addline(buffer, "vec4(");
6060 if (src->reg.type == WINED3DSPR_RASTERIZER)
6062 if (gl_info->supported[ARB_SAMPLE_SHADING])
6064 shader_addline(buffer, "gl_NumSamples");
6066 else
6068 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
6069 shader_addline(buffer, "1");
6072 else
6074 resource_idx = src->reg.idx[0].offset;
6075 resource_type = reg_maps->resource_info[resource_idx].type;
6076 if (resource_type >= ARRAY_SIZE(resource_type_info))
6078 ERR("Unexpected resource type %#x.\n", resource_type);
6079 return;
6081 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
6083 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
6085 shader_addline(buffer, "textureSamples(%s_sampler%u)",
6086 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
6088 else
6090 FIXME("textureSamples() is not supported.\n");
6091 shader_addline(buffer, "1");
6095 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
6098 static void shader_glsl_interpolate(const struct wined3d_shader_instruction *ins)
6100 const struct wined3d_shader_src_param *input = &ins->src[0];
6101 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6102 struct glsl_src_param sample_param;
6103 char dst_swizzle[6];
6104 DWORD write_mask;
6106 write_mask = shader_glsl_append_dst(buffer, ins);
6107 shader_glsl_get_swizzle(input, FALSE, write_mask, dst_swizzle);
6109 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &sample_param);
6110 shader_addline(buffer, "interpolateAtSample(shader_in.reg%u, %s)%s);\n",
6111 input->reg.idx[0].offset, sample_param.param_str, dst_swizzle);
6114 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6116 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6117 struct glsl_src_param coord_param, lod_param, sample_param;
6118 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6119 struct glsl_sample_function sample_function;
6120 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6121 BOOL has_lod_param;
6123 if (wined3d_shader_instruction_has_texel_offset(ins))
6124 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6126 resource_idx = ins->src[1].reg.idx[0].offset;
6127 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6129 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6131 ERR("Invalid resource index %u.\n", resource_idx);
6132 return;
6134 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6136 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6137 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6138 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6139 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6140 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6142 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6143 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6144 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6146 else
6148 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6149 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6150 "%s", coord_param.param_str);
6152 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6155 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6157 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6158 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6159 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6160 struct glsl_sample_function sample_function;
6161 DWORD flags = 0;
6163 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6164 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6165 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6166 flags |= WINED3D_GLSL_SAMPLE_LOD;
6167 if (wined3d_shader_instruction_has_texel_offset(ins))
6168 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6170 resource_idx = ins->src[1].reg.idx[0].offset;
6171 sampler_idx = ins->src[2].reg.idx[0].offset;
6173 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6174 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6176 switch (ins->handler_idx)
6178 case WINED3DSIH_SAMPLE:
6179 break;
6180 case WINED3DSIH_SAMPLE_B:
6181 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6182 lod_param_str = lod_param.param_str;
6183 break;
6184 case WINED3DSIH_SAMPLE_GRAD:
6185 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6186 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6187 dx_param_str = dx_param.param_str;
6188 dy_param_str = dy_param.param_str;
6189 break;
6190 case WINED3DSIH_SAMPLE_LOD:
6191 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6192 lod_param_str = lod_param.param_str;
6193 break;
6194 default:
6195 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6196 break;
6199 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6200 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6201 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6202 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6205 /* Unless EXT_texture_shadow_lod is available, GLSL doesn't provide a function
6206 * to sample from level zero with depth comparison for array textures and cube
6207 * textures. We use textureGrad*() to implement sample_c_lz in that case. */
6208 static void shader_glsl_gen_sample_c_lz_emulation(const struct wined3d_shader_instruction *ins,
6209 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6210 unsigned int coord_size, const char *coord_param, const char *ref_param)
6212 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6213 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6214 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6215 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6216 char dst_swizzle[6];
6218 WARN("Emitting textureGrad() for sample_c_lz.\n");
6220 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6221 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, sample_function->data_type);
6222 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6223 sample_function->offset_size ? "Offset" : "",
6224 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6225 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6226 if (sample_function->offset_size)
6228 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6229 shader_addline(buffer, ", ");
6230 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6232 shader_addline(buffer, "))%s);\n", dst_swizzle);
6235 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6237 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6238 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6239 const struct wined3d_shader_resource_info *resource_info;
6240 const struct wined3d_gl_info *gl_info = priv->gl_info;
6241 struct glsl_src_param coord_param, compare_param;
6242 struct glsl_sample_function sample_function;
6243 const char *lod_param = NULL;
6244 unsigned int coord_size;
6245 DWORD flags = 0;
6247 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6249 lod_param = "0";
6250 flags |= WINED3D_GLSL_SAMPLE_LOD;
6253 if (wined3d_shader_instruction_has_texel_offset(ins))
6254 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6256 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6257 return;
6258 resource_idx = ins->src[1].reg.idx[0].offset;
6259 sampler_idx = ins->src[2].reg.idx[0].offset;
6261 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6262 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6263 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6264 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6265 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6266 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6267 && !gl_info->supported[EXT_TEXTURE_SHADOW_LOD]
6268 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6269 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6271 shader_glsl_gen_sample_c_lz_emulation(ins, sampler_bind_idx, &sample_function,
6272 coord_size, coord_param.param_str, compare_param.param_str);
6274 else
6276 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6277 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6278 coord_size, coord_param.param_str, compare_param.param_str);
6280 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6283 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6285 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6286 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6287 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6288 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6289 struct glsl_src_param coord_param, compare_param, offset_param;
6290 const struct wined3d_gl_info *gl_info = priv->gl_info;
6291 const struct wined3d_shader_resource_info *resource_info;
6292 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6293 unsigned int coord_size, offset_size;
6294 char dst_swizzle[6];
6295 BOOL has_offset;
6297 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6299 FIXME("OpenGL implementation does not support textureGather.\n");
6300 return;
6303 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6304 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6305 || wined3d_shader_instruction_has_texel_offset(ins);
6307 resource_param_idx =
6308 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6309 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6310 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6311 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6312 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6314 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6315 return;
6317 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6319 ERR("Unexpected resource type %#x.\n", resource_info->type);
6320 return;
6322 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6324 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6325 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, resource_info->data_type);
6327 shader_glsl_add_src_param(ins, &ins->src[0], wined3d_mask_from_size(coord_size), &coord_param);
6329 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6330 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6331 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6333 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6334 shader_addline(buffer, ", %s", compare_param.param_str);
6336 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6338 shader_glsl_add_src_param(ins, &ins->src[1], wined3d_mask_from_size(offset_size), &offset_param);
6339 shader_addline(buffer, ", %s", offset_param.param_str);
6341 else if (has_offset)
6343 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6344 shader_addline(buffer, ", ");
6345 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6347 if (component_idx)
6348 shader_addline(buffer, ", %u", component_idx);
6350 shader_addline(buffer, ")%s);\n", dst_swizzle);
6353 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6355 /* FIXME: Make this work for more than just 2D textures */
6356 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6357 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6359 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6361 char dst_mask[6];
6363 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6364 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6365 ins->dst[0].reg.idx[0].offset, dst_mask);
6367 else
6369 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6370 unsigned int reg = ins->src[0].reg.idx[0].offset;
6371 char dst_swizzle[6];
6373 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6375 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6377 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6378 struct glsl_src_param div_param;
6379 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6381 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6383 if (mask_size > 1)
6384 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6385 else
6386 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6388 else
6390 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6395 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6396 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6397 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6398 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6400 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6401 unsigned int sampler_idx = ins->dst[0].reg.idx[0].offset;
6402 struct glsl_sample_function sample_function;
6403 struct glsl_src_param src0_param;
6404 UINT mask_size;
6406 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6408 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6409 * scalar, and projected sampling would require 4.
6411 * It is a dependent read - not valid with conditional NP2 textures
6413 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6414 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6416 switch(mask_size)
6418 case 1:
6419 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6420 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6421 break;
6423 case 2:
6424 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6425 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6426 break;
6428 case 3:
6429 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6430 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6431 break;
6433 default:
6434 FIXME("Unexpected mask size %u\n", mask_size);
6435 break;
6437 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6440 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6441 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6442 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6444 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6445 unsigned int dstreg = ins->dst[0].reg.idx[0].offset;
6446 struct glsl_src_param src0_param;
6447 DWORD dst_mask;
6448 unsigned int mask_size;
6450 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6451 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6452 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6454 if (mask_size > 1) {
6455 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6456 } else {
6457 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6461 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6462 * Calculate the depth as dst.x / dst.y */
6463 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6465 struct glsl_dst_param dst_param;
6467 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6469 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6470 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6471 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6472 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6473 * >= 1.0 or < 0.0
6475 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6476 dst_param.reg_name, dst_param.reg_name);
6479 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6480 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6481 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6482 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6484 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6486 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6487 unsigned int dstreg = ins->dst[0].reg.idx[0].offset;
6488 struct glsl_src_param src0_param;
6490 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6492 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6493 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6496 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6497 * Calculate the 1st of a 2-row matrix multiplication. */
6498 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6500 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6501 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6502 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6503 struct glsl_src_param src0_param;
6505 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6506 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6509 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6510 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6511 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6513 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6514 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6515 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6516 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6517 struct glsl_src_param src0_param;
6519 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6520 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6521 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6524 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6526 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6527 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6528 struct glsl_sample_function sample_function;
6529 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6530 struct glsl_src_param src0_param;
6532 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6533 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6535 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6537 /* Sample the texture using the calculated coordinates */
6538 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6539 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6542 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6543 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6544 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6546 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6547 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6548 struct glsl_sample_function sample_function;
6549 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6550 struct glsl_src_param src0_param;
6552 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6553 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6555 /* Dependent read, not valid with conditional NP2 */
6556 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6558 /* Sample the texture using the calculated coordinates */
6559 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6560 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6562 tex_mx->current_row = 0;
6565 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6566 * Perform the 3rd row of a 3x3 matrix multiply */
6567 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6569 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6570 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6571 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6572 struct glsl_src_param src0_param;
6573 char dst_mask[6];
6575 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6577 shader_glsl_append_dst(ins->ctx->buffer, ins);
6578 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6579 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6581 tex_mx->current_row = 0;
6584 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6585 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6586 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6588 struct glsl_src_param src0_param;
6589 struct glsl_src_param src1_param;
6590 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6591 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6592 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6593 struct glsl_sample_function sample_function;
6594 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6595 char coord_mask[6];
6597 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6598 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6600 /* Perform the last matrix multiply operation */
6601 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6602 /* Reflection calculation */
6603 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6605 /* Dependent read, not valid with conditional NP2 */
6606 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6607 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6609 /* Sample the texture */
6610 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6611 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6612 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6614 tex_mx->current_row = 0;
6617 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6618 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6619 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6621 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6622 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6623 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6624 struct glsl_sample_function sample_function;
6625 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6626 struct glsl_src_param src0_param;
6627 char coord_mask[6];
6629 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6631 /* Perform the last matrix multiply operation */
6632 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6634 /* Construct the eye-ray vector from w coordinates */
6635 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6636 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6637 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6639 /* Dependent read, not valid with conditional NP2 */
6640 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6641 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6643 /* Sample the texture using the calculated coordinates */
6644 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6645 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6646 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6648 tex_mx->current_row = 0;
6651 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6652 * Apply a fake bump map transform.
6653 * texbem is pshader <= 1.3 only, this saves a few version checks
6655 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6657 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6658 struct glsl_sample_function sample_function;
6659 struct glsl_src_param coord_param;
6660 unsigned int sampler_idx;
6661 DWORD mask;
6662 DWORD flags;
6663 char coord_mask[6];
6665 sampler_idx = ins->dst[0].reg.idx[0].offset;
6666 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6667 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6669 /* Dependent read, not valid with conditional NP2 */
6670 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6671 mask = sample_function.coord_mask;
6673 shader_glsl_write_mask_to_str(mask, coord_mask);
6675 /* With projected textures, texbem only divides the static texture coord,
6676 * not the displacement, so we can't let GL handle this. */
6677 if (flags & WINED3D_PSARGS_PROJECTED)
6679 DWORD div_mask=0;
6680 char coord_div_mask[3];
6681 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6683 case WINED3D_TTFF_COUNT1:
6684 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6685 break;
6686 case WINED3D_TTFF_COUNT2:
6687 div_mask = WINED3DSP_WRITEMASK_1;
6688 break;
6689 case WINED3D_TTFF_COUNT3:
6690 div_mask = WINED3DSP_WRITEMASK_2;
6691 break;
6692 case WINED3D_TTFF_COUNT4:
6693 case WINED3D_TTFF_DISABLE:
6694 div_mask = WINED3DSP_WRITEMASK_3;
6695 break;
6697 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6698 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6701 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6703 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6704 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6705 coord_param.param_str, coord_mask);
6707 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6709 struct glsl_src_param luminance_param;
6710 struct glsl_dst_param dst_param;
6712 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6713 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6715 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6716 dst_param.reg_name, dst_param.mask_str,
6717 luminance_param.param_str, sampler_idx, sampler_idx);
6719 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6722 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6724 unsigned int sampler_idx = ins->dst[0].reg.idx[0].offset;
6725 struct glsl_src_param src0_param, src1_param;
6727 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6728 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6730 shader_glsl_append_dst(ins->ctx->buffer, ins);
6731 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6732 src0_param.param_str, sampler_idx, src1_param.param_str);
6735 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6736 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6737 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6739 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6740 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6741 struct glsl_sample_function sample_function;
6742 struct wined3d_string_buffer *reg_name;
6744 reg_name = string_buffer_get(priv->string_buffers);
6745 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6747 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6748 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6749 "%s.wx", reg_name->buffer);
6750 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6752 string_buffer_release(priv->string_buffers, reg_name);
6755 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6756 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6757 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6759 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6760 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6761 struct glsl_sample_function sample_function;
6762 struct wined3d_string_buffer *reg_name;
6764 reg_name = string_buffer_get(priv->string_buffers);
6765 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6767 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6768 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6769 "%s.yz", reg_name->buffer);
6770 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6772 string_buffer_release(priv->string_buffers, reg_name);
6775 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6776 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6777 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6779 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6780 struct glsl_sample_function sample_function;
6781 struct glsl_src_param src0_param;
6783 /* Dependent read, not valid with conditional NP2 */
6784 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6785 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6787 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6788 "%s", src0_param.param_str);
6789 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6792 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6793 * If any of the first 3 components are < 0, discard this pixel */
6794 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6796 if (ins->ctx->reg_maps->shader_version.major >= 4)
6798 shader_glsl_generate_condition(ins);
6799 shader_addline(ins->ctx->buffer, " discard;\n");
6801 else
6803 struct glsl_dst_param dst_param;
6805 /* The argument is a destination parameter, and no writemasks are allowed */
6806 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6808 /* 2.0 shaders compare all 4 components in texkill. */
6809 if (ins->ctx->reg_maps->shader_version.major >= 2)
6810 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6811 /* 1.x shaders only compare the first 3 components, probably due to
6812 * the nature of the texkill instruction as a tex* instruction, and
6813 * phase, which kills all .w components. Even if all 4 components are
6814 * defined, only the first 3 are used. */
6815 else
6816 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6820 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6821 * dst = dot2(src0, src1) + src2 */
6822 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6824 struct glsl_src_param src0_param;
6825 struct glsl_src_param src1_param;
6826 struct glsl_src_param src2_param;
6827 DWORD write_mask;
6828 unsigned int mask_size;
6830 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6831 mask_size = shader_glsl_get_write_mask_size(write_mask);
6833 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6834 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6835 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6837 if (mask_size > 1) {
6838 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6839 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6840 } else {
6841 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6842 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6846 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6847 const struct wined3d_shader_signature *input_signature,
6848 const struct wined3d_shader_reg_maps *reg_maps,
6849 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6851 unsigned int i;
6853 for (i = 0; i < input_signature->element_count; ++i)
6855 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6856 const char *semantic_name;
6857 UINT semantic_idx;
6858 char reg_mask[6];
6860 /* Unused */
6861 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6862 continue;
6864 semantic_name = input->semantic_name;
6865 semantic_idx = input->semantic_idx;
6866 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6868 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6870 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6872 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6873 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6875 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6877 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6879 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6881 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6882 input->register_idx, reg_mask);
6884 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6886 if (gl_info->supported[ARB_SAMPLE_SHADING])
6887 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6888 input->register_idx, reg_mask);
6889 else
6890 FIXME("ARB_sample_shading is not supported.\n");
6892 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6894 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6895 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6896 input->register_idx, reg_mask);
6897 else
6898 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6900 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6902 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6903 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6904 input->register_idx, reg_mask);
6905 else
6906 FIXME("ARB_viewport_array is not supported.\n");
6908 else
6910 if (input->sysval_semantic)
6911 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6912 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6913 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6914 shader_glsl_shader_input_name(gl_info),
6915 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6918 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6920 if (args->pointsprite)
6921 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6922 shader->u.ps.input_reg_map[input->register_idx]);
6923 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6924 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6925 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6926 needs_legacy_glsl_syntax(gl_info)
6927 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6928 else
6929 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6930 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6932 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6934 if (!semantic_idx)
6935 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6936 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6937 else if (semantic_idx == 1)
6938 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6939 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6940 else
6941 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6942 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6944 else
6946 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6947 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6952 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6954 struct glsl_program_key key;
6956 key.vs_id = entry->vs.id;
6957 key.hs_id = entry->hs.id;
6958 key.ds_id = entry->ds.id;
6959 key.gs_id = entry->gs.id;
6960 key.ps_id = entry->ps.id;
6961 key.cs_id = entry->cs.id;
6963 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6965 ERR("Failed to insert program entry.\n");
6969 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6970 const struct glsl_program_key *key)
6972 struct wine_rb_entry *entry;
6974 entry = wine_rb_get(&priv->program_lookup, key);
6975 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6978 /* Context activation is done by the caller. */
6979 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6980 struct glsl_shader_prog_link *entry)
6982 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6984 GL_EXTCALL(glDeleteProgram(entry->id));
6985 if (entry->vs.id)
6986 list_remove(&entry->vs.shader_entry);
6987 if (entry->hs.id)
6988 list_remove(&entry->hs.shader_entry);
6989 if (entry->ds.id)
6990 list_remove(&entry->ds.shader_entry);
6991 if (entry->gs.id)
6992 list_remove(&entry->gs.shader_entry);
6993 if (entry->ps.id)
6994 list_remove(&entry->ps.shader_entry);
6995 if (entry->cs.id)
6996 list_remove(&entry->cs.shader_entry);
6997 free(entry);
7000 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
7001 const struct wined3d_gl_info *gl_info, const unsigned int *map,
7002 const struct wined3d_shader_signature *input_signature,
7003 const struct wined3d_shader_reg_maps *reg_maps_in,
7004 const struct wined3d_shader_signature *output_signature,
7005 const struct wined3d_shader_reg_maps *reg_maps_out)
7007 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
7008 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
7009 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7010 unsigned int in_count = vec4_varyings(3, gl_info);
7011 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
7012 unsigned int in_idx;
7013 DWORD *set = NULL;
7014 unsigned int i, j;
7015 char reg_mask[6];
7017 set = calloc(max_varyings, sizeof(*set));
7019 for (i = 0; i < input_signature->element_count; ++i)
7021 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
7023 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
7024 continue;
7026 in_idx = map[input->register_idx];
7027 /* Declared, but not read register */
7028 if (in_idx == ~0u)
7029 continue;
7030 if (in_idx >= max_varyings)
7032 FIXME("More input varyings declared than supported, expect issues.\n");
7033 continue;
7036 if (in_idx == in_count)
7037 string_buffer_sprintf(destination, "gl_FrontColor");
7038 else if (in_idx == in_count + 1)
7039 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7040 else
7041 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
7043 if (!set[in_idx])
7044 set[in_idx] = ~0u;
7046 for (j = 0; j < output_signature->element_count; ++j)
7048 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
7049 DWORD mask;
7051 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
7052 || input->semantic_idx != output->semantic_idx
7053 || strcmp(input->semantic_name, output->semantic_name)
7054 || !(mask = input->mask & output->mask))
7055 continue;
7057 if (set[in_idx] == ~0u)
7058 set[in_idx] = 0;
7059 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
7060 shader_glsl_write_mask_to_str(mask, reg_mask);
7062 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
7063 destination->buffer, reg_mask, output->register_idx, reg_mask);
7067 for (i = 0; i < max_varyings; ++i)
7069 unsigned int size;
7071 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
7072 continue;
7074 if (set[i] == ~0u)
7075 set[i] = 0;
7077 size = 0;
7078 if (!(set[i] & WINED3DSP_WRITEMASK_0))
7079 reg_mask[size++] = 'x';
7080 if (!(set[i] & WINED3DSP_WRITEMASK_1))
7081 reg_mask[size++] = 'y';
7082 if (!(set[i] & WINED3DSP_WRITEMASK_2))
7083 reg_mask[size++] = 'z';
7084 if (!(set[i] & WINED3DSP_WRITEMASK_3))
7085 reg_mask[size++] = 'w';
7086 reg_mask[size] = '\0';
7088 if (i == in_count)
7089 string_buffer_sprintf(destination, "gl_FrontColor");
7090 else if (i == in_count + 1)
7091 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7092 else
7093 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
7095 if (size == 1)
7096 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
7097 else
7098 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
7101 free(set);
7102 string_buffer_release(&priv->string_buffers, destination);
7105 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
7106 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
7107 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
7108 BOOL rasterizer_setup)
7110 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7111 char reg_mask[6];
7112 unsigned int i;
7114 for (i = 0; i < output_signature->element_count; ++i)
7116 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7118 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7119 continue;
7121 if (output->stream_idx)
7122 continue;
7124 if (output->register_idx >= input_count)
7125 continue;
7127 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7129 shader_addline(buffer,
7130 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7131 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7135 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7136 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7138 unsigned int i, clip_or_cull_index;
7139 const char *name;
7140 char reg_mask[6];
7142 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7143 /* Assign consecutive indices starting from 0. */
7144 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7145 for (i = 0; i < 4; ++i)
7147 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7148 continue;
7150 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7151 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7152 name, clip_or_cull_index, element->register_idx, reg_mask);
7153 ++clip_or_cull_index;
7157 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7158 const struct wined3d_gl_info *gl_info, const unsigned int *map,
7159 const struct wined3d_shader_signature *input_signature,
7160 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7161 const struct wined3d_shader_signature *output_signature,
7162 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7164 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7165 const char *semantic_name;
7166 unsigned int semantic_idx;
7167 char reg_mask[6];
7168 unsigned int i;
7170 /* First, sort out position and point size system values. */
7171 for (i = 0; i < output_signature->element_count; ++i)
7173 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7175 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7176 continue;
7178 if (output->stream_idx)
7179 continue;
7181 semantic_name = output->semantic_name;
7182 semantic_idx = output->semantic_idx;
7183 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7185 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7187 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7188 reg_mask, output->register_idx, reg_mask);
7190 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7192 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7193 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7195 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7197 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7198 output->register_idx, reg_mask);
7200 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7202 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7203 output->register_idx, reg_mask);
7205 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7207 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7209 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7211 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7213 else if (output->sysval_semantic)
7215 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7219 /* Then, setup the pixel shader input. */
7220 if (reg_maps_out->shader_version.major < 4)
7221 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7222 output_signature, reg_maps_out);
7223 else
7224 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7227 /* Context activation is done by the caller. */
7228 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7229 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7230 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7232 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7233 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7234 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7235 const char *semantic_name;
7236 UINT semantic_idx;
7237 char reg_mask[6];
7238 unsigned int i;
7239 GLuint ret;
7241 string_buffer_clear(buffer);
7243 shader_glsl_add_version_declaration(buffer, gl_info);
7245 shader_addline(buffer, "invariant gl_Position;\n");
7247 if (per_vertex_point_size)
7249 shader_addline(buffer, "uniform struct\n{\n");
7250 shader_addline(buffer, " float size_min;\n");
7251 shader_addline(buffer, " float size_max;\n");
7252 shader_addline(buffer, "} ffp_point;\n");
7255 if (ps_major < 3)
7257 DWORD colors_written_mask[2] = {0};
7258 DWORD texcoords_written_mask[WINED3D_MAX_FFP_TEXTURES] = {0};
7260 if (!legacy_syntax)
7262 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7263 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7264 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
7265 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7268 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7270 for (i = 0; i < vs->output_signature.element_count; ++i)
7272 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7273 DWORD write_mask;
7275 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7276 continue;
7278 semantic_name = output->semantic_name;
7279 semantic_idx = output->semantic_idx;
7280 write_mask = output->mask;
7281 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7283 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7285 if (legacy_syntax)
7286 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7287 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7288 else
7289 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7290 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7292 colors_written_mask[semantic_idx] = write_mask;
7294 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7296 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7297 reg_mask, output->register_idx, reg_mask);
7299 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7301 if (semantic_idx < WINED3D_MAX_FFP_TEXTURES)
7303 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7304 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7305 semantic_idx, reg_mask, output->register_idx, reg_mask);
7306 texcoords_written_mask[semantic_idx] = write_mask;
7309 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7311 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7312 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7314 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7316 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7317 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7318 output->register_idx, reg_mask[1]);
7322 for (i = 0; i < 2; ++i)
7324 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7326 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7327 if (!i)
7328 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7329 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7330 reg_mask, reg_mask);
7331 else
7332 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7333 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7334 reg_mask, reg_mask);
7337 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
7339 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7340 continue;
7342 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7344 if (!shader_glsl_full_ffp_varyings(gl_info) && !texcoords_written_mask[i])
7345 continue;
7347 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7348 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7349 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7353 else
7355 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7357 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7358 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7359 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7360 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7363 shader_addline(buffer, "}\n");
7365 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7366 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7367 shader_glsl_compile(gl_info, ret, buffer->buffer);
7369 return ret;
7372 static void shader_glsl_generate_stream_output_setup(struct wined3d_string_buffer *buffer,
7373 const struct wined3d_shader *shader)
7375 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
7376 unsigned int i, register_idx, component_idx;
7378 shader_addline(buffer, "out shader_in_out\n{\n");
7379 for (i = 0; i < so_desc->element_count; ++i)
7381 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7383 if (e->stream_idx)
7385 FIXME("Unhandled stream %u.\n", e->stream_idx);
7386 continue;
7388 if (!e->semantic_name)
7389 continue;
7390 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7391 continue;
7393 if (component_idx || e->component_count != 4)
7395 if (e->component_count == 1)
7396 shader_addline(buffer, "float");
7397 else
7398 shader_addline(buffer, "vec%u", e->component_count);
7400 shader_addline(buffer, " reg%u_%u_%u;\n",
7401 register_idx, component_idx, component_idx + e->component_count - 1);
7403 else
7405 shader_addline(buffer, "vec4 reg%u;\n", register_idx);
7408 shader_addline(buffer, "} shader_out;\n");
7410 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7411 shader->limits->packed_output);
7412 for (i = 0; i < so_desc->element_count; ++i)
7414 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7416 if (e->stream_idx)
7418 FIXME("Unhandled stream %u.\n", e->stream_idx);
7419 continue;
7421 if (!e->semantic_name)
7422 continue;
7423 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7424 continue;
7426 if (component_idx || e->component_count != 4)
7428 DWORD write_mask;
7429 char str_mask[6];
7431 write_mask = wined3d_mask_from_size(e->component_count) << component_idx;
7432 shader_glsl_write_mask_to_str(write_mask, str_mask);
7433 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7434 register_idx, component_idx, component_idx + e->component_count - 1,
7435 register_idx, str_mask);
7437 else
7439 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n", register_idx, register_idx);
7442 shader_addline(buffer, "}\n");
7445 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7446 const struct wined3d_shader *shader, unsigned int input_count,
7447 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const uint32_t *interpolation_mode)
7449 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7450 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7452 if (rasterizer_setup)
7453 input_count = min(vec4_varyings(4, gl_info), input_count);
7455 if (input_count)
7456 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7458 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7459 prefix, shader->limits->packed_output);
7461 if (rasterizer_setup)
7462 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7463 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7464 else
7465 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7466 &shader->reg_maps, "shader_out", rasterizer_setup);
7468 shader_addline(buffer, "}\n");
7471 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7472 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7473 const char *reg_mask)
7475 if (!constant->sysval_semantic)
7477 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7478 return;
7481 switch (constant->sysval_semantic)
7483 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7484 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7485 case WINED3D_SV_TESS_FACTOR_LINEDET:
7486 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7487 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7488 break;
7489 case WINED3D_SV_TESS_FACTOR_QUADINT:
7490 case WINED3D_SV_TESS_FACTOR_TRIINT:
7491 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7492 break;
7493 default:
7494 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7495 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7499 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7500 const struct wined3d_shader_signature *signature, BOOL input_setup)
7502 unsigned int i, register_count, user_constant_index, user_constant_count;
7504 register_count = user_constant_count = 0;
7505 for (i = 0; i < signature->element_count; ++i)
7507 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7508 register_count = max(constant->register_idx + 1, register_count);
7509 if (!constant->sysval_semantic)
7510 ++user_constant_count;
7513 if (user_constant_count)
7514 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7515 input_setup ? "in" : "out", user_constant_count);
7516 if (input_setup)
7517 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7519 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7520 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7522 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7523 char reg_mask[6];
7525 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7527 if (input_setup)
7528 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7529 else
7530 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7532 shader_addline(buffer, " = ");
7534 if (input_setup)
7535 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7536 else
7537 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7539 shader_addline(buffer, ";\n");
7541 shader_addline(buffer, "}\n");
7544 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7545 const struct wined3d_gl_info *gl_info)
7547 const char *output = get_fragment_output(gl_info);
7549 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7550 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7551 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7552 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7553 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7554 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7557 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7558 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7560 const char *output = get_fragment_output(gl_info);
7562 switch (mode)
7564 case WINED3D_FFP_PS_FOG_OFF:
7565 return;
7567 case WINED3D_FFP_PS_FOG_LINEAR:
7568 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7569 break;
7571 case WINED3D_FFP_PS_FOG_EXP:
7572 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7573 break;
7575 case WINED3D_FFP_PS_FOG_EXP2:
7576 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7577 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7578 break;
7580 default:
7581 ERR("Invalid fog mode %#x.\n", mode);
7582 return;
7585 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7586 output, output);
7589 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7590 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7592 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7593 * flipping all the operators here, just negate the comparison below. */
7594 static const char * const comparison_operator[] =
7596 "", /* WINED3D_CMP_NEVER */
7597 "<", /* WINED3D_CMP_LESS */
7598 "==", /* WINED3D_CMP_EQUAL */
7599 "<=", /* WINED3D_CMP_LESSEQUAL */
7600 ">", /* WINED3D_CMP_GREATER */
7601 "!=", /* WINED3D_CMP_NOTEQUAL */
7602 ">=", /* WINED3D_CMP_GREATEREQUAL */
7603 "" /* WINED3D_CMP_ALWAYS */
7606 if (alpha_func == WINED3D_CMP_ALWAYS)
7607 return;
7609 if (alpha_func != WINED3D_CMP_NEVER)
7610 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7611 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7612 shader_addline(buffer, " discard;\n");
7615 static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *buffer,
7616 const char *colour, const char *colour_key_low, const char *colour_key_high)
7618 shader_addline(buffer, "if (all(greaterThanEqual(%s, %s)) && all(lessThan(%s, %s)))\n",
7619 colour, colour_key_low, colour, colour_key_high);
7620 shader_addline(buffer, " discard;\n");
7623 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7624 const struct wined3d_gl_info *gl_info)
7626 if (gl_info->supported[ARB_BINDLESS_TEXTURE])
7627 shader_addline(buffer, "#extension GL_ARB_bindless_texture : enable\n");
7628 if (gl_info->supported[ARB_CULL_DISTANCE])
7629 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7630 if (gl_info->supported[ARB_GPU_SHADER5])
7631 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7632 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7633 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7634 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7635 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7636 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7637 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7638 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7639 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7640 if (gl_info->supported[ARB_SHADER_STENCIL_EXPORT])
7641 shader_addline(buffer, "#extension GL_ARB_shader_stencil_export : enable\n");
7642 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7643 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7644 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7645 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7646 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7647 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7648 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7649 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7650 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7651 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7652 if (gl_info->supported[ARB_TEXTURE_GATHER])
7653 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7654 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7655 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7656 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7657 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7658 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7659 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7660 if (gl_info->supported[EXT_GPU_SHADER4])
7661 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7662 if (gl_info->supported[EXT_SHADER_INTEGER_MIX])
7663 shader_addline(buffer, "#extension GL_EXT_shader_integer_mix : enable\n");
7664 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7665 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7666 if (gl_info->supported[EXT_TEXTURE_SHADOW_LOD])
7667 shader_addline(buffer, "#extension GL_EXT_texture_shadow_lod : enable\n");
7670 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7671 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7672 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7674 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7675 struct wined3d_string_buffer *src, *assignment;
7676 enum wined3d_data_type dst_data_type;
7677 const char *swizzle;
7678 unsigned int i;
7680 if (output_signature->element_count)
7682 src = string_buffer_get(string_buffers);
7683 assignment = string_buffer_get(string_buffers);
7684 for (i = 0; i < output_signature->element_count; ++i)
7686 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7688 /* register_idx is set to ~0u for non-color outputs. */
7689 if (output->register_idx == ~0u)
7690 continue;
7691 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7693 FIXME("Unhandled component type %#x.\n", output->component_type);
7694 continue;
7696 dst_data_type = component_type_info[output->component_type].data_type;
7697 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7698 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7699 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT, 4);
7700 swizzle = args->rt_alpha_swizzle & (1u << output->semantic_idx) ? ".argb" : "";
7701 shader_addline(buffer, "%s%s;\n", assignment->buffer, swizzle);
7703 string_buffer_release(string_buffers, src);
7704 string_buffer_release(string_buffers, assignment);
7706 else
7708 uint32_t mask = shader->reg_maps.rt_mask;
7710 while (mask)
7712 i = wined3d_bit_scan(&mask);
7713 swizzle = args->rt_alpha_swizzle & (1u << i) ? ".argb" : "";
7714 shader_addline(buffer, "color_out%u = ps_out[%u]%s;\n", i, i, swizzle);
7719 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7720 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7721 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7723 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7725 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7726 if (reg_maps->shader_version.major < 2)
7727 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7729 if (args->srgb_correction)
7730 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7732 /* SM < 3 does not replace the fog stage. */
7733 if (reg_maps->shader_version.major < 3)
7734 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7736 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7738 if (reg_maps->sample_mask)
7739 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7741 if (reg_maps->stencil_ref)
7742 shader_addline(buffer, "gl_FragStencilRefARB = floatBitsToInt(stencil_ref);\n");
7744 if (!use_legacy_fragment_output(gl_info))
7745 shader_glsl_generate_color_output(buffer, gl_info, shader, args, string_buffers);
7748 /* Context activation is done by the caller. */
7749 static GLuint shader_glsl_generate_fragment_shader(const struct wined3d_context_gl *context_gl,
7750 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ps_compile_args *args)
7752 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7753 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7754 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7755 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7756 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7757 const char *prefix = shader_glsl_get_prefix(version->type);
7758 BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7759 unsigned int i, extra_constants_needed = 0;
7760 struct shader_glsl_ctx_priv priv_ctx;
7761 GLuint shader_id;
7762 uint32_t map;
7764 if (priv->use_vkd3d)
7765 return shader_glsl_generate_vkd3d_shader(context_gl, priv, shader, GL_FRAGMENT_SHADER);
7767 memset(&priv_ctx, 0, sizeof(priv_ctx));
7768 priv_ctx.gl_info = gl_info;
7769 priv_ctx.cur_ps_args = args;
7770 priv_ctx.string_buffers = string_buffers;
7772 shader_glsl_add_version_declaration(buffer, gl_info);
7774 shader_glsl_enable_extensions(buffer, gl_info);
7775 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7776 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7777 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7778 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7779 if (shader_glsl_use_explicit_attrib_location(gl_info))
7780 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7781 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7782 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7783 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7784 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7785 if (gl_info->supported[ARB_SAMPLE_SHADING])
7786 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7787 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7788 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7790 /* Base Declarations */
7791 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7793 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7795 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7796 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7797 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7798 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7801 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7803 shader_addline(buffer, "uniform struct\n{\n");
7804 shader_addline(buffer, " vec4 color;\n");
7805 shader_addline(buffer, " float density;\n");
7806 shader_addline(buffer, " float end;\n");
7807 shader_addline(buffer, " float scale;\n");
7808 shader_addline(buffer, "} ffp_fog;\n");
7810 if (legacy_syntax)
7812 if (glsl_is_color_reg_read(shader, 0))
7813 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7814 if (glsl_is_color_reg_read(shader, 1))
7815 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7816 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
7817 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7819 else
7821 if (glsl_is_color_reg_read(shader, 0))
7822 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7823 if (glsl_is_color_reg_read(shader, 1))
7824 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7825 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
7826 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
7827 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7831 if (version->major >= 3)
7833 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7835 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7836 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7837 shader->u.ps.interpolation_mode, version->major >= 4);
7838 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7841 map = reg_maps->bumpmat;
7842 while (map)
7844 i = wined3d_bit_scan(&map);
7845 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7847 if (reg_maps->luminanceparams & (1u << i))
7849 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7850 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7851 extra_constants_needed++;
7854 extra_constants_needed++;
7857 if (args->srgb_correction)
7859 shader_addline(buffer, "const vec4 srgb_const0 = ");
7860 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
7861 shader_addline(buffer, ";\n");
7862 shader_addline(buffer, "const vec4 srgb_const1 = ");
7863 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
7864 shader_addline(buffer, ";\n");
7866 if (reg_maps->vpos)
7868 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7870 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7871 shader_addline(buffer, "layout(pixel_center_integer) in vec4 gl_FragCoord;\n");
7873 shader_addline(buffer, "vec4 vpos;\n");
7876 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7877 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7879 if (!use_legacy_fragment_output(gl_info))
7881 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7883 if (args->dual_source_blend)
7884 shader_addline(buffer, "vec4 ps_out[2];\n");
7885 else
7886 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7887 if (output_signature->element_count)
7889 for (i = 0; i < output_signature->element_count; ++i)
7891 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7893 if (output->register_idx == ~0u)
7894 continue;
7895 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7897 FIXME("Unhandled component type %#x.\n", output->component_type);
7898 continue;
7900 if (shader_glsl_use_explicit_attrib_location(gl_info))
7902 if (args->dual_source_blend)
7903 shader_addline(buffer, "layout(location = 0, index = %u) ", output->semantic_idx);
7904 else
7905 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7907 shader_addline(buffer, "out %s4 color_out%u;\n",
7908 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7911 else
7913 uint32_t mask = reg_maps->rt_mask;
7915 while (mask)
7917 i = wined3d_bit_scan(&mask);
7918 if (shader_glsl_use_explicit_attrib_location(gl_info))
7920 if (args->dual_source_blend)
7921 shader_addline(buffer, "layout(location = 0, index = %u) ", i);
7922 else
7923 shader_addline(buffer, "layout(location = %u) ", i);
7925 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7930 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7931 FIXME("Insufficient uniforms to run this shader.\n");
7933 if (shader->u.ps.force_early_depth_stencil)
7934 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7936 shader_addline(buffer, "void main()\n{\n");
7938 if (reg_maps->sample_mask)
7939 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7941 if (reg_maps->stencil_ref)
7942 shader_addline(buffer, "float stencil_ref = uintBitsToFloat(0xffffffffu);\n");
7944 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7945 * add approximately 0.5. This causes off-by-one problems as spotted by
7946 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7947 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7948 * causes precision troubles when we just subtract 0.5.
7950 * To deal with that, just floor() the position. This will eliminate the
7951 * fraction on all cards.
7953 * TODO: Test how this behaves with multisampling.
7955 * An advantage of floor is that it works even if the driver doesn't add
7956 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7957 * to return in gl_FragCoord, even though coordinates specify the pixel
7958 * centers instead of the pixel corners. This code will behave correctly
7959 * on drivers that returns integer values. */
7960 if (reg_maps->vpos)
7962 if (!gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]
7963 && (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER))
7964 shader_addline(buffer, "vpos = floor(gl_FragCoord);\n");
7965 else
7966 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7969 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7971 unsigned int i;
7973 if (legacy_syntax)
7975 if (glsl_is_color_reg_read(shader, 0))
7976 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7977 if (glsl_is_color_reg_read(shader, 1))
7978 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7981 map = reg_maps->texcoord;
7982 while (map)
7984 i = wined3d_bit_scan(&map);
7985 if (args->pointsprite)
7986 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7987 else if (args->texcoords_initialized & (1u << i))
7988 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7989 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7990 else
7991 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7992 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7995 if (legacy_syntax)
7996 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7999 /* Pack 3.0 inputs */
8000 if (reg_maps->shader_version.major >= 3)
8001 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
8002 reg_maps->shader_version.major >= 4);
8004 /* Base Shader Body */
8005 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8006 return 0;
8008 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8009 if (reg_maps->shader_version.major < 4)
8010 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
8012 shader_addline(buffer, "}\n");
8014 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8015 TRACE("Compiling shader object %u.\n", shader_id);
8016 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8018 return shader_id;
8021 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
8022 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8023 const struct vs_compile_args *args)
8025 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8026 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8027 unsigned int i;
8029 /* Unpack outputs. */
8030 shader_addline(buffer, "setup_vs_output(vs_out);\n");
8032 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
8033 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
8034 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
8035 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
8037 if (reg_maps->shader_version.major < 3)
8039 if (args->fog_src == VS_FOG_Z)
8040 shader_addline(buffer, "%s = gl_Position.z;\n",
8041 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
8042 else if (!reg_maps->fog)
8043 shader_addline(buffer, "%s = 0.0;\n",
8044 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
8047 /* We always store the clipplanes without y inversion. */
8048 if (args->clip_enabled)
8050 if (legacy_syntax)
8051 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
8052 else
8053 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8054 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
8057 if (args->point_size && !args->per_vertex_point_size)
8058 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
8060 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8061 shader_glsl_fixup_position(buffer, FALSE);
8064 /* Context activation is done by the caller. */
8065 static GLuint shader_glsl_generate_vertex_shader(const struct wined3d_context_gl *context_gl,
8066 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
8068 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8069 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8070 const struct wined3d_shader_version *version = &reg_maps->shader_version;
8071 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8072 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8073 struct shader_glsl_ctx_priv priv_ctx;
8074 GLuint shader_id;
8075 unsigned int i;
8077 if (priv->use_vkd3d)
8078 return shader_glsl_generate_vkd3d_shader(context_gl, priv, shader, GL_VERTEX_SHADER);
8080 memset(&priv_ctx, 0, sizeof(priv_ctx));
8081 priv_ctx.gl_info = gl_info;
8082 priv_ctx.cur_vs_args = args;
8083 priv_ctx.string_buffers = string_buffers;
8085 shader_glsl_add_version_declaration(buffer, gl_info);
8087 shader_glsl_enable_extensions(buffer, gl_info);
8088 if (gl_info->supported[ARB_DRAW_INSTANCED])
8089 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8090 if (shader_glsl_use_explicit_attrib_location(gl_info))
8091 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8092 if (gl_info->supported[ARB_SHADER_VIEWPORT_LAYER_ARRAY])
8093 shader_addline(buffer, "#extension GL_ARB_shader_viewport_layer_array : enable\n");
8095 /* Base Declarations */
8096 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8098 for (i = 0; i < shader->input_signature.element_count; ++i)
8099 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8101 if (args->point_size && !args->per_vertex_point_size)
8103 shader_addline(buffer, "uniform struct\n{\n");
8104 shader_addline(buffer, " float size;\n");
8105 shader_addline(buffer, " float size_min;\n");
8106 shader_addline(buffer, " float size_max;\n");
8107 shader_addline(buffer, "} ffp_point;\n");
8110 if (!needs_legacy_glsl_syntax(gl_info))
8112 if (args->clip_enabled)
8113 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8115 if (version->major < 3)
8117 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8118 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8119 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
8120 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8124 if (version->major < 4)
8125 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8127 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8128 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8130 if (reg_maps->shader_version.major >= 4)
8131 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8132 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8134 shader_addline(buffer, "void main()\n{\n");
8136 if (reg_maps->input_rel_addressing)
8138 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8139 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8140 for (i = 0; i < shader->input_signature.element_count; ++i)
8142 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8143 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8147 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8148 return 0;
8150 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8151 if (reg_maps->shader_version.major < 4)
8152 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8154 shader_addline(buffer, "}\n");
8156 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8157 TRACE("Compiling shader object %u.\n", shader_id);
8158 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8160 return shader_id;
8163 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8164 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8166 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8167 char reg_mask[6];
8168 unsigned int i;
8170 for (i = 0; i < output_signature->element_count; ++i)
8172 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8174 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8175 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8176 output->register_idx, reg_mask, output->register_idx, reg_mask);
8180 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8181 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8182 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8183 const char *phase_name, unsigned phase_idx)
8185 unsigned int i;
8186 HRESULT hr;
8188 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8189 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8190 for (i = 0; i < phase->temporary_count; ++i)
8191 shader_addline(buffer, "vec4 R%u;\n", i);
8192 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8193 shader_addline(buffer, "}\n");
8194 return hr;
8197 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8198 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8200 if (phase->instance_count)
8202 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8203 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8204 shader_addline(buffer, "}\n");
8206 else
8208 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8212 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context_gl *context_gl,
8213 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8215 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8216 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8217 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8218 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8219 const struct wined3d_hull_shader *hs = &shader->u.hs;
8220 const struct wined3d_shader_phase *phase;
8221 struct shader_glsl_ctx_priv priv_ctx;
8222 GLuint shader_id;
8223 unsigned int i;
8225 if (priv->use_vkd3d)
8226 return shader_glsl_generate_vkd3d_shader(context_gl, priv, shader, GL_TESS_CONTROL_SHADER);
8228 memset(&priv_ctx, 0, sizeof(priv_ctx));
8229 priv_ctx.gl_info = gl_info;
8230 priv_ctx.string_buffers = string_buffers;
8232 shader_glsl_add_version_declaration(buffer, gl_info);
8234 shader_glsl_enable_extensions(buffer, gl_info);
8235 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8237 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8239 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8241 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8242 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8244 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8246 if (hs->phases.control_point)
8248 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8249 shader->limits->packed_output);
8250 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8251 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8252 shader_addline(buffer, "}\n");
8255 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8256 if ((phase = hs->phases.control_point))
8258 for (i = 0; i < phase->temporary_count; ++i)
8259 shader_addline(buffer, "vec4 R%u;\n", i);
8260 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8261 return 0;
8262 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8264 else
8266 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8268 shader_addline(buffer, "}\n");
8270 for (i = 0; i < hs->phases.fork_count; ++i)
8272 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8273 &hs->phases.fork[i], "fork", i)))
8274 return 0;
8277 for (i = 0; i < hs->phases.join_count; ++i)
8279 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8280 &hs->phases.join[i], "join", i)))
8281 return 0;
8284 shader_addline(buffer, "void main()\n{\n");
8285 shader_addline(buffer, "hs_control_point_phase();\n");
8286 if (reg_maps->vocp)
8287 shader_addline(buffer, "barrier();\n");
8288 for (i = 0; i < hs->phases.fork_count; ++i)
8289 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8290 for (i = 0; i < hs->phases.join_count; ++i)
8291 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8292 shader_addline(buffer, "setup_patch_constant_output();\n");
8293 shader_addline(buffer, "}\n");
8295 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8296 TRACE("Compiling shader object %u.\n", shader_id);
8297 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8299 return shader_id;
8302 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8303 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8304 const struct ds_compile_args *args)
8306 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8308 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8309 shader_glsl_fixup_position(buffer, FALSE);
8312 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context_gl *context_gl,
8313 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8315 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8316 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8317 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8318 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8319 struct shader_glsl_ctx_priv priv_ctx;
8320 GLuint shader_id;
8322 if (priv->use_vkd3d)
8323 return shader_glsl_generate_vkd3d_shader(context_gl, priv, shader, GL_TESS_EVALUATION_SHADER);
8325 memset(&priv_ctx, 0, sizeof(priv_ctx));
8326 priv_ctx.gl_info = gl_info;
8327 priv_ctx.cur_ds_args = args;
8328 priv_ctx.string_buffers = string_buffers;
8330 shader_glsl_add_version_declaration(buffer, gl_info);
8332 shader_glsl_enable_extensions(buffer, gl_info);
8333 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8335 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8337 shader_addline(buffer, "layout(");
8338 switch (shader->u.ds.tessellator_domain)
8340 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8341 shader_addline(buffer, "isolines");
8342 break;
8343 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8344 shader_addline(buffer, "quads");
8345 break;
8346 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8347 shader_addline(buffer, "triangles");
8348 break;
8350 switch (args->tessellator_output_primitive)
8352 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8353 shader_addline(buffer, ", ccw");
8354 break;
8355 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8356 shader_addline(buffer, ", cw");
8357 break;
8358 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8359 shader_addline(buffer, ", point_mode");
8360 break;
8361 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8362 break;
8364 switch (args->tessellator_partitioning)
8366 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8367 shader_addline(buffer, ", fractional_odd_spacing");
8368 break;
8369 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8370 shader_addline(buffer, ", fractional_even_spacing");
8371 break;
8372 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8373 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8374 shader_addline(buffer, ", equal_spacing");
8375 break;
8377 shader_addline(buffer, ") in;\n");
8379 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8381 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8382 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8384 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8385 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8386 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8388 shader_addline(buffer, "void main()\n{\n");
8389 shader_addline(buffer, "setup_patch_constant_input();\n");
8391 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8392 return 0;
8394 shader_addline(buffer, "}\n");
8396 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8397 TRACE("Compiling shader object %u.\n", shader_id);
8398 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8400 return shader_id;
8403 /* Context activation is done by the caller. */
8404 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context_gl *context_gl,
8405 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8407 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8408 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8409 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8410 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8411 const struct wined3d_shader_signature_element *output;
8412 enum wined3d_primitive_type primitive_type;
8413 struct shader_glsl_ctx_priv priv_ctx;
8414 unsigned int max_vertices;
8415 unsigned int i, j;
8416 GLuint shader_id;
8418 if (shader->function && priv->use_vkd3d)
8419 return shader_glsl_generate_vkd3d_shader(context_gl, priv, shader, GL_GEOMETRY_SHADER);
8421 memset(&priv_ctx, 0, sizeof(priv_ctx));
8422 priv_ctx.gl_info = gl_info;
8423 priv_ctx.string_buffers = string_buffers;
8425 shader_glsl_add_version_declaration(buffer, gl_info);
8427 shader_glsl_enable_extensions(buffer, gl_info);
8429 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8431 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8432 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8433 if (shader->u.gs.instance_count > 1)
8434 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8435 shader_addline(buffer, ") in;\n");
8437 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8438 if (!(max_vertices = shader->u.gs.vertices_out))
8440 switch (args->primitive_type)
8442 case WINED3D_PT_POINTLIST:
8443 max_vertices = 1;
8444 break;
8445 case WINED3D_PT_LINELIST:
8446 max_vertices = 2;
8447 break;
8448 case WINED3D_PT_TRIANGLELIST:
8449 max_vertices = 3;
8450 break;
8451 default:
8452 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8453 break;
8456 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8457 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8458 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8460 if (!gl_info->supported[ARB_CLIP_CONTROL])
8462 shader_addline(buffer, "uniform vec4 pos_fixup");
8463 if (reg_maps->viewport_array)
8464 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8465 shader_addline(buffer, ";\n");
8468 if (is_rasterization_disabled(shader))
8470 shader_glsl_generate_stream_output_setup(buffer, shader);
8472 else
8474 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8475 gl_info, TRUE, args->interpolation_mode);
8478 shader_addline(buffer, "void main()\n{\n");
8479 if (shader->function)
8481 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8482 return 0;
8484 else
8486 for (i = 0; i < max_vertices; ++i)
8488 for (j = 0; j < shader->output_signature.element_count; ++j)
8490 output = &shader->output_signature.elements[j];
8491 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8492 output->register_idx, i, output->register_idx);
8494 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8495 if (!gl_info->supported[ARB_CLIP_CONTROL])
8496 shader_glsl_fixup_position(buffer, FALSE);
8497 shader_addline(buffer, "EmitVertex();\n");
8500 shader_addline(buffer, "}\n");
8502 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8503 TRACE("Compiling shader object %u.\n", shader_id);
8504 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8506 return shader_id;
8509 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8511 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8512 const struct wined3d_gl_info *gl_info = priv->gl_info;
8513 struct wined3d_string_buffer *buffer = ctx->buffer;
8514 const struct wined3d_shader *shader = ctx->shader;
8516 switch (shader->reg_maps.shader_version.type)
8518 case WINED3D_SHADER_TYPE_PIXEL:
8519 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8520 break;
8521 case WINED3D_SHADER_TYPE_VERTEX:
8522 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8523 break;
8524 case WINED3D_SHADER_TYPE_DOMAIN:
8525 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8526 break;
8527 case WINED3D_SHADER_TYPE_GEOMETRY:
8528 case WINED3D_SHADER_TYPE_COMPUTE:
8529 break;
8530 default:
8531 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8532 break;
8536 /* Context activation is done by the caller. */
8537 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context_gl *context_gl,
8538 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8540 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8541 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8542 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8543 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8544 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8545 struct shader_glsl_ctx_priv priv_ctx;
8546 GLuint shader_id;
8547 unsigned int i;
8549 if (priv->use_vkd3d)
8550 return shader_glsl_generate_vkd3d_shader(context_gl, priv, shader, GL_COMPUTE_SHADER);
8552 memset(&priv_ctx, 0, sizeof(priv_ctx));
8553 priv_ctx.gl_info = gl_info;
8554 priv_ctx.string_buffers = string_buffers;
8556 shader_glsl_add_version_declaration(buffer, gl_info);
8558 shader_glsl_enable_extensions(buffer, gl_info);
8559 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8561 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8563 for (i = 0; i < reg_maps->tgsm_count; ++i)
8565 if (reg_maps->tgsm[i].size)
8566 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8569 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8570 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8572 shader_addline(buffer, "void main()\n{\n");
8573 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8574 shader_addline(buffer, "}\n");
8576 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8577 TRACE("Compiling shader object %u.\n", shader_id);
8578 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8580 return shader_id;
8583 static GLuint find_glsl_fragment_shader(const struct wined3d_context_gl *context_gl,
8584 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ps_compile_args *args)
8586 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8587 struct glsl_shader_private *shader_data;
8588 UINT i;
8589 DWORD new_size;
8590 GLuint ret;
8592 if (!shader->backend_data)
8594 if (!(shader->backend_data = calloc(1, sizeof(*shader_data))))
8596 ERR("Failed to allocate backend data.\n");
8597 return 0;
8600 shader_data = shader->backend_data;
8601 gl_shaders = shader_data->gl_shaders.ps;
8603 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8604 * so a linear search is more performant than a hashmap or a binary search
8605 * (cache coherency etc)
8607 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8609 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8610 return gl_shaders[i].id;
8613 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8614 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8616 if (shader_data->num_gl_shaders)
8618 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8619 new_array = realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8621 else
8623 new_array = malloc(sizeof(*gl_shaders));
8624 new_size = 1;
8627 if(!new_array) {
8628 ERR("Out of memory\n");
8629 return 0;
8631 shader_data->gl_shaders.ps = new_array;
8632 shader_data->shader_array_size = new_size;
8633 gl_shaders = new_array;
8636 gl_shaders[shader_data->num_gl_shaders].args = *args;
8638 string_buffer_clear(&priv->shader_buffer);
8639 ret = shader_glsl_generate_fragment_shader(context_gl, priv, shader, args);
8640 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8642 return ret;
8645 static BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8646 const DWORD use_map)
8648 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8649 return FALSE;
8650 if ((stored->clip_enabled) != new->clip_enabled)
8651 return FALSE;
8652 if (stored->point_size != new->point_size)
8653 return FALSE;
8654 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8655 return FALSE;
8656 if (stored->flatshading != new->flatshading)
8657 return FALSE;
8658 if (stored->next_shader_type != new->next_shader_type)
8659 return FALSE;
8660 if (stored->next_shader_input_count != new->next_shader_input_count)
8661 return FALSE;
8662 if (stored->fog_src != new->fog_src)
8663 return FALSE;
8664 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8667 static GLuint find_glsl_vertex_shader(const struct wined3d_context_gl *context_gl,
8668 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct vs_compile_args *args)
8670 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8671 uint32_t use_map = context_gl->c.stream_info.use_map;
8672 struct glsl_shader_private *shader_data;
8673 unsigned int i, new_size;
8674 GLuint ret;
8676 if (!shader->backend_data)
8678 if (!(shader->backend_data = calloc(1, sizeof(*shader_data))))
8680 ERR("Failed to allocate backend data.\n");
8681 return 0;
8684 shader_data = shader->backend_data;
8685 gl_shaders = shader_data->gl_shaders.vs;
8687 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8688 * so a linear search is more performant than a hashmap or a binary search
8689 * (cache coherency etc)
8691 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8693 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8694 return gl_shaders[i].id;
8697 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8699 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8701 if (shader_data->num_gl_shaders)
8703 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8704 new_array = realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8706 else
8708 new_array = malloc(sizeof(*gl_shaders));
8709 new_size = 1;
8712 if(!new_array) {
8713 ERR("Out of memory\n");
8714 return 0;
8716 shader_data->gl_shaders.vs = new_array;
8717 shader_data->shader_array_size = new_size;
8718 gl_shaders = new_array;
8721 gl_shaders[shader_data->num_gl_shaders].args = *args;
8723 string_buffer_clear(&priv->shader_buffer);
8724 ret = shader_glsl_generate_vertex_shader(context_gl, priv, shader, args);
8725 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8727 return ret;
8730 static GLuint find_glsl_hull_shader(const struct wined3d_context_gl *context_gl,
8731 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8733 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8734 struct glsl_shader_private *shader_data;
8735 unsigned int new_size;
8736 GLuint ret;
8738 if (!shader->backend_data)
8740 if (!(shader->backend_data = calloc(1, sizeof(*shader_data))))
8742 ERR("Failed to allocate backend data.\n");
8743 return 0;
8746 shader_data = shader->backend_data;
8747 gl_shaders = shader_data->gl_shaders.hs;
8749 if (shader_data->num_gl_shaders > 0)
8751 assert(shader_data->num_gl_shaders == 1);
8752 return gl_shaders[0].id;
8755 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8757 assert(!shader_data->gl_shaders.hs);
8758 new_size = 1;
8759 if (!(new_array = malloc(sizeof(*new_array))))
8761 ERR("Failed to allocate GL shaders array.\n");
8762 return 0;
8764 shader_data->gl_shaders.hs = new_array;
8765 shader_data->shader_array_size = new_size;
8766 gl_shaders = new_array;
8768 string_buffer_clear(&priv->shader_buffer);
8769 ret = shader_glsl_generate_hull_shader(context_gl, priv, shader);
8770 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8772 return ret;
8775 static GLuint find_glsl_domain_shader(const struct wined3d_context_gl *context_gl,
8776 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8778 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8779 struct glsl_shader_private *shader_data;
8780 unsigned int i, new_size;
8781 GLuint ret;
8783 if (!shader->backend_data)
8785 if (!(shader->backend_data = calloc(1, sizeof(*shader_data))))
8787 ERR("Failed to allocate backend data.\n");
8788 return 0;
8791 shader_data = shader->backend_data;
8792 gl_shaders = shader_data->gl_shaders.ds;
8794 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8796 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8797 return gl_shaders[i].id;
8800 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8802 if (shader_data->num_gl_shaders)
8804 new_size = shader_data->shader_array_size + 1;
8805 new_array = realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8807 else
8809 new_array = malloc(sizeof(*new_array));
8810 new_size = 1;
8813 if (!new_array)
8815 ERR("Failed to allocate GL shaders array.\n");
8816 return 0;
8818 shader_data->gl_shaders.ds = new_array;
8819 shader_data->shader_array_size = new_size;
8820 gl_shaders = new_array;
8822 string_buffer_clear(&priv->shader_buffer);
8823 ret = shader_glsl_generate_domain_shader(context_gl, priv, shader, args);
8824 gl_shaders[shader_data->num_gl_shaders].args = *args;
8825 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8827 return ret;
8830 static GLuint find_glsl_geometry_shader(const struct wined3d_context_gl *context_gl,
8831 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8833 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8834 struct glsl_shader_private *shader_data;
8835 unsigned int i, new_size;
8836 GLuint ret;
8838 if (!shader->backend_data)
8840 if (!(shader->backend_data = calloc(1, sizeof(*shader_data))))
8842 ERR("Failed to allocate backend data.\n");
8843 return 0;
8846 shader_data = shader->backend_data;
8847 gl_shaders = shader_data->gl_shaders.gs;
8849 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8851 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8852 return gl_shaders[i].id;
8855 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8857 if (shader_data->num_gl_shaders)
8859 new_size = shader_data->shader_array_size + 1;
8860 new_array = realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8862 else
8864 new_array = malloc(sizeof(*new_array));
8865 new_size = 1;
8868 if (!new_array)
8870 ERR("Failed to allocate GL shaders array.\n");
8871 return 0;
8873 shader_data->gl_shaders.gs = new_array;
8874 shader_data->shader_array_size = new_size;
8875 gl_shaders = new_array;
8877 string_buffer_clear(&priv->shader_buffer);
8878 ret = shader_glsl_generate_geometry_shader(context_gl, priv, shader, args);
8879 gl_shaders[shader_data->num_gl_shaders].args = *args;
8880 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8882 return ret;
8885 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8887 switch (mcs)
8889 case WINED3D_MCS_MATERIAL:
8890 return material;
8891 case WINED3D_MCS_COLOR1:
8892 return "ffp_attrib_diffuse";
8893 case WINED3D_MCS_COLOR2:
8894 return "ffp_attrib_specular";
8895 default:
8896 ERR("Invalid material color source %#x.\n", mcs);
8897 return "<invalid>";
8901 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8902 const struct wined3d_ffp_vs_settings *settings, unsigned int idx, BOOL legacy_lighting)
8904 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8905 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8906 if (settings->localviewer)
8907 shader_addline(buffer, "t = dot(normal, ffp_normalize(dir - ffp_normalize(ec_pos.xyz)));\n");
8908 else
8909 shader_addline(buffer, "t = dot(normal, ffp_normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8910 if (settings->specular_enable)
8911 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0%s) specular +="
8912 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n",
8913 legacy_lighting ? " && ffp_material.shininess > 0.0" : "", idx);
8916 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8917 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8919 const char *diffuse, *specular, *emissive, *ambient;
8920 unsigned int i, idx;
8922 if (!settings->lighting)
8924 if (settings->diffuse)
8925 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8926 else
8927 shader_addline(buffer, "ffp_varying_diffuse = vec4(1.0);\n");
8928 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8929 return;
8932 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8933 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8934 shader_addline(buffer, "vec3 dir, dst;\n");
8935 shader_addline(buffer, "float att, t;\n");
8937 if (settings->specular_enable)
8938 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8940 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8941 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8942 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8943 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8945 idx = 0;
8946 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8948 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8949 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8950 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8951 shader_addline(buffer, "dst.x = 1.0;\n");
8952 if (legacy_lighting)
8954 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8955 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8956 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8958 else
8960 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8962 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8963 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8964 if (!legacy_lighting)
8965 shader_addline(buffer, "att = 1.0 / att;\n");
8966 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8967 if (!settings->normal)
8969 shader_addline(buffer, "}\n");
8970 continue;
8972 shader_addline(buffer, "dir = ffp_normalize(dir);\n");
8973 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8974 shader_addline(buffer, "}\n");
8977 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8979 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8980 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8981 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8982 shader_addline(buffer, "dst.x = 1.0;\n");
8983 if (legacy_lighting)
8985 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8986 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8987 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8989 else
8991 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8993 shader_addline(buffer, "dir = ffp_normalize(dir);\n");
8994 shader_addline(buffer, "t = dot(-dir, ffp_normalize(ffp_light[%u].direction));\n", idx);
8995 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8996 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8997 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8998 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8999 idx, idx, idx, idx);
9000 if (legacy_lighting)
9001 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
9002 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
9003 idx, idx, idx);
9004 else
9005 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
9006 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
9007 idx, idx, idx);
9008 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
9009 if (!settings->normal)
9011 shader_addline(buffer, "}\n");
9012 continue;
9014 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
9015 shader_addline(buffer, "}\n");
9018 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
9020 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
9021 if (!settings->normal)
9022 continue;
9023 shader_addline(buffer, "att = 1.0;\n");
9024 shader_addline(buffer, "dir = ffp_normalize(ffp_light[%u].direction.xyz);\n", idx);
9025 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
9028 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
9030 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
9031 if (!settings->normal)
9032 continue;
9033 shader_addline(buffer, "att = 1.0;\n");
9034 shader_addline(buffer, "dir = ffp_normalize(ffp_light[%u].position.xyz);\n", idx);
9035 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
9038 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
9039 ambient, diffuse, emissive);
9040 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
9041 if (settings->specular_enable)
9042 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
9043 else
9044 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
9047 /* Context activation is done by the caller. */
9048 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
9049 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
9051 static const struct attrib_info
9053 const char type[6];
9054 const char name[24];
9056 attrib_info[] =
9058 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
9059 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
9060 /* TODO: Indexed vertex blending */
9061 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
9062 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
9063 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
9064 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
9065 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
9067 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9068 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9069 BOOL output_legacy_fogcoord = legacy_syntax;
9070 BOOL legacy_lighting = priv->legacy_lighting;
9071 GLuint shader_obj;
9072 unsigned int i;
9074 string_buffer_clear(buffer);
9076 shader_glsl_add_version_declaration(buffer, gl_info);
9078 if (shader_glsl_use_explicit_attrib_location(gl_info))
9079 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9081 shader_addline(buffer, "invariant gl_Position;\n");
9083 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
9085 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
9087 if (shader_glsl_use_explicit_attrib_location(gl_info))
9088 shader_addline(buffer, "layout(location = %u) ", i);
9089 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
9091 shader_addline(buffer, "\n");
9093 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9094 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9095 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9096 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9098 shader_addline(buffer, "uniform struct\n{\n");
9099 shader_addline(buffer, " vec4 emissive;\n");
9100 shader_addline(buffer, " vec4 ambient;\n");
9101 shader_addline(buffer, " vec4 diffuse;\n");
9102 shader_addline(buffer, " vec4 specular;\n");
9103 shader_addline(buffer, " float shininess;\n");
9104 shader_addline(buffer, "} ffp_material;\n");
9106 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9107 shader_addline(buffer, "uniform struct\n{\n");
9108 shader_addline(buffer, " vec4 diffuse;\n");
9109 shader_addline(buffer, " vec4 specular;\n");
9110 shader_addline(buffer, " vec4 ambient;\n");
9111 shader_addline(buffer, " vec4 position;\n");
9112 shader_addline(buffer, " vec3 direction;\n");
9113 shader_addline(buffer, " float range;\n");
9114 shader_addline(buffer, " float falloff;\n");
9115 shader_addline(buffer, " float c_att;\n");
9116 shader_addline(buffer, " float l_att;\n");
9117 shader_addline(buffer, " float q_att;\n");
9118 shader_addline(buffer, " float cos_htheta;\n");
9119 shader_addline(buffer, " float cos_hphi;\n");
9120 shader_addline(buffer, "} ffp_light[%u];\n", WINED3D_MAX_ACTIVE_LIGHTS);
9122 if (settings->point_size)
9124 shader_addline(buffer, "uniform struct\n{\n");
9125 shader_addline(buffer, " float size;\n");
9126 shader_addline(buffer, " float size_min;\n");
9127 shader_addline(buffer, " float size_max;\n");
9128 shader_addline(buffer, " float c_att;\n");
9129 shader_addline(buffer, " float l_att;\n");
9130 shader_addline(buffer, " float q_att;\n");
9131 shader_addline(buffer, "} ffp_point;\n");
9134 if (legacy_syntax)
9136 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9137 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9138 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9139 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9141 else
9143 if (settings->clipping)
9144 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9146 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9147 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9148 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9149 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9152 shader_addline(buffer, "\nvec3 ffp_normalize(vec3 n)\n{\n");
9153 shader_addline(buffer, " float lensq = dot(n, n);\n");
9154 shader_addline(buffer, " return lensq == 0.0 ? n : (n * inversesqrt(lensq));\n");
9155 shader_addline(buffer, "}\n");
9157 shader_addline(buffer, "\nvoid main()\n{\n");
9158 shader_addline(buffer, "float m;\n");
9159 shader_addline(buffer, "vec3 r;\n");
9161 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9163 if (attrib_info[i].name[0])
9164 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9165 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9167 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
9169 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9170 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9171 && settings->texcoords & (1u << i))
9172 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9175 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9177 if (settings->transformed)
9179 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9180 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9181 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9183 else
9185 for (i = 0; i < settings->vertexblends; ++i)
9186 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9188 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9189 for (i = 0; i < settings->vertexblends + 1; ++i)
9190 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9192 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9193 if (settings->clipping)
9195 if (legacy_syntax)
9196 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9197 else
9198 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9199 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9201 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9204 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9205 if (settings->normal)
9207 if (!settings->vertexblends)
9209 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9211 else
9213 for (i = 0; i < settings->vertexblends + 1; ++i)
9214 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9217 if (settings->normalize)
9218 shader_addline(buffer, "normal = ffp_normalize(normal);\n");
9221 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9222 if (legacy_syntax)
9224 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9225 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9227 else
9229 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9230 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9233 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
9235 BOOL output_legacy_texcoord = legacy_syntax;
9237 switch (settings->texgen[i] & 0xffff0000)
9239 case WINED3DTSS_TCI_PASSTHRU:
9240 if (settings->texcoords & (1u << i))
9241 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9242 i, i, i);
9243 else if (shader_glsl_full_ffp_varyings(gl_info))
9244 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9245 else
9246 output_legacy_texcoord = FALSE;
9247 break;
9249 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9250 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9251 break;
9253 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9254 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9255 break;
9257 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9258 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9259 " * vec4(reflect(ffp_normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9260 break;
9262 case WINED3DTSS_TCI_SPHEREMAP:
9263 shader_addline(buffer, "r = reflect(ffp_normalize(ec_pos.xyz), normal);\n");
9264 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9265 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9266 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9267 break;
9269 default:
9270 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9271 break;
9273 if (output_legacy_texcoord)
9274 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9277 switch (settings->fog_mode)
9279 case WINED3D_FFP_VS_FOG_OFF:
9280 output_legacy_fogcoord = FALSE;
9281 break;
9283 case WINED3D_FFP_VS_FOG_FOGCOORD:
9284 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w;\n");
9285 break;
9287 case WINED3D_FFP_VS_FOG_RANGE:
9288 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9289 break;
9291 case WINED3D_FFP_VS_FOG_DEPTH:
9292 if (settings->ortho_fog)
9294 if (gl_info->supported[ARB_CLIP_CONTROL])
9295 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9296 else
9297 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9298 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9300 else if (settings->transformed)
9302 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9304 else
9306 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9308 break;
9310 default:
9311 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9312 break;
9314 if (output_legacy_fogcoord)
9315 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9317 if (settings->point_size)
9319 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9320 " + ffp_point.l_att * length(ec_pos.xyz)"
9321 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9322 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9323 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9326 shader_addline(buffer, "}\n");
9328 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9329 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9331 return shader_obj;
9334 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9335 unsigned int argnum, unsigned int stage, DWORD arg)
9337 const char *ret;
9339 if (arg == ARG_UNUSED)
9340 return "<unused arg>";
9342 switch (arg & WINED3DTA_SELECTMASK)
9344 case WINED3DTA_DIFFUSE:
9345 ret = "ffp_varying_diffuse";
9346 break;
9348 case WINED3DTA_CURRENT:
9349 ret = "ret";
9350 break;
9352 case WINED3DTA_TEXTURE:
9353 switch (stage)
9355 case 0: ret = "tex0"; break;
9356 case 1: ret = "tex1"; break;
9357 case 2: ret = "tex2"; break;
9358 case 3: ret = "tex3"; break;
9359 case 4: ret = "tex4"; break;
9360 case 5: ret = "tex5"; break;
9361 case 6: ret = "tex6"; break;
9362 case 7: ret = "tex7"; break;
9363 default:
9364 ret = "<invalid texture>";
9365 break;
9367 break;
9369 case WINED3DTA_TFACTOR:
9370 ret = "tex_factor";
9371 break;
9373 case WINED3DTA_SPECULAR:
9374 ret = "ffp_varying_specular";
9375 break;
9377 case WINED3DTA_TEMP:
9378 ret = "temp_reg";
9379 break;
9381 case WINED3DTA_CONSTANT:
9382 switch (stage)
9384 case 0: ret = "tss_const0"; break;
9385 case 1: ret = "tss_const1"; break;
9386 case 2: ret = "tss_const2"; break;
9387 case 3: ret = "tss_const3"; break;
9388 case 4: ret = "tss_const4"; break;
9389 case 5: ret = "tss_const5"; break;
9390 case 6: ret = "tss_const6"; break;
9391 case 7: ret = "tss_const7"; break;
9392 default:
9393 ret = "<invalid constant>";
9394 break;
9396 break;
9398 default:
9399 return "<unhandled arg>";
9402 if (arg & WINED3DTA_COMPLEMENT)
9404 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9405 if (argnum == 0)
9406 ret = "arg0";
9407 else if (argnum == 1)
9408 ret = "arg1";
9409 else if (argnum == 2)
9410 ret = "arg2";
9413 if (arg & WINED3DTA_ALPHAREPLICATE)
9415 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9416 if (argnum == 0)
9417 ret = "arg0";
9418 else if (argnum == 1)
9419 ret = "arg1";
9420 else if (argnum == 2)
9421 ret = "arg2";
9424 return ret;
9427 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9428 BOOL alpha, BOOL tmp_dst, unsigned int op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9430 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9432 if (color && alpha)
9433 dstmask = "";
9434 else if (color)
9435 dstmask = ".xyz";
9436 else
9437 dstmask = ".w";
9439 dstreg = tmp_dst ? "temp_reg" : "ret";
9441 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9442 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9443 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9445 switch (op)
9447 case WINED3D_TOP_DISABLE:
9448 break;
9450 case WINED3D_TOP_SELECT_ARG1:
9451 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9452 break;
9454 case WINED3D_TOP_SELECT_ARG2:
9455 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9456 break;
9458 case WINED3D_TOP_MODULATE:
9459 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9460 break;
9462 case WINED3D_TOP_MODULATE_4X:
9463 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9464 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9465 break;
9467 case WINED3D_TOP_MODULATE_2X:
9468 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9469 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9470 break;
9472 case WINED3D_TOP_ADD:
9473 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9474 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9475 break;
9477 case WINED3D_TOP_ADD_SIGNED:
9478 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9479 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9480 break;
9482 case WINED3D_TOP_ADD_SIGNED_2X:
9483 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9484 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9485 break;
9487 case WINED3D_TOP_SUBTRACT:
9488 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9489 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9490 break;
9492 case WINED3D_TOP_ADD_SMOOTH:
9493 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9494 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9495 break;
9497 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9498 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9499 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9500 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9501 break;
9503 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9504 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9505 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9506 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9507 break;
9509 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9510 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9511 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9512 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9513 break;
9515 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9516 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9517 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9518 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9519 break;
9521 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9522 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9523 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9524 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9525 break;
9527 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9528 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9529 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9530 break;
9532 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9533 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9534 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9535 break;
9537 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9538 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9539 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9540 break;
9541 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9542 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9543 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9544 break;
9546 case WINED3D_TOP_BUMPENVMAP:
9547 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9548 /* These are handled in the first pass, nothing to do. */
9549 break;
9551 case WINED3D_TOP_DOTPRODUCT3:
9552 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9553 dstreg, dstmask, arg1, arg2, dstmask);
9554 break;
9556 case WINED3D_TOP_MULTIPLY_ADD:
9557 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9558 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9559 break;
9561 case WINED3D_TOP_LERP:
9562 /* MSDN isn't quite right here. */
9563 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9564 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9565 break;
9567 default:
9568 FIXME("Unhandled operation %#x.\n", op);
9569 break;
9573 /* Context activation is done by the caller. */
9574 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9575 const struct ffp_frag_settings *settings, const struct wined3d_context_gl *context_gl)
9577 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9578 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9579 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9580 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9581 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
9582 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9583 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9584 GLuint shader_id;
9585 DWORD arg0, arg1, arg2;
9586 unsigned int stage;
9588 string_buffer_clear(buffer);
9590 /* Find out which textures are read */
9591 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES; ++stage)
9593 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9594 break;
9596 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9597 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9598 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9600 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9601 || (stage == 0 && settings->color_key_enabled))
9602 tex_map |= 1u << stage;
9603 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9604 tfactor_used = TRUE;
9605 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9606 tempreg_used = TRUE;
9607 if (settings->op[stage].tmp_dst)
9608 tempreg_used = TRUE;
9609 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9610 tss_const_map |= 1u << stage;
9612 switch (settings->op[stage].cop)
9614 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9615 lum_map |= 1u << stage;
9616 /* fall through */
9617 case WINED3D_TOP_BUMPENVMAP:
9618 bump_map |= 1u << stage;
9619 /* fall through */
9620 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9621 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9622 tex_map |= 1u << stage;
9623 break;
9625 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9626 tfactor_used = TRUE;
9627 break;
9629 default:
9630 break;
9633 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9634 continue;
9636 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9637 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9638 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9640 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9641 tex_map |= 1u << stage;
9642 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9643 tfactor_used = TRUE;
9644 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9645 tempreg_used = TRUE;
9646 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9647 tss_const_map |= 1u << stage;
9650 shader_glsl_add_version_declaration(buffer, gl_info);
9652 if (shader_glsl_use_explicit_attrib_location(gl_info))
9653 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9654 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9655 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9657 if (!use_legacy_fragment_output(gl_info))
9659 shader_addline(buffer, "vec4 ps_out[1];\n");
9660 if (shader_glsl_use_explicit_attrib_location(gl_info))
9661 shader_addline(buffer, "layout(location = 0) ");
9662 shader_addline(buffer, "out vec4 color_out0;\n");
9665 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9666 shader_addline(buffer, "vec4 ret;\n");
9667 if (tempreg_used || settings->sRGB_write)
9668 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9669 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9671 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES; ++stage)
9673 const char *sampler_type;
9675 if (tss_const_map & (1u << stage))
9676 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9678 if (!(tex_map & (1u << stage)))
9679 continue;
9681 switch (settings->op[stage].tex_type)
9683 case WINED3D_GL_RES_TYPE_TEX_1D:
9684 sampler_type = "1D";
9685 break;
9686 case WINED3D_GL_RES_TYPE_TEX_2D:
9687 sampler_type = "2D";
9688 break;
9689 case WINED3D_GL_RES_TYPE_TEX_3D:
9690 sampler_type = "3D";
9691 break;
9692 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9693 sampler_type = "Cube";
9694 break;
9695 default:
9696 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9697 sampler_type = NULL;
9698 break;
9700 if (sampler_type)
9702 /* We don't use bindless samplers for FFP shaders. */
9703 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9704 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
9705 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9708 shader_addline(buffer, "vec4 tex%u;\n", stage);
9710 if (!(bump_map & (1u << stage)))
9711 continue;
9712 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9714 if (!(lum_map & (1u << stage)))
9715 continue;
9716 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9717 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9719 if (tfactor_used)
9720 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9721 if (settings->color_key_enabled)
9722 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9723 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9725 if (settings->sRGB_write)
9727 shader_addline(buffer, "const vec4 srgb_const0 = ");
9728 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
9729 shader_addline(buffer, ";\n");
9730 shader_addline(buffer, "const vec4 srgb_const1 = ");
9731 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
9732 shader_addline(buffer, ";\n");
9735 shader_addline(buffer, "uniform struct\n{\n");
9736 shader_addline(buffer, " vec4 color;\n");
9737 shader_addline(buffer, " float density;\n");
9738 shader_addline(buffer, " float end;\n");
9739 shader_addline(buffer, " float scale;\n");
9740 shader_addline(buffer, "} ffp_fog;\n");
9742 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9743 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9745 if (legacy_syntax)
9747 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9748 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9749 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9750 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9751 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9753 else
9755 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9756 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9757 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9758 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9759 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9762 shader_addline(buffer, "void main()\n{\n");
9764 if (legacy_syntax)
9766 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9767 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9770 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES; ++stage)
9772 if (tex_map & (1u << stage))
9774 if (settings->pointsprite)
9775 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9776 else if (settings->texcoords_initialized & (1u << stage))
9777 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9778 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9779 else
9780 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9784 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9785 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9787 /* Generate texture sampling instructions */
9788 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9790 const char *texture_function, *coord_mask;
9791 BOOL proj;
9793 if (!(tex_map & (1u << stage)))
9794 continue;
9796 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9798 proj = FALSE;
9800 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9801 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9803 proj = TRUE;
9805 else
9807 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9808 proj = TRUE;
9811 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9812 proj = FALSE;
9814 switch (settings->op[stage].tex_type)
9816 case WINED3D_GL_RES_TYPE_TEX_1D:
9817 texture_function = "texture1D";
9818 coord_mask = "x";
9819 break;
9820 case WINED3D_GL_RES_TYPE_TEX_2D:
9821 texture_function = "texture2D";
9822 coord_mask = "xy";
9823 break;
9824 case WINED3D_GL_RES_TYPE_TEX_3D:
9825 texture_function = "texture3D";
9826 coord_mask = "xyz";
9827 break;
9828 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9829 texture_function = "textureCube";
9830 coord_mask = "xyz";
9831 break;
9832 default:
9833 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9834 texture_function = "";
9835 coord_mask = "xyzw";
9836 proj = FALSE;
9837 break;
9839 if (!legacy_syntax)
9840 texture_function = "texture";
9842 if (stage > 0
9843 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9844 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9846 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9848 /* With projective textures, texbem only divides the static
9849 * texture coordinate, not the displacement, so multiply the
9850 * displacement with the dividing parameter before passing it to
9851 * TXP. */
9852 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9854 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9856 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9857 stage, stage);
9858 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9860 else
9862 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9863 stage, stage);
9864 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9867 else
9869 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9872 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ret.%s%s);\n",
9873 stage, texture_function, proj ? "Proj" : "", stage, coord_mask, proj ? "w" : "");
9875 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9876 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9877 stage, stage - 1, stage - 1, stage - 1);
9879 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9881 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9882 stage, texture_function, proj ? "Proj" : "", stage, stage);
9884 else
9886 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].%s%s);\n",
9887 stage, texture_function, proj ? "Proj" : "", stage, stage, coord_mask, proj ? "w" : "");
9890 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9891 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9892 settings->op[stage].color_fixup);
9895 if (settings->color_key_enabled)
9896 shader_glsl_generate_colour_key_test(buffer, "tex0", "color_key[0]", "color_key[1]");
9898 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9900 /* Generate the main shader */
9901 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES; ++stage)
9903 BOOL op_equal;
9905 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9906 break;
9908 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9909 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9910 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9911 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9912 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9913 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9914 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9915 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9916 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9917 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9918 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9919 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9920 else
9921 op_equal = settings->op[stage].aop == settings->op[stage].cop
9922 && settings->op[stage].carg0 == settings->op[stage].aarg0
9923 && settings->op[stage].carg1 == settings->op[stage].aarg1
9924 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9926 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9928 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9929 settings->op[stage].cop, settings->op[stage].carg0,
9930 settings->op[stage].carg1, settings->op[stage].carg2);
9932 else if (op_equal)
9934 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9935 settings->op[stage].cop, settings->op[stage].carg0,
9936 settings->op[stage].carg1, settings->op[stage].carg2);
9938 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9939 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9941 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9942 settings->op[stage].cop, settings->op[stage].carg0,
9943 settings->op[stage].carg1, settings->op[stage].carg2);
9944 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9945 settings->op[stage].aop, settings->op[stage].aarg0,
9946 settings->op[stage].aarg1, settings->op[stage].aarg2);
9950 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9951 get_fragment_output(gl_info));
9953 if (settings->sRGB_write)
9954 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9956 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9958 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9959 if (!use_legacy_fragment_output(gl_info))
9960 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9962 shader_addline(buffer, "}\n");
9964 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9965 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9967 string_buffer_release(&priv->string_buffers, tex_reg_name);
9968 return shader_id;
9971 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9972 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9974 struct glsl_ffp_vertex_shader *shader;
9975 const struct wine_rb_entry *entry;
9977 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9978 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9980 if (!(shader = malloc(sizeof(*shader))))
9981 return NULL;
9983 shader->desc.settings = *settings;
9984 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9985 list_init(&shader->linked_programs);
9986 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9987 ERR("Failed to insert ffp vertex shader.\n");
9989 return shader;
9992 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9993 const struct ffp_frag_settings *args, const struct wined3d_context_gl *context_gl)
9995 struct glsl_ffp_fragment_shader *glsl_desc;
9996 const struct ffp_frag_desc *desc;
9998 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9999 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
10001 if (!(glsl_desc = malloc(sizeof(*glsl_desc))))
10002 return NULL;
10004 glsl_desc->entry.settings = *args;
10005 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context_gl);
10006 list_init(&glsl_desc->linked_programs);
10007 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
10009 return glsl_desc;
10013 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
10014 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
10016 unsigned int i;
10017 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10019 for (i = 0; i < vs_c_count; ++i)
10021 string_buffer_sprintf(name, "vs_c[%u]", i);
10022 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10024 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
10026 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10028 string_buffer_sprintf(name, "vs_i[%u]", i);
10029 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10032 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10034 string_buffer_sprintf(name, "vs_b[%u]", i);
10035 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10038 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10039 vs->base_vertex_id_location = GL_EXTCALL(glGetUniformLocation(program_id, "base_vertex_id"));
10041 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
10043 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
10044 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10046 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
10047 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
10048 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
10050 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
10051 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10053 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
10054 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
10055 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
10056 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
10057 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
10058 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
10059 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
10061 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
10062 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10063 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
10064 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10065 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
10066 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10067 string_buffer_sprintf(name, "ffp_light[%u].position", i);
10068 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10069 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
10070 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10071 string_buffer_sprintf(name, "ffp_light[%u].range", i);
10072 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10073 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
10074 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10075 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
10076 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10077 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
10078 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10079 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
10080 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10081 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
10082 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10083 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
10084 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10086 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10087 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10088 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10089 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10090 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10091 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10092 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10094 string_buffer_release(&priv->string_buffers, name);
10097 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10098 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10100 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10103 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10104 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10106 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10109 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10110 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10112 unsigned int i;
10113 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10115 for (i = 0; i < ps_c_count; ++i)
10117 string_buffer_sprintf(name, "ps_c[%u]", i);
10118 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10120 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10122 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10124 string_buffer_sprintf(name, "ps_i[%u]", i);
10125 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10128 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10130 string_buffer_sprintf(name, "ps_b[%u]", i);
10131 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10134 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
10136 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10137 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10138 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10139 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10140 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10141 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10142 string_buffer_sprintf(name, "tss_const%u", i);
10143 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10146 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10147 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10149 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10150 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10151 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10152 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10154 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10156 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10158 string_buffer_release(&priv->string_buffers, name);
10161 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10162 const struct wined3d_context_gl *context_gl, struct wined3d_shader *shader)
10164 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10165 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10166 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10167 struct glsl_cs_compiled_shader *gl_shaders;
10168 struct glsl_shader_private *shader_data;
10169 struct glsl_shader_prog_link *entry;
10170 GLuint shader_id, program_id;
10172 if (!(entry = malloc(sizeof(*entry))))
10174 ERR("Out of memory.\n");
10175 return E_OUTOFMEMORY;
10178 if (!(shader->backend_data = calloc(1, sizeof(*shader_data))))
10180 ERR("Failed to allocate backend data.\n");
10181 free(entry);
10182 return E_OUTOFMEMORY;
10184 shader_data = shader->backend_data;
10186 if (!(shader_data->gl_shaders.cs = malloc(sizeof(*gl_shaders))))
10188 ERR("Failed to allocate GL shader array.\n");
10189 free(entry);
10190 free(shader->backend_data);
10191 shader->backend_data = NULL;
10192 return E_OUTOFMEMORY;
10194 shader_data->shader_array_size = 1;
10195 gl_shaders = shader_data->gl_shaders.cs;
10197 TRACE("Compiling compute shader %p.\n", shader);
10199 string_buffer_clear(buffer);
10200 shader_id = shader_glsl_generate_compute_shader(context_gl, priv, shader);
10201 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10203 program_id = GL_EXTCALL(glCreateProgram());
10204 TRACE("Created new GLSL shader program %u.\n", program_id);
10206 entry->id = program_id;
10207 entry->vs.id = 0;
10208 entry->hs.id = 0;
10209 entry->ds.id = 0;
10210 entry->gs.id = 0;
10211 entry->ps.id = 0;
10212 entry->cs.id = shader_id;
10213 entry->constant_version = 0;
10214 entry->shader_controlled_clip_distances = 0;
10215 add_glsl_program_entry(priv, entry);
10217 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10218 GL_EXTCALL(glAttachShader(program_id, shader_id));
10219 checkGLcall("glAttachShader");
10221 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10223 TRACE("Linking GLSL shader program %u.\n", program_id);
10224 GL_EXTCALL(glLinkProgram(program_id));
10225 shader_glsl_validate_link(gl_info, program_id);
10227 GL_EXTCALL(glUseProgram(program_id));
10228 checkGLcall("glUseProgram");
10229 shader_glsl_load_program_resources(context_gl, priv, program_id, shader);
10230 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10232 entry->constant_update_mask = 0;
10234 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10235 checkGLcall("glUseProgram");
10236 return WINED3D_OK;
10239 static GLuint find_glsl_compute_shader(const struct wined3d_context_gl *context_gl,
10240 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10242 struct glsl_shader_private *shader_data;
10244 if (!shader->backend_data)
10246 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10247 if (FAILED(shader_glsl_compile_compute_shader(priv, context_gl, shader)))
10249 ERR("Failed to compile compute shader %p.\n", shader);
10250 return 0;
10253 shader_data = shader->backend_data;
10254 return shader_data->gl_shaders.cs[0].id;
10257 /* Context activation is done by the caller. */
10258 static void set_glsl_compute_shader_program(const struct wined3d_context_gl *context_gl,
10259 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10261 struct glsl_shader_prog_link *entry;
10262 struct wined3d_shader *shader;
10263 struct glsl_program_key key;
10264 GLuint cs_id;
10266 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10267 return;
10269 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10271 WARN("Compute shader is NULL.\n");
10272 ctx_data->glsl_program = NULL;
10273 return;
10276 cs_id = find_glsl_compute_shader(context_gl, priv, shader);
10277 memset(&key, 0, sizeof(key));
10278 key.cs_id = cs_id;
10279 if (!(entry = get_glsl_program_entry(priv, &key)))
10280 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10281 ctx_data->glsl_program = entry;
10284 /* Context activation is done by the caller. */
10285 static void set_glsl_shader_program(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
10286 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10288 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
10289 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10290 const struct wined3d_shader *pre_rasterization_shader;
10291 struct wined3d_shader *hshader, *dshader, *gshader;
10292 struct glsl_shader_prog_link *entry = NULL;
10293 struct wined3d_shader *vshader = NULL;
10294 struct wined3d_shader *pshader = NULL;
10295 GLuint reorder_shader_id = 0;
10296 struct glsl_program_key key;
10297 uint32_t attribs_map;
10298 GLuint program_id;
10299 unsigned int i;
10300 GLuint vs_id = 0;
10301 GLuint hs_id = 0;
10302 GLuint ds_id = 0;
10303 GLuint gs_id = 0;
10304 GLuint ps_id = 0;
10305 struct list *ps_list, *vs_list;
10306 struct wined3d_string_buffer *tmp_name;
10308 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10310 vs_id = ctx_data->glsl_program->vs.id;
10311 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10313 if (use_vs(state))
10314 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10316 else if (use_vs(state))
10318 struct vs_compile_args vs_compile_args;
10320 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10322 find_vs_compile_args(state, vshader, &vs_compile_args, &context_gl->c);
10323 vs_id = find_glsl_vertex_shader(context_gl, priv, vshader, &vs_compile_args);
10324 vs_list = &vshader->linked_programs;
10326 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10328 struct glsl_ffp_vertex_shader *ffp_shader;
10329 struct wined3d_ffp_vs_settings settings;
10331 wined3d_ffp_get_vs_settings(&context_gl->c, state, &settings);
10332 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10333 vs_id = ffp_shader->id;
10334 vs_list = &ffp_shader->linked_programs;
10337 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10338 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10339 hs_id = ctx_data->glsl_program->hs.id;
10340 else if (hshader)
10341 hs_id = find_glsl_hull_shader(context_gl, priv, hshader);
10343 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10344 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10346 ds_id = ctx_data->glsl_program->ds.id;
10348 else if (dshader)
10350 struct ds_compile_args args;
10352 find_ds_compile_args(state, dshader, &args, &context_gl->c);
10353 ds_id = find_glsl_domain_shader(context_gl, priv, dshader, &args);
10356 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10357 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10359 gs_id = ctx_data->glsl_program->gs.id;
10361 else if (gshader)
10363 struct gs_compile_args args;
10365 find_gs_compile_args(state, gshader, &args, &context_gl->c);
10366 gs_id = find_glsl_geometry_shader(context_gl, priv, gshader, &args);
10369 /* A pixel shader is not used when rasterization is disabled. */
10370 if (is_rasterization_disabled(gshader))
10372 ps_id = 0;
10373 ps_list = NULL;
10375 else if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10377 ps_id = ctx_data->glsl_program->ps.id;
10378 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10380 if (use_ps(state))
10381 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10383 else if (use_ps(state))
10385 struct ps_compile_args ps_compile_args;
10386 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10387 find_ps_compile_args(state, pshader, context_gl->c.stream_info.position_transformed,
10388 &ps_compile_args, &context_gl->c);
10389 ps_id = find_glsl_fragment_shader(context_gl, priv, pshader, &ps_compile_args);
10390 ps_list = &pshader->linked_programs;
10392 else if (priv->fragment_pipe == &glsl_fragment_pipe
10393 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10395 struct glsl_ffp_fragment_shader *ffp_shader;
10396 struct ffp_frag_settings settings;
10398 wined3d_ffp_get_fs_settings(&context_gl->c, state, &settings);
10399 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context_gl);
10400 ps_id = ffp_shader->id;
10401 ps_list = &ffp_shader->linked_programs;
10404 key.vs_id = vs_id;
10405 key.hs_id = hs_id;
10406 key.ds_id = ds_id;
10407 key.gs_id = gs_id;
10408 key.ps_id = ps_id;
10409 key.cs_id = 0;
10410 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10412 ctx_data->glsl_program = entry;
10413 return;
10416 /* If we get to this point, then no matching program exists, so we create one */
10417 program_id = GL_EXTCALL(glCreateProgram());
10418 TRACE("Created new GLSL shader program %u.\n", program_id);
10420 /* Create the entry */
10421 entry = malloc(sizeof(*entry));
10422 entry->id = program_id;
10423 entry->vs.id = vs_id;
10424 entry->hs.id = hs_id;
10425 entry->ds.id = ds_id;
10426 entry->gs.id = gs_id;
10427 entry->ps.id = ps_id;
10428 entry->cs.id = 0;
10429 entry->constant_version = 0;
10430 entry->shader_controlled_clip_distances = 0;
10431 /* Add the hash table entry */
10432 add_glsl_program_entry(priv, entry);
10434 /* Set the current program */
10435 ctx_data->glsl_program = entry;
10437 /* Attach GLSL vshader */
10438 if (vs_id)
10440 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10441 GL_EXTCALL(glAttachShader(program_id, vs_id));
10442 checkGLcall("glAttachShader");
10444 list_add_head(vs_list, &entry->vs.shader_entry);
10447 if (vshader)
10449 attribs_map = vshader->reg_maps.input_registers;
10450 if (vshader->reg_maps.shader_version.major < 4)
10452 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10453 state->primitive_type == WINED3D_PT_POINTLIST && vshader->reg_maps.point_size,
10454 d3d_info->emulated_flatshading
10455 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10456 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10457 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10458 checkGLcall("glAttachShader");
10459 /* Flag the reorder function for deletion, it will be freed
10460 * automatically when the program is destroyed. */
10461 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10464 else
10466 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10469 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10471 /* Bind vertex attributes to a corresponding index number to match
10472 * the same index numbers as ARB_vertex_programs (makes loading
10473 * vertex attributes simpler). With this method, we can use the
10474 * exact same code to load the attributes later for both ARB and
10475 * GLSL shaders.
10477 * We have to do this here because we need to know the Program ID
10478 * in order to make the bindings work, and it has to be done prior
10479 * to linking the GLSL program. */
10480 tmp_name = string_buffer_get(&priv->string_buffers);
10481 while (attribs_map)
10483 i = wined3d_bit_scan(&attribs_map);
10484 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10485 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10486 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10488 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10489 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10490 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10491 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10494 checkGLcall("glBindAttribLocation");
10496 if (!use_legacy_fragment_output(gl_info))
10498 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
10500 string_buffer_sprintf(tmp_name, "color_out%u", i);
10501 if (state->blend_state && state->blend_state->dual_source)
10502 GL_EXTCALL(glBindFragDataLocationIndexed(program_id, 0, i, tmp_name->buffer));
10503 else
10504 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10505 checkGLcall("glBindFragDataLocation");
10508 string_buffer_release(&priv->string_buffers, tmp_name);
10511 if (hshader)
10513 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10514 GL_EXTCALL(glAttachShader(program_id, hs_id));
10515 checkGLcall("glAttachShader");
10517 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10520 if (dshader)
10522 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10523 GL_EXTCALL(glAttachShader(program_id, ds_id));
10524 checkGLcall("glAttachShader");
10526 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10529 if (gshader)
10531 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10532 GL_EXTCALL(glAttachShader(program_id, gs_id));
10533 checkGLcall("glAttachShader");
10535 shader_glsl_init_transform_feedback(context_gl, priv, program_id, gshader);
10537 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10540 /* Attach GLSL pshader */
10541 if (ps_id)
10543 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10544 GL_EXTCALL(glAttachShader(program_id, ps_id));
10545 checkGLcall("glAttachShader");
10547 list_add_head(ps_list, &entry->ps.shader_entry);
10550 /* Link the program */
10551 TRACE("Linking GLSL shader program %u.\n", program_id);
10552 GL_EXTCALL(glLinkProgram(program_id));
10553 shader_glsl_validate_link(gl_info, program_id);
10555 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10556 vshader ? vshader->limits->constant_float : 0);
10557 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10558 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10559 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10560 pshader ? pshader->limits->constant_float : 0);
10561 checkGLcall("find glsl program uniform locations");
10563 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10564 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10566 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10567 entry->shader_controlled_clip_distances = 1;
10568 entry->clip_distance_mask = wined3d_mask_from_size(clip_distance_count);
10571 if (needs_legacy_glsl_syntax(gl_info))
10573 if (pshader && pshader->reg_maps.shader_version.major >= 3
10574 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10576 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10577 entry->vs.vertex_color_clamp = GL_FALSE;
10579 else
10581 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10584 else
10586 /* With core profile we never change vertex_color_clamp from
10587 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10588 * glClampColorARB(). */
10589 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10592 /* Set the shader to allow uniform loading on it */
10593 GL_EXTCALL(glUseProgram(program_id));
10594 checkGLcall("glUseProgram");
10596 entry->constant_update_mask = 0;
10597 if (vshader)
10599 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10600 if (vshader->reg_maps.integer_constants)
10601 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10602 if (vshader->reg_maps.boolean_constants)
10603 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10604 if (entry->vs.pos_fixup_location != -1)
10605 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10606 if (entry->vs.base_vertex_id_location != -1)
10607 entry->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
10609 shader_glsl_load_program_resources(context_gl, priv, program_id, vshader);
10611 else
10613 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10614 | WINED3D_SHADER_CONST_FFP_PROJ | WINED3D_SHADER_CONST_FFP_LIGHTS;
10616 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10618 if (entry->vs.modelview_matrix_location[i] != -1)
10620 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10621 break;
10625 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
10627 if (entry->vs.texture_matrix_location[i] != -1)
10629 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10630 break;
10633 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10634 || entry->vs.material_specular_location != -1
10635 || entry->vs.material_emissive_location != -1
10636 || entry->vs.material_shininess_location != -1)
10637 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10639 if (entry->vs.clip_planes_location != -1)
10640 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10641 if (entry->vs.pointsize_min_location != -1)
10642 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10644 if (hshader)
10645 shader_glsl_load_program_resources(context_gl, priv, program_id, hshader);
10647 if (dshader)
10649 if (entry->ds.pos_fixup_location != -1)
10650 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10652 shader_glsl_load_program_resources(context_gl, priv, program_id, dshader);
10655 if (gshader)
10657 if (entry->gs.pos_fixup_location != -1)
10658 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10660 shader_glsl_load_program_resources(context_gl, priv, program_id, gshader);
10663 if (ps_id)
10665 if (pshader)
10667 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10668 if (pshader->reg_maps.integer_constants)
10669 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10670 if (pshader->reg_maps.boolean_constants)
10671 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10673 shader_glsl_load_program_resources(context_gl, priv, program_id, pshader);
10674 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10676 else
10678 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10680 shader_glsl_load_samplers(&context_gl->c, priv, program_id, NULL);
10683 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
10685 if (entry->ps.bumpenv_mat_location[i] != -1)
10687 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10688 break;
10692 if (entry->ps.fog_color_location != -1)
10693 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10694 if (entry->ps.alpha_test_ref_location != -1)
10695 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10696 if (entry->ps.color_key_location != -1)
10697 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10701 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10703 struct wined3d_device *device = shader->device;
10704 struct wined3d_context *context;
10706 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10708 context = context_acquire(device, NULL, 0);
10709 shader_glsl_compile_compute_shader(shader_priv, wined3d_context_gl(context), shader);
10710 context_release(context);
10714 /* Context activation is done by the caller. */
10715 static void shader_glsl_update_graphics_program(struct shader_glsl_priv *priv,
10716 struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
10718 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10719 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10720 struct glsl_shader_prog_link *glsl_program;
10721 GLenum current_vertex_color_clamp;
10722 GLuint program_id, prev_id;
10724 priv->vertex_pipe->vp_apply_draw_state(&context_gl->c, state);
10725 priv->fragment_pipe->fp_apply_draw_state(&context_gl->c, state);
10727 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10728 set_glsl_shader_program(context_gl, state, priv, ctx_data);
10729 glsl_program = ctx_data->glsl_program;
10731 if (glsl_program)
10733 program_id = glsl_program->id;
10734 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10735 if (glsl_program->shader_controlled_clip_distances)
10736 wined3d_context_gl_enable_clip_distances(context_gl, glsl_program->clip_distance_mask);
10738 else
10740 program_id = 0;
10741 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10744 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10746 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10747 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10749 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10750 checkGLcall("glClampColorARB");
10752 else
10754 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10758 TRACE("Using GLSL program %u.\n", program_id);
10760 if (prev_id != program_id)
10762 GL_EXTCALL(glUseProgram(program_id));
10763 checkGLcall("glUseProgram");
10765 if (glsl_program)
10766 context_gl->c.constant_update_mask |= glsl_program->constant_update_mask;
10769 context_gl->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10772 static void shader_glsl_load_bindless_samplers(struct shader_glsl_priv *priv, struct wined3d_context_gl *context_gl,
10773 const struct wined3d_state *state, enum wined3d_shader_type shader_type)
10775 const struct wined3d_device_gl *device_gl = wined3d_device_gl(context_gl->c.device);
10776 const struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10777 const struct wined3d_shader *shader = state->shader[shader_type];
10778 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10779 const char *prefix = shader_glsl_get_prefix(shader_type);
10780 struct wined3d_string_buffer *sampler_name;
10782 /* Note that we don't use bindless samplers for FFP shaders. */
10783 if (!shader)
10784 return;
10786 sampler_name = string_buffer_get(&priv->string_buffers);
10788 for (unsigned int i = 0; i < shader->reg_maps.sampler_map.count; ++i)
10790 const struct wined3d_shader_sampler_map_entry *entry = &shader->reg_maps.sampler_map.entries[i];
10791 struct wined3d_shader_resource_view *view;
10792 struct wined3d_sampler *sampler;
10793 GLuint64 handle;
10794 GLint name_loc;
10796 /* No need to bother with the texture unit map; we're binding directly to uniforms. */
10798 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, entry->bind_idx);
10799 name_loc = GL_EXTCALL(glGetUniformLocation(ctx_data->glsl_program->id, sampler_name->buffer));
10800 if (name_loc == -1)
10801 continue;
10803 if ((view = state->shader_resource_view[shader_type][entry->resource_idx]))
10805 if (entry->sampler_idx == WINED3D_SAMPLER_DEFAULT)
10806 sampler = device_gl->d.default_sampler;
10807 else if (!(sampler = state->sampler[shader_type][entry->sampler_idx]))
10808 sampler = device_gl->d.null_sampler;
10810 handle = wined3d_shader_resource_view_gl_get_bindless_handle(
10811 wined3d_shader_resource_view_gl(view), wined3d_sampler_gl(sampler), context_gl);
10813 else
10815 WARN("No resource view bound at index %u, %u.\n", shader_type, entry->resource_idx);
10816 handle = wined3d_device_gl_get_dummy_bindless_handle(device_gl,
10817 shader->reg_maps.resource_info[entry->resource_idx].type);
10819 GL_EXTCALL(glUniformHandleui64ARB(name_loc, handle));
10820 checkGLcall("glUniformHandleui64ARB");
10823 string_buffer_release(&priv->string_buffers, sampler_name);
10826 static void shader_glsl_apply_draw_state(void *shader_priv, struct wined3d_context *context,
10827 const struct wined3d_state *state)
10829 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10830 struct shader_glsl_priv *priv = shader_priv;
10832 if (context->shader_update_mask & ~(1u << WINED3D_SHADER_TYPE_COMPUTE))
10833 shader_glsl_update_graphics_program(priv, context_gl, state);
10835 if (context->constant_update_mask)
10836 shader_glsl_load_constants(priv, context, state);
10838 if (context->update_shader_resource_bindings && context_gl->gl_info->supported[ARB_BINDLESS_TEXTURE])
10840 for (unsigned int type = 0; type < WINED3D_SHADER_TYPE_GRAPHICS_COUNT; ++type)
10841 shader_glsl_load_bindless_samplers(priv, context_gl, state, type);
10845 static void shader_glsl_update_compute_program(struct shader_glsl_priv *priv,
10846 struct wined3d_context_gl *context_gl, const struct wined3d_state *state)
10848 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10849 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10850 GLuint program_id, prev_id;
10852 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10853 set_glsl_compute_shader_program(context_gl, state, priv, ctx_data);
10854 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10856 TRACE("Using GLSL program %u.\n", program_id);
10858 if (prev_id != program_id)
10860 GL_EXTCALL(glUseProgram(program_id));
10861 checkGLcall("glUseProgram");
10864 context_gl->c.shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10865 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10866 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10867 | (1u << WINED3D_SHADER_TYPE_HULL)
10868 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10871 static void shader_glsl_apply_compute_state(void *shader_priv, struct wined3d_context *context,
10872 const struct wined3d_state *state)
10874 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10875 struct shader_glsl_priv *priv = shader_priv;
10877 if (context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE))
10878 shader_glsl_update_compute_program(priv, context_gl, state);
10880 if (context->update_compute_shader_resource_bindings && context_gl->gl_info->supported[ARB_BINDLESS_TEXTURE])
10881 shader_glsl_load_bindless_samplers(priv, context_gl, state, WINED3D_SHADER_TYPE_COMPUTE);
10884 /* "context" is not necessarily the currently active context. */
10885 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10887 struct glsl_context_data *ctx_data = context->shader_backend_data;
10889 ctx_data->glsl_program = NULL;
10890 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10891 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10892 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10893 | (1u << WINED3D_SHADER_TYPE_HULL)
10894 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10895 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10898 /* Context activation is done by the caller. */
10899 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10901 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10902 struct glsl_context_data *ctx_data = context->shader_backend_data;
10903 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10904 struct shader_glsl_priv *priv = shader_priv;
10906 shader_glsl_invalidate_current_program(context);
10907 GL_EXTCALL(glUseProgram(0));
10908 checkGLcall("glUseProgram");
10910 priv->vertex_pipe->vp_disable(context);
10911 priv->fragment_pipe->fp_disable(context);
10913 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10915 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10916 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10917 checkGLcall("glClampColorARB");
10921 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10922 const struct glsl_shader_prog_link *program)
10924 const struct glsl_context_data *ctx_data;
10925 struct wined3d_context *context;
10926 unsigned int i;
10928 for (i = 0; i < device->context_count; ++i)
10930 context = device->contexts[i];
10931 ctx_data = context->shader_backend_data;
10933 if (ctx_data->glsl_program == program)
10934 shader_glsl_invalidate_current_program(context);
10938 static void shader_glsl_destroy(struct wined3d_shader *shader)
10940 struct glsl_shader_private *shader_data = shader->backend_data;
10941 struct wined3d_device *device = shader->device;
10942 struct shader_glsl_priv *priv = device->shader_priv;
10943 const struct wined3d_gl_info *gl_info;
10944 const struct list *linked_programs;
10945 struct wined3d_context *context;
10947 if (!shader_data || !shader_data->num_gl_shaders)
10949 free(shader_data);
10950 shader->backend_data = NULL;
10951 return;
10954 context = context_acquire(device, NULL, 0);
10955 gl_info = wined3d_context_gl(context)->gl_info;
10957 TRACE("Deleting linked programs.\n");
10958 linked_programs = &shader->linked_programs;
10959 if (!list_empty(linked_programs))
10961 struct glsl_shader_prog_link *entry, *entry2;
10962 UINT i;
10964 switch (shader->reg_maps.shader_version.type)
10966 case WINED3D_SHADER_TYPE_PIXEL:
10968 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10970 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10972 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10973 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10974 checkGLcall("glDeleteShader");
10976 free(shader_data->gl_shaders.ps);
10978 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10979 struct glsl_shader_prog_link, ps.shader_entry)
10981 shader_glsl_invalidate_contexts_program(device, entry);
10982 delete_glsl_program_entry(priv, gl_info, entry);
10985 break;
10988 case WINED3D_SHADER_TYPE_VERTEX:
10990 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10992 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10994 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10995 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10996 checkGLcall("glDeleteShader");
10998 free(shader_data->gl_shaders.vs);
11000 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11001 struct glsl_shader_prog_link, vs.shader_entry)
11003 shader_glsl_invalidate_contexts_program(device, entry);
11004 delete_glsl_program_entry(priv, gl_info, entry);
11007 break;
11010 case WINED3D_SHADER_TYPE_HULL:
11012 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
11014 for (i = 0; i < shader_data->num_gl_shaders; ++i)
11016 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
11017 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
11018 checkGLcall("glDeleteShader");
11020 free(shader_data->gl_shaders.hs);
11022 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11023 struct glsl_shader_prog_link, hs.shader_entry)
11025 shader_glsl_invalidate_contexts_program(device, entry);
11026 delete_glsl_program_entry(priv, gl_info, entry);
11029 break;
11032 case WINED3D_SHADER_TYPE_DOMAIN:
11034 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
11036 for (i = 0; i < shader_data->num_gl_shaders; ++i)
11038 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
11039 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
11040 checkGLcall("glDeleteShader");
11042 free(shader_data->gl_shaders.ds);
11044 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11045 struct glsl_shader_prog_link, ds.shader_entry)
11047 shader_glsl_invalidate_contexts_program(device, entry);
11048 delete_glsl_program_entry(priv, gl_info, entry);
11051 break;
11054 case WINED3D_SHADER_TYPE_GEOMETRY:
11056 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
11058 for (i = 0; i < shader_data->num_gl_shaders; ++i)
11060 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
11061 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
11062 checkGLcall("glDeleteShader");
11064 free(shader_data->gl_shaders.gs);
11066 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11067 struct glsl_shader_prog_link, gs.shader_entry)
11069 shader_glsl_invalidate_contexts_program(device, entry);
11070 delete_glsl_program_entry(priv, gl_info, entry);
11073 break;
11076 case WINED3D_SHADER_TYPE_COMPUTE:
11078 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
11080 for (i = 0; i < shader_data->num_gl_shaders; ++i)
11082 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
11083 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
11084 checkGLcall("glDeleteShader");
11086 free(shader_data->gl_shaders.cs);
11088 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
11089 struct glsl_shader_prog_link, cs.shader_entry)
11091 shader_glsl_invalidate_contexts_program(device, entry);
11092 delete_glsl_program_entry(priv, gl_info, entry);
11095 break;
11098 default:
11099 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
11100 break;
11104 free(shader->backend_data);
11105 shader->backend_data = NULL;
11107 context_release(context);
11110 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
11112 const struct glsl_program_key *k = key;
11113 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
11114 const struct glsl_shader_prog_link, program_lookup_entry);
11115 int ret;
11117 if ((ret = wined3d_uint32_compare(k->vs_id, prog->vs.id)))
11118 return ret;
11119 if ((ret = wined3d_uint32_compare(k->gs_id, prog->gs.id)))
11120 return ret;
11121 if ((ret = wined3d_uint32_compare(k->ps_id, prog->ps.id)))
11122 return ret;
11123 if ((ret = wined3d_uint32_compare(k->hs_id, prog->hs.id)))
11124 return ret;
11125 if ((ret = wined3d_uint32_compare(k->ds_id, prog->ds.id)))
11126 return ret;
11127 if ((ret = wined3d_uint32_compare(k->cs_id, prog->cs.id)))
11128 return ret;
11130 return 0;
11133 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
11135 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
11136 + constant_count * sizeof(*heap->contained)
11137 + constant_count * sizeof(*heap->positions);
11138 void *mem;
11140 if (!(mem = malloc(size)))
11142 ERR("Failed to allocate memory\n");
11143 return FALSE;
11146 heap->entries = mem;
11147 heap->entries[1].version = 0;
11148 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11149 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11150 heap->positions = (unsigned int *)(heap->contained + constant_count);
11151 heap->size = 1;
11153 return TRUE;
11156 static void constant_free(struct constant_heap *heap)
11158 free(heap->entries);
11161 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11162 const struct wined3d_fragment_pipe_ops *fragment_pipe)
11164 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11165 void *vertex_priv, *fragment_priv;
11166 struct shader_glsl_priv *priv;
11168 if (!(priv = calloc(1, sizeof(*priv))))
11169 return E_OUTOFMEMORY;
11171 if (wined3d_settings.shader_backend == WINED3D_SHADER_BACKEND_GLSL_VKD3D)
11173 TRACE("Using %s.\n", vkd3d_shader_get_version(NULL, NULL));
11174 priv->use_vkd3d = true;
11177 string_buffer_list_init(&priv->string_buffers);
11179 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11181 ERR("Failed to initialize vertex pipe.\n");
11182 free(priv);
11183 return E_FAIL;
11186 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11188 ERR("Failed to initialize fragment pipe.\n");
11189 vertex_pipe->vp_free(device, NULL);
11190 free(priv);
11191 return E_FAIL;
11194 if (!string_buffer_init(&priv->shader_buffer))
11196 ERR("Failed to initialize shader buffer.\n");
11197 goto fail;
11200 if (!(priv->stack = calloc(stack_size, sizeof(*priv->stack))))
11202 ERR("Failed to allocate memory.\n");
11203 goto fail;
11206 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11208 ERR("Failed to initialize vertex shader constant heap\n");
11209 goto fail;
11212 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11214 ERR("Failed to initialize pixel shader constant heap\n");
11215 goto fail;
11218 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11220 priv->next_constant_version = 1;
11221 priv->vertex_pipe = vertex_pipe;
11222 priv->fragment_pipe = fragment_pipe;
11223 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11225 device->vertex_priv = vertex_priv;
11226 device->fragment_priv = fragment_priv;
11227 device->shader_priv = priv;
11229 return WINED3D_OK;
11231 fail:
11232 constant_free(&priv->pconst_heap);
11233 constant_free(&priv->vconst_heap);
11234 free(priv->stack);
11235 string_buffer_free(&priv->shader_buffer);
11236 fragment_pipe->free_private(device, NULL);
11237 vertex_pipe->vp_free(device, NULL);
11238 free(priv);
11239 return E_OUTOFMEMORY;
11242 /* Context activation is done by the caller. */
11243 static void shader_glsl_free(struct wined3d_device *device, struct wined3d_context *context)
11245 struct shader_glsl_priv *priv = device->shader_priv;
11247 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11248 constant_free(&priv->pconst_heap);
11249 constant_free(&priv->vconst_heap);
11250 free(priv->stack);
11251 string_buffer_list_cleanup(&priv->string_buffers);
11252 string_buffer_free(&priv->shader_buffer);
11253 priv->fragment_pipe->free_private(device, context);
11254 priv->vertex_pipe->vp_free(device, context);
11256 free(device->shader_priv);
11257 device->shader_priv = NULL;
11260 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11262 struct glsl_context_data *ctx_data;
11264 if (!(ctx_data = calloc(1, sizeof(*ctx_data))))
11265 return FALSE;
11266 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11267 context->shader_backend_data = ctx_data;
11268 return TRUE;
11271 static void shader_glsl_free_context_data(struct wined3d_context *context)
11273 free(context->shader_backend_data);
11276 static void shader_glsl_init_context_state(struct wined3d_context *context)
11278 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11279 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11281 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11282 checkGLcall("GL_PROGRAM_POINT_SIZE");
11285 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11287 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11288 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11289 && gl_info->supported[ARB_TEXTURE_SWIZZLE]
11290 && gl_info->supported[EXT_SHADER_INTEGER_MIX];
11292 if (shader_model_4
11293 && gl_info->supported[ARB_COMPUTE_SHADER]
11294 && gl_info->supported[ARB_CULL_DISTANCE]
11295 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11296 && gl_info->supported[ARB_GPU_SHADER5]
11297 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11298 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11299 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11300 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11301 && gl_info->supported[ARB_TESSELLATION_SHADER]
11302 && gl_info->supported[ARB_TEXTURE_GATHER]
11303 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11304 return 5;
11306 if (shader_model_4)
11307 return 4;
11309 /* Support for texldd and texldl instructions in pixel shaders is required
11310 * for SM3. */
11311 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11312 return 3;
11314 return 2;
11317 static void shader_glsl_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
11319 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl_const(adapter)->gl_info;
11320 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11322 TRACE("Shader model %u.\n", shader_model);
11324 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11325 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11326 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11327 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11328 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11329 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11331 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11332 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11334 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11335 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11336 caps->varying_count = gl_info->limits.glsl_varyings;
11338 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11339 * Direct3D minimum requirement.
11341 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11342 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11344 * The problem is that the refrast clamps temporary results in the shader to
11345 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11346 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11347 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11348 * offer a way to query this.
11350 if (shader_model >= 4)
11351 caps->ps_1x_max_value = FLT_MAX;
11352 else
11353 caps->ps_1x_max_value = 1024.0f;
11355 /* Ideally we'd only set caps like sRGB writes here if supported by both
11356 * the shader backend and the fragment pipe, but we can get called before
11357 * shader_glsl_alloc(). */
11358 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
11359 caps->wined3d_caps |= WINED3D_SHADER_CAP_OUTPUT_INTERPOLATION;
11360 if (shader_glsl_full_ffp_varyings(gl_info))
11361 caps->wined3d_caps |= WINED3D_SHADER_CAP_FULL_FFP_VARYINGS;
11364 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11366 /* We support everything except YUV conversions. */
11367 return !is_complex_fixup(fixup);
11370 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11372 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11373 /* WINED3DSIH_ADD */ shader_glsl_binop,
11374 /* WINED3DSIH_AND */ shader_glsl_binop,
11375 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11376 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11377 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11378 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11379 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11380 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11381 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11382 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11383 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11384 /* WINED3DSIH_BEM */ shader_glsl_bem,
11385 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11386 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11387 /* WINED3DSIH_BREAK */ shader_glsl_break,
11388 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11389 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11390 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11391 /* WINED3DSIH_CALL */ shader_glsl_call,
11392 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11393 /* WINED3DSIH_CASE */ shader_glsl_case,
11394 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11395 /* WINED3DSIH_CND */ shader_glsl_cnd,
11396 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11397 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11398 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11399 /* WINED3DSIH_CRS */ shader_glsl_cross,
11400 /* WINED3DSIH_CUT */ shader_glsl_cut,
11401 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11402 /* WINED3DSIH_DCL */ shader_glsl_nop,
11403 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11404 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11405 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11406 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11407 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11408 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11409 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11410 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11411 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11412 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11413 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11414 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11415 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11416 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11417 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11418 /* WINED3DSIH_DCL_INPUT_PS_SGV */ shader_glsl_nop,
11419 /* WINED3DSIH_DCL_INPUT_PS_SIV */ shader_glsl_nop,
11420 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11421 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11422 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11423 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11424 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11425 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11426 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11427 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11428 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11429 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11430 /* WINED3DSIH_DCL_STREAM */ NULL,
11431 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11432 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11433 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11434 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11435 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11436 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11437 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11438 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11439 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11440 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11441 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11442 /* WINED3DSIH_DEF */ shader_glsl_nop,
11443 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11444 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11445 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11446 /* WINED3DSIH_DIV */ shader_glsl_binop,
11447 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11448 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11449 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11450 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11451 /* WINED3DSIH_DST */ shader_glsl_dst,
11452 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11453 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11454 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11455 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11456 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11457 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11458 /* WINED3DSIH_ELSE */ shader_glsl_else,
11459 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11460 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11461 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11462 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11463 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11464 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11465 /* WINED3DSIH_EQ */ shader_glsl_relop,
11466 /* WINED3DSIH_EVAL_CENTROID */ NULL,
11467 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ shader_glsl_interpolate,
11468 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11469 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11470 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11471 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11472 /* WINED3DSIH_FCALL */ NULL,
11473 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11474 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11475 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11476 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11477 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11478 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11479 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11480 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11481 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11482 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11483 /* WINED3DSIH_GE */ shader_glsl_relop,
11484 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11485 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11486 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11487 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11488 /* WINED3DSIH_IADD */ shader_glsl_binop,
11489 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11490 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11491 /* WINED3DSIH_IF */ shader_glsl_if,
11492 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11493 /* WINED3DSIH_IGE */ shader_glsl_relop,
11494 /* WINED3DSIH_ILT */ shader_glsl_relop,
11495 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11496 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11497 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11498 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11499 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11500 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11501 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11502 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11503 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11504 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11505 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11506 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11507 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11508 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11509 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11510 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11511 /* WINED3DSIH_INE */ shader_glsl_relop,
11512 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11513 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11514 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11515 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11516 /* WINED3DSIH_LABEL */ shader_glsl_label,
11517 /* WINED3DSIH_LD */ shader_glsl_ld,
11518 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11519 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11520 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11521 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11522 /* WINED3DSIH_LIT */ shader_glsl_lit,
11523 /* WINED3DSIH_LOD */ NULL,
11524 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11525 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11526 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11527 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11528 /* WINED3DSIH_LT */ shader_glsl_relop,
11529 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11530 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11531 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11532 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11533 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11534 /* WINED3DSIH_MAD */ shader_glsl_mad,
11535 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11536 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11537 /* WINED3DSIH_MOV */ shader_glsl_mov,
11538 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11539 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11540 /* WINED3DSIH_MUL */ shader_glsl_binop,
11541 /* WINED3DSIH_NE */ shader_glsl_relop,
11542 /* WINED3DSIH_NOP */ shader_glsl_nop,
11543 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11544 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11545 /* WINED3DSIH_OR */ shader_glsl_binop,
11546 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11547 /* WINED3DSIH_POW */ shader_glsl_pow,
11548 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11549 /* WINED3DSIH_REP */ shader_glsl_rep,
11550 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11551 /* WINED3DSIH_RET */ shader_glsl_ret,
11552 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11553 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11554 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11555 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11556 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11557 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11558 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11559 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11560 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11561 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11562 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11563 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11564 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11565 /* WINED3DSIH_SAMPLE_POS */ NULL,
11566 /* WINED3DSIH_SETP */ NULL,
11567 /* WINED3DSIH_SGE */ shader_glsl_compare,
11568 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11569 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11570 /* WINED3DSIH_SLT */ shader_glsl_compare,
11571 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11572 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11573 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11574 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11575 /* WINED3DSIH_SUB */ shader_glsl_binop,
11576 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11577 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11578 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11579 /* WINED3DSIH_TEX */ shader_glsl_tex,
11580 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11581 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11582 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11583 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11584 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11585 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11586 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11587 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11588 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11589 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11590 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11591 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11592 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11593 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11594 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11595 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11596 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11597 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11598 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11599 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11600 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11601 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11602 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11603 /* WINED3DSIH_UGE */ shader_glsl_relop,
11604 /* WINED3DSIH_ULT */ shader_glsl_relop,
11605 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11606 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11607 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11608 /* WINED3DSIH_USHR */ shader_glsl_binop,
11609 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11610 /* WINED3DSIH_XOR */ shader_glsl_binop,
11613 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11614 SHADER_HANDLER hw_fct;
11616 /* Select handler */
11617 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11619 /* Unhandled opcode */
11620 if (!hw_fct)
11622 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11623 return;
11625 hw_fct(ins);
11627 shader_glsl_add_instruction_modifiers(ins);
11630 static uint64_t shader_glsl_shader_compile(struct wined3d_context *context, const struct wined3d_shader_desc *shader_desc,
11631 enum wined3d_shader_type shader_type)
11633 ERR("Not implemented.\n");
11634 return 0;
11637 const struct wined3d_shader_backend_ops glsl_shader_backend =
11639 shader_glsl_handle_instruction,
11640 shader_glsl_precompile,
11641 shader_glsl_apply_draw_state,
11642 shader_glsl_apply_compute_state,
11643 shader_glsl_disable,
11644 shader_glsl_update_float_vertex_constants,
11645 shader_glsl_update_float_pixel_constants,
11646 shader_glsl_destroy,
11647 shader_glsl_alloc,
11648 shader_glsl_free,
11649 shader_glsl_allocate_context_data,
11650 shader_glsl_free_context_data,
11651 shader_glsl_init_context_state,
11652 shader_glsl_get_caps,
11653 shader_glsl_color_fixup_supported,
11654 shader_glsl_shader_compile,
11657 static void glsl_vertex_pipe_vp_apply_draw_state(struct wined3d_context *context, const struct wined3d_state *state) {}
11659 static void glsl_vertex_pipe_vp_disable(const struct wined3d_context *context) {}
11661 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_adapter *adapter, struct wined3d_vertex_caps *caps)
11663 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl_const(adapter)->gl_info;
11665 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11666 caps->max_active_lights = WINED3D_MAX_ACTIVE_LIGHTS;
11667 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11668 caps->max_vertex_blend_matrix_index = 0;
11669 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11670 | WINED3DVTXPCAPS_MATERIALSOURCE7
11671 | WINED3DVTXPCAPS_VERTEXFOG
11672 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11673 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11674 | WINED3DVTXPCAPS_LOCALVIEWER
11675 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11676 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11677 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11678 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11681 static unsigned int glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_adapter *adapter)
11683 if (wined3d_adapter_gl_const(adapter)->gl_info.supported[WINED3D_GL_LEGACY_CONTEXT])
11684 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11685 return 0;
11688 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11690 struct shader_glsl_priv *priv;
11692 if (shader_backend == &glsl_shader_backend)
11694 priv = shader_priv;
11695 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11696 return priv;
11699 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11701 return NULL;
11704 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *param)
11706 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11707 struct glsl_ffp_vertex_shader, desc.entry);
11708 struct glsl_shader_prog_link *program, *program2;
11709 struct glsl_ffp_destroy_ctx *ctx = param;
11710 const struct wined3d_gl_info *gl_info;
11712 gl_info = ctx->context_gl->gl_info;
11713 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11714 struct glsl_shader_prog_link, vs.shader_entry)
11716 delete_glsl_program_entry(ctx->priv, gl_info, program);
11718 GL_EXTCALL(glDeleteShader(shader->id));
11719 free(shader);
11722 /* Context activation is done by the caller. */
11723 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device, struct wined3d_context *context)
11725 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11726 struct shader_glsl_priv *priv = device->vertex_priv;
11727 struct glsl_ffp_destroy_ctx ctx;
11729 ctx.priv = priv;
11730 ctx.context_gl = context_gl;
11731 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11734 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11735 const struct wined3d_state *state, DWORD state_id) {}
11737 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11738 const struct wined3d_state *state, DWORD state_id)
11740 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11743 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11744 const struct wined3d_state *state, DWORD state_id)
11746 const struct wined3d_vertex_declaration *vdecl = state->vertex_declaration;
11747 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11748 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11749 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11750 BOOL transformed = context->stream_info.position_transformed;
11751 BOOL wasrhw = context->last_was_rhw;
11752 bool point_size = vdecl && vdecl->point_size;
11753 bool specular = vdecl && vdecl->specular;
11754 bool diffuse = vdecl && vdecl->diffuse;
11755 bool normal = vdecl && vdecl->normal;
11756 unsigned int i;
11758 context->last_was_rhw = transformed;
11760 /* If the vertex declaration contains a transformed position attribute,
11761 * the draw uses the fixed function vertex pipeline regardless of any
11762 * vertex shader set by the application. */
11763 if (transformed != wasrhw
11764 || context->stream_info.swizzle_map != context->last_swizzle_map)
11765 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11767 context->last_swizzle_map = context->stream_info.swizzle_map;
11769 if (!use_vs(state))
11771 if (context->last_was_vshader)
11773 if (legacy_clip_planes)
11774 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11775 clipplane(context, state, STATE_CLIPPLANE(i));
11776 else
11777 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11780 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11782 /* Because of settings->texcoords, we have to regenerate the vertex
11783 * shader on a vdecl change if there aren't enough varyings to just
11784 * always output all the texture coordinates.
11786 * Likewise, we have to invalidate the shader when using per-vertex
11787 * colours and specular attribute presence changes, or when
11788 * normal, diffuse, or point size presence changes. */
11789 if (!shader_glsl_full_ffp_varyings(gl_info) || diffuse != context->last_was_diffuse
11790 || (state->render_states[WINED3D_RS_COLORVERTEX] && specular != context->last_was_specular)
11791 || normal != context->last_was_normal || point_size != context->last_was_point_size)
11792 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11794 if (use_ps(state)
11795 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11796 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11797 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11799 else
11801 if (!context->last_was_vshader)
11803 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11804 if (legacy_clip_planes)
11805 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11806 clipplane(context, state, STATE_CLIPPLANE(i));
11807 else
11808 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11812 context->last_was_vshader = use_vs(state);
11813 context->last_was_diffuse = diffuse;
11814 context->last_was_specular = specular;
11815 context->last_was_normal = normal;
11816 context->last_was_point_size = point_size;
11819 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11820 const struct wined3d_state *state, DWORD state_id)
11822 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11823 /* Different vertex shaders potentially require a different vertex attributes setup. */
11824 if (!isStateDirty(context, STATE_VDECL))
11825 context_apply_state(context, state, STATE_VDECL);
11828 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11829 const struct wined3d_state *state, DWORD state_id)
11831 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11832 * winding) are defined in Hull Shaders, while in GLSL those are
11833 * specified in Tessellation Evaluation Shaders. */
11834 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11836 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11837 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11840 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11841 const struct wined3d_state *state, DWORD state_id)
11843 struct glsl_context_data *ctx_data = context->shader_backend_data;
11844 BOOL rasterization_disabled;
11846 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11847 if (ctx_data->rasterization_disabled != rasterization_disabled)
11848 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11849 ctx_data->rasterization_disabled = rasterization_disabled;
11851 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11852 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11853 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11854 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11855 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11858 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11859 const struct wined3d_state *state, DWORD state_id)
11861 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11862 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11863 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11864 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11865 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11866 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11867 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11870 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11871 const struct wined3d_state *state, DWORD state_id)
11873 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11876 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11877 const struct wined3d_state *state, DWORD state_id)
11879 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11882 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11884 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11885 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11886 unsigned int k;
11888 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11889 | WINED3D_SHADER_CONST_FFP_LIGHTS
11890 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11892 if (needs_legacy_glsl_syntax(gl_info))
11894 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11896 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11897 clipplane(context, state, STATE_CLIPPLANE(k));
11900 else
11902 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11906 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11907 const struct wined3d_state *state, DWORD state_id)
11909 /* Table fog behavior depends on the projection matrix. */
11910 if (state->render_states[WINED3D_RS_FOGENABLE]
11911 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11912 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11913 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11916 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11917 const struct wined3d_state *state, DWORD state_id)
11919 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11920 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11921 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11922 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11923 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11924 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11927 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11928 const struct wined3d_state *state, DWORD state_id)
11930 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11933 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11934 const struct wined3d_state *state, DWORD state_id)
11936 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11939 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11940 const struct wined3d_state *state, DWORD state_id)
11942 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11945 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11946 const struct wined3d_state *state, DWORD state_id)
11948 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11951 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11952 const struct wined3d_state *state, DWORD state_id)
11954 if (!use_vs(state))
11955 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11958 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11959 const struct wined3d_state *state, DWORD state_id)
11961 static unsigned int once;
11963 if (state->primitive_type == WINED3D_PT_POINTLIST
11964 && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11965 FIXME("Non-point sprite points not supported in core profile.\n");
11968 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11969 const struct wined3d_state *state, DWORD state_id)
11971 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11974 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11975 const struct wined3d_state *state, DWORD state_id)
11977 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11978 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11979 UINT index = state_id - STATE_CLIPPLANE(0);
11981 if (index >= gl_info->limits.user_clip_distances)
11982 return;
11984 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11987 static const struct wined3d_state_entry_template glsl_vertex_pipe_vp_states[] =
11989 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11990 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11991 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11992 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11993 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11994 {STATE_MATERIAL, {STATE_MATERIAL, glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11995 /* Clip planes */
11996 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11997 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11998 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11999 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
12000 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
12001 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
12002 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
12003 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
12004 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
12005 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
12006 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
12007 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
12008 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
12009 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
12010 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
12011 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
12012 /* Lights */
12013 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12014 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12015 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12016 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12017 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12018 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12019 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12020 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12021 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
12022 /* Viewport */
12023 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
12024 /* Transform states */
12025 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
12026 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
12027 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12028 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12029 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12030 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12031 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12032 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12033 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12034 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
12035 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
12036 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
12037 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
12038 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
12039 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12040 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12041 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12042 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12043 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12044 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12045 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12046 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
12047 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12048 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12049 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12050 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12051 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12052 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12053 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12054 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12055 /* Fog */
12056 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12057 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12058 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12059 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12060 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
12061 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
12062 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12063 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12064 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12065 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12066 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12067 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12068 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12069 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12070 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12071 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12072 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
12073 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
12074 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
12075 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
12076 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
12077 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
12078 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12079 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12080 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12081 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
12082 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12083 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
12084 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
12085 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
12088 /* TODO:
12089 * - Implement vertex tweening. */
12090 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
12092 .vp_apply_draw_state = glsl_vertex_pipe_vp_apply_draw_state,
12093 .vp_disable = glsl_vertex_pipe_vp_disable,
12094 .vp_get_caps = glsl_vertex_pipe_vp_get_caps,
12095 .vp_get_emul_mask = glsl_vertex_pipe_vp_get_emul_mask,
12096 .vp_alloc = glsl_vertex_pipe_vp_alloc,
12097 .vp_free = glsl_vertex_pipe_vp_free,
12098 .vp_states = glsl_vertex_pipe_vp_states,
12101 static void glsl_fragment_pipe_apply_draw_state(struct wined3d_context *context, const struct wined3d_state *state)
12103 /* Nothing to do. */
12106 static void glsl_fragment_pipe_disable(const struct wined3d_context *context)
12110 static void glsl_fragment_pipe_get_caps(const struct wined3d_adapter *adapter, struct fragment_caps *caps)
12112 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl_const(adapter)->gl_info;
12114 memset(caps, 0, sizeof(*caps));
12115 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12116 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12117 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12118 | WINED3DTEXOPCAPS_SELECTARG1
12119 | WINED3DTEXOPCAPS_SELECTARG2
12120 | WINED3DTEXOPCAPS_MODULATE4X
12121 | WINED3DTEXOPCAPS_MODULATE2X
12122 | WINED3DTEXOPCAPS_MODULATE
12123 | WINED3DTEXOPCAPS_ADDSIGNED2X
12124 | WINED3DTEXOPCAPS_ADDSIGNED
12125 | WINED3DTEXOPCAPS_ADD
12126 | WINED3DTEXOPCAPS_SUBTRACT
12127 | WINED3DTEXOPCAPS_ADDSMOOTH
12128 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12129 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12130 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12131 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12132 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12133 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12134 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12135 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12136 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12137 | WINED3DTEXOPCAPS_DOTPRODUCT3
12138 | WINED3DTEXOPCAPS_MULTIPLYADD
12139 | WINED3DTEXOPCAPS_LERP
12140 | WINED3DTEXOPCAPS_BUMPENVMAP
12141 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12142 caps->max_blend_stages = WINED3D_MAX_FFP_TEXTURES;
12143 caps->max_textures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], WINED3D_MAX_FFP_TEXTURES);
12146 static unsigned int glsl_fragment_pipe_get_emul_mask(const struct wined3d_adapter *adapter)
12148 if (wined3d_adapter_gl_const(adapter)->gl_info.supported[WINED3D_GL_LEGACY_CONTEXT])
12149 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12150 return 0;
12153 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12155 struct shader_glsl_priv *priv;
12157 if (shader_backend == &glsl_shader_backend)
12159 priv = shader_priv;
12160 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12161 return priv;
12164 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12166 return NULL;
12169 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *param)
12171 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12172 struct glsl_ffp_fragment_shader, entry.entry);
12173 struct glsl_shader_prog_link *program, *program2;
12174 struct glsl_ffp_destroy_ctx *ctx = param;
12175 const struct wined3d_gl_info *gl_info;
12177 gl_info = ctx->context_gl->gl_info;
12178 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12179 struct glsl_shader_prog_link, ps.shader_entry)
12181 delete_glsl_program_entry(ctx->priv, gl_info, program);
12183 GL_EXTCALL(glDeleteShader(shader->id));
12184 free(shader);
12187 /* Context activation is done by the caller. */
12188 static void glsl_fragment_pipe_free(struct wined3d_device *device, struct wined3d_context *context)
12190 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12191 struct shader_glsl_priv *priv = device->fragment_priv;
12192 struct glsl_ffp_destroy_ctx ctx;
12194 ctx.priv = priv;
12195 ctx.context_gl = context_gl;
12196 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12199 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12200 const struct wined3d_state *state, DWORD state_id)
12202 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12205 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12206 const struct wined3d_state *state, DWORD state_id)
12208 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12211 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12212 const struct wined3d_state *state, DWORD state_id)
12214 BOOL use_vshader = use_vs(state);
12215 enum fogsource new_source;
12216 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12217 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12219 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12221 if (!state->render_states[WINED3D_RS_FOGENABLE])
12222 return;
12224 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12226 if (use_vshader || state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE
12227 || context->stream_info.position_transformed)
12228 new_source = FOGSOURCE_VS;
12229 else
12230 new_source = FOGSOURCE_FFP;
12232 else
12234 new_source = FOGSOURCE_FFP;
12237 if (new_source != context->fog_source || fogstart == fogend)
12239 context->fog_source = new_source;
12240 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12244 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12245 const struct wined3d_state *state, DWORD state_id)
12247 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12249 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12250 if (!shader_glsl_full_ffp_varyings(gl_info))
12251 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12253 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12254 glsl_fragment_pipe_fog(context, state, state_id);
12257 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12258 const struct wined3d_state *state, DWORD state_id)
12260 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12262 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12263 if (!shader_glsl_full_ffp_varyings(gl_info))
12264 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12267 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12268 const struct wined3d_state *state, DWORD state_id)
12270 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12273 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12274 const struct wined3d_state *state, DWORD state_id)
12276 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12277 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12278 float ref = wined3d_alpha_ref(state);
12280 if (func)
12282 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12283 checkGLcall("glAlphaFunc");
12287 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12288 const struct wined3d_state *state, DWORD state_id)
12290 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12293 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12294 const struct wined3d_state *state, DWORD state_id)
12296 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12298 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12300 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12301 checkGLcall("glEnable(GL_ALPHA_TEST)");
12303 else
12305 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12306 checkGLcall("glDisable(GL_ALPHA_TEST)");
12310 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12311 const struct wined3d_state *state, DWORD state_id)
12313 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12316 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12317 const struct wined3d_state *state, DWORD state_id)
12319 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12322 static const struct wined3d_state_entry_template glsl_fragment_pipe_state_template[] =
12324 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12325 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12326 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12327 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12328 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12329 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12330 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12331 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12332 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12333 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12334 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12335 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12336 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12337 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12338 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12339 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12340 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12341 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12342 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12343 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12344 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12345 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12346 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12347 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12348 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12349 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12350 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12351 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12352 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12353 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12354 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12355 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12356 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12357 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12358 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12359 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12360 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12361 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12362 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12363 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12364 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12365 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12366 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12367 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12368 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12369 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12370 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12371 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12372 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12373 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12374 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12375 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12376 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12377 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12378 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12379 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12380 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12381 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12382 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12383 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12384 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12385 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12386 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12387 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12388 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12389 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12390 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12391 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12392 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12393 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12394 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12395 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12396 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12397 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12398 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12399 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12400 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12401 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12402 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12403 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12404 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12405 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12406 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12407 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12408 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12409 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12410 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12411 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12412 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12413 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12414 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12415 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12416 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12417 {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 },
12418 {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 },
12419 {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 },
12420 {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 },
12421 {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 },
12422 {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 },
12423 {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 },
12424 {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 },
12425 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12426 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12427 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12428 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12431 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12433 return TRUE;
12436 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12440 const struct wined3d_fragment_pipe_ops glsl_fragment_pipe =
12442 .fp_apply_draw_state = glsl_fragment_pipe_apply_draw_state,
12443 .fp_disable = glsl_fragment_pipe_disable,
12444 .get_caps = glsl_fragment_pipe_get_caps,
12445 .get_emul_mask = glsl_fragment_pipe_get_emul_mask,
12446 .alloc_private = glsl_fragment_pipe_alloc,
12447 .free_private = glsl_fragment_pipe_free,
12448 .allocate_context_data = glsl_fragment_pipe_alloc_context_data,
12449 .free_context_data = glsl_fragment_pipe_free_context_data,
12450 .color_fixup_supported = shader_glsl_color_fixup_supported,
12451 .states = glsl_fragment_pipe_state_template,
12454 struct glsl_blitter_args
12456 GLenum texture_type;
12457 struct color_fixup_desc fixup;
12458 unsigned short use_colour_key;
12461 struct glsl_blitter_program
12463 struct wine_rb_entry entry;
12464 struct glsl_blitter_args args;
12465 GLuint id;
12468 struct wined3d_glsl_blitter
12470 struct wined3d_blitter blitter;
12471 struct wined3d_string_buffer_list string_buffers;
12472 struct wine_rb_tree programs;
12473 GLuint palette_texture;
12476 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12478 const struct glsl_blitter_args *a = key;
12479 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12481 return memcmp(a, b, sizeof(*a));
12484 /* Context activation is done by the caller. */
12485 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12487 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12488 struct wined3d_context_gl *context_gl = ctx;
12489 const struct wined3d_gl_info *gl_info;
12491 gl_info = context_gl->gl_info;
12492 GL_EXTCALL(glDeleteProgram(program->id));
12493 checkGLcall("glDeleteProgram()");
12494 free(program);
12497 /* Context activation is done by the caller. */
12498 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12500 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12501 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12502 struct wined3d_glsl_blitter *glsl_blitter;
12503 struct wined3d_blitter *next;
12505 if ((next = blitter->next))
12506 next->ops->blitter_destroy(next, context);
12508 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12510 if (glsl_blitter->palette_texture)
12511 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12513 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context_gl);
12514 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12516 free(glsl_blitter);
12519 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12520 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12521 const char *output, const char *tex_type, const char *swizzle)
12523 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12524 shader_addline(buffer, "\nvoid main()\n{\n");
12525 /* The alpha-component contains the palette index. */
12526 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12527 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12528 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12529 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12530 * the middle. */
12531 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12532 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12533 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12534 if (args->use_colour_key)
12535 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12536 shader_addline(buffer, "}\n");
12539 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12540 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12541 const char *tex_type)
12543 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12544 char chroma, luminance;
12545 const char *tex;
12547 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12548 * macropixel, giving effectively 16 bits per pixel. The color consists of
12549 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12550 * luminance values, one for each single pixel it contains, and one U and
12551 * one V value shared between both pixels.
12553 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12554 * component contains the luminance and alpha the chroma. With UYVY it is
12555 * vice versa. Thus take the format into account when generating the read
12556 * swizzles
12558 * Reading the Y value is straightforward - just sample the texture. The
12559 * hardware takes care of filtering in the horizontal and vertical
12560 * direction.
12562 * Reading the U and V values is harder. We have to avoid filtering
12563 * horizontally, because that would mix the U and V values of one pixel or
12564 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12565 * get an unfiltered read, regardless of the filtering setting. Vertical
12566 * filtering works automatically though - the U and V values of two rows
12567 * are mixed nicely.
12569 * Apart of avoiding filtering issues, the code has to know which value it
12570 * just read, and where it can find the other one. To determine this, it
12571 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12572 * accordingly.
12574 * Handling horizontal filtering of U and V values requires reading a 2nd
12575 * pair of pixels, extracting U and V and mixing them. This is not
12576 * implemented yet.
12578 * An alternative implementation idea is to load the texture as A8R8G8B8
12579 * texture, with width / 2. This way one read gives all 3 values, finding
12580 * U and V is easy in an unfiltered situation. Finding the luminance on
12581 * the other hand requires finding out if it is an odd or even pixel. The
12582 * real drawback of this approach is filtering. This would have to be
12583 * emulated completely in the shader, reading up two 2 packed pixels in up
12584 * to 2 rows and interpolating both horizontally and vertically. Beyond
12585 * that it would require adjustments to the texture handling code to deal
12586 * with the width scaling. */
12588 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12590 chroma = 'x';
12591 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12593 else
12595 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12596 luminance = 'x';
12599 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12601 /* First we have to read the chroma values. This means we need at least
12602 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12603 * unmodified chroma, we have to rid ourselves of the filtering when we
12604 * sample the texture. */
12605 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12606 /* We must not allow filtering between pixel x and x+1, this would mix U
12607 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12608 * center is at 0.5, so add 0.5. */
12609 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12610 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12612 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12613 * and 0.75 for the even and odd pixels respectively. */
12614 /* Put the value into either of the chroma values. */
12615 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12616 shader_addline(buffer, " if (even)\n");
12617 shader_addline(buffer, " chroma.y = luminance;\n");
12618 shader_addline(buffer, " else\n");
12619 shader_addline(buffer, " chroma.x = luminance;\n");
12621 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12622 * current one. Otherwise, sample the left pixel. */
12623 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12624 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12626 /* Put the value into the other chroma. */
12627 shader_addline(buffer, " if (even)\n");
12628 shader_addline(buffer, " chroma.x = luminance;\n");
12629 shader_addline(buffer, " else\n");
12630 shader_addline(buffer, " chroma.y = luminance;\n");
12632 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12633 * the current one and lerp the two U and V values. */
12635 /* This gives the correctly filtered luminance value. */
12636 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12639 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12640 const struct wined3d_gl_info *gl_info, const char *tex_type)
12642 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12643 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12645 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12646 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12647 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12648 * have only half the pitch of the luminance plane, the packing into the
12649 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12650 * luminance data it looks approximately like this:
12652 * +----------------------------------+----
12653 * | |
12654 * | |
12655 * | |
12656 * | |
12657 * | | 2
12658 * | LUMINANCE | -
12659 * | | 3
12660 * | |
12661 * | |
12662 * | |
12663 * | |
12664 * +----------------+-----------------+----
12665 * | | |
12666 * | V even rows | V odd rows |
12667 * | | | 1
12668 * +----------------+------------------ -
12669 * | | | 3
12670 * | U even rows | U odd rows |
12671 * | | |
12672 * +----------------+-----------------+----
12673 * | | |
12674 * | 0.5 | 0.5 |
12676 * So it appears as if there are 4 chroma images, but in fact the odd rows
12677 * in the chroma images are in the same row as the even ones. So it is
12678 * kinda tricky to read. */
12680 /* First sample the chroma values. */
12681 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12682 /* The chroma planes have only half the width. */
12683 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12685 /* The first value is between 2/3 and 5/6 of the texture's height, so
12686 * scale+bias the coordinate. Also read the right side of the image when
12687 * reading odd lines.
12689 * Don't forget to clamp the y values in into the range, otherwise we'll
12690 * get filtering bleeding. */
12692 /* Read odd lines from the right side (add 0.5 to the x coordinate). Keep
12693 * in mind that each line of the chroma plane corresponds to 2 lines of the
12694 * resulting image. */
12695 shader_addline(buffer, " if (fract(texcoord.y * size.y * 0.25) >= 0.5)\n");
12696 shader_addline(buffer, " texcoord.x += 0.5;\n");
12698 /* Clamp, keep the half pixel origin in mind. */
12699 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12700 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12702 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12704 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12705 * 6/6th No need to clamp because we're just reusing the already clamped
12706 * value from above. */
12707 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12708 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12710 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12711 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12712 * values from bleeding into the sampled luminance values due to
12713 * filtering. */
12714 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12715 /* Multiply the y coordinate by 2/3 and clamp it. */
12716 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12717 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12720 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12721 const struct wined3d_gl_info *gl_info, const char *tex_type)
12723 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12724 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12726 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12727 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12728 * effective bitdepth is 12 bits per pixel. If the whole texture is
12729 * interpreted as luminance data it looks approximately like this:
12731 * +----------------------------------+----
12732 * | |
12733 * | |
12734 * | |
12735 * | |
12736 * | | 2
12737 * | LUMINANCE | -
12738 * | | 3
12739 * | |
12740 * | |
12741 * | |
12742 * | |
12743 * +----------------------------------+----
12744 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12745 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12746 * | | 1
12747 * | | -
12748 * | | 3
12749 * | |
12750 * | |
12751 * +----------------------------------+---- */
12753 /* First sample the chroma values. */
12754 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12755 /* We only have half the number of chroma pixels. */
12756 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12757 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12759 /* We must not allow filtering horizontally, this would mix U and V.
12760 * Vertical filtering is ok. However, bear in mind that the pixel center
12761 * is at 0.5, so add 0.5. */
12763 /* Convert to non-normalised coordinates so we can find the individual
12764 * pixel. */
12765 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12766 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12767 * 0.5 to hit the center of the pixel. Then convert back to normalised
12768 * coordinates. */
12769 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12770 /* Clamp, keep the half pixel origin in mind. */
12771 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12773 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12774 /* Add 1.0 / size.x to sample the adjacent texel. */
12775 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12776 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12778 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12779 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12780 * values from bleeding into the sampled luminance values due to
12781 * filtering. */
12782 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12783 /* Multiply the y coordinate by 2/3 and clamp it. */
12784 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12785 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12788 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12789 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12790 const char *output, const char *tex_type, const char *swizzle)
12792 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12794 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12795 shader_addline(buffer, "float luminance;\n");
12796 shader_addline(buffer, "vec2 texcoord;\n");
12797 shader_addline(buffer, "vec2 chroma;\n");
12798 shader_addline(buffer, "uniform vec2 size;\n");
12800 shader_addline(buffer, "\nvoid main()\n{\n");
12802 switch (complex_fixup)
12804 case COMPLEX_FIXUP_UYVY:
12805 case COMPLEX_FIXUP_YUY2:
12806 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12807 break;
12809 case COMPLEX_FIXUP_YV12:
12810 gen_yv12_read(buffer, gl_info, tex_type);
12811 break;
12813 case COMPLEX_FIXUP_NV12:
12814 gen_nv12_read(buffer, gl_info, tex_type);
12815 break;
12817 case COMPLEX_FIXUP_YUV:
12818 /* With APPLE_rgb_422, things are much simpler. The only thing we
12819 * have to do here is Y'CbCr to RGB conversion. */
12820 shader_addline(buffer, " vec3 yuv = vec3(texture%s(sampler, out_texcoord.xy));\n",
12821 needs_legacy_glsl_syntax(gl_info) ? tex_type : "");
12822 shader_addline(buffer, " luminance = yuv.y;\n");
12823 shader_addline(buffer, " chroma = yuv.xz;\n");
12824 break;
12826 default:
12827 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12828 string_buffer_free(buffer);
12829 return;
12832 /* Calculate the final result. Formula is taken from
12833 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12834 * ranges from -0.5 to 0.5. */
12835 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12837 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12838 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12839 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12840 if (args->use_colour_key)
12841 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12843 shader_addline(buffer, "}\n");
12846 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12847 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12848 const char *output, const char *tex_type, const char *swizzle)
12850 shader_addline(buffer, "\nvoid main()\n{\n");
12851 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12852 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12853 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12854 if (args->use_colour_key)
12855 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12856 shader_addline(buffer, "}\n");
12859 /* Context activation is done by the caller. */
12860 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12861 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12863 static const struct
12865 GLenum texture_target;
12866 const char texture_type[7];
12867 const char texcoord_swizzle[4];
12869 texture_data[] =
12871 {GL_TEXTURE_2D, "2D", "xy"},
12872 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12874 static const char vshader_main[] =
12875 "\n"
12876 "void main()\n"
12877 "{\n"
12878 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12879 " out_texcoord = texcoord;\n"
12880 "}\n";
12881 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12882 struct wined3d_string_buffer *buffer, *output;
12883 GLuint program, vshader_id, fshader_id;
12884 const char *tex_type = NULL, *swizzle = NULL, *ptr;
12885 unsigned int i;
12886 GLint loc;
12888 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12890 if (args->texture_type == texture_data[i].texture_target)
12892 tex_type = texture_data[i].texture_type;
12893 swizzle = texture_data[i].texcoord_swizzle;
12894 break;
12897 if (i == ARRAY_SIZE(texture_data))
12899 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12900 return 0;
12903 program = GL_EXTCALL(glCreateProgram());
12905 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12907 buffer = string_buffer_get(&blitter->string_buffers);
12908 shader_glsl_add_version_declaration(buffer, gl_info);
12909 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12910 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12911 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12912 shader_addline(buffer, vshader_main);
12914 ptr = buffer->buffer;
12915 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12916 GL_EXTCALL(glAttachShader(program, vshader_id));
12917 GL_EXTCALL(glDeleteShader(vshader_id));
12919 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12921 string_buffer_clear(buffer);
12922 shader_glsl_add_version_declaration(buffer, gl_info);
12923 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12924 if (args->use_colour_key)
12926 shader_addline(buffer, "uniform struct\n{\n");
12927 shader_addline(buffer, " vec4 low, high;\n");
12928 shader_addline(buffer, "} colour_key;\n");
12930 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12931 /* TODO: Declare the out variable with the correct type (and put it in the
12932 * blitter args). */
12933 if (!use_legacy_fragment_output(gl_info))
12934 shader_addline(buffer, "out vec4 ps_out[1];\n");
12936 output = string_buffer_get(&blitter->string_buffers);
12937 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12939 switch (complex_fixup)
12941 case COMPLEX_FIXUP_P8:
12942 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12943 break;
12944 case COMPLEX_FIXUP_YUY2:
12945 case COMPLEX_FIXUP_UYVY:
12946 case COMPLEX_FIXUP_YV12:
12947 case COMPLEX_FIXUP_NV12:
12948 case COMPLEX_FIXUP_YUV:
12949 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12950 break;
12951 case COMPLEX_FIXUP_NONE:
12952 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12955 string_buffer_release(&blitter->string_buffers, output);
12957 ptr = buffer->buffer;
12958 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12959 string_buffer_release(&blitter->string_buffers, buffer);
12960 GL_EXTCALL(glAttachShader(program, fshader_id));
12961 GL_EXTCALL(glDeleteShader(fshader_id));
12963 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12964 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12966 if (!use_legacy_fragment_output(gl_info))
12967 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12969 GL_EXTCALL(glCompileShader(vshader_id));
12970 print_glsl_info_log(gl_info, vshader_id, FALSE);
12971 GL_EXTCALL(glCompileShader(fshader_id));
12972 print_glsl_info_log(gl_info, fshader_id, FALSE);
12973 GL_EXTCALL(glLinkProgram(program));
12974 shader_glsl_validate_link(gl_info, program);
12976 GL_EXTCALL(glUseProgram(program));
12977 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12978 GL_EXTCALL(glUniform1i(loc, 0));
12979 if (complex_fixup == COMPLEX_FIXUP_P8)
12981 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12982 GL_EXTCALL(glUniform1i(loc, 1));
12985 return program;
12988 /* Context activation is done by the caller. */
12989 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12990 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl)
12992 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12993 const struct wined3d_palette *palette;
12995 palette = texture_gl->t.swapchain ? texture_gl->t.swapchain->palette : NULL;
12997 if (!blitter->palette_texture)
12998 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
13000 wined3d_context_gl_active_texture(context_gl, gl_info, 1);
13001 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
13002 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
13003 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
13004 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
13006 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
13007 checkGLcall("glBindBuffer");
13009 if (palette)
13011 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
13012 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
13014 else
13016 static const DWORD black;
13018 FIXME("P8 texture loaded without a palette.\n");
13019 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
13020 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
13023 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
13026 /* Context activation is done by the caller. */
13027 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
13028 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl, BOOL use_colour_key)
13030 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13031 struct glsl_blitter_program *program;
13032 struct glsl_blitter_args args;
13033 struct wine_rb_entry *entry;
13035 memset(&args, 0, sizeof(args));
13036 args.texture_type = texture_gl->target;
13037 args.fixup = texture_gl->t.resource.format->color_fixup;
13038 args.use_colour_key = use_colour_key;
13040 if ((entry = wine_rb_get(&blitter->programs, &args)))
13041 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
13043 if (!(program = malloc(sizeof(*program))))
13045 ERR("Failed to allocate blitter program memory.\n");
13046 return NULL;
13049 program->args = args;
13050 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
13052 WARN("Failed to generate blitter program.\n");
13053 free(program);
13054 return NULL;
13057 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
13059 ERR("Failed to store blitter program.\n");
13060 GL_EXTCALL(glDeleteProgram(program->id));
13061 free(program);
13062 return NULL;
13065 return program;
13068 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
13069 const struct wined3d_texture_gl *src_texture, DWORD src_location,
13070 const struct wined3d_texture_gl *dst_texture, DWORD dst_location)
13072 const struct wined3d_resource *src_resource = &src_texture->t.resource;
13073 const struct wined3d_resource *dst_resource = &dst_texture->t.resource;
13074 const struct wined3d_format *src_format = src_resource->format;
13075 const struct wined3d_format *dst_format = dst_resource->format;
13076 bool src_ds, dst_ds;
13077 BOOL decompress;
13079 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13081 src_ds = src_format->depth_size || src_format->stencil_size;
13082 dst_ds = dst_format->depth_size || dst_format->stencil_size;
13083 /* We could in principle support raw depth/stencil <-> colour blits as
13084 * well in some cases, but note that typeless formats like e.g.
13085 * R16_TYPELESS may use a normalised GL format for depth/stencil
13086 * resources, and a floating-point format for colour resources, */
13087 if (src_ds && dst_ds)
13088 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13089 else if (!src_ds && !dst_ds)
13090 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13093 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY
13094 && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13096 TRACE("Unsupported blit_op %#x.\n", blit_op);
13097 return FALSE;
13100 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13101 return FALSE;
13103 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13104 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13106 TRACE("Multi-sample source textures not supported.\n");
13107 return FALSE;
13110 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13112 TRACE("Destination resource does not have GPU access.\n");
13113 return FALSE;
13116 /* We don't necessarily want to blit from resources without
13117 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13118 * compressed textures. */
13119 decompress = (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
13120 && !(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED);
13121 if (!decompress && !(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13123 TRACE("Source resource does not have GPU access.\n");
13124 return FALSE;
13127 if (!is_identity_fixup(dst_format->color_fixup)
13128 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13130 TRACE("Destination fixups are not supported.\n");
13131 return FALSE;
13134 if (!((dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
13135 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
13137 TRACE("Destination texture is not FBO attachable.\n");
13138 return FALSE;
13141 TRACE("Returning supported.\n");
13142 return TRUE;
13145 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13146 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13147 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13148 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13149 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
13150 const struct wined3d_format *resolve_format)
13152 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
13153 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
13154 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
13155 struct wined3d_device *device = dst_texture->resource.device;
13156 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13157 struct wined3d_texture *staging_texture = NULL;
13158 struct wined3d_glsl_blitter *glsl_blitter;
13159 struct wined3d_color_key alpha_test_key;
13160 struct glsl_blitter_program *program;
13161 struct wined3d_blitter *next;
13162 unsigned int src_level;
13163 GLint location;
13164 RECT d;
13166 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
13167 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
13168 "colour_key %p, filter %s, resolve format %p.\n",
13169 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13170 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13171 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
13173 if (!glsl_blitter_supported(op, context, src_texture_gl, src_location, dst_texture_gl, dst_location))
13175 if (!(next = blitter->next))
13177 ERR("No blitter to handle blit op %#x.\n", op);
13178 return dst_location;
13181 TRACE("Forwarding to blitter %p.\n", next);
13182 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13183 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
13184 resolve_format);
13187 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13189 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13191 struct wined3d_resource_desc desc;
13192 struct wined3d_box upload_box;
13193 HRESULT hr;
13195 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13197 src_level = src_sub_resource_idx % src_texture->level_count;
13198 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13199 desc.format = src_texture->resource.format->id;
13200 desc.multisample_type = src_texture->resource.multisample_type;
13201 desc.multisample_quality = src_texture->resource.multisample_quality;
13202 desc.usage = WINED3DUSAGE_CS;
13203 desc.bind_flags = 0;
13204 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13205 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13206 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13207 desc.depth = 1;
13208 desc.size = 0;
13210 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13211 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13213 ERR("Failed to create staging texture, hr %#lx.\n", hr);
13214 return dst_location;
13217 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13218 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13219 src_texture, src_sub_resource_idx, &upload_box);
13221 src_texture = staging_texture;
13222 src_texture_gl = wined3d_texture_gl(src_texture);
13223 src_sub_resource_idx = 0;
13225 else
13227 wined3d_texture_load(src_texture, context, FALSE);
13230 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
13231 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13232 else
13233 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13235 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
13236 wined3d_context_gl_apply_blit_state(context_gl, device);
13238 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13240 d = *dst_rect;
13241 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
13242 dst_rect = &d;
13245 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13247 const struct wined3d_format *f = src_texture->resource.format;
13249 alpha_test_key.color_space_low_value = 0;
13250 alpha_test_key.color_space_high_value = ~(wined3d_mask_from_size(f->alpha_size) << f->alpha_offset);
13251 colour_key = &alpha_test_key;
13253 else if (op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY)
13255 colour_key = NULL;
13258 if (!(program = glsl_blitter_get_program(glsl_blitter, context_gl, src_texture_gl, !!colour_key)))
13260 ERR("Failed to get blitter program.\n");
13261 return dst_location;
13263 GL_EXTCALL(glUseProgram(program->id));
13264 switch (get_complex_fixup(program->args.fixup))
13266 case COMPLEX_FIXUP_P8:
13267 glsl_blitter_upload_palette(glsl_blitter, context_gl, src_texture_gl);
13268 break;
13270 case COMPLEX_FIXUP_YUY2:
13271 case COMPLEX_FIXUP_UYVY:
13272 case COMPLEX_FIXUP_YV12:
13273 case COMPLEX_FIXUP_NV12:
13274 case COMPLEX_FIXUP_YUV:
13275 src_level = src_sub_resource_idx % src_texture->level_count;
13276 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13277 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_width(src_texture, src_level),
13278 wined3d_texture_get_level_height(src_texture, src_level)));
13279 break;
13281 default:
13282 break;
13284 if (colour_key)
13286 struct wined3d_color float_key[2];
13288 wined3d_format_get_float_color_key(src_texture->resource.format, colour_key, float_key);
13289 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.low"));
13290 GL_EXTCALL(glUniform4fv(location, 1, &float_key[0].r));
13291 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.high"));
13292 GL_EXTCALL(glUniform4fv(location, 1, &float_key[1].r));
13294 wined3d_context_gl_draw_shaded_quad(context_gl, src_texture_gl, src_sub_resource_idx, src_rect, dst_rect, filter);
13295 GL_EXTCALL(glUseProgram(0));
13297 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13298 gl_info->gl_ops.gl.p_glFlush();
13300 if (staging_texture)
13301 wined3d_texture_decref(staging_texture);
13303 return dst_location;
13306 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13307 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13308 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *color, float depth, unsigned int stencil)
13310 struct wined3d_blitter *next;
13312 if ((next = blitter->next))
13313 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13314 clear_rects, draw_rect, flags, color, depth, stencil);
13317 static const struct wined3d_blitter_ops glsl_blitter_ops =
13319 glsl_blitter_destroy,
13320 glsl_blitter_clear,
13321 glsl_blitter_blit,
13324 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13325 const struct wined3d_device *device)
13327 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl(device->adapter)->gl_info;
13328 struct wined3d_glsl_blitter *blitter;
13330 if (device->shader_backend != &glsl_shader_backend)
13331 return NULL;
13333 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13334 return NULL;
13336 if (!(blitter = malloc(sizeof(*blitter))))
13338 ERR("Failed to allocate blitter.\n");
13339 return NULL;
13342 TRACE("Created blitter %p.\n", blitter);
13344 blitter->blitter.ops = &glsl_blitter_ops;
13345 blitter->blitter.next = *next;
13346 string_buffer_list_init(&blitter->string_buffers);
13347 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13348 blitter->palette_texture = 0;
13349 *next = &blitter->blitter;
13351 return *next;