wined3d/glsl: Flush NaN to zero in ftoi.
[wine.git] / dlls / wined3d / glsl_shader.c
blob4a6338223c900d4a94718681d5aab3dea81eea65
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 struct wined3d_string_buffer shader_buffer;
129 struct wined3d_string_buffer_list string_buffers;
130 struct wine_rb_tree program_lookup;
131 struct constant_heap vconst_heap;
132 struct constant_heap pconst_heap;
133 unsigned char *stack;
134 UINT next_constant_version;
136 const struct wined3d_vertex_pipe_ops *vertex_pipe;
137 const struct wined3d_fragment_pipe_ops *fragment_pipe;
138 struct wine_rb_tree ffp_vertex_shaders;
139 struct wine_rb_tree ffp_fragment_shaders;
140 BOOL ffp_proj_control;
141 BOOL legacy_lighting;
144 struct glsl_vs_program
146 struct list shader_entry;
147 GLuint id;
148 GLenum vertex_color_clamp;
149 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
150 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
151 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
152 GLint pos_fixup_location;
153 GLint base_vertex_id_location;
155 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
156 GLint projection_matrix_location;
157 GLint normal_matrix_location;
158 GLint texture_matrix_location[WINED3D_MAX_FFP_TEXTURES];
159 GLint material_ambient_location;
160 GLint material_diffuse_location;
161 GLint material_specular_location;
162 GLint material_emissive_location;
163 GLint material_shininess_location;
164 GLint light_ambient_location;
165 struct
167 GLint diffuse;
168 GLint specular;
169 GLint ambient;
170 GLint position;
171 GLint direction;
172 GLint range;
173 GLint falloff;
174 GLint c_att;
175 GLint l_att;
176 GLint q_att;
177 GLint cos_htheta;
178 GLint cos_hphi;
179 } light_location[WINED3D_MAX_ACTIVE_LIGHTS];
180 GLint pointsize_location;
181 GLint pointsize_min_location;
182 GLint pointsize_max_location;
183 GLint pointsize_c_att_location;
184 GLint pointsize_l_att_location;
185 GLint pointsize_q_att_location;
186 GLint clip_planes_location;
189 struct glsl_hs_program
191 struct list shader_entry;
192 GLuint id;
195 struct glsl_ds_program
197 struct list shader_entry;
198 GLuint id;
200 GLint pos_fixup_location;
203 struct glsl_gs_program
205 struct list shader_entry;
206 GLuint id;
208 GLint pos_fixup_location;
211 struct glsl_ps_program
213 struct list shader_entry;
214 GLuint id;
215 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
216 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
217 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
218 GLint bumpenv_mat_location[WINED3D_MAX_FFP_TEXTURES];
219 GLint bumpenv_lum_scale_location[WINED3D_MAX_FFP_TEXTURES];
220 GLint bumpenv_lum_offset_location[WINED3D_MAX_FFP_TEXTURES];
221 GLint tss_constant_location[WINED3D_MAX_FFP_TEXTURES];
222 GLint tex_factor_location;
223 GLint specular_enable_location;
224 GLint fog_color_location;
225 GLint fog_density_location;
226 GLint fog_end_location;
227 GLint fog_scale_location;
228 GLint alpha_test_ref_location;
229 GLint ycorrection_location;
230 GLint np2_fixup_location;
231 GLint color_key_location;
232 const struct ps_np2fixup_info *np2_fixup_info;
235 struct glsl_cs_program
237 struct list shader_entry;
238 GLuint id;
241 /* Struct to maintain data about a linked GLSL program */
242 struct glsl_shader_prog_link
244 struct wine_rb_entry program_lookup_entry;
245 struct glsl_vs_program vs;
246 struct glsl_hs_program hs;
247 struct glsl_ds_program ds;
248 struct glsl_gs_program gs;
249 struct glsl_ps_program ps;
250 struct glsl_cs_program cs;
251 GLuint id;
252 DWORD constant_update_mask;
253 unsigned int constant_version;
254 DWORD shader_controlled_clip_distances : 1;
255 DWORD clip_distance_mask : 8; /* WINED3D_MAX_CLIP_DISTANCES, 8 */
256 DWORD padding : 23;
259 struct glsl_program_key
261 GLuint vs_id;
262 GLuint hs_id;
263 GLuint ds_id;
264 GLuint gs_id;
265 GLuint ps_id;
266 GLuint cs_id;
269 struct shader_glsl_ctx_priv
271 const struct wined3d_gl_info *gl_info;
272 const struct vs_compile_args *cur_vs_args;
273 const struct ds_compile_args *cur_ds_args;
274 const struct ps_compile_args *cur_ps_args;
275 struct ps_np2fixup_info *cur_np2fixup_info;
276 struct wined3d_string_buffer_list *string_buffers;
279 struct glsl_context_data
281 struct glsl_shader_prog_link *glsl_program;
282 GLenum vertex_color_clamp;
283 BOOL rasterization_disabled;
286 struct glsl_ps_compiled_shader
288 struct ps_compile_args args;
289 struct ps_np2fixup_info np2fixup;
290 GLuint id;
293 struct glsl_vs_compiled_shader
295 struct vs_compile_args args;
296 GLuint id;
299 struct glsl_hs_compiled_shader
301 GLuint id;
304 struct glsl_ds_compiled_shader
306 struct ds_compile_args args;
307 GLuint id;
310 struct glsl_gs_compiled_shader
312 struct gs_compile_args args;
313 GLuint id;
316 struct glsl_cs_compiled_shader
318 GLuint id;
321 struct glsl_shader_private
323 union
325 struct glsl_vs_compiled_shader *vs;
326 struct glsl_hs_compiled_shader *hs;
327 struct glsl_ds_compiled_shader *ds;
328 struct glsl_gs_compiled_shader *gs;
329 struct glsl_ps_compiled_shader *ps;
330 struct glsl_cs_compiled_shader *cs;
331 } gl_shaders;
332 unsigned int num_gl_shaders, shader_array_size;
335 struct glsl_ffp_vertex_shader
337 struct wined3d_ffp_vs_desc desc;
338 GLuint id;
339 struct list linked_programs;
342 struct glsl_ffp_fragment_shader
344 struct ffp_frag_desc entry;
345 GLuint id;
346 struct list linked_programs;
349 struct glsl_ffp_destroy_ctx
351 struct shader_glsl_priv *priv;
352 const struct wined3d_context_gl *context_gl;
355 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
357 static const char *debug_gl_shader_type(GLenum type)
359 switch (type)
361 #define WINED3D_TO_STR(u) case u: return #u
362 WINED3D_TO_STR(GL_VERTEX_SHADER);
363 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
364 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
365 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
366 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
367 WINED3D_TO_STR(GL_COMPUTE_SHADER);
368 #undef WINED3D_TO_STR
369 default:
370 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
374 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
376 switch (type)
378 case WINED3D_SHADER_TYPE_VERTEX:
379 return "vs";
381 case WINED3D_SHADER_TYPE_HULL:
382 return "hs";
384 case WINED3D_SHADER_TYPE_DOMAIN:
385 return "ds";
387 case WINED3D_SHADER_TYPE_GEOMETRY:
388 return "gs";
390 case WINED3D_SHADER_TYPE_PIXEL:
391 return "ps";
393 case WINED3D_SHADER_TYPE_COMPUTE:
394 return "cs";
396 default:
397 FIXME("Unhandled shader type %#x.\n", type);
398 return "unknown";
402 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
404 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
405 return 440;
406 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
407 return 150;
408 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
409 return 130;
410 else
411 return 120;
414 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
415 const struct wined3d_gl_info *gl_info)
417 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
420 static unsigned int shader_glsl_full_ffp_varyings(const struct wined3d_gl_info *gl_info)
422 /* On core profile we have to also count diffuse and specular colours and
423 * the fog coordinate. */
424 return gl_info->limits.glsl_varyings >= (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
425 ? WINED3D_MAX_FFP_TEXTURES * 4 : (WINED3D_MAX_FFP_TEXTURES + 2) * 4 + 1);
428 static void shader_glsl_append_imm_vec(struct wined3d_string_buffer *buffer,
429 const float *values, unsigned int size, const struct wined3d_gl_info *gl_info)
431 const int *int_values = (const int *)values;
432 unsigned int i;
433 char str[17];
435 if (!gl_info->supported[ARB_SHADER_BIT_ENCODING])
437 if (size > 1)
438 shader_addline(buffer, "vec%u(", size);
440 for (i = 0; i < size; ++i)
442 wined3d_ftoa(values[i], str);
443 shader_addline(buffer, i ? ", %s" : "%s", str);
446 if (size > 1)
447 shader_addline(buffer, ")");
449 return;
452 shader_addline(buffer, "intBitsToFloat(");
453 if (size > 1)
454 shader_addline(buffer, "ivec%u(", size);
456 for (i = 0; i < size; ++i)
458 wined3d_ftoa(values[i], str);
459 shader_addline(buffer, i ? ", %#x /* %s */" : "%#x /* %s */", int_values[i], str);
462 if (size > 1)
463 shader_addline(buffer, ")");
464 shader_addline(buffer, ")");
467 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
468 const int *values, unsigned int size)
470 int i;
472 if (!size || size > 4)
474 ERR("Invalid vector size %u.\n", size);
475 return;
478 if (size > 1)
479 shader_addline(buffer, "ivec%u(", size);
481 for (i = 0; i < size; ++i)
482 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
484 if (size > 1)
485 shader_addline(buffer, ")");
488 /* Context activation is done by the caller. */
489 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
491 int length = 0;
492 char *log;
494 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
495 return;
497 if (program)
498 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
499 else
500 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
502 /* A size of 1 is just a null-terminated string, so the log should be bigger than
503 * that if there are errors. */
504 if (length > 1)
506 const char *ptr, *end, *line;
508 log = heap_alloc(length);
509 if (program)
510 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
511 else
512 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
514 ptr = log;
515 /* The info log is supposed to be zero-terminated. Note that at least
516 * some versions of fglrx don't terminate the string properly. The
517 * reported length does include the supposed terminator though, so we
518 * don't care here. */
519 end = &ptr[length - 1];
520 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
522 WARN("Info log received from GLSL shader #%u:\n", id);
523 while ((line = wined3d_get_line(&ptr, end)))
525 WARN(" %.*s", (int)(ptr - line), line);
528 else
530 FIXME("Info log received from GLSL shader #%u:\n", id);
531 while ((line = wined3d_get_line(&ptr, end)))
533 FIXME(" %.*s", (int)(ptr - line), line);
536 heap_free(log);
540 /* Context activation is done by the caller. */
541 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
543 const char *ptr, *end, *line;
545 TRACE("Compiling shader object %u.\n", shader);
547 if (TRACE_ON(d3d_shader))
549 ptr = src;
550 end = ptr + strlen(ptr);
551 while ((line = wined3d_get_line(&ptr, end)))
553 TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
557 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
558 checkGLcall("glShaderSource");
559 GL_EXTCALL(glCompileShader(shader));
560 checkGLcall("glCompileShader");
561 print_glsl_info_log(gl_info, shader, FALSE);
564 /* Context activation is done by the caller. */
565 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
567 GLint i, shader_count, source_size = -1;
568 GLuint *shaders;
569 char *source = NULL;
571 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
572 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
574 ERR("Failed to allocate shader array memory.\n");
575 return;
578 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
579 for (i = 0; i < shader_count; ++i)
581 const char *ptr, *end, *line;
582 GLint tmp;
584 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
586 if (source_size < tmp)
588 heap_free(source);
590 if (!(source = heap_alloc(tmp)))
592 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
593 heap_free(shaders);
594 return;
596 source_size = tmp;
599 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
600 ptr = source;
601 end = &ptr[tmp - 1];
603 FIXME("Shader %u:\n", shaders[i]);
604 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
605 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
606 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
607 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
608 FIXME("\n");
609 while ((line = wined3d_get_line(&ptr, end)))
611 FIXME(" %.*s", (int)(ptr - line), line);
613 FIXME("\n");
616 heap_free(source);
617 heap_free(shaders);
620 /* Context activation is done by the caller. */
621 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
623 GLint tmp;
625 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
626 return;
628 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
629 if (!tmp)
631 FIXME("Program %u link status invalid.\n", program);
632 shader_glsl_dump_program_source(gl_info, program);
635 print_glsl_info_log(gl_info, program, TRUE);
638 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
640 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
641 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
642 * versions. */
643 return shader_glsl_get_version(gl_info) >= 140;
646 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
648 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
651 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
652 struct shader_glsl_priv *priv, GLuint program_id,
653 const struct wined3d_shader_reg_maps *reg_maps)
655 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
656 struct wined3d_string_buffer *name;
657 unsigned int i, base, count;
658 GLuint block_idx;
660 if (shader_glsl_use_layout_binding_qualifier(gl_info))
661 return;
663 name = string_buffer_get(&priv->string_buffers);
664 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
665 for (i = 0; i < count; ++i)
667 if (!reg_maps->cb_sizes[i])
668 continue;
670 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
671 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
672 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
674 checkGLcall("glUniformBlockBinding");
675 string_buffer_release(&priv->string_buffers, name);
678 /* Context activation is done by the caller. */
679 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
680 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
681 unsigned int base, unsigned int count, const unsigned int *tex_unit_map)
683 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
684 unsigned int i, mapped_unit;
685 GLint name_loc;
687 for (i = 0; i < count; ++i)
689 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
690 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
691 if (name_loc == -1)
692 continue;
694 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
695 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
697 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
698 continue;
701 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
702 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
704 checkGLcall("Load sampler bindings");
705 string_buffer_release(&priv->string_buffers, sampler_name);
708 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
709 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
711 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
712 const unsigned int *tex_unit_map;
713 unsigned int base, count;
715 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
716 if (sampler_idx >= count)
717 return WINED3D_UNMAPPED_STAGE;
718 if (!tex_unit_map)
719 return base + sampler_idx;
720 return tex_unit_map[base + sampler_idx];
723 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
724 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
725 unsigned int sampler_idx)
727 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
728 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
729 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
730 else
731 ERR("Unmapped sampler %u.\n", sampler_idx);
734 /* Context activation is done by the caller. */
735 static void shader_glsl_load_samplers(const struct wined3d_context *context,
736 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
738 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
739 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
740 const struct wined3d_shader_version *shader_version;
741 const unsigned int *tex_unit_map;
742 unsigned int base, count;
743 const char *prefix;
745 if (shader_glsl_use_layout_binding_qualifier(gl_info))
746 return;
748 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
749 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
750 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
751 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
754 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
755 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
757 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
759 if (icb)
761 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
762 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
763 GLint icb_location;
765 string_buffer_sprintf(icb_name, "%s_icb", prefix);
766 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
767 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
768 checkGLcall("Load immediate constant buffer");
770 string_buffer_release(&priv->string_buffers, icb_name);
774 /* Context activation is done by the caller. */
775 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
776 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
778 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
779 struct wined3d_string_buffer *name;
780 GLint location;
781 unsigned int i;
783 if (shader_glsl_use_layout_binding_qualifier(gl_info))
784 return;
786 name = string_buffer_get(&priv->string_buffers);
787 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
789 if (!reg_maps->uav_resource_info[i].type)
790 continue;
792 string_buffer_sprintf(name, "%s_image%u", prefix, i);
793 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
794 if (location == -1)
795 continue;
797 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
798 GL_EXTCALL(glUniform1i(location, i));
800 checkGLcall("Load image bindings");
801 string_buffer_release(&priv->string_buffers, name);
804 /* Context activation is done by the caller. */
805 static void shader_glsl_load_program_resources(const struct wined3d_context_gl *context_gl,
806 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
808 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
810 shader_glsl_init_uniform_block_bindings(context_gl->gl_info, priv, program_id, reg_maps);
811 shader_glsl_load_icb(context_gl->gl_info, priv, program_id, reg_maps);
812 /* Texture unit mapping is set up to be the same each time the shader
813 * program is used so we can hardcode the sampler uniform values. */
814 shader_glsl_load_samplers(&context_gl->c, priv, program_id, reg_maps);
817 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
818 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
820 if (varyings && *strings)
822 char *ptr = *strings;
824 varyings[*varying_count] = ptr;
826 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
827 ptr += buffer->content_size + 1;
829 *strings = ptr;
832 *strings_length += buffer->content_size + 1;
833 ++(*varying_count);
836 static void append_transform_feedback_skip_components(const char **varyings,
837 unsigned int *varying_count, char **strings, unsigned int *strings_length,
838 struct wined3d_string_buffer *buffer, unsigned int component_count)
840 unsigned int j;
842 for (j = 0; j < component_count / 4; ++j)
844 string_buffer_sprintf(buffer, "gl_SkipComponents4");
845 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
847 if (component_count % 4)
849 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
850 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
854 static BOOL shader_glsl_generate_transform_feedback_varyings(struct wined3d_string_buffer *buffer,
855 const char **varyings, unsigned int *varying_count, char *strings, unsigned int *strings_length,
856 GLenum buffer_mode, struct wined3d_shader *shader)
858 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
859 unsigned int buffer_idx, count, length, highest_output_slot, stride;
860 unsigned int i, register_idx, component_idx;
861 BOOL have_varyings_to_record = FALSE;
863 count = length = 0;
864 highest_output_slot = 0;
865 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
867 stride = 0;
869 for (i = 0; i < so_desc->element_count; ++i)
871 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
873 highest_output_slot = max(highest_output_slot, e->output_slot);
874 if (e->output_slot != buffer_idx)
875 continue;
877 if (e->stream_idx)
879 FIXME("Unhandled stream %u.\n", e->stream_idx);
880 continue;
883 stride += e->component_count;
885 if (!e->semantic_name)
887 append_transform_feedback_skip_components(varyings, &count,
888 &strings, &length, buffer, e->component_count);
889 continue;
892 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
893 continue;
895 if (component_idx || e->component_count != 4)
897 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
899 FIXME("Unsupported component range %u-%u.\n", component_idx, e->component_count);
900 append_transform_feedback_skip_components(varyings, &count,
901 &strings, &length, buffer, e->component_count);
902 continue;
905 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
906 register_idx, component_idx, component_idx + e->component_count - 1);
907 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
909 else
911 string_buffer_sprintf(buffer, "shader_in_out.reg%u", register_idx);
912 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
915 have_varyings_to_record = TRUE;
918 if (buffer_idx < so_desc->buffer_stride_count
919 && stride < so_desc->buffer_strides[buffer_idx] / 4)
921 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
922 append_transform_feedback_skip_components(varyings, &count,
923 &strings, &length, buffer, component_count);
926 if (highest_output_slot <= buffer_idx)
927 break;
929 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
931 string_buffer_sprintf(buffer, "gl_NextBuffer");
932 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
936 if (varying_count)
937 *varying_count = count;
938 if (strings_length)
939 *strings_length = length;
941 return have_varyings_to_record;
944 static void shader_glsl_init_transform_feedback(const struct wined3d_context_gl *context_gl,
945 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
947 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
948 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
949 struct wined3d_string_buffer *buffer;
950 unsigned int i, count, length;
951 const char **varyings;
952 char *strings;
953 GLenum mode;
955 if (!so_desc)
956 return;
958 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
960 mode = GL_INTERLEAVED_ATTRIBS;
962 else
964 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
966 for (i = 0; i < so_desc->element_count; ++i)
968 if (!so_desc->elements[i].semantic_name)
970 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
971 return;
973 ++element_count[so_desc->elements[i].output_slot];
976 if (element_count[0] == so_desc->element_count)
978 mode = GL_INTERLEAVED_ATTRIBS;
980 else
982 mode = GL_SEPARATE_ATTRIBS;
983 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
985 if (element_count[i] != 1)
986 break;
988 for (; i < ARRAY_SIZE(element_count); ++i)
990 if (element_count[i])
992 FIXME("Only single element per buffer is allowed in separate mode.\n");
993 return;
999 buffer = string_buffer_get(&priv->string_buffers);
1001 if (!shader_glsl_generate_transform_feedback_varyings(buffer, NULL, &count, NULL, &length, mode, shader))
1003 FIXME("No varyings to record, disabling transform feedback.\n");
1004 shader->u.gs.so_desc = NULL;
1005 string_buffer_release(&priv->string_buffers, buffer);
1006 return;
1009 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
1011 ERR("Out of memory.\n");
1012 string_buffer_release(&priv->string_buffers, buffer);
1013 return;
1015 if (!(strings = heap_calloc(length, sizeof(*strings))))
1017 ERR("Out of memory.\n");
1018 heap_free(varyings);
1019 string_buffer_release(&priv->string_buffers, buffer);
1020 return;
1023 shader_glsl_generate_transform_feedback_varyings(buffer, varyings, NULL, strings, NULL, mode, shader);
1024 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
1025 checkGLcall("glTransformFeedbackVaryings");
1027 heap_free(varyings);
1028 heap_free(strings);
1029 string_buffer_release(&priv->string_buffers, buffer);
1032 /* Context activation is done by the caller. */
1033 static void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
1034 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
1036 unsigned int start = ~0U, end = 0;
1037 int stack_idx = 0;
1038 unsigned int heap_idx = 1;
1039 unsigned int idx;
1041 if (heap->entries[heap_idx].version <= version) return;
1043 idx = heap->entries[heap_idx].idx;
1044 if (constant_locations[idx] != -1)
1045 start = end = idx;
1046 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1048 while (stack_idx >= 0)
1050 /* Note that we fall through to the next case statement. */
1051 switch(stack[stack_idx])
1053 case HEAP_NODE_TRAVERSE_LEFT:
1055 unsigned int left_idx = heap_idx << 1;
1056 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1058 heap_idx = left_idx;
1059 idx = heap->entries[heap_idx].idx;
1060 if (constant_locations[idx] != -1)
1062 if (start > idx)
1063 start = idx;
1064 if (end < idx)
1065 end = idx;
1068 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1069 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1070 break;
1074 case HEAP_NODE_TRAVERSE_RIGHT:
1076 unsigned int right_idx = (heap_idx << 1) + 1;
1077 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1079 heap_idx = right_idx;
1080 idx = heap->entries[heap_idx].idx;
1081 if (constant_locations[idx] != -1)
1083 if (start > idx)
1084 start = idx;
1085 if (end < idx)
1086 end = idx;
1089 stack[stack_idx++] = HEAP_NODE_POP;
1090 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1091 break;
1095 case HEAP_NODE_POP:
1096 heap_idx >>= 1;
1097 --stack_idx;
1098 break;
1101 if (start <= end)
1102 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1103 checkGLcall("walk_constant_heap()");
1106 /* Context activation is done by the caller. */
1107 static void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1108 GLint location, const struct wined3d_vec4 *data)
1110 GLfloat clamped_constant[4];
1112 if (location == -1) return;
1114 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1115 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1116 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1117 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1119 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1122 /* Context activation is done by the caller. */
1123 static void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1124 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1125 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1127 int stack_idx = 0;
1128 unsigned int heap_idx = 1;
1129 unsigned int idx;
1131 if (heap->entries[heap_idx].version <= version) return;
1133 idx = heap->entries[heap_idx].idx;
1134 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1135 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1137 while (stack_idx >= 0)
1139 /* Note that we fall through to the next case statement. */
1140 switch(stack[stack_idx])
1142 case HEAP_NODE_TRAVERSE_LEFT:
1144 unsigned int left_idx = heap_idx << 1;
1145 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1147 heap_idx = left_idx;
1148 idx = heap->entries[heap_idx].idx;
1149 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1151 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1152 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1153 break;
1157 case HEAP_NODE_TRAVERSE_RIGHT:
1159 unsigned int right_idx = (heap_idx << 1) + 1;
1160 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1162 heap_idx = right_idx;
1163 idx = heap->entries[heap_idx].idx;
1164 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1166 stack[stack_idx++] = HEAP_NODE_POP;
1167 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1168 break;
1172 case HEAP_NODE_POP:
1173 heap_idx >>= 1;
1174 --stack_idx;
1175 break;
1178 checkGLcall("walk_constant_heap_clamped()");
1181 /* Context activation is done by the caller. */
1182 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, struct wined3d_context_gl *context_gl,
1183 struct wined3d_buffer *constant_buffer, const GLint *constant_locations, const struct constant_heap *heap,
1184 unsigned char *stack, unsigned int version)
1186 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1187 const struct wined3d_shader_lconst *lconst;
1189 if (constant_buffer)
1191 const struct wined3d_vec4 *constants = wined3d_buffer_load_sysmem(constant_buffer, &context_gl->c);
1193 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1194 if (shader->reg_maps.shader_version.major == 1
1195 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1196 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1197 else
1198 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1201 if (!shader->load_local_constsF)
1203 TRACE("No need to load local float constants for this shader.\n");
1204 return;
1207 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1208 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1210 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1212 checkGLcall("glUniform4fv()");
1215 /* Context activation is done by the caller. */
1216 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, struct wined3d_context_gl *context_gl,
1217 struct wined3d_buffer *constant_buffer, const GLint locations[WINED3D_MAX_CONSTS_I], uint32_t constants_set)
1219 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1220 unsigned int i;
1221 struct list* ptr;
1223 if (constant_buffer)
1225 const struct wined3d_ivec4 *constants = wined3d_buffer_load_sysmem(constant_buffer, &context_gl->c);
1227 while (constants_set)
1229 /* We found this uniform name in the program - go ahead and send the data */
1230 i = wined3d_bit_scan(&constants_set);
1231 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1235 /* Load immediate constants */
1236 ptr = list_head(&shader->constantsI);
1237 while (ptr)
1239 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1240 unsigned int idx = lconst->idx;
1241 const GLint *values = (const GLint *)lconst->value;
1243 /* We found this uniform name in the program - go ahead and send the data */
1244 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1245 ptr = list_next(&shader->constantsI, ptr);
1247 checkGLcall("glUniform4iv()");
1250 /* Context activation is done by the caller. */
1251 static void shader_glsl_load_constants_b(const struct wined3d_shader *shader, struct wined3d_context_gl *context_gl,
1252 struct wined3d_buffer *constant_buffer, const GLint locations[WINED3D_MAX_CONSTS_B], uint32_t constants_set)
1254 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1255 unsigned int i;
1256 struct list* ptr;
1258 if (constant_buffer)
1260 const BOOL *constants = wined3d_buffer_load_sysmem(constant_buffer, &context_gl->c);
1262 while (constants_set)
1264 i = wined3d_bit_scan(&constants_set);
1265 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1269 /* Load immediate constants */
1270 ptr = list_head(&shader->constantsB);
1271 while (ptr)
1273 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1274 unsigned int idx = lconst->idx;
1275 const GLint *values = (const GLint *)lconst->value;
1277 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1278 ptr = list_next(&shader->constantsB, ptr);
1280 checkGLcall("glUniform1iv()");
1283 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1285 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1288 /* Context activation is done by the caller (state handler). */
1289 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1290 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1292 struct
1294 float sx, sy;
1296 np2fixup_constants[WINED3D_MAX_FRAGMENT_SAMPLERS];
1297 UINT fixup = ps->np2_fixup_info->active;
1298 UINT i;
1300 for (i = 0; fixup; fixup >>= 1, ++i)
1302 const struct wined3d_texture *tex = state->textures[i];
1303 unsigned char idx = ps->np2_fixup_info->idx[i];
1305 if (!tex)
1307 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1308 continue;
1311 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1312 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1315 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1318 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1320 struct wined3d_matrix temp;
1321 unsigned int i, j;
1323 for (i = 0; i < 4; ++i)
1324 for (j = 0; j < 4; ++j)
1325 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1327 *out = temp;
1330 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context_gl *context_gl,
1331 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1333 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1334 struct wined3d_matrix mv;
1335 float mat[3 * 3];
1337 if (prog->vs.normal_matrix_location == -1)
1338 return;
1340 get_modelview_matrix(&context_gl->c, state, 0, &mv);
1341 compute_normal_matrix(mat, context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING, &mv);
1343 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1344 checkGLcall("glUniformMatrix3fv");
1347 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context_gl *context_gl,
1348 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1350 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1351 struct wined3d_matrix mat;
1353 if (tex >= WINED3D_MAX_FFP_TEXTURES)
1354 return;
1355 if (prog->vs.texture_matrix_location[tex] == -1)
1356 return;
1358 get_texture_matrix(&context_gl->c, state, tex, &mat);
1359 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1360 checkGLcall("glUniformMatrix4fv");
1363 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context_gl *context_gl,
1364 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1366 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1368 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1370 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1371 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1373 else
1375 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1377 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1379 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1380 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1381 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1382 checkGLcall("setting FFP material uniforms");
1385 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context_gl *context_gl,
1386 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1388 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1389 struct wined3d_color color;
1391 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1392 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1393 checkGLcall("glUniform3fv");
1396 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context_gl *context_gl,
1397 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1398 struct glsl_shader_prog_link *prog)
1400 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1401 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1402 struct wined3d_vec4 vec4;
1404 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1405 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1406 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1408 switch (light_info->OriginalParms.type)
1410 case WINED3D_LIGHT_POINT:
1411 wined3d_vec4_transform(&vec4, &light_info->position, view);
1412 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1413 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1414 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1415 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1416 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1417 break;
1419 case WINED3D_LIGHT_SPOT:
1420 wined3d_vec4_transform(&vec4, &light_info->position, view);
1421 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1423 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1424 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1426 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1427 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1428 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1429 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1430 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1431 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1432 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1433 break;
1435 case WINED3D_LIGHT_DIRECTIONAL:
1436 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1437 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1438 break;
1440 case WINED3D_LIGHT_PARALLELPOINT:
1441 wined3d_vec4_transform(&vec4, &light_info->position, view);
1442 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1443 break;
1445 default:
1446 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1448 checkGLcall("setting FFP lights uniforms");
1451 static void shader_glsl_pointsize_uniform(const struct wined3d_context_gl *context_gl,
1452 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1454 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1455 float size, att[3];
1456 float min, max;
1458 get_pointsize_minmax(&context_gl->c, state, &min, &max);
1460 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1461 checkGLcall("glUniform1f");
1462 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1463 checkGLcall("glUniform1f");
1465 get_pointsize(&context_gl->c, state, &size, att);
1467 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1468 checkGLcall("glUniform1f");
1469 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1470 checkGLcall("glUniform1f");
1471 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1472 checkGLcall("glUniform1f");
1473 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1474 checkGLcall("glUniform1f");
1477 static void shader_glsl_load_fog_uniform(const struct wined3d_context_gl *context_gl,
1478 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1480 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1481 struct wined3d_color color;
1482 float start, end, scale;
1483 union
1485 DWORD d;
1486 float f;
1487 } tmpvalue;
1489 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1490 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1491 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1492 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1493 get_fog_start_end(&context_gl->c, state, &start, &end);
1494 scale = 1.0f / (end - start);
1495 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1496 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1497 checkGLcall("fog emulation uniforms");
1500 static void shader_glsl_clip_plane_uniform(const struct wined3d_context_gl *context_gl,
1501 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1503 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1504 struct wined3d_matrix matrix;
1505 struct wined3d_vec4 plane;
1507 plane = state->clip_planes[index];
1509 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1510 if (!use_vs(state))
1512 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1513 transpose_matrix(&matrix, &matrix);
1514 wined3d_vec4_transform(&plane, &plane, &matrix);
1517 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1520 /* Context activation is done by the caller (state handler). */
1521 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1522 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1524 struct wined3d_color float_key[2];
1525 const struct wined3d_texture *texture = state->textures[0];
1527 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1528 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1531 /* Context activation is done by the caller (state handler). */
1532 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1533 const struct wined3d_state *state)
1535 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1536 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1537 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1538 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1539 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1540 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1541 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1542 struct wined3d_device *device = context->device;
1543 struct shader_glsl_priv *priv = shader_priv;
1544 unsigned int constant_version;
1545 DWORD update_mask;
1546 int i;
1548 /* No GLSL program set - nothing to do. */
1549 if (!prog)
1550 return;
1552 constant_version = prog->constant_version;
1553 update_mask = context->constant_update_mask & prog->constant_update_mask;
1555 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1556 shader_glsl_load_constants_f(vshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_VS_F],
1557 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1559 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1560 shader_glsl_load_constants_i(vshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_VS_I],
1561 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1563 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1564 shader_glsl_load_constants_b(vshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_VS_B],
1565 prog->vs.uniform_b_locations, vshader->reg_maps.boolean_constants);
1567 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1569 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1570 shader_glsl_clip_plane_uniform(context_gl, state, i, prog);
1573 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1574 shader_glsl_pointsize_uniform(context_gl, state, prog);
1576 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1578 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1579 max(state->viewport_count, 1) : 1;
1580 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1581 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1582 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1583 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1584 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1585 else
1586 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1587 checkGLcall("glUniform4fv");
1590 if (update_mask & WINED3D_SHADER_CONST_BASE_VERTEX_ID)
1592 GL_EXTCALL(glUniform1i(prog->vs.base_vertex_id_location, state->base_vertex_index));
1593 checkGLcall("base vertex id");
1596 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1598 struct wined3d_matrix mat;
1600 get_modelview_matrix(context, state, 0, &mat);
1601 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1602 checkGLcall("glUniformMatrix4fv");
1604 shader_glsl_ffp_vertex_normalmatrix_uniform(context_gl, state, prog);
1607 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1609 struct wined3d_matrix mat;
1611 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1613 if (prog->vs.modelview_matrix_location[i] == -1)
1614 break;
1616 get_modelview_matrix(context, state, i, &mat);
1617 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1618 checkGLcall("glUniformMatrix4fv");
1622 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1624 struct wined3d_matrix projection;
1626 get_projection_matrix(context, state, &projection);
1627 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1628 checkGLcall("glUniformMatrix4fv");
1631 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1633 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
1634 shader_glsl_ffp_vertex_texmatrix_uniform(context_gl, state, i, prog);
1637 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1638 shader_glsl_ffp_vertex_material_uniform(context_gl, state, prog);
1640 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1642 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1643 DWORD point_count = 0;
1644 DWORD spot_count = 0;
1645 DWORD directional_count = 0;
1647 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1649 if (!state->light_state.lights[i])
1650 continue;
1652 switch (state->light_state.lights[i]->OriginalParms.type)
1654 case WINED3D_LIGHT_POINT:
1655 ++point_count;
1656 break;
1657 case WINED3D_LIGHT_SPOT:
1658 ++spot_count;
1659 break;
1660 case WINED3D_LIGHT_DIRECTIONAL:
1661 ++directional_count;
1662 break;
1663 case WINED3D_LIGHT_PARALLELPOINT:
1664 break;
1665 default:
1666 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
1667 break;
1670 point_idx = 0;
1671 spot_idx = point_idx + point_count;
1672 directional_idx = spot_idx + spot_count;
1673 parallel_point_idx = directional_idx + directional_count;
1675 shader_glsl_ffp_vertex_lightambient_uniform(context_gl, state, prog);
1676 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1678 const struct wined3d_light_info *light_info = state->light_state.lights[i];
1679 unsigned int idx;
1681 if (!light_info)
1682 continue;
1684 switch (light_info->OriginalParms.type)
1686 case WINED3D_LIGHT_POINT:
1687 idx = point_idx++;
1688 break;
1689 case WINED3D_LIGHT_SPOT:
1690 idx = spot_idx++;
1691 break;
1692 case WINED3D_LIGHT_DIRECTIONAL:
1693 idx = directional_idx++;
1694 break;
1695 case WINED3D_LIGHT_PARALLELPOINT:
1696 idx = parallel_point_idx++;
1697 break;
1698 default:
1699 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1700 continue;
1702 shader_glsl_ffp_vertex_light_uniform(context_gl, state, idx, light_info, prog);
1706 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1707 shader_glsl_load_constants_f(pshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_PS_F],
1708 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1710 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1711 shader_glsl_load_constants_i(pshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_PS_I],
1712 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1714 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1715 shader_glsl_load_constants_b(pshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_PS_B],
1716 prog->ps.uniform_b_locations, pshader->reg_maps.boolean_constants);
1718 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1720 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
1722 if (prog->ps.bumpenv_mat_location[i] == -1)
1723 continue;
1725 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1726 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1728 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1730 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1731 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1732 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1733 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1737 checkGLcall("bump env uniforms");
1740 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1742 const struct wined3d_vec4 correction_params =
1744 /* Position is relative to the framebuffer, not the viewport. */
1745 context->render_offscreen ? 0.0f : (float)state->fb.render_targets[0]->height,
1746 context->render_offscreen ? 1.0f : -1.0f,
1747 0.0f,
1748 0.0f,
1751 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1754 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1755 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1756 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1757 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1759 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1761 struct wined3d_color color;
1763 if (prog->ps.tex_factor_location != -1)
1765 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1766 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1769 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1770 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1771 else
1772 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1774 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
1776 if (prog->ps.tss_constant_location[i] == -1)
1777 continue;
1779 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1780 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1783 checkGLcall("fixed function uniforms");
1786 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1787 shader_glsl_load_fog_uniform(context_gl, state, prog);
1789 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1791 float ref = wined3d_alpha_ref(state);
1793 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1794 checkGLcall("alpha test emulation uniform");
1797 if (priv->next_constant_version == UINT_MAX)
1799 TRACE("Max constant version reached, resetting to 0.\n");
1800 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1801 priv->next_constant_version = 1;
1803 else
1805 prog->constant_version = priv->next_constant_version++;
1809 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1811 struct constant_entry *entries = heap->entries;
1812 unsigned int *positions = heap->positions;
1813 unsigned int heap_idx, parent_idx;
1815 if (!heap->contained[idx])
1817 heap_idx = heap->size++;
1818 heap->contained[idx] = TRUE;
1820 else
1822 heap_idx = positions[idx];
1825 while (heap_idx > 1)
1827 parent_idx = heap_idx >> 1;
1829 if (new_version <= entries[parent_idx].version) break;
1831 entries[heap_idx] = entries[parent_idx];
1832 positions[entries[parent_idx].idx] = heap_idx;
1833 heap_idx = parent_idx;
1836 entries[heap_idx].version = new_version;
1837 entries[heap_idx].idx = idx;
1838 positions[idx] = heap_idx;
1841 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1843 struct shader_glsl_priv *priv = device->shader_priv;
1844 struct constant_heap *heap = &priv->vconst_heap;
1845 UINT i;
1847 for (i = start; i < count + start; ++i)
1849 update_heap_entry(heap, i, priv->next_constant_version);
1853 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1855 struct shader_glsl_priv *priv = device->shader_priv;
1856 struct constant_heap *heap = &priv->pconst_heap;
1857 UINT i;
1859 for (i = start; i < count + start; ++i)
1861 update_heap_entry(heap, i, priv->next_constant_version);
1865 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1867 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1868 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1869 if(shader_major > 3) return ret;
1871 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1872 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1873 return ret;
1876 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1878 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
1881 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1883 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
1884 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
1887 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
1889 return shader_glsl_get_version(gl_info) >= 150;
1892 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1894 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1897 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1898 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1900 va_list args;
1901 int ret;
1903 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1904 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1905 for (;;)
1907 va_start(args, format);
1908 ret = shader_vaddline(buffer, format, args);
1909 va_end(args);
1910 if (!ret)
1911 return;
1912 if (!string_buffer_resize(buffer, ret))
1913 return;
1917 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1918 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1920 va_list args;
1921 int ret;
1923 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1924 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1925 for (;;)
1927 va_start(args, format);
1928 ret = shader_vaddline(buffer, format, args);
1929 va_end(args);
1930 if (!ret)
1931 return;
1932 if (!string_buffer_resize(buffer, ret))
1933 return;
1937 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
1939 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
1942 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
1944 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
1947 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
1949 switch (mode)
1951 case WINED3DSIM_CONSTANT:
1952 return "flat ";
1953 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
1954 return "noperspective ";
1955 default:
1956 FIXME("Unhandled interpolation mode %#x.\n", mode);
1957 case WINED3DSIM_NONE:
1958 case WINED3DSIM_LINEAR:
1959 return "";
1963 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
1964 const uint32_t *packed_interpolation_mode, unsigned int register_idx)
1966 return wined3d_extract_bits(packed_interpolation_mode,
1967 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
1970 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
1971 struct wined3d_string_buffer *buffer, unsigned int element_count,
1972 const uint32_t *interpolation_mode, BOOL unroll)
1974 enum wined3d_shader_interpolation_mode mode;
1975 unsigned int i;
1977 if (shader_glsl_use_interface_blocks(gl_info))
1979 if (unroll)
1981 shader_addline(buffer, "in shader_in_out {\n");
1982 for (i = 0; i < element_count; ++i)
1984 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
1985 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
1987 shader_addline(buffer, "} shader_in;\n");
1989 else
1991 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
1994 else
1996 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2000 static BOOL needs_interpolation_qualifiers_for_shader_outputs(const struct wined3d_gl_info *gl_info)
2002 /* In GLSL 4.40+ it is fine to specify interpolation qualifiers only in
2003 * fragment shaders. In older GLSL versions interpolation qualifiers must
2004 * match between shader stages. */
2005 return gl_info->glsl_version < MAKEDWORD_VERSION(4, 40);
2008 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2009 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2010 const uint32_t *interpolation_mode)
2012 enum wined3d_shader_interpolation_mode mode;
2013 unsigned int i;
2015 if (shader_glsl_use_interface_blocks(gl_info))
2017 if (rasterizer_setup)
2019 shader_addline(buffer, "out shader_in_out {\n");
2020 for (i = 0; i < element_count; ++i)
2022 const char *interpolation_qualifiers = "";
2023 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2025 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2026 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2028 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2030 shader_addline(buffer, "} shader_out;\n");
2032 else
2034 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2037 else
2039 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2043 static BOOL use_legacy_fragment_output(const struct wined3d_gl_info *gl_info)
2045 /* Technically 1.30 does support user-defined fragment shader outputs but
2046 * we might not have glBindFragDataLocation() available (i.e. GL version
2047 * might be < 3.0). */
2048 return gl_info->glsl_version <= MAKEDWORD_VERSION(1, 30);
2051 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2053 return use_legacy_fragment_output(gl_info) ? "gl_FragData" : "ps_out";
2056 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2058 switch (primitive_type)
2060 case WINED3D_PT_POINTLIST:
2061 return "points";
2063 case WINED3D_PT_LINELIST:
2064 return "lines";
2066 case WINED3D_PT_LINESTRIP:
2067 return "line_strip";
2069 case WINED3D_PT_TRIANGLELIST:
2070 return "triangles";
2072 case WINED3D_PT_TRIANGLESTRIP:
2073 return "triangle_strip";
2075 case WINED3D_PT_LINELIST_ADJ:
2076 return "lines_adjacency";
2078 case WINED3D_PT_TRIANGLELIST_ADJ:
2079 return "triangles_adjacency";
2081 default:
2082 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2083 return "";
2087 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2089 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2090 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2091 DWORD input_reg_used = shader->u.ps.input_reg_used;
2092 unsigned int i;
2094 if (reg_maps->shader_version.major < 3)
2095 return input_reg_used & (1u << idx);
2097 for (i = 0; i < input_signature->element_count; ++i)
2099 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2101 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2102 continue;
2104 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2105 && input->semantic_idx == idx)
2106 return input_reg_used & (1u << input->register_idx);
2108 return FALSE;
2111 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2112 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2113 unsigned int index)
2115 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2116 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2117 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2118 index, scalar_type, scalar_type, index);
2121 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2122 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2124 unsigned int index = e->register_idx;
2125 enum wined3d_component_type type;
2127 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2129 shader_addline(buffer, "uniform int base_vertex_id;\n");
2130 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID - base_vertex_id), 0.0, 0.0, 0.0);\n", index);
2131 return;
2133 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2135 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2136 index);
2137 return;
2139 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2140 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2142 if (shader_glsl_use_explicit_attrib_location(gl_info))
2143 shader_addline(buffer, "layout(location = %u) ", index);
2145 type = e->component_type;
2146 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2148 FIXME("Unhandled type %#x.\n", type);
2149 type = WINED3D_TYPE_FLOAT;
2151 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2152 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2153 else
2154 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2155 component_type_info[type].glsl_vector_type,
2156 component_type_info[type].glsl_scalar_type, index);
2159 /** Generate the variable & register declarations for the GLSL output target */
2160 static void shader_generate_glsl_declarations(const struct wined3d_context_gl *context_gl,
2161 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2162 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2164 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2165 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2166 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2167 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2168 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2169 unsigned int uniform_block_base, uniform_block_count;
2170 enum wined3d_shader_resource_type resource_type;
2171 const struct wined3d_shader_lconst *lconst;
2172 const char *prefix;
2173 unsigned int i;
2174 uint32_t map;
2176 if (wined3d_settings.strict_shader_math)
2177 shader_addline(buffer, "#pragma optionNV(fastmath off)\n");
2179 prefix = shader_glsl_get_prefix(version->type);
2181 /* Prototype the subroutines */
2182 map = reg_maps->labels;
2183 while (map)
2185 i = wined3d_bit_scan(&map);
2186 shader_addline(buffer, "void subroutine%u();\n", i);
2189 if (version->type != WINED3D_SHADER_TYPE_PIXEL && version->type != WINED3D_SHADER_TYPE_COMPUTE)
2191 if (version->type == WINED3D_SHADER_TYPE_HULL)
2192 shader_addline(buffer, "out gl_PerVertex { invariant vec4 gl_Position; } gl_out[];\n");
2193 else
2194 shader_addline(buffer, "invariant gl_Position;\n");
2197 /* Declare the constants (aka uniforms) */
2198 if (shader->limits->constant_float > 0)
2200 unsigned max_constantsF;
2202 /* Unless the shader uses indirect addressing, always declare the
2203 * maximum array size and ignore that we need some uniforms privately.
2204 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2205 * and immediate values, still declare VC[256]. If the shader needs
2206 * more uniforms than we have it won't work in any case. If it uses
2207 * less, the compiler will figure out which uniforms are really used
2208 * and strip them out. This allows a shader to use c255 on a dx9 card,
2209 * as long as it doesn't also use all the other constants.
2211 * If the shader uses indirect addressing the compiler must assume
2212 * that all declared uniforms are used. In this case, declare only the
2213 * amount that we're assured to have.
2215 * Thus we run into problems in these two cases:
2216 * 1) The shader really uses more uniforms than supported.
2217 * 2) The shader uses indirect addressing, less constants than
2218 * supported, but uses a constant index > #supported consts. */
2219 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2221 /* No indirect addressing here. */
2222 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2224 else
2226 if (reg_maps->usesrelconstF)
2228 /* Subtract the other potential uniforms from the max
2229 * available (bools, ints, and 1 row of projection matrix).
2230 * Subtract another uniform for immediate values, which have
2231 * to be loaded via uniform by the driver as well. The shader
2232 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2233 * shader code, so one vec4 should be enough. (Unfortunately
2234 * the Nvidia driver doesn't store 128 and -128 in one float).
2236 * Writing gl_ClipVertex requires one uniform for each
2237 * clipplane as well. */
2238 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2239 if (vs_args->clip_enabled)
2240 max_constantsF -= gl_info->limits.user_clip_distances;
2241 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2242 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2243 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2244 * for now take this into account when calculating the number of available constants
2246 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2247 /* Set by driver quirks in directx.c */
2248 max_constantsF -= gl_info->reserved_glsl_constants;
2250 if (max_constantsF < shader->limits->constant_float)
2252 static unsigned int once;
2254 if (!once++)
2255 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2256 " it may not render correctly.\n");
2257 else
2258 WARN("The hardware does not support enough uniform components to run this shader.\n");
2261 else
2263 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2266 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2267 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2270 /* Always declare the full set of constants, the compiler can remove the
2271 * unused ones because d3d doesn't (yet) support indirect int and bool
2272 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2273 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2274 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2276 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2277 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2279 /* Declare immediate constant buffer */
2280 if (reg_maps->icb)
2281 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2283 /* Declare constant buffers */
2284 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2285 &uniform_block_base, &uniform_block_count);
2286 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2288 if (reg_maps->cb_sizes[i])
2290 shader_addline(buffer, "layout(std140");
2291 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2292 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2293 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2294 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2298 /* Declare texture samplers */
2299 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2301 struct wined3d_shader_sampler_map_entry *entry;
2302 const char *sampler_type_prefix, *sampler_type;
2303 BOOL shadow_sampler, tex_rect;
2305 entry = &reg_maps->sampler_map.entries[i];
2307 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2309 ERR("Invalid resource index %u.\n", entry->resource_idx);
2310 continue;
2313 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2315 case WINED3D_DATA_FLOAT:
2316 case WINED3D_DATA_UNORM:
2317 case WINED3D_DATA_SNORM:
2318 sampler_type_prefix = "";
2319 break;
2321 case WINED3D_DATA_INT:
2322 sampler_type_prefix = "i";
2323 break;
2325 case WINED3D_DATA_UINT:
2326 sampler_type_prefix = "u";
2327 break;
2329 default:
2330 sampler_type_prefix = "";
2331 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2332 break;
2335 shadow_sampler = shader_sampler_is_shadow(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2336 resource_type = version->type == WINED3D_SHADER_TYPE_PIXEL
2337 ? pixelshader_get_resource_type(reg_maps, entry->resource_idx, ps_args->tex_types)
2338 : reg_maps->resource_info[entry->resource_idx].type;
2340 switch (resource_type)
2342 case WINED3D_SHADER_RESOURCE_BUFFER:
2343 sampler_type = "samplerBuffer";
2344 break;
2346 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2347 if (shadow_sampler)
2348 sampler_type = "sampler1DShadow";
2349 else
2350 sampler_type = "sampler1D";
2351 break;
2353 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2354 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2355 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2356 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2357 if (shadow_sampler)
2359 if (tex_rect)
2360 sampler_type = "sampler2DRectShadow";
2361 else
2362 sampler_type = "sampler2DShadow";
2364 else
2366 if (tex_rect)
2367 sampler_type = "sampler2DRect";
2368 else
2369 sampler_type = "sampler2D";
2371 break;
2373 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2374 if (shadow_sampler)
2375 FIXME("Unsupported 3D shadow sampler.\n");
2376 sampler_type = "sampler3D";
2377 break;
2379 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2380 if (shadow_sampler)
2381 sampler_type = "samplerCubeShadow";
2382 else
2383 sampler_type = "samplerCube";
2384 break;
2386 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2387 if (shadow_sampler)
2388 sampler_type = "sampler1DArrayShadow";
2389 else
2390 sampler_type = "sampler1DArray";
2391 break;
2393 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2394 if (shadow_sampler)
2395 sampler_type = "sampler2DArrayShadow";
2396 else
2397 sampler_type = "sampler2DArray";
2398 break;
2400 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2401 if (shadow_sampler)
2402 sampler_type = "samplerCubeArrayShadow";
2403 else
2404 sampler_type = "samplerCubeArray";
2405 break;
2407 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2408 sampler_type = "sampler2DMS";
2409 break;
2411 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2412 sampler_type = "sampler2DMSArray";
2413 break;
2415 default:
2416 sampler_type = "unsupported_sampler";
2417 FIXME("Unhandled resource type %#x.\n", resource_type);
2418 break;
2421 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2422 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, version, entry->bind_idx);
2423 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2424 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2427 /* Declare images */
2428 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2430 const char *image_type_prefix, *image_type, *read_format;
2432 if (!reg_maps->uav_resource_info[i].type)
2433 continue;
2435 switch (reg_maps->uav_resource_info[i].data_type)
2437 case WINED3D_DATA_FLOAT:
2438 case WINED3D_DATA_UNORM:
2439 case WINED3D_DATA_SNORM:
2440 image_type_prefix = "";
2441 read_format = "r32f";
2442 break;
2444 case WINED3D_DATA_INT:
2445 image_type_prefix = "i";
2446 read_format = "r32i";
2447 break;
2449 case WINED3D_DATA_UINT:
2450 image_type_prefix = "u";
2451 read_format = "r32ui";
2452 break;
2454 default:
2455 image_type_prefix = "";
2456 read_format = "";
2457 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2458 break;
2461 switch (reg_maps->uav_resource_info[i].type)
2463 case WINED3D_SHADER_RESOURCE_BUFFER:
2464 image_type = "imageBuffer";
2465 break;
2467 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2468 image_type = "image1D";
2469 break;
2471 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2472 image_type = "image2D";
2473 break;
2475 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2476 image_type = "image3D";
2477 break;
2479 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2480 image_type = "image1DArray";
2481 break;
2483 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2484 image_type = "image2DArray";
2485 break;
2487 default:
2488 image_type = "unsupported_image";
2489 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2490 break;
2493 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2494 shader_addline(buffer, "layout(binding = %u)\n", i);
2495 if (reg_maps->uav_read_mask & (1u << i))
2496 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2497 read_format, image_type_prefix, image_type, prefix, i);
2498 else
2499 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2500 image_type_prefix, image_type, prefix, i);
2502 if (reg_maps->uav_counter_mask & (1u << i))
2503 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2504 i, prefix, i);
2507 /* Declare address variables */
2508 map = reg_maps->address;
2509 while (map)
2511 i = wined3d_bit_scan(&map);
2512 shader_addline(buffer, "ivec4 A%u;\n", i);
2515 /* Declare output register temporaries */
2516 if (shader->limits->packed_output)
2517 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2519 /* Declare temporary variables */
2520 if (reg_maps->temporary_count)
2522 for (i = 0; i < reg_maps->temporary_count; ++i)
2523 shader_addline(buffer, "vec4 R%u;\n", i);
2525 else if (version->major < 4)
2527 map = reg_maps->temporary;
2528 while (map)
2530 i = wined3d_bit_scan(&map);
2531 shader_addline(buffer, "vec4 R%u;\n", i);
2535 /* Declare indexable temporary variables */
2536 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2538 if (idx_temp_reg->component_count != 4)
2539 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2540 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2543 /* Declare loop registers aLx */
2544 if (version->major < 4)
2546 for (i = 0; i < reg_maps->loop_depth; ++i)
2548 shader_addline(buffer, "int aL%u;\n", i);
2549 shader_addline(buffer, "int tmpInt%u;\n", i);
2553 /* Temporary variables for matrix operations */
2554 shader_addline(buffer, "vec4 tmp0;\n");
2555 shader_addline(buffer, "vec4 tmp1;\n");
2556 if (gl_info->supported[ARB_GPU_SHADER5])
2557 shader_addline(buffer, "precise vec4 tmp_precise[2];\n");
2558 else
2559 shader_addline(buffer, "/* precise */ vec4 tmp_precise[2];\n");
2561 if (!shader->load_local_constsF)
2563 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2565 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2566 shader_glsl_append_imm_vec(buffer, (const float *)lconst->value, ARRAY_SIZE(lconst->value), gl_info);
2567 shader_addline(buffer, ";\n");
2572 /* Prototypes */
2573 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
2574 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2575 enum wined3d_data_type data_type);
2577 /** Used for opcode modifiers - They multiply the result by the specified amount */
2578 static const char * const shift_glsl_tab[] = {
2579 "", /* 0 (none) */
2580 "2.0 * ", /* 1 (x2) */
2581 "4.0 * ", /* 2 (x4) */
2582 "8.0 * ", /* 3 (x8) */
2583 "16.0 * ", /* 4 (x16) */
2584 "32.0 * ", /* 5 (x32) */
2585 "", /* 6 (x64) */
2586 "", /* 7 (x128) */
2587 "", /* 8 (d256) */
2588 "", /* 9 (d128) */
2589 "", /* 10 (d64) */
2590 "", /* 11 (d32) */
2591 "0.0625 * ", /* 12 (d16) */
2592 "0.125 * ", /* 13 (d8) */
2593 "0.25 * ", /* 14 (d4) */
2594 "0.5 * " /* 15 (d2) */
2597 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2598 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2599 const char *in_reg, const char *in_regswizzle, char *out_str)
2601 switch (src_modifier)
2603 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2604 case WINED3DSPSM_DW:
2605 case WINED3DSPSM_NONE:
2606 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2607 break;
2608 case WINED3DSPSM_NEG:
2609 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2610 break;
2611 case WINED3DSPSM_NOT:
2612 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2613 break;
2614 case WINED3DSPSM_BIAS:
2615 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2616 break;
2617 case WINED3DSPSM_BIASNEG:
2618 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2619 break;
2620 case WINED3DSPSM_SIGN:
2621 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2622 break;
2623 case WINED3DSPSM_SIGNNEG:
2624 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2625 break;
2626 case WINED3DSPSM_COMP:
2627 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2628 break;
2629 case WINED3DSPSM_X2:
2630 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2631 break;
2632 case WINED3DSPSM_X2NEG:
2633 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2634 break;
2635 case WINED3DSPSM_ABS:
2636 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2637 break;
2638 case WINED3DSPSM_ABSNEG:
2639 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2640 break;
2641 default:
2642 FIXME("Unhandled modifier %u\n", src_modifier);
2643 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2647 static void shader_glsl_fixup_scalar_register_variable(struct wined3d_string_buffer *register_name,
2648 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2650 /* The ARB_shading_language_420pack extension allows swizzle operations on
2651 * scalars. */
2652 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2653 string_buffer_sprintf(register_name, "%s", glsl_variable);
2654 else
2655 string_buffer_sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2658 /** Writes the GLSL variable name that corresponds to the register that the
2659 * DX opcode parameter is trying to access */
2660 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2661 enum wined3d_data_type data_type, struct wined3d_string_buffer *register_name,
2662 BOOL *is_swizzled, const struct wined3d_shader_context *ctx)
2664 /* oPos, oFog and oPts in D3D */
2665 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2667 const struct wined3d_shader *shader = ctx->shader;
2668 const struct wined3d_shader_reg_maps *reg_maps = ctx->reg_maps;
2669 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2670 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2671 const char *prefix = shader_glsl_get_prefix(version->type);
2672 const struct wined3d_gl_info *gl_info = priv->gl_info;
2673 struct glsl_src_param rel_param0, rel_param1;
2675 if (reg->idx[0].offset != ~0u && reg->idx[0].rel_addr)
2676 shader_glsl_add_src_param_ext(ctx, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0,
2677 &rel_param0, reg->idx[0].rel_addr->reg.data_type);
2678 if (reg->idx[1].offset != ~0u && reg->idx[1].rel_addr)
2679 shader_glsl_add_src_param_ext(ctx, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0,
2680 &rel_param1, reg->idx[1].rel_addr->reg.data_type);
2681 if (is_swizzled)
2682 *is_swizzled = FALSE;
2684 switch (reg->type)
2686 case WINED3DSPR_TEMP:
2687 string_buffer_sprintf(register_name, "R%u", reg->idx[0].offset);
2688 break;
2690 case WINED3DSPR_INPUT:
2691 case WINED3DSPR_INCONTROLPOINT:
2692 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2694 if (reg->idx[0].rel_addr)
2695 FIXME("VS3 input registers relative addressing.\n");
2696 if (is_swizzled && priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2697 *is_swizzled = TRUE;
2698 if (reg->idx[0].rel_addr)
2700 string_buffer_sprintf(register_name, "%s_in[%s + %u]",
2701 prefix, rel_param0.param_str, reg->idx[0].offset);
2703 else
2705 string_buffer_sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2707 break;
2710 if (version->type == WINED3D_SHADER_TYPE_HULL
2711 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2712 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2714 if (reg->idx[0].rel_addr)
2716 if (reg->idx[1].rel_addr)
2717 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2718 rel_param0.param_str, reg->idx[0].offset,
2719 rel_param1.param_str, reg->idx[1].offset);
2720 else
2721 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2722 rel_param0.param_str, reg->idx[0].offset,
2723 reg->idx[1].offset);
2725 else if (reg->idx[1].rel_addr)
2726 string_buffer_sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2727 rel_param1.param_str, reg->idx[1].offset);
2728 else
2729 string_buffer_sprintf(register_name, "shader_in[%u].reg[%u]",
2730 reg->idx[0].offset, reg->idx[1].offset);
2731 break;
2734 /* pixel shaders >= 3.0 */
2735 if (version->major >= 3)
2737 unsigned int idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2738 unsigned int in_count = vec4_varyings(version->major, gl_info);
2740 if (reg->idx[0].rel_addr)
2742 /* Removing a + 0 would be an obvious optimization, but
2743 * OS X doesn't see the NOP operation there. */
2744 if (idx)
2746 if (needs_legacy_glsl_syntax(gl_info)
2747 && shader->u.ps.declared_in_count > in_count)
2749 string_buffer_sprintf(register_name,
2750 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2751 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2752 prefix, rel_param0.param_str, idx);
2754 else
2756 string_buffer_sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2759 else
2761 if (needs_legacy_glsl_syntax(gl_info)
2762 && shader->u.ps.declared_in_count > in_count)
2764 string_buffer_sprintf(register_name,
2765 "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2766 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2767 prefix, rel_param0.param_str);
2769 else
2771 string_buffer_sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2775 else
2777 if (idx == in_count)
2778 string_buffer_sprintf(register_name, "gl_Color");
2779 else if (idx == in_count + 1)
2780 string_buffer_sprintf(register_name, "gl_SecondaryColor");
2781 else
2782 string_buffer_sprintf(register_name, "%s_in[%u]", prefix, idx);
2785 else
2787 if (!reg->idx[0].offset)
2788 string_buffer_sprintf(register_name, "ffp_varying_diffuse");
2789 else
2790 string_buffer_sprintf(register_name, "ffp_varying_specular");
2791 break;
2793 break;
2795 case WINED3DSPR_CONST:
2797 /* Relative addressing */
2798 if (reg->idx[0].rel_addr)
2800 if (wined3d_settings.check_float_constants)
2801 string_buffer_sprintf(register_name,
2802 "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2803 rel_param0.param_str, reg->idx[0].offset,
2804 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2805 prefix, rel_param0.param_str, reg->idx[0].offset);
2806 else if (reg->idx[0].offset)
2807 string_buffer_sprintf(register_name, "%s_c[%s + %u]",
2808 prefix, rel_param0.param_str, reg->idx[0].offset);
2809 else
2810 string_buffer_sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2812 else
2814 if (shader_constant_is_local(shader, reg->idx[0].offset))
2815 string_buffer_sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2816 else
2817 string_buffer_sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2820 break;
2822 case WINED3DSPR_CONSTINT:
2823 string_buffer_sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2824 break;
2826 case WINED3DSPR_CONSTBOOL:
2827 string_buffer_sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2828 break;
2830 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2831 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2832 string_buffer_sprintf(register_name, "T%u", reg->idx[0].offset);
2833 else
2834 string_buffer_sprintf(register_name, "A%u", reg->idx[0].offset);
2835 break;
2837 case WINED3DSPR_LOOP:
2838 string_buffer_sprintf(register_name, "aL%u", ctx->state->current_loop_reg - 1);
2839 break;
2841 case WINED3DSPR_SAMPLER:
2842 string_buffer_sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2843 break;
2845 case WINED3DSPR_COLOROUT:
2846 if (reg->idx[0].offset >= gl_info->limits.buffers)
2847 WARN("Write to render target %u, only %d supported.\n",
2848 reg->idx[0].offset, gl_info->limits.buffers);
2850 string_buffer_sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2851 break;
2853 case WINED3DSPR_RASTOUT:
2854 string_buffer_sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2855 break;
2857 case WINED3DSPR_DEPTHOUT:
2858 case WINED3DSPR_DEPTHOUTGE:
2859 case WINED3DSPR_DEPTHOUTLE:
2860 string_buffer_sprintf(register_name, "gl_FragDepth");
2861 break;
2863 case WINED3DSPR_ATTROUT:
2864 if (!reg->idx[0].offset)
2865 string_buffer_sprintf(register_name, "%s_out[8]", prefix);
2866 else
2867 string_buffer_sprintf(register_name, "%s_out[9]", prefix);
2868 break;
2870 case WINED3DSPR_TEXCRDOUT:
2871 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2872 if (reg->idx[0].rel_addr)
2873 string_buffer_sprintf(register_name, "%s_out[%s + %u]",
2874 prefix, rel_param0.param_str, reg->idx[0].offset);
2875 else
2876 string_buffer_sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2877 break;
2879 case WINED3DSPR_MISCTYPE:
2880 if (!reg->idx[0].offset)
2882 /* vPos */
2883 string_buffer_sprintf(register_name, "vpos");
2885 else if (reg->idx[0].offset == 1)
2887 /* Note that gl_FrontFacing is a bool, while vFace is
2888 * a float for which the sign determines front/back */
2889 string_buffer_sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2891 else
2893 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2894 string_buffer_sprintf(register_name, "unrecognized_register");
2896 break;
2898 case WINED3DSPR_IMMCONST:
2899 switch (reg->immconst_type)
2901 case WINED3D_IMMCONST_SCALAR:
2902 switch (data_type)
2904 case WINED3D_DATA_UNORM:
2905 case WINED3D_DATA_SNORM:
2906 case WINED3D_DATA_FLOAT:
2907 string_buffer_clear(register_name);
2908 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 1, gl_info);
2909 break;
2910 case WINED3D_DATA_INT:
2911 string_buffer_sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2912 break;
2913 case WINED3D_DATA_RESOURCE:
2914 case WINED3D_DATA_SAMPLER:
2915 case WINED3D_DATA_UINT:
2916 string_buffer_sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2917 break;
2918 default:
2919 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2920 break;
2922 break;
2924 case WINED3D_IMMCONST_VEC4:
2925 switch (data_type)
2927 case WINED3D_DATA_UNORM:
2928 case WINED3D_DATA_SNORM:
2929 case WINED3D_DATA_FLOAT:
2930 string_buffer_clear(register_name);
2931 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 4, gl_info);
2932 break;
2933 case WINED3D_DATA_INT:
2934 string_buffer_sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2935 reg->u.immconst_data[0], reg->u.immconst_data[1],
2936 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2937 break;
2938 case WINED3D_DATA_RESOURCE:
2939 case WINED3D_DATA_SAMPLER:
2940 case WINED3D_DATA_UINT:
2941 string_buffer_sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2942 reg->u.immconst_data[0], reg->u.immconst_data[1],
2943 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2944 break;
2945 default:
2946 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2947 break;
2949 break;
2951 default:
2952 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2953 string_buffer_sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2955 break;
2957 case WINED3DSPR_CONSTBUFFER:
2958 if (reg->idx[1].rel_addr)
2959 string_buffer_sprintf(register_name, "%s_cb%u[%s + %u]",
2960 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2961 else
2962 string_buffer_sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2963 break;
2965 case WINED3DSPR_IMMCONSTBUFFER:
2966 if (reg->idx[0].rel_addr)
2967 string_buffer_sprintf(register_name, "%s_icb[%s + %u]",
2968 prefix, rel_param0.param_str, reg->idx[0].offset);
2969 else
2970 string_buffer_sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2971 break;
2973 case WINED3DSPR_PRIMID:
2974 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2975 string_buffer_sprintf(register_name, "gl_PrimitiveIDIn");
2976 else
2977 string_buffer_sprintf(register_name, "gl_PrimitiveID");
2978 break;
2980 case WINED3DSPR_IDXTEMP:
2981 if (reg->idx[1].rel_addr)
2982 string_buffer_sprintf(register_name, "X%u[%s + %u]",
2983 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2984 else
2985 string_buffer_sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2986 break;
2988 case WINED3DSPR_LOCALTHREADINDEX:
2989 shader_glsl_fixup_scalar_register_variable(register_name,
2990 "int(gl_LocalInvocationIndex)", gl_info);
2991 break;
2993 case WINED3DSPR_GSINSTID:
2994 case WINED3DSPR_OUTPOINTID:
2995 shader_glsl_fixup_scalar_register_variable(register_name,
2996 "gl_InvocationID", gl_info);
2997 break;
2999 case WINED3DSPR_THREADID:
3000 string_buffer_sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3001 break;
3003 case WINED3DSPR_THREADGROUPID:
3004 string_buffer_sprintf(register_name, "ivec3(gl_WorkGroupID)");
3005 break;
3007 case WINED3DSPR_LOCALTHREADID:
3008 string_buffer_sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3009 break;
3011 case WINED3DSPR_FORKINSTID:
3012 case WINED3DSPR_JOININSTID:
3013 shader_glsl_fixup_scalar_register_variable(register_name,
3014 "phase_instance_id", gl_info);
3015 break;
3017 case WINED3DSPR_TESSCOORD:
3018 string_buffer_sprintf(register_name, "gl_TessCoord");
3019 break;
3021 case WINED3DSPR_OUTCONTROLPOINT:
3022 if (reg->idx[0].rel_addr)
3024 if (reg->idx[1].rel_addr)
3025 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3026 rel_param0.param_str, reg->idx[0].offset,
3027 rel_param1.param_str, reg->idx[1].offset);
3028 else
3029 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3030 rel_param0.param_str, reg->idx[0].offset,
3031 reg->idx[1].offset);
3033 else if (reg->idx[1].rel_addr)
3035 string_buffer_sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3036 reg->idx[0].offset, rel_param1.param_str,
3037 reg->idx[1].offset);
3039 else
3041 string_buffer_sprintf(register_name, "shader_out[%u].reg[%u]",
3042 reg->idx[0].offset, reg->idx[1].offset);
3044 break;
3046 case WINED3DSPR_PATCHCONST:
3047 if (version->type == WINED3D_SHADER_TYPE_HULL)
3048 string_buffer_sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3049 else
3050 string_buffer_sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3051 break;
3053 case WINED3DSPR_COVERAGE:
3054 string_buffer_sprintf(register_name, "gl_SampleMaskIn[0]");
3055 break;
3057 case WINED3DSPR_SAMPLEMASK:
3058 string_buffer_sprintf(register_name, "sample_mask");
3059 break;
3061 default:
3062 FIXME("Unhandled register type %#x.\n", reg->type);
3063 string_buffer_sprintf(register_name, "unrecognised_register");
3064 break;
3068 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3070 *str++ = '.';
3071 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3072 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3073 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3074 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3075 *str = '\0';
3078 /* Get the GLSL write mask for the destination register */
3079 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3081 DWORD mask = param->write_mask;
3083 if (shader_is_scalar(&param->reg))
3085 mask = WINED3DSP_WRITEMASK_0;
3086 *write_mask = '\0';
3088 else
3090 shader_glsl_write_mask_to_str(mask, write_mask);
3093 return mask;
3096 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3098 unsigned int size = 0;
3100 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3101 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3102 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3103 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3105 return size;
3108 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3109 unsigned int component_idx)
3111 /* swizzle bits fields: wwzzyyxx */
3112 return (swizzle >> (2 * component_idx)) & 0x3;
3115 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3117 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3118 * but addressed as "rgba". To fix this we need to swap the register's x
3119 * and z components. */
3120 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3121 unsigned int i;
3123 *str++ = '.';
3124 for (i = 0; i < 4; ++i)
3126 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3127 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3129 *str = '\0';
3132 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3133 BOOL fixup, DWORD mask, char *swizzle_str)
3135 if (shader_is_scalar(&param->reg))
3136 *swizzle_str = '\0';
3137 else
3138 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3141 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3142 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type, unsigned int size)
3144 if (dst_data_type == WINED3D_DATA_UNORM || dst_data_type == WINED3D_DATA_SNORM)
3145 dst_data_type = WINED3D_DATA_FLOAT;
3146 if (src_data_type == WINED3D_DATA_UNORM || src_data_type == WINED3D_DATA_SNORM)
3147 src_data_type = WINED3D_DATA_FLOAT;
3149 if (dst_data_type == src_data_type)
3151 string_buffer_sprintf(dst_param, "%s", src_param);
3152 return;
3155 if (src_data_type == WINED3D_DATA_FLOAT)
3157 switch (dst_data_type)
3159 case WINED3D_DATA_INT:
3160 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3161 return;
3162 case WINED3D_DATA_RESOURCE:
3163 case WINED3D_DATA_SAMPLER:
3164 case WINED3D_DATA_UINT:
3165 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3166 return;
3167 default:
3168 break;
3172 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3174 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3175 return;
3178 if (src_data_type == WINED3D_DATA_INT)
3180 switch (dst_data_type)
3182 case WINED3D_DATA_FLOAT:
3183 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3184 return;
3185 case WINED3D_DATA_UINT:
3186 if (size == 1)
3187 string_buffer_sprintf(dst_param, "uint(%s)", src_param);
3188 else
3189 string_buffer_sprintf(dst_param, "uvec%u(%s)", size, src_param);
3190 return;
3191 default:
3192 break;
3196 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3197 string_buffer_sprintf(dst_param, "%s", src_param);
3200 /* From a given parameter token, generate the corresponding GLSL string.
3201 * Also, return the actual register name and swizzle in case the
3202 * caller needs this information as well. */
3203 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
3204 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3205 enum wined3d_data_type data_type)
3207 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3208 struct wined3d_string_buffer *param_str = string_buffer_get(priv->string_buffers);
3209 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3210 enum wined3d_data_type param_data_type;
3211 BOOL is_color = FALSE;
3212 char swizzle_str[6];
3213 unsigned int size;
3215 glsl_src->param_str[0] = '\0';
3216 swizzle_str[0] = '\0';
3218 shader_glsl_get_register_name(&wined3d_src->reg, data_type, reg_name, &is_color, ctx);
3219 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3221 switch (wined3d_src->reg.type)
3223 case WINED3DSPR_IMMCONST:
3224 param_data_type = data_type;
3225 size = wined3d_src->reg.immconst_type == WINED3D_IMMCONST_SCALAR ? 1 : 4;
3226 break;
3227 case WINED3DSPR_FORKINSTID:
3228 case WINED3DSPR_GSINSTID:
3229 case WINED3DSPR_JOININSTID:
3230 case WINED3DSPR_LOCALTHREADINDEX:
3231 case WINED3DSPR_OUTPOINTID:
3232 case WINED3DSPR_PRIMID:
3233 param_data_type = WINED3D_DATA_INT;
3234 size = 1;
3235 break;
3236 case WINED3DSPR_LOCALTHREADID:
3237 case WINED3DSPR_THREADGROUPID:
3238 case WINED3DSPR_THREADID:
3239 param_data_type = WINED3D_DATA_INT;
3240 size = 3;
3241 break;
3242 default:
3243 param_data_type = WINED3D_DATA_FLOAT;
3244 size = 4;
3245 break;
3248 shader_glsl_sprintf_cast(param_str, reg_name->buffer, data_type, param_data_type, size);
3249 shader_glsl_gen_modifier(wined3d_src->modifiers, param_str->buffer, swizzle_str, glsl_src->param_str);
3251 string_buffer_release(priv->string_buffers, reg_name);
3252 string_buffer_release(priv->string_buffers, param_str);
3255 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3256 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3258 shader_glsl_add_src_param_ext(ins->ctx, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3261 /* From a given parameter token, generate the corresponding GLSL string.
3262 * Also, return the actual register name and swizzle in case the
3263 * caller needs this information as well. */
3264 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3265 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3267 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3268 struct wined3d_string_buffer *reg_name;
3269 size_t len;
3271 glsl_dst->mask_str[0] = '\0';
3273 reg_name = string_buffer_get(priv->string_buffers);
3274 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type, reg_name, NULL, ins->ctx);
3275 len = min(reg_name->content_size, ARRAY_SIZE(glsl_dst->reg_name) - 1);
3276 memcpy(glsl_dst->reg_name, reg_name->buffer, len);
3277 glsl_dst->reg_name[len] = '\0';
3278 string_buffer_release(priv->string_buffers, reg_name);
3280 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3283 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3284 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3285 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3286 unsigned int dst_idx, enum wined3d_data_type data_type)
3288 struct glsl_dst_param glsl_dst;
3289 DWORD mask;
3291 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3293 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3294 sprintf(glsl_dst.reg_name, "tmp_precise[%u]", dst_idx);
3296 switch (data_type)
3298 case WINED3D_DATA_FLOAT:
3299 case WINED3D_DATA_UNORM:
3300 case WINED3D_DATA_SNORM:
3301 shader_addline(buffer, "%s%s = %s(",
3302 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3303 break;
3304 case WINED3D_DATA_INT:
3305 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3306 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3307 break;
3308 case WINED3D_DATA_RESOURCE:
3309 case WINED3D_DATA_SAMPLER:
3310 case WINED3D_DATA_UINT:
3311 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3312 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3313 break;
3314 default:
3315 FIXME("Unhandled data type %#x.\n", data_type);
3316 shader_addline(buffer, "%s%s = %s(",
3317 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3318 break;
3322 return mask;
3325 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3326 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3328 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3331 /** Process GLSL instruction modifiers */
3332 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3334 const struct wined3d_shader_dst_param *dst;
3335 struct glsl_dst_param dst_param;
3336 DWORD modifiers;
3337 unsigned int i;
3339 for (i = 0; i < ins->dst_count; ++i)
3341 if ((dst = &ins->dst[i])->reg.type == WINED3DSPR_NULL)
3342 continue;
3344 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3346 shader_glsl_add_dst_param(ins, dst, &dst_param);
3347 shader_addline(ins->ctx->buffer, "%s%s = tmp_precise[%u]%s;\n",
3348 dst_param.reg_name, dst_param.mask_str, i, dst_param.mask_str);
3351 if (!(modifiers = dst->modifiers))
3352 continue;
3354 shader_glsl_add_dst_param(ins, dst, &dst_param);
3356 if (modifiers & WINED3DSPDM_SATURATE)
3358 /* _SAT means to clamp the value of the register to between 0 and 1 */
3359 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3360 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3363 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3365 FIXME("_centroid modifier not handled\n");
3368 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3370 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3375 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3377 switch (op)
3379 case WINED3D_SHADER_REL_OP_GT: return ">";
3380 case WINED3D_SHADER_REL_OP_EQ: return "==";
3381 case WINED3D_SHADER_REL_OP_GE: return ">=";
3382 case WINED3D_SHADER_REL_OP_LT: return "<";
3383 case WINED3D_SHADER_REL_OP_NE: return "!=";
3384 case WINED3D_SHADER_REL_OP_LE: return "<=";
3385 default:
3386 FIXME("Unrecognized operator %#x.\n", op);
3387 return "(\?\?)";
3391 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3393 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3396 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3397 unsigned int *coord_size, unsigned int *deriv_size)
3399 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3400 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3402 *coord_size = resource_type_info[resource_type].coord_size;
3403 *deriv_size = *coord_size;
3404 if (is_array)
3405 --(*deriv_size);
3408 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3409 DWORD resource_idx, DWORD sampler_idx, uint32_t flags, struct glsl_sample_function *sample_function)
3411 enum wined3d_shader_resource_type resource_type;
3412 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3413 const struct wined3d_gl_info *gl_info = priv->gl_info;
3414 BOOL shadow = shader_sampler_is_shadow(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3415 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3416 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3417 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3418 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3419 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3420 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3421 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3422 const char *base = "texture", *type_part = "", *suffix = "";
3423 unsigned int coord_size, deriv_size;
3425 resource_type = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3426 ? pixelshader_get_resource_type(ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
3427 : ctx->reg_maps->resource_info[resource_idx].type;
3429 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3431 if (resource_type >= ARRAY_SIZE(resource_type_info))
3433 ERR("Unexpected resource type %#x.\n", resource_type);
3434 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3437 /* Note that there's no such thing as a projected cube texture. */
3438 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3439 projected = FALSE;
3441 if (needs_legacy_glsl_syntax(gl_info))
3443 if (shadow)
3444 base = "shadow";
3446 type_part = resource_type_info[resource_type].type_part;
3447 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3448 type_part = "2DRect";
3449 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3450 FIXME("Unhandled resource type %#x.\n", resource_type);
3452 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3454 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3455 suffix = "ARB";
3456 else
3457 FIXME("Unsupported grad function.\n");
3461 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3463 static const uint32_t texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3464 if (flags & ~texel_fetch_flags)
3465 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3467 base = "texelFetch";
3468 type_part = "";
3471 sample_function->name = string_buffer_get(priv->string_buffers);
3472 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3473 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3475 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3476 if (shadow)
3477 ++coord_size;
3478 sample_function->offset_size = offset ? deriv_size : 0;
3479 sample_function->coord_mask = wined3d_mask_from_size(coord_size);
3480 sample_function->deriv_mask = wined3d_mask_from_size(deriv_size);
3481 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3484 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3485 struct glsl_sample_function *sample_function)
3487 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3489 string_buffer_release(priv->string_buffers, sample_function->name);
3492 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3493 BOOL sign_fixup, enum fixup_channel_source channel_source)
3495 switch(channel_source)
3497 case CHANNEL_SOURCE_ZERO:
3498 strcat(arguments, "0.0");
3499 break;
3501 case CHANNEL_SOURCE_ONE:
3502 strcat(arguments, "1.0");
3503 break;
3505 case CHANNEL_SOURCE_X:
3506 strcat(arguments, reg_name);
3507 strcat(arguments, ".x");
3508 break;
3510 case CHANNEL_SOURCE_Y:
3511 strcat(arguments, reg_name);
3512 strcat(arguments, ".y");
3513 break;
3515 case CHANNEL_SOURCE_Z:
3516 strcat(arguments, reg_name);
3517 strcat(arguments, ".z");
3518 break;
3520 case CHANNEL_SOURCE_W:
3521 strcat(arguments, reg_name);
3522 strcat(arguments, ".w");
3523 break;
3525 default:
3526 FIXME("Unhandled channel source %#x\n", channel_source);
3527 strcat(arguments, "undefined");
3528 break;
3531 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3534 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3535 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3537 unsigned int mask_size, remaining;
3538 DWORD fixup_mask = 0;
3539 char arguments[256];
3540 char mask_str[6];
3542 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3543 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3544 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3545 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3546 if (!(mask &= fixup_mask))
3547 return;
3549 if (is_complex_fixup(fixup))
3551 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3552 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3553 return;
3556 shader_glsl_write_mask_to_str(mask, mask_str);
3557 mask_size = shader_glsl_get_write_mask_size(mask);
3559 arguments[0] = '\0';
3560 remaining = mask_size;
3561 if (mask & WINED3DSP_WRITEMASK_0)
3563 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3564 if (--remaining) strcat(arguments, ", ");
3566 if (mask & WINED3DSP_WRITEMASK_1)
3568 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3569 if (--remaining) strcat(arguments, ", ");
3571 if (mask & WINED3DSP_WRITEMASK_2)
3573 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3574 if (--remaining) strcat(arguments, ", ");
3576 if (mask & WINED3DSP_WRITEMASK_3)
3578 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3579 if (--remaining) strcat(arguments, ", ");
3582 if (mask_size > 1)
3583 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3584 else
3585 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3588 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3590 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3591 struct wined3d_string_buffer *reg_name;
3593 reg_name = string_buffer_get(priv->string_buffers);
3594 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, NULL, ins->ctx);
3595 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name->buffer, ins->dst[0].write_mask, fixup);
3596 string_buffer_release(priv->string_buffers, reg_name);
3599 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3600 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3601 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3602 const char *coord_reg_fmt, ...)
3604 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3605 char dst_swizzle[6];
3606 struct color_fixup_desc fixup;
3607 BOOL np2_fixup = FALSE;
3608 va_list args;
3609 int ret;
3611 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3613 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3614 * We actually rely on it for vertex shaders and SM4+. */
3615 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3617 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3618 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3620 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3621 np2_fixup = TRUE;
3623 else
3625 fixup = COLOR_FIXUP_IDENTITY;
3628 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, sample_function->data_type);
3630 if (sample_function->output_single_component)
3631 shader_addline(ins->ctx->buffer, "vec4(");
3633 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3634 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3636 for (;;)
3638 va_start(args, coord_reg_fmt);
3639 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3640 va_end(args);
3641 if (!ret)
3642 break;
3643 if (!string_buffer_resize(ins->ctx->buffer, ret))
3644 break;
3647 if (np2_fixup)
3649 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3650 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3652 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3654 case 1:
3655 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3656 idx >> 1, (idx % 2) ? "z" : "x");
3657 break;
3658 case 2:
3659 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3660 idx >> 1, (idx % 2) ? "zw" : "xy");
3661 break;
3662 case 3:
3663 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3664 idx >> 1, (idx % 2) ? "zw" : "xy");
3665 break;
3666 case 4:
3667 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3668 idx >> 1, (idx % 2) ? "zw" : "xy");
3669 break;
3672 if (dx && dy)
3673 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3674 else if (bias)
3675 shader_addline(ins->ctx->buffer, ", %s", bias);
3676 if (sample_function->offset_size)
3678 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3679 shader_addline(ins->ctx->buffer, ", ");
3680 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3682 shader_addline(ins->ctx->buffer, ")");
3684 if (sample_function->output_single_component)
3685 shader_addline(ins->ctx->buffer, ")");
3687 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3689 if (!is_identity_fixup(fixup))
3690 shader_glsl_color_correction(ins, fixup);
3693 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3695 /* Write the final position.
3697 * OpenGL coordinates specify the center of the pixel while D3D coords
3698 * specify the corner. The offsets are stored in z and w in
3699 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3700 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3701 * a MAD. */
3702 if (use_viewport_index)
3704 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3705 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3707 else
3709 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3710 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3713 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3714 * in utils.c
3716 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3717 * shaders are run before the homogeneous divide, so we have to take the w
3718 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3719 * z = z * 2 - w. */
3720 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3723 /*****************************************************************************
3724 * Begin processing individual instruction opcodes
3725 ****************************************************************************/
3727 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3729 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3730 struct glsl_src_param src0_param;
3731 struct glsl_src_param src1_param;
3732 DWORD write_mask;
3733 const char *op;
3735 /* Determine the GLSL operator to use based on the opcode */
3736 switch (ins->handler_idx)
3738 case WINED3DSIH_ADD: op = "+"; break;
3739 case WINED3DSIH_AND: op = "&"; break;
3740 case WINED3DSIH_DIV: op = "/"; break;
3741 case WINED3DSIH_IADD: op = "+"; break;
3742 case WINED3DSIH_ISHL: op = "<<"; break;
3743 case WINED3DSIH_ISHR: op = ">>"; break;
3744 case WINED3DSIH_MUL: op = "*"; break;
3745 case WINED3DSIH_OR: op = "|"; break;
3746 case WINED3DSIH_SUB: op = "-"; break;
3747 case WINED3DSIH_USHR: op = ">>"; break;
3748 case WINED3DSIH_XOR: op = "^"; break;
3749 default:
3750 op = "<unhandled operator>";
3751 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3752 break;
3755 write_mask = shader_glsl_append_dst(buffer, ins);
3756 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3757 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3758 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3761 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3763 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3764 struct glsl_src_param src0_param;
3765 struct glsl_src_param src1_param;
3766 unsigned int mask_size;
3767 DWORD write_mask;
3768 const char *op;
3770 write_mask = shader_glsl_append_dst(buffer, ins);
3771 mask_size = shader_glsl_get_write_mask_size(write_mask);
3772 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3773 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3775 if (mask_size > 1)
3777 switch (ins->handler_idx)
3779 case WINED3DSIH_EQ: op = "equal"; break;
3780 case WINED3DSIH_IEQ: op = "equal"; break;
3781 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3782 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3783 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3784 case WINED3DSIH_LT: op = "lessThan"; break;
3785 case WINED3DSIH_ILT: op = "lessThan"; break;
3786 case WINED3DSIH_ULT: op = "lessThan"; break;
3787 case WINED3DSIH_NE: op = "notEqual"; break;
3788 case WINED3DSIH_INE: op = "notEqual"; break;
3789 default:
3790 op = "<unhandled operator>";
3791 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3792 break;
3795 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3796 mask_size, op, src0_param.param_str, src1_param.param_str);
3798 else
3800 switch (ins->handler_idx)
3802 case WINED3DSIH_EQ: op = "=="; break;
3803 case WINED3DSIH_IEQ: op = "=="; break;
3804 case WINED3DSIH_GE: op = ">="; break;
3805 case WINED3DSIH_IGE: op = ">="; break;
3806 case WINED3DSIH_UGE: op = ">="; break;
3807 case WINED3DSIH_LT: op = "<"; break;
3808 case WINED3DSIH_ILT: op = "<"; break;
3809 case WINED3DSIH_ULT: op = "<"; break;
3810 case WINED3DSIH_NE: op = "!="; break;
3811 case WINED3DSIH_INE: op = "!="; break;
3812 default:
3813 op = "<unhandled operator>";
3814 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3815 break;
3818 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3819 src0_param.param_str, op, src1_param.param_str);
3823 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3825 struct glsl_src_param src_param;
3826 DWORD write_mask;
3827 const char *op;
3829 switch (ins->handler_idx)
3831 case WINED3DSIH_INEG: op = "-"; break;
3832 case WINED3DSIH_NOT: op = "~"; break;
3833 default:
3834 op = "<unhandled operator>";
3835 ERR("Unhandled opcode %s.\n",
3836 debug_d3dshaderinstructionhandler(ins->handler_idx));
3837 break;
3840 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3841 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3842 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3845 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3847 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3848 struct glsl_src_param src0_param;
3849 struct glsl_src_param src1_param;
3850 DWORD write_mask;
3852 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3853 * If not, we can emulate it. */
3854 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3855 FIXME("64-bit integer multiplies not implemented.\n");
3857 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3859 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3860 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3861 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3863 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3864 src0_param.param_str, src1_param.param_str);
3868 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3870 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3871 struct glsl_src_param src0_param, src1_param;
3872 DWORD write_mask;
3874 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3876 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3878 char dst_mask[6];
3880 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3881 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3882 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3883 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3884 dst_mask, src0_param.param_str, src1_param.param_str);
3886 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3887 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3888 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3889 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3891 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
3892 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3894 else
3896 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3897 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3898 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3899 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3902 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3904 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3905 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3906 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3907 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3911 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3912 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3914 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3915 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3916 const struct wined3d_gl_info *gl_info = priv->gl_info;
3917 struct glsl_src_param src0_param;
3918 DWORD write_mask;
3920 write_mask = shader_glsl_append_dst(buffer, ins);
3921 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3923 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3924 * shader versions WINED3DSIO_MOVA is used for this. */
3925 if (ins->ctx->reg_maps->shader_version.major == 1
3926 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3927 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3929 /* This is a simple floor() */
3930 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3931 if (mask_size > 1) {
3932 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3933 } else {
3934 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3937 else if (ins->handler_idx == WINED3DSIH_MOVA)
3939 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3941 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3943 if (mask_size > 1)
3944 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3945 else
3946 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3948 else
3950 if (mask_size > 1)
3951 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3952 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3953 else
3954 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3955 src0_param.param_str, src0_param.param_str);
3958 else
3960 shader_addline(buffer, "%s);\n", src0_param.param_str);
3964 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3965 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3967 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3968 struct glsl_src_param src0_param;
3969 struct glsl_src_param src1_param;
3970 DWORD dst_write_mask, src_write_mask;
3971 unsigned int dst_size;
3973 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3974 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3976 /* dp4 works on vec4, dp3 on vec3, etc. */
3977 if (ins->handler_idx == WINED3DSIH_DP4)
3978 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3979 else if (ins->handler_idx == WINED3DSIH_DP3)
3980 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3981 else
3982 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3984 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3985 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3987 if (dst_size > 1) {
3988 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3989 } else {
3990 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3994 /* Note that this instruction has some restrictions. The destination write mask
3995 * can't contain the w component, and the source swizzles have to be .xyzw */
3996 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3998 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3999 struct glsl_src_param src0_param;
4000 struct glsl_src_param src1_param;
4001 char dst_mask[6];
4003 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4004 shader_glsl_append_dst(ins->ctx->buffer, ins);
4005 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4006 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4007 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4010 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4012 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4014 if (!stream)
4015 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4016 else
4017 FIXME("Unhandled primitive stream %u.\n", stream);
4020 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4021 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4022 * GLSL uses the value as-is. */
4023 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4025 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4026 struct glsl_src_param src0_param;
4027 struct glsl_src_param src1_param;
4028 DWORD dst_write_mask;
4029 unsigned int dst_size;
4031 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4032 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4034 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4035 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4037 if (dst_size > 1)
4039 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4040 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4042 else
4044 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4045 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4049 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4050 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4052 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4053 bool y_correction = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4054 ? priv->cur_ps_args->y_correction : false;
4055 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4056 struct glsl_src_param src_param;
4057 const char *instruction;
4058 DWORD write_mask;
4059 unsigned i;
4061 /* Determine the GLSL function to use based on the opcode */
4062 /* TODO: Possibly make this a table for faster lookups */
4063 switch (ins->handler_idx)
4065 case WINED3DSIH_ABS: instruction = "abs"; break;
4066 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4067 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4068 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4069 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4070 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4071 case WINED3DSIH_DSY: instruction = y_correction ? "ycorrection.y * dFdy" : "dFdy"; break;
4072 case WINED3DSIH_DSY_COARSE: instruction = y_correction ? "ycorrection.y * dFdyCoarse" : "dFdyCoarse"; break;
4073 case WINED3DSIH_DSY_FINE: instruction = y_correction ? "ycorrection.y * dFdyFine" : "dFdyFine"; break;
4074 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4075 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4076 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4077 case WINED3DSIH_FRC: instruction = "fract"; break;
4078 case WINED3DSIH_IMAX: instruction = "max"; break;
4079 case WINED3DSIH_IMIN: instruction = "min"; break;
4080 case WINED3DSIH_MAX: instruction = "max"; break;
4081 case WINED3DSIH_MIN: instruction = "min"; break;
4082 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4083 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4084 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4085 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4086 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4087 case WINED3DSIH_UMAX: instruction = "max"; break;
4088 case WINED3DSIH_UMIN: instruction = "min"; break;
4089 default: instruction = "";
4090 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4091 break;
4094 write_mask = shader_glsl_append_dst(buffer, ins);
4096 /* In D3D bits are numbered from the most significant bit. */
4097 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4098 shader_addline(buffer, "31 - ");
4099 shader_addline(buffer, "%s(", instruction);
4101 if (ins->src_count)
4103 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4104 shader_addline(buffer, "%s", src_param.param_str);
4105 for (i = 1; i < ins->src_count; ++i)
4107 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4108 shader_addline(buffer, ", %s", src_param.param_str);
4112 shader_addline(buffer, "));\n");
4115 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4117 struct wined3d_shader_dst_param dst;
4118 struct glsl_src_param src;
4119 DWORD write_mask;
4120 const char *fmt;
4121 unsigned int i;
4123 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4124 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4126 dst = ins->dst[0];
4127 for (i = 0; i < 4; ++i)
4129 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4130 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4131 &dst, 0, dst.reg.data_type)))
4132 continue;
4134 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4135 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4139 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4141 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4142 struct wined3d_shader_dst_param dst;
4143 struct glsl_src_param src[4];
4144 const char *instruction;
4145 BOOL tmp_dst = FALSE;
4146 char mask_char[6];
4147 unsigned int i, j;
4148 DWORD write_mask;
4150 switch (ins->handler_idx)
4152 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4153 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4154 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4155 default:
4156 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4157 return;
4160 for (i = 0; i < ins->src_count; ++i)
4162 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4163 && ins->dst[0].reg.type == ins->src[i].reg.type)
4164 tmp_dst = TRUE;
4167 dst = ins->dst[0];
4168 for (i = 0; i < 4; ++i)
4170 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4171 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4172 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4173 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4174 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, 0, dst.reg.data_type)))
4175 continue;
4177 for (j = 0; j < ins->src_count; ++j)
4178 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4179 shader_addline(buffer, "%s(", instruction);
4180 for (j = 0; j < ins->src_count - 2; ++j)
4181 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4182 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4185 if (tmp_dst)
4187 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
4188 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4189 shader_addline(buffer, "tmp0%s);\n", mask_char);
4193 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4195 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4197 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4198 struct glsl_src_param src_param;
4199 unsigned int mask_size;
4200 DWORD write_mask;
4201 char dst_mask[6];
4203 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4204 mask_size = shader_glsl_get_write_mask_size(write_mask);
4205 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4207 if (mask_size > 3)
4208 shader_addline(buffer, "tmp0.x = dot(vec3(%s), vec3(%s));\n",
4209 src_param.param_str, src_param.param_str);
4210 else
4211 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4212 src_param.param_str, src_param.param_str);
4213 shader_glsl_append_dst(buffer, ins);
4215 shader_addline(buffer, "tmp0.x == 0.0 ? %s : (%s * inversesqrt(tmp0.x)));\n",
4216 src_param.param_str, src_param.param_str);
4219 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4221 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4222 ins->ctx->reg_maps->shader_version.minor);
4223 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4224 struct glsl_src_param src0_param;
4225 const char *prefix, *suffix;
4226 unsigned int dst_size;
4227 DWORD dst_write_mask;
4229 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4230 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4232 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4233 dst_write_mask = WINED3DSP_WRITEMASK_3;
4235 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4237 switch (ins->handler_idx)
4239 case WINED3DSIH_EXP:
4240 case WINED3DSIH_EXPP:
4241 prefix = "exp2(";
4242 suffix = ")";
4243 break;
4245 case WINED3DSIH_LOG:
4246 case WINED3DSIH_LOGP:
4247 prefix = "log2(abs(";
4248 suffix = "))";
4249 break;
4251 case WINED3DSIH_RCP:
4252 prefix = "1.0 / ";
4253 suffix = "";
4254 break;
4256 case WINED3DSIH_RSQ:
4257 prefix = "inversesqrt(abs(";
4258 suffix = "))";
4259 break;
4261 default:
4262 prefix = "";
4263 suffix = "";
4264 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4265 break;
4268 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4269 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4270 else
4271 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4274 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4275 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4276 * dst.x = 2^(floor(src))
4277 * dst.y = src - floor(src)
4278 * dst.z = 2^src (partial precision is allowed, but optional)
4279 * dst.w = 1.0;
4280 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4281 * dst = 2^src; (partial precision is allowed, but optional)
4283 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4285 if (ins->ctx->reg_maps->shader_version.major < 2)
4287 struct glsl_src_param src_param;
4288 char dst_mask[6];
4290 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4292 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4293 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4294 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4295 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4297 shader_glsl_append_dst(ins->ctx->buffer, ins);
4298 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4299 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4300 return;
4303 shader_glsl_scalar_op(ins);
4306 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4307 const char *vector_constructor, const char *scalar_constructor)
4309 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4310 struct glsl_src_param src_param;
4311 unsigned int mask_size;
4312 DWORD write_mask;
4314 write_mask = shader_glsl_append_dst(buffer, ins);
4315 mask_size = shader_glsl_get_write_mask_size(write_mask);
4316 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4318 if (mask_size > 1)
4319 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4320 else
4321 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4324 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4326 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4327 struct glsl_src_param src_param;
4328 unsigned int mask_size;
4329 DWORD write_mask;
4331 write_mask = shader_glsl_append_dst(buffer, ins);
4332 mask_size = shader_glsl_get_write_mask_size(write_mask);
4333 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4335 if (mask_size > 1)
4336 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",
4337 mask_size, src_param.param_str, mask_size, mask_size, src_param.param_str, mask_size, mask_size, src_param.param_str);
4338 else
4339 shader_addline(buffer, "mix(mix(int(max(%s, -2147483648.0)), 0x7fffffff, %s >= 2147483648.0), 0, isnan(%s)));\n",
4340 src_param.param_str, src_param.param_str, src_param.param_str);
4343 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4345 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4346 struct glsl_src_param src_param;
4347 unsigned int mask_size;
4348 DWORD write_mask;
4350 write_mask = shader_glsl_append_dst(buffer, ins);
4351 mask_size = shader_glsl_get_write_mask_size(write_mask);
4352 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4354 if (mask_size > 1)
4355 shader_addline(buffer, "mix(uvec%u(max(%s, vec%u(0.0))), uvec%u(0xffffffffu), greaterThanEqual(%s, vec%u(4294967296.0))));\n",
4356 mask_size, src_param.param_str, mask_size, mask_size, src_param.param_str, mask_size);
4357 else
4358 shader_addline(buffer, "mix(uint(max(%s, 0.0)), 0xffffffffu, %s >= 4294967296.0));\n",
4359 src_param.param_str, src_param.param_str);
4362 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4364 shader_glsl_cast(ins, "vec", "float");
4367 /** Process signed comparison opcodes in GLSL. */
4368 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4370 struct glsl_src_param src0_param;
4371 struct glsl_src_param src1_param;
4372 DWORD write_mask;
4373 unsigned int mask_size;
4375 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4376 mask_size = shader_glsl_get_write_mask_size(write_mask);
4377 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4378 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4380 if (mask_size > 1) {
4381 const char *compare;
4383 switch(ins->handler_idx)
4385 case WINED3DSIH_SLT: compare = "lessThan"; break;
4386 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4387 default: compare = "";
4388 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4391 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4392 src0_param.param_str, src1_param.param_str);
4393 } else {
4394 switch(ins->handler_idx)
4396 case WINED3DSIH_SLT:
4397 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4398 * to return 0.0 but step returns 1.0 because step is not < x
4399 * An alternative is a bvec compare padded with an unused second component.
4400 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4401 * issue. Playing with not() is not possible either because not() does not accept
4402 * a scalar.
4404 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4405 src0_param.param_str, src1_param.param_str);
4406 break;
4407 case WINED3DSIH_SGE:
4408 /* Here we can use the step() function and safe a conditional */
4409 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4410 break;
4411 default:
4412 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4418 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4420 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4421 struct wined3d_shader_dst_param dst[2];
4422 struct glsl_src_param src[3];
4423 unsigned int i, j, k;
4424 char mask_char[6];
4425 DWORD write_mask;
4426 BOOL tmp_dst[2];
4428 for (i = 0; i < ins->dst_count; ++i)
4430 tmp_dst[i] = FALSE;
4431 for (j = 0; j < ins->src_count; ++j)
4433 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4434 && ins->dst[i].reg.type == ins->src[j].reg.type)
4435 tmp_dst[i] = TRUE;
4439 dst[0] = ins->dst[0];
4440 dst[1] = ins->dst[1];
4441 for (i = 0; i < 4; ++i)
4443 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4445 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4446 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4447 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4448 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], j, dst[j].reg.data_type)))
4449 continue;
4451 for (k = 0; k < ARRAY_SIZE(src); ++k)
4452 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4454 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4455 src[0].param_str, src[1].param_str, src[2].param_str);
4459 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4461 if (tmp_dst[i])
4463 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4464 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], i, ins->dst[i].reg.data_type);
4465 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4470 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4472 const char *condition_prefix, *condition_suffix;
4473 struct wined3d_shader_dst_param dst;
4474 struct glsl_src_param src0_param;
4475 struct glsl_src_param src1_param;
4476 struct glsl_src_param src2_param;
4477 BOOL temp_destination = FALSE;
4478 DWORD cmp_channel = 0;
4479 unsigned int i, j;
4480 char mask_char[6];
4481 DWORD write_mask;
4483 switch (ins->handler_idx)
4485 case WINED3DSIH_CMP:
4486 condition_prefix = "";
4487 condition_suffix = " >= 0.0";
4488 break;
4490 case WINED3DSIH_CND:
4491 condition_prefix = "";
4492 condition_suffix = " > 0.5";
4493 break;
4495 case WINED3DSIH_MOVC:
4496 condition_prefix = "bool(";
4497 condition_suffix = ")";
4498 break;
4500 default:
4501 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4502 condition_prefix = "<unhandled prefix>";
4503 condition_suffix = "<unhandled suffix>";
4504 break;
4507 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4509 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4510 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4511 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4512 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4514 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4515 condition_prefix, src0_param.param_str, condition_suffix,
4516 src1_param.param_str, src2_param.param_str);
4517 return;
4520 dst = ins->dst[0];
4522 /* Splitting the instruction up in multiple lines imposes a problem:
4523 * The first lines may overwrite source parameters of the following lines.
4524 * Deal with that by using a temporary destination register if needed. */
4525 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4526 && ins->src[0].reg.type == dst.reg.type)
4527 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4528 && ins->src[1].reg.type == dst.reg.type)
4529 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4530 && ins->src[2].reg.type == dst.reg.type))
4531 temp_destination = TRUE;
4533 /* Cycle through all source0 channels. */
4534 for (i = 0; i < 4; ++i)
4536 write_mask = 0;
4537 /* Find the destination channels which use the current source0 channel. */
4538 for (j = 0; j < 4; ++j)
4540 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4542 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4543 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4546 dst.write_mask = ins->dst[0].write_mask & write_mask;
4548 if (temp_destination)
4550 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4551 continue;
4552 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4554 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type)))
4555 continue;
4557 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4558 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4559 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4561 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4562 condition_prefix, src0_param.param_str, condition_suffix,
4563 src1_param.param_str, src2_param.param_str);
4566 if (temp_destination)
4568 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4569 shader_glsl_append_dst(ins->ctx->buffer, ins);
4570 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4574 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4575 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4576 * the compare is done per component of src0. */
4577 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4579 struct glsl_src_param src0_param;
4580 struct glsl_src_param src1_param;
4581 struct glsl_src_param src2_param;
4582 DWORD write_mask;
4583 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4584 ins->ctx->reg_maps->shader_version.minor);
4586 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4588 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4589 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4590 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4591 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4593 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4594 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4595 else
4596 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4597 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4598 return;
4601 shader_glsl_conditional_move(ins);
4604 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4605 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4607 struct glsl_src_param src0_param;
4608 struct glsl_src_param src1_param;
4609 struct glsl_src_param src2_param;
4610 DWORD write_mask;
4612 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4613 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4614 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4615 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4616 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4617 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4620 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4621 Vertex shaders to GLSL codes */
4622 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4624 int i;
4625 int nComponents = 0;
4626 struct wined3d_shader_dst_param tmp_dst = {{0}};
4627 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4628 struct wined3d_shader_instruction tmp_ins;
4630 memset(&tmp_ins, 0, sizeof(tmp_ins));
4632 /* Set constants for the temporary argument */
4633 tmp_ins.ctx = ins->ctx;
4634 tmp_ins.dst_count = 1;
4635 tmp_ins.dst = &tmp_dst;
4636 tmp_ins.src_count = 2;
4637 tmp_ins.src = tmp_src;
4639 switch(ins->handler_idx)
4641 case WINED3DSIH_M4x4:
4642 nComponents = 4;
4643 tmp_ins.handler_idx = WINED3DSIH_DP4;
4644 break;
4645 case WINED3DSIH_M4x3:
4646 nComponents = 3;
4647 tmp_ins.handler_idx = WINED3DSIH_DP4;
4648 break;
4649 case WINED3DSIH_M3x4:
4650 nComponents = 4;
4651 tmp_ins.handler_idx = WINED3DSIH_DP3;
4652 break;
4653 case WINED3DSIH_M3x3:
4654 nComponents = 3;
4655 tmp_ins.handler_idx = WINED3DSIH_DP3;
4656 break;
4657 case WINED3DSIH_M3x2:
4658 nComponents = 2;
4659 tmp_ins.handler_idx = WINED3DSIH_DP3;
4660 break;
4661 default:
4662 break;
4665 tmp_dst = ins->dst[0];
4666 tmp_src[0] = ins->src[0];
4667 tmp_src[1] = ins->src[1];
4668 for (i = 0; i < nComponents; ++i)
4670 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4671 shader_glsl_dot(&tmp_ins);
4672 ++tmp_src[1].reg.idx[0].offset;
4677 The LRP instruction performs a component-wise linear interpolation
4678 between the second and third operands using the first operand as the
4679 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4680 This is equivalent to mix(src2, src1, src0);
4682 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4684 struct glsl_src_param src0_param;
4685 struct glsl_src_param src1_param;
4686 struct glsl_src_param src2_param;
4687 DWORD write_mask;
4689 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4691 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4692 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4693 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4695 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4696 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4699 /** Process the WINED3DSIO_LIT instruction in GLSL:
4700 * dst.x = dst.w = 1.0
4701 * dst.y = (src0.x > 0) ? src0.x
4702 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4703 * where src.w is clamped at +- 128
4705 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4707 struct glsl_src_param src0_param;
4708 struct glsl_src_param src1_param;
4709 struct glsl_src_param src3_param;
4710 char dst_mask[6];
4712 shader_glsl_append_dst(ins->ctx->buffer, ins);
4713 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4715 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4716 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4717 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4719 /* The sdk specifies the instruction like this
4720 * dst.x = 1.0;
4721 * if(src.x > 0.0) dst.y = src.x
4722 * else dst.y = 0.0.
4723 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4724 * else dst.z = 0.0;
4725 * dst.w = 1.0;
4726 * (where power = src.w clamped between -128 and 128)
4728 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4729 * dst.x = 1.0 ... No further explanation needed
4730 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4731 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4732 * dst.w = 1.0. ... Nothing fancy.
4734 * So we still have one conditional in there. So do this:
4735 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4737 * 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),
4738 * 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.
4739 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4741 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4742 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4743 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4745 shader_addline(ins->ctx->buffer,
4746 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4747 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4748 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4749 src0_param.param_str, src3_param.param_str, dst_mask);
4752 /** Process the WINED3DSIO_DST instruction in GLSL:
4753 * dst.x = 1.0
4754 * dst.y = src0.x * src0.y
4755 * dst.z = src0.z
4756 * dst.w = src1.w
4758 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4760 struct glsl_src_param src0y_param;
4761 struct glsl_src_param src0z_param;
4762 struct glsl_src_param src1y_param;
4763 struct glsl_src_param src1w_param;
4764 char dst_mask[6];
4766 shader_glsl_append_dst(ins->ctx->buffer, ins);
4767 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4769 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4770 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4771 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4772 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4774 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4775 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4778 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4779 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4780 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4782 * dst.x = cos(src0.?)
4783 * dst.y = sin(src0.?)
4784 * dst.z = dst.z
4785 * dst.w = dst.w
4787 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4789 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4790 struct glsl_src_param src0_param;
4791 DWORD write_mask;
4793 if (ins->ctx->reg_maps->shader_version.major < 4)
4795 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4797 write_mask = shader_glsl_append_dst(buffer, ins);
4798 switch (write_mask)
4800 case WINED3DSP_WRITEMASK_0:
4801 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4802 break;
4804 case WINED3DSP_WRITEMASK_1:
4805 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4806 break;
4808 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4809 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4810 src0_param.param_str, src0_param.param_str);
4811 break;
4813 default:
4814 ERR("Write mask should be .x, .y or .xy\n");
4815 break;
4818 return;
4821 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4824 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4826 char dst_mask[6];
4828 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4829 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4830 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4832 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4833 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4834 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4836 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4837 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4839 else
4841 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4842 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4843 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4846 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4848 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4849 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4850 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4854 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4855 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4856 * generate invalid code
4858 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4860 struct glsl_src_param src0_param;
4861 DWORD write_mask;
4863 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4864 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4866 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4869 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4870 * Start a for() loop where src1.y is the initial value of aL,
4871 * increment aL by src1.z for a total of src1.x iterations.
4872 * Need to use a temporary variable for this operation.
4874 /* FIXME: I don't think nested loops will work correctly this way. */
4875 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4877 struct wined3d_shader_parser_state *state = ins->ctx->state;
4878 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4879 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4880 const struct wined3d_shader *shader = ins->ctx->shader;
4881 const struct wined3d_shader_lconst *constant;
4882 struct wined3d_string_buffer *reg_name;
4883 const unsigned int *control_values = NULL;
4885 if (ins->ctx->reg_maps->shader_version.major < 4)
4887 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4888 * class hardware doesn't support real varying indexing, but Microsoft
4889 * designed this feature for Shader model 2.x+. If the loop control is
4890 * known at compile time, the GLSL compiler can unroll the loop, and
4891 * replace indirect addressing with direct addressing. */
4892 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4894 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4896 if (constant->idx == ins->src[1].reg.idx[0].offset)
4898 control_values = constant->value;
4899 break;
4904 if (control_values)
4906 struct wined3d_shader_loop_control loop_control;
4907 loop_control.count = control_values[0];
4908 loop_control.start = control_values[1];
4909 loop_control.step = (int)control_values[2];
4911 if (loop_control.step > 0)
4913 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4914 state->current_loop_depth, loop_control.start,
4915 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4916 state->current_loop_depth, loop_control.step);
4918 else if (loop_control.step < 0)
4920 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4921 state->current_loop_depth, loop_control.start,
4922 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4923 state->current_loop_depth, loop_control.step);
4925 else
4927 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4928 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4929 state->current_loop_depth, loop_control.count,
4930 state->current_loop_depth);
4933 else
4935 reg_name = string_buffer_get(priv->string_buffers);
4936 shader_glsl_get_register_name(&ins->src[1].reg, ins->src[1].reg.data_type, reg_name, NULL, ins->ctx);
4938 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4939 state->current_loop_depth, state->current_loop_reg, reg_name->buffer,
4940 state->current_loop_depth, reg_name->buffer,
4941 state->current_loop_depth, state->current_loop_reg, reg_name->buffer);
4943 string_buffer_release(priv->string_buffers, reg_name);
4947 ++state->current_loop_reg;
4949 else
4951 shader_addline(buffer, "for (;;)\n{\n");
4954 ++state->current_loop_depth;
4957 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4959 struct wined3d_shader_parser_state *state = ins->ctx->state;
4961 shader_addline(ins->ctx->buffer, "}\n");
4963 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4965 --state->current_loop_depth;
4966 --state->current_loop_reg;
4969 if (ins->handler_idx == WINED3DSIH_ENDREP)
4971 --state->current_loop_depth;
4975 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4977 struct wined3d_shader_parser_state *state = ins->ctx->state;
4978 const struct wined3d_shader *shader = ins->ctx->shader;
4979 const struct wined3d_shader_lconst *constant;
4980 struct glsl_src_param src0_param;
4981 const unsigned int *control_values = NULL;
4983 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4984 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4986 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4988 if (constant->idx == ins->src[0].reg.idx[0].offset)
4990 control_values = constant->value;
4991 break;
4996 if (control_values)
4998 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4999 state->current_loop_depth, state->current_loop_depth,
5000 control_values[0], state->current_loop_depth);
5002 else
5004 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5005 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
5006 state->current_loop_depth, state->current_loop_depth,
5007 src0_param.param_str, state->current_loop_depth);
5010 ++state->current_loop_depth;
5013 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5015 struct glsl_src_param src0_param;
5017 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5018 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5021 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5023 struct glsl_src_param src0_param;
5025 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5026 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5029 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5031 shader_addline(ins->ctx->buffer, "default:\n");
5034 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
5036 struct glsl_src_param src_param;
5037 const char *condition;
5039 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5040 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5041 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
5044 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5046 shader_glsl_generate_condition(ins);
5047 shader_addline(ins->ctx->buffer, "{\n");
5050 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5052 struct glsl_src_param src0_param;
5053 struct glsl_src_param src1_param;
5055 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5056 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5058 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5059 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5062 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5064 shader_addline(ins->ctx->buffer, "} else {\n");
5067 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5069 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5070 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5071 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5073 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5074 if (!priv->gl_info->supported[ARB_CLIP_CONTROL])
5075 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5077 if (!stream)
5078 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5079 else
5080 FIXME("Unhandled primitive stream %u.\n", stream);
5083 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5085 shader_addline(ins->ctx->buffer, "break;\n");
5088 /* FIXME: According to MSDN the compare is done per component. */
5089 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5091 struct glsl_src_param src0_param;
5092 struct glsl_src_param src1_param;
5094 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5095 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5097 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5098 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5101 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5103 const char *op;
5105 switch (ins->handler_idx)
5107 case WINED3DSIH_BREAKP:
5108 op = "break;";
5109 break;
5110 case WINED3DSIH_CONTINUEP:
5111 op = "continue;";
5112 break;
5113 case WINED3DSIH_RETP:
5114 op = "return;";
5115 break;
5116 default:
5117 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5118 return;
5121 shader_glsl_generate_condition(ins);
5122 if (ins->handler_idx == WINED3DSIH_RETP)
5124 shader_addline(ins->ctx->buffer, "{\n");
5125 shader_glsl_generate_shader_epilogue(ins->ctx);
5127 shader_addline(ins->ctx->buffer, " %s\n", op);
5128 if (ins->handler_idx == WINED3DSIH_RETP)
5129 shader_addline(ins->ctx->buffer, "}\n");
5132 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5134 shader_addline(ins->ctx->buffer, "continue;\n");
5137 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5139 shader_addline(ins->ctx->buffer, "}\n");
5140 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5142 /* Subroutines appear at the end of the shader. */
5143 ins->ctx->state->in_subroutine = TRUE;
5146 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5148 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5151 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5153 struct glsl_src_param src1_param;
5155 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5156 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5157 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5160 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5162 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5164 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5166 shader_glsl_generate_shader_epilogue(ins->ctx);
5167 shader_addline(ins->ctx->buffer, "return;\n");
5171 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5173 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5174 ins->ctx->reg_maps->shader_version.minor);
5175 struct glsl_sample_function sample_function;
5176 DWORD sample_flags = 0;
5177 unsigned int resource_idx;
5178 DWORD mask = 0, swizzle;
5179 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5180 enum wined3d_shader_resource_type resource_type;
5182 /* 1.0-1.4: Use destination register as sampler source.
5183 * 2.0+: Use provided sampler source. */
5184 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5185 resource_idx = ins->dst[0].reg.idx[0].offset;
5186 else
5187 resource_idx = ins->src[1].reg.idx[0].offset;
5189 resource_type = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
5190 ? pixelshader_get_resource_type(ins->ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
5191 : ins->ctx->reg_maps->resource_info[resource_idx].type;
5193 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5195 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5196 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5198 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5199 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5201 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5202 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5204 case WINED3D_TTFF_COUNT1:
5205 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5206 break;
5207 case WINED3D_TTFF_COUNT2:
5208 mask = WINED3DSP_WRITEMASK_1;
5209 break;
5210 case WINED3D_TTFF_COUNT3:
5211 mask = WINED3DSP_WRITEMASK_2;
5212 break;
5213 case WINED3D_TTFF_COUNT4:
5214 case WINED3D_TTFF_DISABLE:
5215 mask = WINED3DSP_WRITEMASK_3;
5216 break;
5220 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5222 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5224 if (src_mod == WINED3DSPSM_DZ) {
5225 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5226 mask = WINED3DSP_WRITEMASK_2;
5227 } else if (src_mod == WINED3DSPSM_DW) {
5228 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5229 mask = WINED3DSP_WRITEMASK_3;
5232 else
5234 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5235 && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5237 /* ps 2.0 texldp instruction always divides by the fourth component. */
5238 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5239 mask = WINED3DSP_WRITEMASK_3;
5243 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5244 mask |= sample_function.coord_mask;
5245 sample_function.coord_mask = mask;
5247 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5248 else swizzle = ins->src[1].swizzle;
5250 /* 1.0-1.3: Use destination register as coordinate source.
5251 1.4+: Use provided coordinate source register. */
5252 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5254 char coord_mask[6];
5255 shader_glsl_write_mask_to_str(mask, coord_mask);
5256 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5257 "T%u%s", resource_idx, coord_mask);
5259 else
5261 struct glsl_src_param coord_param;
5262 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5263 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5265 struct glsl_src_param bias;
5266 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5267 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5268 NULL, "%s", coord_param.param_str);
5269 } else {
5270 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5271 "%s", coord_param.param_str);
5274 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5277 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5279 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5280 const struct wined3d_gl_info *gl_info = priv->gl_info;
5281 struct glsl_src_param coord_param, dx_param, dy_param;
5282 struct glsl_sample_function sample_function;
5283 DWORD sampler_idx;
5284 DWORD swizzle = ins->src[1].swizzle;
5286 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5288 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5289 shader_glsl_tex(ins);
5290 return;
5293 sampler_idx = ins->src[1].reg.idx[0].offset;
5295 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5296 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5297 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5298 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5300 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5301 NULL, NULL, "%s", coord_param.param_str);
5302 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5305 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5307 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5308 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5309 const struct wined3d_gl_info *gl_info = priv->gl_info;
5310 struct glsl_src_param coord_param, lod_param;
5311 struct glsl_sample_function sample_function;
5312 DWORD swizzle = ins->src[1].swizzle;
5313 DWORD sampler_idx;
5315 sampler_idx = ins->src[1].reg.idx[0].offset;
5317 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5318 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5320 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5322 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5323 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5325 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5326 * However, the NVIDIA drivers allow them in fragment shaders as well,
5327 * even without the appropriate extension. */
5328 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5330 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5331 "%s", coord_param.param_str);
5332 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5335 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5336 unsigned int resource_idx, unsigned int sampler_idx)
5338 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5339 unsigned int i;
5341 for (i = 0; i < sampler_map->count; ++i)
5343 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5344 return entries[i].bind_idx;
5347 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5349 return ~0u;
5352 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5354 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5355 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5356 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5357 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5358 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5359 struct glsl_src_param structure_idx, offset, data, data2;
5360 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5361 enum wined3d_shader_resource_type resource_type;
5362 struct wined3d_string_buffer *address;
5363 enum wined3d_data_type data_type;
5364 unsigned int resource_idx, stride;
5365 const char *op, *resource;
5366 DWORD coord_mask;
5367 BOOL is_tgsm;
5369 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5370 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5371 if (is_tgsm)
5373 if (resource_idx >= reg_maps->tgsm_count)
5375 ERR("Invalid TGSM index %u.\n", resource_idx);
5376 return;
5378 resource = "g";
5379 data_type = WINED3D_DATA_UINT;
5380 coord_mask = 1;
5381 stride = reg_maps->tgsm[resource_idx].stride;
5383 else
5385 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5387 ERR("Invalid UAV index %u.\n", resource_idx);
5388 return;
5390 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5391 if (resource_type >= ARRAY_SIZE(resource_type_info))
5393 ERR("Unexpected resource type %#x.\n", resource_type);
5394 return;
5396 resource = "image";
5397 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5398 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5399 stride = reg_maps->uav_resource_info[resource_idx].stride;
5402 switch (ins->handler_idx)
5404 case WINED3DSIH_ATOMIC_AND:
5405 case WINED3DSIH_IMM_ATOMIC_AND:
5406 if (is_tgsm)
5407 op = "atomicAnd";
5408 else
5409 op = "imageAtomicAnd";
5410 break;
5411 case WINED3DSIH_ATOMIC_CMP_STORE:
5412 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5413 if (is_tgsm)
5414 op = "atomicCompSwap";
5415 else
5416 op = "imageAtomicCompSwap";
5417 break;
5418 case WINED3DSIH_ATOMIC_IADD:
5419 case WINED3DSIH_IMM_ATOMIC_IADD:
5420 if (is_tgsm)
5421 op = "atomicAdd";
5422 else
5423 op = "imageAtomicAdd";
5424 break;
5425 case WINED3DSIH_ATOMIC_IMAX:
5426 case WINED3DSIH_IMM_ATOMIC_IMAX:
5427 if (is_tgsm)
5428 op = "atomicMax";
5429 else
5430 op = "imageAtomicMax";
5431 if (data_type != WINED3D_DATA_INT)
5433 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5434 return;
5436 break;
5437 case WINED3DSIH_ATOMIC_IMIN:
5438 case WINED3DSIH_IMM_ATOMIC_IMIN:
5439 if (is_tgsm)
5440 op = "atomicMin";
5441 else
5442 op = "imageAtomicMin";
5443 if (data_type != WINED3D_DATA_INT)
5445 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5446 return;
5448 break;
5449 case WINED3DSIH_ATOMIC_OR:
5450 case WINED3DSIH_IMM_ATOMIC_OR:
5451 if (is_tgsm)
5452 op = "atomicOr";
5453 else
5454 op = "imageAtomicOr";
5455 break;
5456 case WINED3DSIH_ATOMIC_UMAX:
5457 case WINED3DSIH_IMM_ATOMIC_UMAX:
5458 if (is_tgsm)
5459 op = "atomicMax";
5460 else
5461 op = "imageAtomicMax";
5462 if (data_type != WINED3D_DATA_UINT)
5464 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5465 return;
5467 break;
5468 case WINED3DSIH_ATOMIC_UMIN:
5469 case WINED3DSIH_IMM_ATOMIC_UMIN:
5470 if (is_tgsm)
5471 op = "atomicMin";
5472 else
5473 op = "imageAtomicMin";
5474 if (data_type != WINED3D_DATA_UINT)
5476 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5477 return;
5479 break;
5480 case WINED3DSIH_ATOMIC_XOR:
5481 case WINED3DSIH_IMM_ATOMIC_XOR:
5482 if (is_tgsm)
5483 op = "atomicXor";
5484 else
5485 op = "imageAtomicXor";
5486 break;
5487 case WINED3DSIH_IMM_ATOMIC_EXCH:
5488 if (is_tgsm)
5489 op = "atomicExchange";
5490 else
5491 op = "imageAtomicExchange";
5492 break;
5493 default:
5494 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5495 return;
5498 address = string_buffer_get(priv->string_buffers);
5499 if (stride)
5501 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5502 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5503 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5505 else
5507 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5508 string_buffer_sprintf(address, "%s", offset.param_str);
5509 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5510 shader_addline(address, "/ 4");
5513 if (is_imm_instruction)
5514 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5516 if (is_tgsm)
5517 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5518 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5519 else
5520 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5521 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5523 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5524 shader_addline(buffer, "%s", data.param_str);
5525 if (ins->src_count >= 3)
5527 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5528 shader_addline(buffer, ", %s", data2.param_str);
5531 if (is_imm_instruction)
5532 shader_addline(buffer, ")");
5533 shader_addline(buffer, ");\n");
5535 string_buffer_release(priv->string_buffers, address);
5538 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5540 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5541 const char *op;
5543 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5544 op = "atomicCounterIncrement";
5545 else
5546 op = "atomicCounterDecrement";
5548 shader_glsl_append_dst(ins->ctx->buffer, ins);
5549 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5552 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5554 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5555 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5556 enum wined3d_shader_resource_type resource_type;
5557 struct glsl_src_param image_coord_param;
5558 enum wined3d_data_type data_type;
5559 DWORD coord_mask, write_mask;
5560 unsigned int uav_idx;
5561 char dst_swizzle[6];
5563 uav_idx = ins->src[1].reg.idx[0].offset;
5564 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5566 ERR("Invalid UAV index %u.\n", uav_idx);
5567 return;
5569 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5570 if (resource_type >= ARRAY_SIZE(resource_type_info))
5572 ERR("Unexpected resource type %#x.\n", resource_type);
5573 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5575 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5576 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5578 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5579 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5581 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5582 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5583 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5586 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5588 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5589 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5590 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5591 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5592 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5593 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5594 struct glsl_src_param structure_idx, offset;
5595 struct wined3d_string_buffer *address;
5596 struct wined3d_shader_dst_param dst;
5597 const char *function, *resource;
5599 resource_idx = src->reg.idx[0].offset;
5600 if (src->reg.type == WINED3DSPR_RESOURCE)
5602 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5604 ERR("Invalid resource index %u.\n", resource_idx);
5605 return;
5607 stride = reg_maps->resource_info[resource_idx].stride;
5608 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5609 function = "texelFetch";
5610 resource = "sampler";
5612 else if (src->reg.type == WINED3DSPR_UAV)
5614 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5616 ERR("Invalid UAV index %u.\n", resource_idx);
5617 return;
5619 stride = reg_maps->uav_resource_info[resource_idx].stride;
5620 bind_idx = resource_idx;
5621 function = "imageLoad";
5622 resource = "image";
5624 else
5626 if (resource_idx >= reg_maps->tgsm_count)
5628 ERR("Invalid TGSM index %u.\n", resource_idx);
5629 return;
5631 stride = reg_maps->tgsm[resource_idx].stride;
5632 bind_idx = resource_idx;
5633 function = NULL;
5634 resource = "g";
5637 address = string_buffer_get(priv->string_buffers);
5638 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5640 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5641 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5643 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5644 shader_addline(address, "%s / 4", offset.param_str);
5646 dst = ins->dst[0];
5647 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5649 /* The instruction is split into multiple lines. The first lines may
5650 * overwrite source parameters of the following lines. */
5651 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5652 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5655 for (i = 0; i < 4; ++i)
5657 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5658 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type))
5659 continue;
5661 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5662 if (function)
5663 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5664 function, prefix, resource, bind_idx, address->buffer, swizzle);
5665 else
5666 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5667 prefix, resource, bind_idx, address->buffer, swizzle);
5670 string_buffer_release(priv->string_buffers, address);
5673 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5675 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5676 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5677 struct glsl_src_param image_coord_param, image_data_param;
5678 enum wined3d_shader_resource_type resource_type;
5679 enum wined3d_data_type data_type;
5680 unsigned int uav_idx;
5681 DWORD coord_mask;
5683 uav_idx = ins->dst[0].reg.idx[0].offset;
5684 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5686 ERR("Invalid UAV index %u.\n", uav_idx);
5687 return;
5689 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5690 if (resource_type >= ARRAY_SIZE(resource_type_info))
5692 ERR("Unexpected resource type %#x.\n", resource_type);
5693 return;
5695 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5696 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5698 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5699 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5700 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5701 shader_glsl_get_prefix(version->type), uav_idx,
5702 image_coord_param.param_str, image_data_param.param_str);
5705 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5707 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5708 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5709 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5710 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5711 struct glsl_src_param structure_idx, offset, data;
5712 unsigned int i, resource_idx, stride, src_idx = 0;
5713 struct wined3d_string_buffer *address;
5714 DWORD write_mask;
5715 BOOL is_tgsm;
5717 resource_idx = ins->dst[0].reg.idx[0].offset;
5718 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5719 if (is_tgsm)
5721 if (resource_idx >= reg_maps->tgsm_count)
5723 ERR("Invalid TGSM index %u.\n", resource_idx);
5724 return;
5726 stride = reg_maps->tgsm[resource_idx].stride;
5728 else
5730 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5732 ERR("Invalid UAV index %u.\n", resource_idx);
5733 return;
5735 stride = reg_maps->uav_resource_info[resource_idx].stride;
5738 address = string_buffer_get(priv->string_buffers);
5739 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5741 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5742 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5744 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5745 shader_addline(address, "%s / 4", offset.param_str);
5747 for (i = 0; i < 4; ++i)
5749 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5750 continue;
5752 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5754 if (is_tgsm)
5755 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5756 prefix, resource_idx, address->buffer, i, data.param_str);
5757 else
5758 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5759 prefix, resource_idx, address->buffer, i, data.param_str);
5762 string_buffer_release(priv->string_buffers, address);
5765 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5767 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5768 unsigned int sync_flags = ins->flags;
5770 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5772 shader_addline(buffer, "barrier();\n");
5773 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5776 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5778 shader_addline(buffer, "memoryBarrierShared();\n");
5779 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5782 if (sync_flags & WINED3DSSF_GLOBAL_UAV)
5784 shader_addline(buffer, "memoryBarrier();\n");
5785 sync_flags &= ~WINED3DSSF_GLOBAL_UAV;
5788 if (sync_flags)
5789 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5792 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5793 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5795 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5796 unsigned int idx = reg->idx[0].offset;
5798 if (reg->type == WINED3DSPR_RESOURCE)
5800 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5802 ERR("Invalid resource index %u.\n", idx);
5803 return NULL;
5805 return &reg_maps->resource_info[idx];
5808 if (reg->type == WINED3DSPR_UAV)
5810 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5812 ERR("Invalid UAV index %u.\n", idx);
5813 return NULL;
5815 return &reg_maps->uav_resource_info[idx];
5818 FIXME("Unhandled register type %#x.\n", reg->type);
5819 return NULL;
5822 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5824 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5825 const struct wined3d_shader_resource_info *resource_info;
5826 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5827 unsigned int resource_idx;
5828 char dst_swizzle[6];
5829 DWORD write_mask;
5831 write_mask = shader_glsl_append_dst(buffer, ins);
5832 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5834 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5835 return;
5836 resource_idx = ins->src[0].reg.idx[0].offset;
5838 shader_addline(buffer, "ivec2(");
5839 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5841 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5842 resource_idx, WINED3D_SAMPLER_DEFAULT);
5843 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5845 else
5847 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5849 if (resource_info->stride)
5850 shader_addline(buffer, " / %u", resource_info->stride);
5851 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5852 shader_addline(buffer, " * 4");
5853 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5856 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5858 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5859 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5862 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5864 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5867 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5869 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5870 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5871 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5872 const struct wined3d_gl_info *gl_info = priv->gl_info;
5873 enum wined3d_shader_resource_type resource_type;
5874 enum wined3d_shader_register_type reg_type;
5875 unsigned int resource_idx, bind_idx, i;
5876 enum wined3d_data_type dst_data_type;
5877 struct glsl_src_param lod_param;
5878 BOOL supports_mipmaps;
5879 char dst_swizzle[6];
5880 DWORD write_mask;
5882 dst_data_type = ins->dst[0].reg.data_type;
5883 if (ins->flags == WINED3DSI_RESINFO_UINT)
5884 dst_data_type = WINED3D_DATA_UINT;
5885 else if (ins->flags)
5886 FIXME("Unhandled flags %#x.\n", ins->flags);
5888 reg_type = ins->src[1].reg.type;
5889 resource_idx = ins->src[1].reg.idx[0].offset;
5890 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5891 if (reg_type == WINED3DSPR_RESOURCE)
5893 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5894 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5895 resource_idx, WINED3D_SAMPLER_DEFAULT);
5897 else
5899 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5900 bind_idx = resource_idx;
5903 if (resource_type >= ARRAY_SIZE(resource_type_info))
5905 ERR("Unexpected resource type %#x.\n", resource_type);
5906 return;
5909 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, dst_data_type);
5910 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5912 if (dst_data_type == WINED3D_DATA_UINT)
5913 shader_addline(buffer, "uvec4(");
5914 else
5915 shader_addline(buffer, "vec4(");
5917 if (reg_type == WINED3DSPR_RESOURCE)
5919 shader_addline(buffer, "textureSize(%s_sampler%u",
5920 shader_glsl_get_prefix(version->type), bind_idx);
5922 else
5924 shader_addline(buffer, "imageSize(%s_image%u",
5925 shader_glsl_get_prefix(version->type), bind_idx);
5928 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5929 if (supports_mipmaps)
5930 shader_addline(buffer, ", %s", lod_param.param_str);
5931 shader_addline(buffer, "), ");
5933 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5934 shader_addline(buffer, "0, ");
5936 if (supports_mipmaps)
5938 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5940 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5941 shader_glsl_get_prefix(version->type), bind_idx);
5943 else
5945 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
5946 shader_addline(buffer, "1");
5949 else
5951 shader_addline(buffer, "1");
5954 shader_addline(buffer, ")%s);\n", dst_swizzle);
5957 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
5959 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5960 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5961 const struct wined3d_gl_info *gl_info = priv->gl_info;
5962 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5963 const struct wined3d_shader_dst_param *dst = ins->dst;
5964 const struct wined3d_shader_src_param *src = ins->src;
5965 enum wined3d_shader_resource_type resource_type;
5966 enum wined3d_data_type dst_data_type;
5967 unsigned int resource_idx, bind_idx;
5968 char dst_swizzle[6];
5969 DWORD write_mask;
5971 dst_data_type = dst->reg.data_type;
5972 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
5973 dst_data_type = WINED3D_DATA_UINT;
5974 else if (ins->flags)
5975 FIXME("Unhandled flags %#x.\n", ins->flags);
5977 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, 0, dst_data_type);
5978 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
5980 if (dst_data_type == WINED3D_DATA_UINT)
5981 shader_addline(buffer, "uvec4(");
5982 else
5983 shader_addline(buffer, "vec4(");
5985 if (src->reg.type == WINED3DSPR_RASTERIZER)
5987 if (gl_info->supported[ARB_SAMPLE_SHADING])
5989 shader_addline(buffer, "gl_NumSamples");
5991 else
5993 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
5994 shader_addline(buffer, "1");
5997 else
5999 resource_idx = src->reg.idx[0].offset;
6000 resource_type = reg_maps->resource_info[resource_idx].type;
6001 if (resource_type >= ARRAY_SIZE(resource_type_info))
6003 ERR("Unexpected resource type %#x.\n", resource_type);
6004 return;
6006 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
6008 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
6010 shader_addline(buffer, "textureSamples(%s_sampler%u)",
6011 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
6013 else
6015 FIXME("textureSamples() is not supported.\n");
6016 shader_addline(buffer, "1");
6020 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
6023 static void shader_glsl_interpolate(const struct wined3d_shader_instruction *ins)
6025 const struct wined3d_shader_src_param *input = &ins->src[0];
6026 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6027 struct glsl_src_param sample_param;
6028 char dst_swizzle[6];
6029 DWORD write_mask;
6031 write_mask = shader_glsl_append_dst(buffer, ins);
6032 shader_glsl_get_swizzle(input, FALSE, write_mask, dst_swizzle);
6034 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &sample_param);
6035 shader_addline(buffer, "interpolateAtSample(shader_in.reg%u, %s)%s);\n",
6036 input->reg.idx[0].offset, sample_param.param_str, dst_swizzle);
6039 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6041 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6042 struct glsl_src_param coord_param, lod_param, sample_param;
6043 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6044 struct glsl_sample_function sample_function;
6045 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6046 BOOL has_lod_param;
6048 if (wined3d_shader_instruction_has_texel_offset(ins))
6049 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6051 resource_idx = ins->src[1].reg.idx[0].offset;
6052 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6054 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6056 ERR("Invalid resource index %u.\n", resource_idx);
6057 return;
6059 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6061 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6062 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6063 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6064 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6065 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6067 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6068 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6069 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6071 else
6073 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6074 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6075 "%s", coord_param.param_str);
6077 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6080 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6082 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6083 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6084 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6085 struct glsl_sample_function sample_function;
6086 DWORD flags = 0;
6088 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6089 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6090 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6091 flags |= WINED3D_GLSL_SAMPLE_LOD;
6092 if (wined3d_shader_instruction_has_texel_offset(ins))
6093 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6095 resource_idx = ins->src[1].reg.idx[0].offset;
6096 sampler_idx = ins->src[2].reg.idx[0].offset;
6098 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6099 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6101 switch (ins->handler_idx)
6103 case WINED3DSIH_SAMPLE:
6104 break;
6105 case WINED3DSIH_SAMPLE_B:
6106 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6107 lod_param_str = lod_param.param_str;
6108 break;
6109 case WINED3DSIH_SAMPLE_GRAD:
6110 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6111 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6112 dx_param_str = dx_param.param_str;
6113 dy_param_str = dy_param.param_str;
6114 break;
6115 case WINED3DSIH_SAMPLE_LOD:
6116 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6117 lod_param_str = lod_param.param_str;
6118 break;
6119 default:
6120 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6121 break;
6124 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6125 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6126 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6127 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6130 /* Unless EXT_texture_shadow_lod is available, GLSL doesn't provide a function
6131 * to sample from level zero with depth comparison for array textures and cube
6132 * textures. We use textureGrad*() to implement sample_c_lz in that case. */
6133 static void shader_glsl_gen_sample_c_lz_emulation(const struct wined3d_shader_instruction *ins,
6134 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6135 unsigned int coord_size, const char *coord_param, const char *ref_param)
6137 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6138 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6139 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6140 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6141 char dst_swizzle[6];
6143 WARN("Emitting textureGrad() for sample_c_lz.\n");
6145 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6146 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, sample_function->data_type);
6147 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6148 sample_function->offset_size ? "Offset" : "",
6149 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6150 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6151 if (sample_function->offset_size)
6153 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6154 shader_addline(buffer, ", ");
6155 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6157 shader_addline(buffer, "))%s);\n", dst_swizzle);
6160 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6162 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6163 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6164 const struct wined3d_shader_resource_info *resource_info;
6165 const struct wined3d_gl_info *gl_info = priv->gl_info;
6166 struct glsl_src_param coord_param, compare_param;
6167 struct glsl_sample_function sample_function;
6168 const char *lod_param = NULL;
6169 unsigned int coord_size;
6170 DWORD flags = 0;
6172 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6174 lod_param = "0";
6175 flags |= WINED3D_GLSL_SAMPLE_LOD;
6178 if (wined3d_shader_instruction_has_texel_offset(ins))
6179 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6181 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6182 return;
6183 resource_idx = ins->src[1].reg.idx[0].offset;
6184 sampler_idx = ins->src[2].reg.idx[0].offset;
6186 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6187 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6188 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6189 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6190 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6191 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6192 && !gl_info->supported[EXT_TEXTURE_SHADOW_LOD]
6193 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6194 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6196 shader_glsl_gen_sample_c_lz_emulation(ins, sampler_bind_idx, &sample_function,
6197 coord_size, coord_param.param_str, compare_param.param_str);
6199 else
6201 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6202 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6203 coord_size, coord_param.param_str, compare_param.param_str);
6205 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6208 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6210 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6211 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6212 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6213 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6214 struct glsl_src_param coord_param, compare_param, offset_param;
6215 const struct wined3d_gl_info *gl_info = priv->gl_info;
6216 const struct wined3d_shader_resource_info *resource_info;
6217 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6218 unsigned int coord_size, offset_size;
6219 char dst_swizzle[6];
6220 BOOL has_offset;
6222 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6224 FIXME("OpenGL implementation does not support textureGather.\n");
6225 return;
6228 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6229 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6230 || wined3d_shader_instruction_has_texel_offset(ins);
6232 resource_param_idx =
6233 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6234 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6235 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6236 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6237 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6239 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6240 return;
6242 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6244 ERR("Unexpected resource type %#x.\n", resource_info->type);
6245 return;
6247 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6249 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6250 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, resource_info->data_type);
6252 shader_glsl_add_src_param(ins, &ins->src[0], wined3d_mask_from_size(coord_size), &coord_param);
6254 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6255 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6256 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6258 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6259 shader_addline(buffer, ", %s", compare_param.param_str);
6261 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6263 shader_glsl_add_src_param(ins, &ins->src[1], wined3d_mask_from_size(offset_size), &offset_param);
6264 shader_addline(buffer, ", %s", offset_param.param_str);
6266 else if (has_offset)
6268 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6269 shader_addline(buffer, ", ");
6270 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6272 if (component_idx)
6273 shader_addline(buffer, ", %u", component_idx);
6275 shader_addline(buffer, ")%s);\n", dst_swizzle);
6278 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6280 /* FIXME: Make this work for more than just 2D textures */
6281 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6282 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6284 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6286 char dst_mask[6];
6288 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6289 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6290 ins->dst[0].reg.idx[0].offset, dst_mask);
6292 else
6294 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6295 unsigned int reg = ins->src[0].reg.idx[0].offset;
6296 char dst_swizzle[6];
6298 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6300 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6302 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6303 struct glsl_src_param div_param;
6304 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6306 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6308 if (mask_size > 1)
6309 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6310 else
6311 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6313 else
6315 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6320 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6321 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6322 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6323 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6325 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6326 unsigned int sampler_idx = ins->dst[0].reg.idx[0].offset;
6327 struct glsl_sample_function sample_function;
6328 struct glsl_src_param src0_param;
6329 UINT mask_size;
6331 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6333 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6334 * scalar, and projected sampling would require 4.
6336 * It is a dependent read - not valid with conditional NP2 textures
6338 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6339 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6341 switch(mask_size)
6343 case 1:
6344 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6345 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6346 break;
6348 case 2:
6349 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6350 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6351 break;
6353 case 3:
6354 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6355 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6356 break;
6358 default:
6359 FIXME("Unexpected mask size %u\n", mask_size);
6360 break;
6362 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6365 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6366 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6367 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6369 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6370 unsigned int dstreg = ins->dst[0].reg.idx[0].offset;
6371 struct glsl_src_param src0_param;
6372 DWORD dst_mask;
6373 unsigned int mask_size;
6375 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6376 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6377 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6379 if (mask_size > 1) {
6380 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6381 } else {
6382 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6386 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6387 * Calculate the depth as dst.x / dst.y */
6388 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6390 struct glsl_dst_param dst_param;
6392 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6394 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6395 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6396 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6397 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6398 * >= 1.0 or < 0.0
6400 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6401 dst_param.reg_name, dst_param.reg_name);
6404 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6405 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6406 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6407 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6409 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6411 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6412 unsigned int dstreg = ins->dst[0].reg.idx[0].offset;
6413 struct glsl_src_param src0_param;
6415 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6417 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6418 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6421 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6422 * Calculate the 1st of a 2-row matrix multiplication. */
6423 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6425 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6426 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6427 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6428 struct glsl_src_param src0_param;
6430 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6431 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6434 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6435 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6436 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6438 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6439 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6440 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6441 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6442 struct glsl_src_param src0_param;
6444 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6445 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6446 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6449 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6451 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6452 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6453 struct glsl_sample_function sample_function;
6454 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6455 struct glsl_src_param src0_param;
6457 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6458 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6460 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6462 /* Sample the texture using the calculated coordinates */
6463 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6464 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6467 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6468 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6469 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6471 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6472 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6473 struct glsl_sample_function sample_function;
6474 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6475 struct glsl_src_param src0_param;
6477 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6478 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6480 /* Dependent read, not valid with conditional NP2 */
6481 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6483 /* Sample the texture using the calculated coordinates */
6484 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6485 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6487 tex_mx->current_row = 0;
6490 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6491 * Perform the 3rd row of a 3x3 matrix multiply */
6492 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6494 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6495 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6496 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6497 struct glsl_src_param src0_param;
6498 char dst_mask[6];
6500 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6502 shader_glsl_append_dst(ins->ctx->buffer, ins);
6503 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6504 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6506 tex_mx->current_row = 0;
6509 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6510 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6511 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6513 struct glsl_src_param src0_param;
6514 struct glsl_src_param src1_param;
6515 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6516 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6517 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6518 struct glsl_sample_function sample_function;
6519 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6520 char coord_mask[6];
6522 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6523 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6525 /* Perform the last matrix multiply operation */
6526 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6527 /* Reflection calculation */
6528 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6530 /* Dependent read, not valid with conditional NP2 */
6531 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6532 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6534 /* Sample the texture */
6535 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6536 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6537 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6539 tex_mx->current_row = 0;
6542 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6543 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6544 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6546 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6547 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6548 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6549 struct glsl_sample_function sample_function;
6550 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6551 struct glsl_src_param src0_param;
6552 char coord_mask[6];
6554 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6556 /* Perform the last matrix multiply operation */
6557 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6559 /* Construct the eye-ray vector from w coordinates */
6560 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6561 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6562 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6564 /* Dependent read, not valid with conditional NP2 */
6565 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6566 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6568 /* Sample the texture using the calculated coordinates */
6569 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6570 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6571 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6573 tex_mx->current_row = 0;
6576 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6577 * Apply a fake bump map transform.
6578 * texbem is pshader <= 1.3 only, this saves a few version checks
6580 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6582 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6583 struct glsl_sample_function sample_function;
6584 struct glsl_src_param coord_param;
6585 unsigned int sampler_idx;
6586 DWORD mask;
6587 DWORD flags;
6588 char coord_mask[6];
6590 sampler_idx = ins->dst[0].reg.idx[0].offset;
6591 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6592 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6594 /* Dependent read, not valid with conditional NP2 */
6595 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6596 mask = sample_function.coord_mask;
6598 shader_glsl_write_mask_to_str(mask, coord_mask);
6600 /* With projected textures, texbem only divides the static texture coord,
6601 * not the displacement, so we can't let GL handle this. */
6602 if (flags & WINED3D_PSARGS_PROJECTED)
6604 DWORD div_mask=0;
6605 char coord_div_mask[3];
6606 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6608 case WINED3D_TTFF_COUNT1:
6609 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6610 break;
6611 case WINED3D_TTFF_COUNT2:
6612 div_mask = WINED3DSP_WRITEMASK_1;
6613 break;
6614 case WINED3D_TTFF_COUNT3:
6615 div_mask = WINED3DSP_WRITEMASK_2;
6616 break;
6617 case WINED3D_TTFF_COUNT4:
6618 case WINED3D_TTFF_DISABLE:
6619 div_mask = WINED3DSP_WRITEMASK_3;
6620 break;
6622 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6623 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6626 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6628 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6629 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6630 coord_param.param_str, coord_mask);
6632 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6634 struct glsl_src_param luminance_param;
6635 struct glsl_dst_param dst_param;
6637 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6638 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6640 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6641 dst_param.reg_name, dst_param.mask_str,
6642 luminance_param.param_str, sampler_idx, sampler_idx);
6644 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6647 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6649 unsigned int sampler_idx = ins->dst[0].reg.idx[0].offset;
6650 struct glsl_src_param src0_param, src1_param;
6652 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6653 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6655 shader_glsl_append_dst(ins->ctx->buffer, ins);
6656 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6657 src0_param.param_str, sampler_idx, src1_param.param_str);
6660 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6661 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6662 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6664 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6665 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6666 struct glsl_sample_function sample_function;
6667 struct wined3d_string_buffer *reg_name;
6669 reg_name = string_buffer_get(priv->string_buffers);
6670 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6672 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6673 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6674 "%s.wx", reg_name->buffer);
6675 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6677 string_buffer_release(priv->string_buffers, reg_name);
6680 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6681 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6682 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6684 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6685 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6686 struct glsl_sample_function sample_function;
6687 struct wined3d_string_buffer *reg_name;
6689 reg_name = string_buffer_get(priv->string_buffers);
6690 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6692 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6693 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6694 "%s.yz", reg_name->buffer);
6695 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6697 string_buffer_release(priv->string_buffers, reg_name);
6700 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6701 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6702 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6704 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6705 struct glsl_sample_function sample_function;
6706 struct glsl_src_param src0_param;
6708 /* Dependent read, not valid with conditional NP2 */
6709 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6710 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6712 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6713 "%s", src0_param.param_str);
6714 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6717 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6718 * If any of the first 3 components are < 0, discard this pixel */
6719 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6721 if (ins->ctx->reg_maps->shader_version.major >= 4)
6723 shader_glsl_generate_condition(ins);
6724 shader_addline(ins->ctx->buffer, " discard;\n");
6726 else
6728 struct glsl_dst_param dst_param;
6730 /* The argument is a destination parameter, and no writemasks are allowed */
6731 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6733 /* 2.0 shaders compare all 4 components in texkill. */
6734 if (ins->ctx->reg_maps->shader_version.major >= 2)
6735 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6736 /* 1.x shaders only compare the first 3 components, probably due to
6737 * the nature of the texkill instruction as a tex* instruction, and
6738 * phase, which kills all .w components. Even if all 4 components are
6739 * defined, only the first 3 are used. */
6740 else
6741 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6745 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6746 * dst = dot2(src0, src1) + src2 */
6747 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6749 struct glsl_src_param src0_param;
6750 struct glsl_src_param src1_param;
6751 struct glsl_src_param src2_param;
6752 DWORD write_mask;
6753 unsigned int mask_size;
6755 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6756 mask_size = shader_glsl_get_write_mask_size(write_mask);
6758 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6759 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6760 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6762 if (mask_size > 1) {
6763 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6764 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6765 } else {
6766 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6767 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6771 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6772 const struct wined3d_shader_signature *input_signature,
6773 const struct wined3d_shader_reg_maps *reg_maps,
6774 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6776 unsigned int i;
6778 for (i = 0; i < input_signature->element_count; ++i)
6780 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6781 const char *semantic_name;
6782 UINT semantic_idx;
6783 char reg_mask[6];
6785 /* Unused */
6786 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6787 continue;
6789 semantic_name = input->semantic_name;
6790 semantic_idx = input->semantic_idx;
6791 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6793 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6795 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6797 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6798 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6800 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6802 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6804 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6806 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6807 input->register_idx, reg_mask);
6809 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6811 if (gl_info->supported[ARB_SAMPLE_SHADING])
6812 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6813 input->register_idx, reg_mask);
6814 else
6815 FIXME("ARB_sample_shading is not supported.\n");
6817 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6819 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6820 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6821 input->register_idx, reg_mask);
6822 else
6823 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6825 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6827 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6828 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6829 input->register_idx, reg_mask);
6830 else
6831 FIXME("ARB_viewport_array is not supported.\n");
6833 else
6835 if (input->sysval_semantic)
6836 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6837 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6838 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6839 shader_glsl_shader_input_name(gl_info),
6840 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6843 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6845 if (args->pointsprite)
6846 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6847 shader->u.ps.input_reg_map[input->register_idx]);
6848 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6849 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6850 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6851 needs_legacy_glsl_syntax(gl_info)
6852 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6853 else
6854 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6855 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6857 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6859 if (!semantic_idx)
6860 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6861 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6862 else if (semantic_idx == 1)
6863 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6864 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6865 else
6866 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6867 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6869 else
6871 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6872 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6877 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6879 struct glsl_program_key key;
6881 key.vs_id = entry->vs.id;
6882 key.hs_id = entry->hs.id;
6883 key.ds_id = entry->ds.id;
6884 key.gs_id = entry->gs.id;
6885 key.ps_id = entry->ps.id;
6886 key.cs_id = entry->cs.id;
6888 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6890 ERR("Failed to insert program entry.\n");
6894 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6895 const struct glsl_program_key *key)
6897 struct wine_rb_entry *entry;
6899 entry = wine_rb_get(&priv->program_lookup, key);
6900 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6903 /* Context activation is done by the caller. */
6904 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6905 struct glsl_shader_prog_link *entry)
6907 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6909 GL_EXTCALL(glDeleteProgram(entry->id));
6910 if (entry->vs.id)
6911 list_remove(&entry->vs.shader_entry);
6912 if (entry->hs.id)
6913 list_remove(&entry->hs.shader_entry);
6914 if (entry->ds.id)
6915 list_remove(&entry->ds.shader_entry);
6916 if (entry->gs.id)
6917 list_remove(&entry->gs.shader_entry);
6918 if (entry->ps.id)
6919 list_remove(&entry->ps.shader_entry);
6920 if (entry->cs.id)
6921 list_remove(&entry->cs.shader_entry);
6922 heap_free(entry);
6925 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6926 const struct wined3d_gl_info *gl_info, const unsigned int *map,
6927 const struct wined3d_shader_signature *input_signature,
6928 const struct wined3d_shader_reg_maps *reg_maps_in,
6929 const struct wined3d_shader_signature *output_signature,
6930 const struct wined3d_shader_reg_maps *reg_maps_out)
6932 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6933 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6934 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6935 unsigned int in_count = vec4_varyings(3, gl_info);
6936 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6937 unsigned int in_idx;
6938 DWORD *set = NULL;
6939 unsigned int i, j;
6940 char reg_mask[6];
6942 set = heap_calloc(max_varyings, sizeof(*set));
6944 for (i = 0; i < input_signature->element_count; ++i)
6946 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6948 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6949 continue;
6951 in_idx = map[input->register_idx];
6952 /* Declared, but not read register */
6953 if (in_idx == ~0u)
6954 continue;
6955 if (in_idx >= max_varyings)
6957 FIXME("More input varyings declared than supported, expect issues.\n");
6958 continue;
6961 if (in_idx == in_count)
6962 string_buffer_sprintf(destination, "gl_FrontColor");
6963 else if (in_idx == in_count + 1)
6964 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6965 else
6966 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6968 if (!set[in_idx])
6969 set[in_idx] = ~0u;
6971 for (j = 0; j < output_signature->element_count; ++j)
6973 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6974 DWORD mask;
6976 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6977 || input->semantic_idx != output->semantic_idx
6978 || strcmp(input->semantic_name, output->semantic_name)
6979 || !(mask = input->mask & output->mask))
6980 continue;
6982 if (set[in_idx] == ~0u)
6983 set[in_idx] = 0;
6984 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6985 shader_glsl_write_mask_to_str(mask, reg_mask);
6987 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6988 destination->buffer, reg_mask, output->register_idx, reg_mask);
6992 for (i = 0; i < max_varyings; ++i)
6994 unsigned int size;
6996 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6997 continue;
6999 if (set[i] == ~0u)
7000 set[i] = 0;
7002 size = 0;
7003 if (!(set[i] & WINED3DSP_WRITEMASK_0))
7004 reg_mask[size++] = 'x';
7005 if (!(set[i] & WINED3DSP_WRITEMASK_1))
7006 reg_mask[size++] = 'y';
7007 if (!(set[i] & WINED3DSP_WRITEMASK_2))
7008 reg_mask[size++] = 'z';
7009 if (!(set[i] & WINED3DSP_WRITEMASK_3))
7010 reg_mask[size++] = 'w';
7011 reg_mask[size] = '\0';
7013 if (i == in_count)
7014 string_buffer_sprintf(destination, "gl_FrontColor");
7015 else if (i == in_count + 1)
7016 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7017 else
7018 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
7020 if (size == 1)
7021 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
7022 else
7023 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
7026 heap_free(set);
7027 string_buffer_release(&priv->string_buffers, destination);
7030 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
7031 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
7032 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
7033 BOOL rasterizer_setup)
7035 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7036 char reg_mask[6];
7037 unsigned int i;
7039 for (i = 0; i < output_signature->element_count; ++i)
7041 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7043 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7044 continue;
7046 if (output->stream_idx)
7047 continue;
7049 if (output->register_idx >= input_count)
7050 continue;
7052 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7054 shader_addline(buffer,
7055 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7056 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7060 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7061 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7063 unsigned int i, clip_or_cull_index;
7064 const char *name;
7065 char reg_mask[6];
7067 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7068 /* Assign consecutive indices starting from 0. */
7069 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7070 for (i = 0; i < 4; ++i)
7072 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7073 continue;
7075 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7076 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7077 name, clip_or_cull_index, element->register_idx, reg_mask);
7078 ++clip_or_cull_index;
7082 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7083 const struct wined3d_gl_info *gl_info, const unsigned int *map,
7084 const struct wined3d_shader_signature *input_signature,
7085 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7086 const struct wined3d_shader_signature *output_signature,
7087 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7089 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7090 const char *semantic_name;
7091 unsigned int semantic_idx;
7092 char reg_mask[6];
7093 unsigned int i;
7095 /* First, sort out position and point size system values. */
7096 for (i = 0; i < output_signature->element_count; ++i)
7098 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7100 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7101 continue;
7103 if (output->stream_idx)
7104 continue;
7106 semantic_name = output->semantic_name;
7107 semantic_idx = output->semantic_idx;
7108 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7110 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7112 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7113 reg_mask, output->register_idx, reg_mask);
7115 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7117 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7118 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7120 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7122 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7123 output->register_idx, reg_mask);
7125 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7127 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7128 output->register_idx, reg_mask);
7130 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7132 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7134 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7136 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7138 else if (output->sysval_semantic)
7140 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7144 /* Then, setup the pixel shader input. */
7145 if (reg_maps_out->shader_version.major < 4)
7146 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7147 output_signature, reg_maps_out);
7148 else
7149 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7152 /* Context activation is done by the caller. */
7153 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7154 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7155 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7157 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7158 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7159 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7160 const char *semantic_name;
7161 UINT semantic_idx;
7162 char reg_mask[6];
7163 unsigned int i;
7164 GLuint ret;
7166 string_buffer_clear(buffer);
7168 shader_glsl_add_version_declaration(buffer, gl_info);
7170 shader_addline(buffer, "invariant gl_Position;\n");
7172 if (per_vertex_point_size)
7174 shader_addline(buffer, "uniform struct\n{\n");
7175 shader_addline(buffer, " float size_min;\n");
7176 shader_addline(buffer, " float size_max;\n");
7177 shader_addline(buffer, "} ffp_point;\n");
7180 if (ps_major < 3)
7182 DWORD colors_written_mask[2] = {0};
7183 DWORD texcoords_written_mask[WINED3D_MAX_FFP_TEXTURES] = {0};
7185 if (!legacy_syntax)
7187 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7188 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7189 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
7190 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7193 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7195 for (i = 0; i < vs->output_signature.element_count; ++i)
7197 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7198 DWORD write_mask;
7200 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7201 continue;
7203 semantic_name = output->semantic_name;
7204 semantic_idx = output->semantic_idx;
7205 write_mask = output->mask;
7206 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7208 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7210 if (legacy_syntax)
7211 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7212 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7213 else
7214 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7215 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7217 colors_written_mask[semantic_idx] = write_mask;
7219 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7221 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7222 reg_mask, output->register_idx, reg_mask);
7224 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7226 if (semantic_idx < WINED3D_MAX_FFP_TEXTURES)
7228 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7229 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7230 semantic_idx, reg_mask, output->register_idx, reg_mask);
7231 texcoords_written_mask[semantic_idx] = write_mask;
7234 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7236 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7237 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7239 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7241 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7242 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7243 output->register_idx, reg_mask[1]);
7247 for (i = 0; i < 2; ++i)
7249 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7251 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7252 if (!i)
7253 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7254 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7255 reg_mask, reg_mask);
7256 else
7257 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7258 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7259 reg_mask, reg_mask);
7262 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
7264 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7265 continue;
7267 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7269 if (!shader_glsl_full_ffp_varyings(gl_info) && !texcoords_written_mask[i])
7270 continue;
7272 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7273 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7274 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7278 else
7280 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7282 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7283 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7284 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7285 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7288 shader_addline(buffer, "}\n");
7290 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7291 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7292 shader_glsl_compile(gl_info, ret, buffer->buffer);
7294 return ret;
7297 static void shader_glsl_generate_stream_output_setup(struct wined3d_string_buffer *buffer,
7298 const struct wined3d_shader *shader)
7300 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
7301 unsigned int i, register_idx, component_idx;
7303 shader_addline(buffer, "out shader_in_out\n{\n");
7304 for (i = 0; i < so_desc->element_count; ++i)
7306 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7308 if (e->stream_idx)
7310 FIXME("Unhandled stream %u.\n", e->stream_idx);
7311 continue;
7313 if (!e->semantic_name)
7314 continue;
7315 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7316 continue;
7318 if (component_idx || e->component_count != 4)
7320 if (e->component_count == 1)
7321 shader_addline(buffer, "float");
7322 else
7323 shader_addline(buffer, "vec%u", e->component_count);
7325 shader_addline(buffer, " reg%u_%u_%u;\n",
7326 register_idx, component_idx, component_idx + e->component_count - 1);
7328 else
7330 shader_addline(buffer, "vec4 reg%u;\n", register_idx);
7333 shader_addline(buffer, "} shader_out;\n");
7335 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7336 shader->limits->packed_output);
7337 for (i = 0; i < so_desc->element_count; ++i)
7339 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7341 if (e->stream_idx)
7343 FIXME("Unhandled stream %u.\n", e->stream_idx);
7344 continue;
7346 if (!e->semantic_name)
7347 continue;
7348 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7349 continue;
7351 if (component_idx || e->component_count != 4)
7353 DWORD write_mask;
7354 char str_mask[6];
7356 write_mask = wined3d_mask_from_size(e->component_count) << component_idx;
7357 shader_glsl_write_mask_to_str(write_mask, str_mask);
7358 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7359 register_idx, component_idx, component_idx + e->component_count - 1,
7360 register_idx, str_mask);
7362 else
7364 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n", register_idx, register_idx);
7367 shader_addline(buffer, "}\n");
7370 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7371 const struct wined3d_shader *shader, unsigned int input_count,
7372 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const uint32_t *interpolation_mode)
7374 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7375 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7377 if (rasterizer_setup)
7378 input_count = min(vec4_varyings(4, gl_info), input_count);
7380 if (input_count)
7381 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7383 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7384 prefix, shader->limits->packed_output);
7386 if (rasterizer_setup)
7387 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7388 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7389 else
7390 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7391 &shader->reg_maps, "shader_out", rasterizer_setup);
7393 shader_addline(buffer, "}\n");
7396 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7397 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7398 const char *reg_mask)
7400 if (!constant->sysval_semantic)
7402 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7403 return;
7406 switch (constant->sysval_semantic)
7408 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7409 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7410 case WINED3D_SV_TESS_FACTOR_LINEDET:
7411 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7412 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7413 break;
7414 case WINED3D_SV_TESS_FACTOR_QUADINT:
7415 case WINED3D_SV_TESS_FACTOR_TRIINT:
7416 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7417 break;
7418 default:
7419 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7420 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7424 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7425 const struct wined3d_shader_signature *signature, BOOL input_setup)
7427 unsigned int i, register_count, user_constant_index, user_constant_count;
7429 register_count = user_constant_count = 0;
7430 for (i = 0; i < signature->element_count; ++i)
7432 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7433 register_count = max(constant->register_idx + 1, register_count);
7434 if (!constant->sysval_semantic)
7435 ++user_constant_count;
7438 if (user_constant_count)
7439 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7440 input_setup ? "in" : "out", user_constant_count);
7441 if (input_setup)
7442 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7444 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7445 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7447 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7448 char reg_mask[6];
7450 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7452 if (input_setup)
7453 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7454 else
7455 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7457 shader_addline(buffer, " = ");
7459 if (input_setup)
7460 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7461 else
7462 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7464 shader_addline(buffer, ";\n");
7466 shader_addline(buffer, "}\n");
7469 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7470 const struct wined3d_gl_info *gl_info)
7472 const char *output = get_fragment_output(gl_info);
7474 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7475 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7476 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7477 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7478 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7479 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7482 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7483 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7485 const char *output = get_fragment_output(gl_info);
7487 switch (mode)
7489 case WINED3D_FFP_PS_FOG_OFF:
7490 return;
7492 case WINED3D_FFP_PS_FOG_LINEAR:
7493 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7494 break;
7496 case WINED3D_FFP_PS_FOG_EXP:
7497 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7498 break;
7500 case WINED3D_FFP_PS_FOG_EXP2:
7501 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7502 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7503 break;
7505 default:
7506 ERR("Invalid fog mode %#x.\n", mode);
7507 return;
7510 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7511 output, output);
7514 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7515 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7517 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7518 * flipping all the operators here, just negate the comparison below. */
7519 static const char * const comparison_operator[] =
7521 "", /* WINED3D_CMP_NEVER */
7522 "<", /* WINED3D_CMP_LESS */
7523 "==", /* WINED3D_CMP_EQUAL */
7524 "<=", /* WINED3D_CMP_LESSEQUAL */
7525 ">", /* WINED3D_CMP_GREATER */
7526 "!=", /* WINED3D_CMP_NOTEQUAL */
7527 ">=", /* WINED3D_CMP_GREATEREQUAL */
7528 "" /* WINED3D_CMP_ALWAYS */
7531 if (alpha_func == WINED3D_CMP_ALWAYS)
7532 return;
7534 if (alpha_func != WINED3D_CMP_NEVER)
7535 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7536 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7537 shader_addline(buffer, " discard;\n");
7540 static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *buffer,
7541 const char *colour, const char *colour_key_low, const char *colour_key_high)
7543 shader_addline(buffer, "if (all(greaterThanEqual(%s, %s)) && all(lessThan(%s, %s)))\n",
7544 colour, colour_key_low, colour, colour_key_high);
7545 shader_addline(buffer, " discard;\n");
7548 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7549 const struct wined3d_gl_info *gl_info)
7551 if (gl_info->supported[ARB_CULL_DISTANCE])
7552 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7553 if (gl_info->supported[ARB_GPU_SHADER5])
7554 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7555 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7556 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7557 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7558 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7559 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7560 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7561 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7562 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7563 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7564 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7565 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7566 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7567 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7568 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7569 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7570 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7571 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7572 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7573 if (gl_info->supported[ARB_TEXTURE_GATHER])
7574 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7575 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7576 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7577 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7578 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7579 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7580 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7581 if (gl_info->supported[EXT_GPU_SHADER4])
7582 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7583 if (gl_info->supported[EXT_SHADER_INTEGER_MIX])
7584 shader_addline(buffer, "#extension GL_EXT_shader_integer_mix : enable\n");
7585 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7586 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7587 if (gl_info->supported[EXT_TEXTURE_SHADOW_LOD])
7588 shader_addline(buffer, "#extension GL_EXT_texture_shadow_lod : enable\n");
7591 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7592 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7593 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7595 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7596 struct wined3d_string_buffer *src, *assignment;
7597 enum wined3d_data_type dst_data_type;
7598 const char *swizzle;
7599 unsigned int i;
7601 if (output_signature->element_count)
7603 src = string_buffer_get(string_buffers);
7604 assignment = string_buffer_get(string_buffers);
7605 for (i = 0; i < output_signature->element_count; ++i)
7607 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7609 /* register_idx is set to ~0u for non-color outputs. */
7610 if (output->register_idx == ~0u)
7611 continue;
7612 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7614 FIXME("Unhandled component type %#x.\n", output->component_type);
7615 continue;
7617 dst_data_type = component_type_info[output->component_type].data_type;
7618 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7619 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7620 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT, 4);
7621 swizzle = args->rt_alpha_swizzle & (1u << output->semantic_idx) ? ".argb" : "";
7622 shader_addline(buffer, "%s%s;\n", assignment->buffer, swizzle);
7624 string_buffer_release(string_buffers, src);
7625 string_buffer_release(string_buffers, assignment);
7627 else
7629 uint32_t mask = shader->reg_maps.rt_mask;
7631 while (mask)
7633 i = wined3d_bit_scan(&mask);
7634 swizzle = args->rt_alpha_swizzle & (1u << i) ? ".argb" : "";
7635 shader_addline(buffer, "color_out%u = ps_out[%u]%s;\n", i, i, swizzle);
7640 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7641 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7642 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7644 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7646 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7647 if (reg_maps->shader_version.major < 2)
7648 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7650 if (args->srgb_correction)
7651 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7653 /* SM < 3 does not replace the fog stage. */
7654 if (reg_maps->shader_version.major < 3)
7655 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7657 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7659 if (reg_maps->sample_mask)
7660 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7662 if (!use_legacy_fragment_output(gl_info))
7663 shader_glsl_generate_color_output(buffer, gl_info, shader, args, string_buffers);
7666 /* Context activation is done by the caller. */
7667 static GLuint shader_glsl_generate_fragment_shader(const struct wined3d_context_gl *context_gl,
7668 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7669 const struct wined3d_shader *shader, const struct ps_compile_args *args,
7670 struct ps_np2fixup_info *np2fixup_info)
7672 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7673 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7674 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7675 const char *prefix = shader_glsl_get_prefix(version->type);
7676 BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7677 unsigned int i, extra_constants_needed = 0;
7678 struct shader_glsl_ctx_priv priv_ctx;
7679 GLuint shader_id;
7680 uint32_t map;
7682 memset(&priv_ctx, 0, sizeof(priv_ctx));
7683 priv_ctx.gl_info = gl_info;
7684 priv_ctx.cur_ps_args = args;
7685 priv_ctx.cur_np2fixup_info = np2fixup_info;
7686 priv_ctx.string_buffers = string_buffers;
7688 shader_glsl_add_version_declaration(buffer, gl_info);
7690 shader_glsl_enable_extensions(buffer, gl_info);
7691 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7692 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7693 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7694 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7695 if (shader_glsl_use_explicit_attrib_location(gl_info))
7696 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7697 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7698 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7699 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7700 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7701 if (gl_info->supported[ARB_SAMPLE_SHADING])
7702 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7703 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7704 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7705 /* The spec says that it doesn't have to be explicitly enabled, but the
7706 * nvidia drivers write a warning if we don't do so. */
7707 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7708 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7710 /* Base Declarations */
7711 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7713 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7715 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7716 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7717 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7718 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7721 /* Declare uniforms for NP2 texcoord fixup:
7722 * This is NOT done inside the loop that declares the texture samplers
7723 * since the NP2 fixup code is currently only used for the GeforceFX
7724 * series and when forcing the ARB_npot extension off. Modern cards just
7725 * skip the code anyway, so put it inside a separate loop. */
7726 if (args->np2_fixup)
7728 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7729 unsigned int cur = 0;
7731 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7732 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7733 * samplerNP2Fixup stores texture dimensions and is updated through
7734 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7736 for (i = 0; i < shader->limits->sampler; ++i)
7738 enum wined3d_shader_resource_type resource_type;
7740 resource_type = pixelshader_get_resource_type(reg_maps, i, args->tex_types);
7742 if (!resource_type || !(args->np2_fixup & (1u << i)))
7743 continue;
7745 if (resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7747 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7748 continue;
7751 fixup->idx[i] = cur++;
7754 fixup->num_consts = (cur + 1) >> 1;
7755 fixup->active = args->np2_fixup;
7756 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7759 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7761 shader_addline(buffer, "uniform struct\n{\n");
7762 shader_addline(buffer, " vec4 color;\n");
7763 shader_addline(buffer, " float density;\n");
7764 shader_addline(buffer, " float end;\n");
7765 shader_addline(buffer, " float scale;\n");
7766 shader_addline(buffer, "} ffp_fog;\n");
7768 if (legacy_syntax)
7770 if (glsl_is_color_reg_read(shader, 0))
7771 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7772 if (glsl_is_color_reg_read(shader, 1))
7773 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7774 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
7775 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7777 else
7779 if (glsl_is_color_reg_read(shader, 0))
7780 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7781 if (glsl_is_color_reg_read(shader, 1))
7782 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7783 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
7784 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
7785 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7789 if (version->major >= 3)
7791 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7793 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7794 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7795 shader->u.ps.interpolation_mode, version->major >= 4);
7796 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7799 map = reg_maps->bumpmat;
7800 while (map)
7802 i = wined3d_bit_scan(&map);
7803 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7805 if (reg_maps->luminanceparams & (1u << i))
7807 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7808 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7809 extra_constants_needed++;
7812 extra_constants_needed++;
7815 if (args->srgb_correction)
7817 shader_addline(buffer, "const vec4 srgb_const0 = ");
7818 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
7819 shader_addline(buffer, ";\n");
7820 shader_addline(buffer, "const vec4 srgb_const1 = ");
7821 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
7822 shader_addline(buffer, ";\n");
7824 if ((reg_maps->usesdsy && wined3d_settings.offscreen_rendering_mode != ORM_FBO)
7825 || (reg_maps->vpos && !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]))
7827 ++extra_constants_needed;
7828 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7830 if (reg_maps->vpos)
7832 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7834 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7835 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7836 args->y_correction ? "origin_upper_left, " : "");
7837 else if (args->y_correction)
7838 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7840 shader_addline(buffer, "vec4 vpos;\n");
7843 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7844 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7846 if (!use_legacy_fragment_output(gl_info))
7848 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7850 if (args->dual_source_blend)
7851 shader_addline(buffer, "vec4 ps_out[2];\n");
7852 else
7853 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7854 if (output_signature->element_count)
7856 for (i = 0; i < output_signature->element_count; ++i)
7858 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7860 if (output->register_idx == ~0u)
7861 continue;
7862 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7864 FIXME("Unhandled component type %#x.\n", output->component_type);
7865 continue;
7867 if (shader_glsl_use_explicit_attrib_location(gl_info))
7869 if (args->dual_source_blend)
7870 shader_addline(buffer, "layout(location = 0, index = %u) ", output->semantic_idx);
7871 else
7872 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7874 shader_addline(buffer, "out %s4 color_out%u;\n",
7875 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7878 else
7880 uint32_t mask = reg_maps->rt_mask;
7882 while (mask)
7884 i = wined3d_bit_scan(&mask);
7885 if (shader_glsl_use_explicit_attrib_location(gl_info))
7887 if (args->dual_source_blend)
7888 shader_addline(buffer, "layout(location = 0, index = %u) ", i);
7889 else
7890 shader_addline(buffer, "layout(location = %u) ", i);
7892 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7897 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7898 FIXME("Insufficient uniforms to run this shader.\n");
7900 if (shader->u.ps.force_early_depth_stencil)
7901 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7903 shader_addline(buffer, "void main()\n{\n");
7905 if (reg_maps->sample_mask)
7906 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7908 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7909 * add approximately 0.5. This causes off-by-one problems as spotted by
7910 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7911 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7912 * causes precision troubles when we just subtract 0.5.
7914 * To deal with that, just floor() the position. This will eliminate the
7915 * fraction on all cards.
7917 * TODO: Test how this behaves with multisampling.
7919 * An advantage of floor is that it works even if the driver doesn't add
7920 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7921 * to return in gl_FragCoord, even though coordinates specify the pixel
7922 * centers instead of the pixel corners. This code will behave correctly
7923 * on drivers that returns integer values. */
7924 if (reg_maps->vpos)
7926 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7927 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7928 else if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7929 shader_addline(buffer,
7930 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7931 else
7932 shader_addline(buffer,
7933 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7936 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7938 unsigned int i;
7940 if (legacy_syntax)
7942 if (glsl_is_color_reg_read(shader, 0))
7943 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7944 if (glsl_is_color_reg_read(shader, 1))
7945 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7948 map = reg_maps->texcoord;
7949 while (map)
7951 i = wined3d_bit_scan(&map);
7952 if (args->pointsprite)
7953 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7954 else if (args->texcoords_initialized & (1u << i))
7955 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7956 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7957 else
7958 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7959 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7962 if (legacy_syntax)
7963 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7966 /* Pack 3.0 inputs */
7967 if (reg_maps->shader_version.major >= 3)
7968 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7969 reg_maps->shader_version.major >= 4);
7971 /* Base Shader Body */
7972 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7973 return 0;
7975 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7976 if (reg_maps->shader_version.major < 4)
7977 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
7979 shader_addline(buffer, "}\n");
7981 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7982 TRACE("Compiling shader object %u.\n", shader_id);
7983 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7985 return shader_id;
7988 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7989 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7990 const struct vs_compile_args *args)
7992 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7993 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7994 unsigned int i;
7996 /* Unpack outputs. */
7997 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7999 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
8000 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
8001 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
8002 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
8004 if (reg_maps->shader_version.major < 3)
8006 if (args->fog_src == VS_FOG_Z)
8007 shader_addline(buffer, "%s = gl_Position.z;\n",
8008 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
8009 else if (!reg_maps->fog)
8010 shader_addline(buffer, "%s = 0.0;\n",
8011 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
8014 /* We always store the clipplanes without y inversion. */
8015 if (args->clip_enabled)
8017 if (legacy_syntax)
8018 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
8019 else
8020 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8021 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
8024 if (args->point_size && !args->per_vertex_point_size)
8025 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
8027 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8028 shader_glsl_fixup_position(buffer, FALSE);
8031 /* Context activation is done by the caller. */
8032 static GLuint shader_glsl_generate_vertex_shader(const struct wined3d_context_gl *context_gl,
8033 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
8035 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8036 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8037 const struct wined3d_shader_version *version = &reg_maps->shader_version;
8038 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8039 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8040 struct shader_glsl_ctx_priv priv_ctx;
8041 GLuint shader_id;
8042 unsigned int i;
8044 memset(&priv_ctx, 0, sizeof(priv_ctx));
8045 priv_ctx.gl_info = gl_info;
8046 priv_ctx.cur_vs_args = args;
8047 priv_ctx.string_buffers = string_buffers;
8049 shader_glsl_add_version_declaration(buffer, gl_info);
8051 shader_glsl_enable_extensions(buffer, gl_info);
8052 if (gl_info->supported[ARB_DRAW_INSTANCED])
8053 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8054 if (shader_glsl_use_explicit_attrib_location(gl_info))
8055 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8056 if (gl_info->supported[ARB_SHADER_VIEWPORT_LAYER_ARRAY])
8057 shader_addline(buffer, "#extension GL_ARB_shader_viewport_layer_array : enable\n");
8059 /* Base Declarations */
8060 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8062 for (i = 0; i < shader->input_signature.element_count; ++i)
8063 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8065 if (args->point_size && !args->per_vertex_point_size)
8067 shader_addline(buffer, "uniform struct\n{\n");
8068 shader_addline(buffer, " float size;\n");
8069 shader_addline(buffer, " float size_min;\n");
8070 shader_addline(buffer, " float size_max;\n");
8071 shader_addline(buffer, "} ffp_point;\n");
8074 if (!needs_legacy_glsl_syntax(gl_info))
8076 if (args->clip_enabled)
8077 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8079 if (version->major < 3)
8081 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8082 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8083 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
8084 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8088 if (version->major < 4)
8089 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8091 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8092 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8094 if (reg_maps->shader_version.major >= 4)
8095 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8096 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8098 shader_addline(buffer, "void main()\n{\n");
8100 if (reg_maps->input_rel_addressing)
8102 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8103 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8104 for (i = 0; i < shader->input_signature.element_count; ++i)
8106 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8107 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8111 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8112 return 0;
8114 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8115 if (reg_maps->shader_version.major < 4)
8116 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8118 shader_addline(buffer, "}\n");
8120 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8121 TRACE("Compiling shader object %u.\n", shader_id);
8122 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8124 return shader_id;
8127 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8128 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8130 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8131 char reg_mask[6];
8132 unsigned int i;
8134 for (i = 0; i < output_signature->element_count; ++i)
8136 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8138 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8139 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8140 output->register_idx, reg_mask, output->register_idx, reg_mask);
8144 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8145 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8146 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8147 const char *phase_name, unsigned phase_idx)
8149 unsigned int i;
8150 HRESULT hr;
8152 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8153 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8154 for (i = 0; i < phase->temporary_count; ++i)
8155 shader_addline(buffer, "vec4 R%u;\n", i);
8156 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8157 shader_addline(buffer, "}\n");
8158 return hr;
8161 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8162 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8164 if (phase->instance_count)
8166 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8167 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8168 shader_addline(buffer, "}\n");
8170 else
8172 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8176 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context_gl *context_gl,
8177 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8179 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8180 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8181 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8182 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8183 const struct wined3d_hull_shader *hs = &shader->u.hs;
8184 const struct wined3d_shader_phase *phase;
8185 struct shader_glsl_ctx_priv priv_ctx;
8186 GLuint shader_id;
8187 unsigned int i;
8189 memset(&priv_ctx, 0, sizeof(priv_ctx));
8190 priv_ctx.gl_info = gl_info;
8191 priv_ctx.string_buffers = string_buffers;
8193 shader_glsl_add_version_declaration(buffer, gl_info);
8195 shader_glsl_enable_extensions(buffer, gl_info);
8196 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8198 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8200 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8202 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8203 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8205 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8207 if (hs->phases.control_point)
8209 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8210 shader->limits->packed_output);
8211 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8212 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8213 shader_addline(buffer, "}\n");
8216 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8217 if ((phase = hs->phases.control_point))
8219 for (i = 0; i < phase->temporary_count; ++i)
8220 shader_addline(buffer, "vec4 R%u;\n", i);
8221 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8222 return 0;
8223 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8225 else
8227 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8229 shader_addline(buffer, "}\n");
8231 for (i = 0; i < hs->phases.fork_count; ++i)
8233 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8234 &hs->phases.fork[i], "fork", i)))
8235 return 0;
8238 for (i = 0; i < hs->phases.join_count; ++i)
8240 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8241 &hs->phases.join[i], "join", i)))
8242 return 0;
8245 shader_addline(buffer, "void main()\n{\n");
8246 shader_addline(buffer, "hs_control_point_phase();\n");
8247 if (reg_maps->vocp)
8248 shader_addline(buffer, "barrier();\n");
8249 for (i = 0; i < hs->phases.fork_count; ++i)
8250 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8251 for (i = 0; i < hs->phases.join_count; ++i)
8252 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8253 shader_addline(buffer, "setup_patch_constant_output();\n");
8254 shader_addline(buffer, "}\n");
8256 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8257 TRACE("Compiling shader object %u.\n", shader_id);
8258 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8260 return shader_id;
8263 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8264 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8265 const struct ds_compile_args *args)
8267 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8269 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8270 shader_glsl_fixup_position(buffer, FALSE);
8273 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context_gl *context_gl,
8274 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8276 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8277 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8278 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8279 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8280 struct shader_glsl_ctx_priv priv_ctx;
8281 GLuint shader_id;
8283 memset(&priv_ctx, 0, sizeof(priv_ctx));
8284 priv_ctx.gl_info = gl_info;
8285 priv_ctx.cur_ds_args = args;
8286 priv_ctx.string_buffers = string_buffers;
8288 shader_glsl_add_version_declaration(buffer, gl_info);
8290 shader_glsl_enable_extensions(buffer, gl_info);
8291 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8293 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8295 shader_addline(buffer, "layout(");
8296 switch (shader->u.ds.tessellator_domain)
8298 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8299 shader_addline(buffer, "isolines");
8300 break;
8301 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8302 shader_addline(buffer, "quads");
8303 break;
8304 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8305 shader_addline(buffer, "triangles");
8306 break;
8308 switch (args->tessellator_output_primitive)
8310 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8311 if (args->render_offscreen)
8312 shader_addline(buffer, ", ccw");
8313 else
8314 shader_addline(buffer, ", cw");
8315 break;
8316 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8317 if (args->render_offscreen)
8318 shader_addline(buffer, ", cw");
8319 else
8320 shader_addline(buffer, ", ccw");
8321 break;
8322 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8323 shader_addline(buffer, ", point_mode");
8324 break;
8325 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8326 break;
8328 switch (args->tessellator_partitioning)
8330 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8331 shader_addline(buffer, ", fractional_odd_spacing");
8332 break;
8333 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8334 shader_addline(buffer, ", fractional_even_spacing");
8335 break;
8336 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8337 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8338 shader_addline(buffer, ", equal_spacing");
8339 break;
8341 shader_addline(buffer, ") in;\n");
8343 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8345 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8346 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8348 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8349 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8350 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8352 shader_addline(buffer, "void main()\n{\n");
8353 shader_addline(buffer, "setup_patch_constant_input();\n");
8355 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8356 return 0;
8358 shader_addline(buffer, "}\n");
8360 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8361 TRACE("Compiling shader object %u.\n", shader_id);
8362 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8364 return shader_id;
8367 /* Context activation is done by the caller. */
8368 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context_gl *context_gl,
8369 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8371 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8372 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8373 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8374 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8375 const struct wined3d_shader_signature_element *output;
8376 enum wined3d_primitive_type primitive_type;
8377 struct shader_glsl_ctx_priv priv_ctx;
8378 unsigned int max_vertices;
8379 unsigned int i, j;
8380 GLuint shader_id;
8382 memset(&priv_ctx, 0, sizeof(priv_ctx));
8383 priv_ctx.gl_info = gl_info;
8384 priv_ctx.string_buffers = string_buffers;
8386 shader_glsl_add_version_declaration(buffer, gl_info);
8388 shader_glsl_enable_extensions(buffer, gl_info);
8390 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8392 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8393 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8394 if (shader->u.gs.instance_count > 1)
8395 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8396 shader_addline(buffer, ") in;\n");
8398 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8399 if (!(max_vertices = shader->u.gs.vertices_out))
8401 switch (args->primitive_type)
8403 case WINED3D_PT_POINTLIST:
8404 max_vertices = 1;
8405 break;
8406 case WINED3D_PT_LINELIST:
8407 max_vertices = 2;
8408 break;
8409 case WINED3D_PT_TRIANGLELIST:
8410 max_vertices = 3;
8411 break;
8412 default:
8413 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8414 break;
8417 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8418 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8419 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8421 if (!gl_info->supported[ARB_CLIP_CONTROL])
8423 shader_addline(buffer, "uniform vec4 pos_fixup");
8424 if (reg_maps->viewport_array)
8425 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8426 shader_addline(buffer, ";\n");
8429 if (is_rasterization_disabled(shader))
8431 shader_glsl_generate_stream_output_setup(buffer, shader);
8433 else
8435 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8436 gl_info, TRUE, args->interpolation_mode);
8439 shader_addline(buffer, "void main()\n{\n");
8440 if (shader->function)
8442 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8443 return 0;
8445 else
8447 for (i = 0; i < max_vertices; ++i)
8449 for (j = 0; j < shader->output_signature.element_count; ++j)
8451 output = &shader->output_signature.elements[j];
8452 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8453 output->register_idx, i, output->register_idx);
8455 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8456 if (!gl_info->supported[ARB_CLIP_CONTROL])
8457 shader_glsl_fixup_position(buffer, FALSE);
8458 shader_addline(buffer, "EmitVertex();\n");
8461 shader_addline(buffer, "}\n");
8463 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8464 TRACE("Compiling shader object %u.\n", shader_id);
8465 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8467 return shader_id;
8470 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8472 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8473 const struct wined3d_gl_info *gl_info = priv->gl_info;
8474 struct wined3d_string_buffer *buffer = ctx->buffer;
8475 const struct wined3d_shader *shader = ctx->shader;
8477 switch (shader->reg_maps.shader_version.type)
8479 case WINED3D_SHADER_TYPE_PIXEL:
8480 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8481 break;
8482 case WINED3D_SHADER_TYPE_VERTEX:
8483 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8484 break;
8485 case WINED3D_SHADER_TYPE_DOMAIN:
8486 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8487 break;
8488 case WINED3D_SHADER_TYPE_GEOMETRY:
8489 case WINED3D_SHADER_TYPE_COMPUTE:
8490 break;
8491 default:
8492 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8493 break;
8497 /* Context activation is done by the caller. */
8498 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context_gl *context_gl,
8499 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8500 const struct wined3d_shader *shader)
8502 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8503 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8504 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8505 struct shader_glsl_ctx_priv priv_ctx;
8506 GLuint shader_id;
8507 unsigned int i;
8509 memset(&priv_ctx, 0, sizeof(priv_ctx));
8510 priv_ctx.gl_info = gl_info;
8511 priv_ctx.string_buffers = string_buffers;
8513 shader_glsl_add_version_declaration(buffer, gl_info);
8515 shader_glsl_enable_extensions(buffer, gl_info);
8516 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8518 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8520 for (i = 0; i < reg_maps->tgsm_count; ++i)
8522 if (reg_maps->tgsm[i].size)
8523 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8526 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8527 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8529 shader_addline(buffer, "void main()\n{\n");
8530 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8531 shader_addline(buffer, "}\n");
8533 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8534 TRACE("Compiling shader object %u.\n", shader_id);
8535 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8537 return shader_id;
8540 static GLuint find_glsl_fragment_shader(const struct wined3d_context_gl *context_gl,
8541 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8542 struct wined3d_shader *shader,
8543 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8545 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8546 struct glsl_shader_private *shader_data;
8547 struct ps_np2fixup_info *np2fixup;
8548 UINT i;
8549 DWORD new_size;
8550 GLuint ret;
8552 if (!shader->backend_data)
8554 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8556 ERR("Failed to allocate backend data.\n");
8557 return 0;
8560 shader_data = shader->backend_data;
8561 gl_shaders = shader_data->gl_shaders.ps;
8563 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8564 * so a linear search is more performant than a hashmap or a binary search
8565 * (cache coherency etc)
8567 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8569 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8571 if (args->np2_fixup)
8572 *np2fixup_info = &gl_shaders[i].np2fixup;
8573 return gl_shaders[i].id;
8577 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8578 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8580 if (shader_data->num_gl_shaders)
8582 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8583 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8585 else
8587 new_array = heap_alloc(sizeof(*gl_shaders));
8588 new_size = 1;
8591 if(!new_array) {
8592 ERR("Out of memory\n");
8593 return 0;
8595 shader_data->gl_shaders.ps = new_array;
8596 shader_data->shader_array_size = new_size;
8597 gl_shaders = new_array;
8600 gl_shaders[shader_data->num_gl_shaders].args = *args;
8602 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8603 memset(np2fixup, 0, sizeof(*np2fixup));
8604 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8606 string_buffer_clear(buffer);
8607 ret = shader_glsl_generate_fragment_shader(context_gl, buffer, string_buffers, shader, args, np2fixup);
8608 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8610 return ret;
8613 static BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8614 const DWORD use_map)
8616 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8617 return FALSE;
8618 if ((stored->clip_enabled) != new->clip_enabled)
8619 return FALSE;
8620 if (stored->point_size != new->point_size)
8621 return FALSE;
8622 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8623 return FALSE;
8624 if (stored->flatshading != new->flatshading)
8625 return FALSE;
8626 if (stored->next_shader_type != new->next_shader_type)
8627 return FALSE;
8628 if (stored->next_shader_input_count != new->next_shader_input_count)
8629 return FALSE;
8630 if (stored->fog_src != new->fog_src)
8631 return FALSE;
8632 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8635 static GLuint find_glsl_vertex_shader(const struct wined3d_context_gl *context_gl,
8636 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct vs_compile_args *args)
8638 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8639 uint32_t use_map = context_gl->c.stream_info.use_map;
8640 struct glsl_shader_private *shader_data;
8641 unsigned int i, new_size;
8642 GLuint ret;
8644 if (!shader->backend_data)
8646 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8648 ERR("Failed to allocate backend data.\n");
8649 return 0;
8652 shader_data = shader->backend_data;
8653 gl_shaders = shader_data->gl_shaders.vs;
8655 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8656 * so a linear search is more performant than a hashmap or a binary search
8657 * (cache coherency etc)
8659 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8661 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8662 return gl_shaders[i].id;
8665 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8667 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8669 if (shader_data->num_gl_shaders)
8671 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8672 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8674 else
8676 new_array = heap_alloc(sizeof(*gl_shaders));
8677 new_size = 1;
8680 if(!new_array) {
8681 ERR("Out of memory\n");
8682 return 0;
8684 shader_data->gl_shaders.vs = new_array;
8685 shader_data->shader_array_size = new_size;
8686 gl_shaders = new_array;
8689 gl_shaders[shader_data->num_gl_shaders].args = *args;
8691 string_buffer_clear(&priv->shader_buffer);
8692 ret = shader_glsl_generate_vertex_shader(context_gl, priv, shader, args);
8693 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8695 return ret;
8698 static GLuint find_glsl_hull_shader(const struct wined3d_context_gl *context_gl,
8699 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8701 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8702 struct glsl_shader_private *shader_data;
8703 unsigned int new_size;
8704 GLuint ret;
8706 if (!shader->backend_data)
8708 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8710 ERR("Failed to allocate backend data.\n");
8711 return 0;
8714 shader_data = shader->backend_data;
8715 gl_shaders = shader_data->gl_shaders.hs;
8717 if (shader_data->num_gl_shaders > 0)
8719 assert(shader_data->num_gl_shaders == 1);
8720 return gl_shaders[0].id;
8723 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8725 assert(!shader_data->gl_shaders.hs);
8726 new_size = 1;
8727 if (!(new_array = heap_alloc(sizeof(*new_array))))
8729 ERR("Failed to allocate GL shaders array.\n");
8730 return 0;
8732 shader_data->gl_shaders.hs = new_array;
8733 shader_data->shader_array_size = new_size;
8734 gl_shaders = new_array;
8736 string_buffer_clear(&priv->shader_buffer);
8737 ret = shader_glsl_generate_hull_shader(context_gl, priv, shader);
8738 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8740 return ret;
8743 static GLuint find_glsl_domain_shader(const struct wined3d_context_gl *context_gl,
8744 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8746 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8747 struct glsl_shader_private *shader_data;
8748 unsigned int i, new_size;
8749 GLuint ret;
8751 if (!shader->backend_data)
8753 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8755 ERR("Failed to allocate backend data.\n");
8756 return 0;
8759 shader_data = shader->backend_data;
8760 gl_shaders = shader_data->gl_shaders.ds;
8762 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8764 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8765 return gl_shaders[i].id;
8768 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8770 if (shader_data->num_gl_shaders)
8772 new_size = shader_data->shader_array_size + 1;
8773 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8775 else
8777 new_array = heap_alloc(sizeof(*new_array));
8778 new_size = 1;
8781 if (!new_array)
8783 ERR("Failed to allocate GL shaders array.\n");
8784 return 0;
8786 shader_data->gl_shaders.ds = new_array;
8787 shader_data->shader_array_size = new_size;
8788 gl_shaders = new_array;
8790 string_buffer_clear(&priv->shader_buffer);
8791 ret = shader_glsl_generate_domain_shader(context_gl, priv, shader, args);
8792 gl_shaders[shader_data->num_gl_shaders].args = *args;
8793 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8795 return ret;
8798 static GLuint find_glsl_geometry_shader(const struct wined3d_context_gl *context_gl,
8799 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8801 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8802 struct glsl_shader_private *shader_data;
8803 unsigned int i, new_size;
8804 GLuint ret;
8806 if (!shader->backend_data)
8808 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8810 ERR("Failed to allocate backend data.\n");
8811 return 0;
8814 shader_data = shader->backend_data;
8815 gl_shaders = shader_data->gl_shaders.gs;
8817 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8819 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8820 return gl_shaders[i].id;
8823 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8825 if (shader_data->num_gl_shaders)
8827 new_size = shader_data->shader_array_size + 1;
8828 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8830 else
8832 new_array = heap_alloc(sizeof(*new_array));
8833 new_size = 1;
8836 if (!new_array)
8838 ERR("Failed to allocate GL shaders array.\n");
8839 return 0;
8841 shader_data->gl_shaders.gs = new_array;
8842 shader_data->shader_array_size = new_size;
8843 gl_shaders = new_array;
8845 string_buffer_clear(&priv->shader_buffer);
8846 ret = shader_glsl_generate_geometry_shader(context_gl, priv, shader, args);
8847 gl_shaders[shader_data->num_gl_shaders].args = *args;
8848 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8850 return ret;
8853 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8855 switch (mcs)
8857 case WINED3D_MCS_MATERIAL:
8858 return material;
8859 case WINED3D_MCS_COLOR1:
8860 return "ffp_attrib_diffuse";
8861 case WINED3D_MCS_COLOR2:
8862 return "ffp_attrib_specular";
8863 default:
8864 ERR("Invalid material color source %#x.\n", mcs);
8865 return "<invalid>";
8869 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8870 const struct wined3d_ffp_vs_settings *settings, unsigned int idx, BOOL legacy_lighting)
8872 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8873 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8874 if (settings->localviewer)
8875 shader_addline(buffer, "t = dot(normal, ffp_normalize(dir - ffp_normalize(ec_pos.xyz)));\n");
8876 else
8877 shader_addline(buffer, "t = dot(normal, ffp_normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8878 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0%s) specular +="
8879 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n",
8880 legacy_lighting ? " && ffp_material.shininess > 0.0" : "", idx);
8883 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8884 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8886 const char *diffuse, *specular, *emissive, *ambient;
8887 unsigned int i, idx;
8889 if (!settings->lighting)
8891 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8892 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8893 return;
8896 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8897 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8898 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8899 shader_addline(buffer, "vec3 dir, dst;\n");
8900 shader_addline(buffer, "float att, t;\n");
8902 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8903 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8904 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8905 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8907 idx = 0;
8908 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8910 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8911 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8912 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8913 shader_addline(buffer, "dst.x = 1.0;\n");
8914 if (legacy_lighting)
8916 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8917 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8918 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8920 else
8922 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8924 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8925 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8926 if (!legacy_lighting)
8927 shader_addline(buffer, "att = 1.0 / att;\n");
8928 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8929 if (!settings->normal)
8931 shader_addline(buffer, "}\n");
8932 continue;
8934 shader_addline(buffer, "dir = ffp_normalize(dir);\n");
8935 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8936 shader_addline(buffer, "}\n");
8939 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8941 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8942 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8943 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8944 shader_addline(buffer, "dst.x = 1.0;\n");
8945 if (legacy_lighting)
8947 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8948 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8949 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8951 else
8953 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8955 shader_addline(buffer, "dir = ffp_normalize(dir);\n");
8956 shader_addline(buffer, "t = dot(-dir, ffp_normalize(ffp_light[%u].direction));\n", idx);
8957 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8958 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8959 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8960 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8961 idx, idx, idx, idx);
8962 if (legacy_lighting)
8963 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8964 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8965 idx, idx, idx);
8966 else
8967 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8968 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8969 idx, idx, idx);
8970 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8971 if (!settings->normal)
8973 shader_addline(buffer, "}\n");
8974 continue;
8976 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8977 shader_addline(buffer, "}\n");
8980 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8982 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8983 if (!settings->normal)
8984 continue;
8985 shader_addline(buffer, "att = 1.0;\n");
8986 shader_addline(buffer, "dir = ffp_normalize(ffp_light[%u].direction.xyz);\n", idx);
8987 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8990 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8992 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8993 if (!settings->normal)
8994 continue;
8995 shader_addline(buffer, "att = 1.0;\n");
8996 shader_addline(buffer, "dir = ffp_normalize(ffp_light[%u].position.xyz);\n", idx);
8997 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
9000 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
9001 ambient, diffuse, emissive);
9002 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
9003 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
9006 /* Context activation is done by the caller. */
9007 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
9008 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
9010 static const struct attrib_info
9012 const char type[6];
9013 const char name[24];
9015 attrib_info[] =
9017 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
9018 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
9019 /* TODO: Indexed vertex blending */
9020 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
9021 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
9022 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
9023 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
9024 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
9026 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9027 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9028 BOOL output_legacy_fogcoord = legacy_syntax;
9029 BOOL legacy_lighting = priv->legacy_lighting;
9030 GLuint shader_obj;
9031 unsigned int i;
9033 string_buffer_clear(buffer);
9035 shader_glsl_add_version_declaration(buffer, gl_info);
9037 if (shader_glsl_use_explicit_attrib_location(gl_info))
9038 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9040 shader_addline(buffer, "invariant gl_Position;\n");
9042 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
9044 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
9046 if (shader_glsl_use_explicit_attrib_location(gl_info))
9047 shader_addline(buffer, "layout(location = %u) ", i);
9048 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
9050 shader_addline(buffer, "\n");
9052 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9053 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9054 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9055 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9057 shader_addline(buffer, "uniform struct\n{\n");
9058 shader_addline(buffer, " vec4 emissive;\n");
9059 shader_addline(buffer, " vec4 ambient;\n");
9060 shader_addline(buffer, " vec4 diffuse;\n");
9061 shader_addline(buffer, " vec4 specular;\n");
9062 shader_addline(buffer, " float shininess;\n");
9063 shader_addline(buffer, "} ffp_material;\n");
9065 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9066 shader_addline(buffer, "uniform struct\n{\n");
9067 shader_addline(buffer, " vec4 diffuse;\n");
9068 shader_addline(buffer, " vec4 specular;\n");
9069 shader_addline(buffer, " vec4 ambient;\n");
9070 shader_addline(buffer, " vec4 position;\n");
9071 shader_addline(buffer, " vec3 direction;\n");
9072 shader_addline(buffer, " float range;\n");
9073 shader_addline(buffer, " float falloff;\n");
9074 shader_addline(buffer, " float c_att;\n");
9075 shader_addline(buffer, " float l_att;\n");
9076 shader_addline(buffer, " float q_att;\n");
9077 shader_addline(buffer, " float cos_htheta;\n");
9078 shader_addline(buffer, " float cos_hphi;\n");
9079 shader_addline(buffer, "} ffp_light[%u];\n", WINED3D_MAX_ACTIVE_LIGHTS);
9081 if (settings->point_size)
9083 shader_addline(buffer, "uniform struct\n{\n");
9084 shader_addline(buffer, " float size;\n");
9085 shader_addline(buffer, " float size_min;\n");
9086 shader_addline(buffer, " float size_max;\n");
9087 shader_addline(buffer, " float c_att;\n");
9088 shader_addline(buffer, " float l_att;\n");
9089 shader_addline(buffer, " float q_att;\n");
9090 shader_addline(buffer, "} ffp_point;\n");
9093 if (legacy_syntax)
9095 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9096 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9097 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9098 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9100 else
9102 if (settings->clipping)
9103 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9105 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9106 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9107 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9108 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9111 shader_addline(buffer, "\nvec3 ffp_normalize(vec3 n)\n{\n");
9112 shader_addline(buffer, " float lensq = dot(n, n);\n");
9113 shader_addline(buffer, " return lensq == 0.0 ? n : (n * inversesqrt(lensq));\n");
9114 shader_addline(buffer, "}\n");
9116 shader_addline(buffer, "\nvoid main()\n{\n");
9117 shader_addline(buffer, "float m;\n");
9118 shader_addline(buffer, "vec3 r;\n");
9120 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9122 if (attrib_info[i].name[0])
9123 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9124 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9126 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
9128 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9129 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9130 && settings->texcoords & (1u << i))
9131 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9134 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9136 if (settings->transformed)
9138 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9139 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9140 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9142 else
9144 for (i = 0; i < settings->vertexblends; ++i)
9145 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9147 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9148 for (i = 0; i < settings->vertexblends + 1; ++i)
9149 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9151 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9152 if (settings->clipping)
9154 if (legacy_syntax)
9155 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9156 else
9157 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9158 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9160 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9163 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9164 if (settings->normal)
9166 if (!settings->vertexblends)
9168 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9170 else
9172 for (i = 0; i < settings->vertexblends + 1; ++i)
9173 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9176 if (settings->normalize)
9177 shader_addline(buffer, "normal = ffp_normalize(normal);\n");
9180 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9181 if (legacy_syntax)
9183 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9184 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9186 else
9188 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9189 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9192 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
9194 BOOL output_legacy_texcoord = legacy_syntax;
9196 switch (settings->texgen[i] & 0xffff0000)
9198 case WINED3DTSS_TCI_PASSTHRU:
9199 if (settings->texcoords & (1u << i))
9200 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9201 i, i, i);
9202 else if (shader_glsl_full_ffp_varyings(gl_info))
9203 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9204 else
9205 output_legacy_texcoord = FALSE;
9206 break;
9208 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9209 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9210 break;
9212 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9213 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9214 break;
9216 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9217 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9218 " * vec4(reflect(ffp_normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9219 break;
9221 case WINED3DTSS_TCI_SPHEREMAP:
9222 shader_addline(buffer, "r = reflect(ffp_normalize(ec_pos.xyz), normal);\n");
9223 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9224 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9225 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9226 break;
9228 default:
9229 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9230 break;
9232 if (output_legacy_texcoord)
9233 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9236 switch (settings->fog_mode)
9238 case WINED3D_FFP_VS_FOG_OFF:
9239 output_legacy_fogcoord = FALSE;
9240 break;
9242 case WINED3D_FFP_VS_FOG_FOGCOORD:
9243 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9244 break;
9246 case WINED3D_FFP_VS_FOG_RANGE:
9247 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9248 break;
9250 case WINED3D_FFP_VS_FOG_DEPTH:
9251 if (settings->ortho_fog)
9253 if (gl_info->supported[ARB_CLIP_CONTROL])
9254 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9255 else
9256 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9257 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9259 else if (settings->transformed)
9261 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9263 else
9265 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9267 break;
9269 default:
9270 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9271 break;
9273 if (output_legacy_fogcoord)
9274 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9276 if (settings->point_size)
9278 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9279 " + ffp_point.l_att * length(ec_pos.xyz)"
9280 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9281 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9282 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9285 shader_addline(buffer, "}\n");
9287 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9288 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9290 return shader_obj;
9293 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9294 unsigned int argnum, unsigned int stage, DWORD arg)
9296 const char *ret;
9298 if (arg == ARG_UNUSED)
9299 return "<unused arg>";
9301 switch (arg & WINED3DTA_SELECTMASK)
9303 case WINED3DTA_DIFFUSE:
9304 ret = "ffp_varying_diffuse";
9305 break;
9307 case WINED3DTA_CURRENT:
9308 ret = "ret";
9309 break;
9311 case WINED3DTA_TEXTURE:
9312 switch (stage)
9314 case 0: ret = "tex0"; break;
9315 case 1: ret = "tex1"; break;
9316 case 2: ret = "tex2"; break;
9317 case 3: ret = "tex3"; break;
9318 case 4: ret = "tex4"; break;
9319 case 5: ret = "tex5"; break;
9320 case 6: ret = "tex6"; break;
9321 case 7: ret = "tex7"; break;
9322 default:
9323 ret = "<invalid texture>";
9324 break;
9326 break;
9328 case WINED3DTA_TFACTOR:
9329 ret = "tex_factor";
9330 break;
9332 case WINED3DTA_SPECULAR:
9333 ret = "ffp_varying_specular";
9334 break;
9336 case WINED3DTA_TEMP:
9337 ret = "temp_reg";
9338 break;
9340 case WINED3DTA_CONSTANT:
9341 switch (stage)
9343 case 0: ret = "tss_const0"; break;
9344 case 1: ret = "tss_const1"; break;
9345 case 2: ret = "tss_const2"; break;
9346 case 3: ret = "tss_const3"; break;
9347 case 4: ret = "tss_const4"; break;
9348 case 5: ret = "tss_const5"; break;
9349 case 6: ret = "tss_const6"; break;
9350 case 7: ret = "tss_const7"; break;
9351 default:
9352 ret = "<invalid constant>";
9353 break;
9355 break;
9357 default:
9358 return "<unhandled arg>";
9361 if (arg & WINED3DTA_COMPLEMENT)
9363 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9364 if (argnum == 0)
9365 ret = "arg0";
9366 else if (argnum == 1)
9367 ret = "arg1";
9368 else if (argnum == 2)
9369 ret = "arg2";
9372 if (arg & WINED3DTA_ALPHAREPLICATE)
9374 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9375 if (argnum == 0)
9376 ret = "arg0";
9377 else if (argnum == 1)
9378 ret = "arg1";
9379 else if (argnum == 2)
9380 ret = "arg2";
9383 return ret;
9386 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9387 BOOL alpha, BOOL tmp_dst, unsigned int op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9389 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9391 if (color && alpha)
9392 dstmask = "";
9393 else if (color)
9394 dstmask = ".xyz";
9395 else
9396 dstmask = ".w";
9398 dstreg = tmp_dst ? "temp_reg" : "ret";
9400 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9401 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9402 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9404 switch (op)
9406 case WINED3D_TOP_DISABLE:
9407 break;
9409 case WINED3D_TOP_SELECT_ARG1:
9410 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9411 break;
9413 case WINED3D_TOP_SELECT_ARG2:
9414 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9415 break;
9417 case WINED3D_TOP_MODULATE:
9418 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9419 break;
9421 case WINED3D_TOP_MODULATE_4X:
9422 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9423 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9424 break;
9426 case WINED3D_TOP_MODULATE_2X:
9427 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9428 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9429 break;
9431 case WINED3D_TOP_ADD:
9432 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9433 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9434 break;
9436 case WINED3D_TOP_ADD_SIGNED:
9437 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9438 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9439 break;
9441 case WINED3D_TOP_ADD_SIGNED_2X:
9442 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9443 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9444 break;
9446 case WINED3D_TOP_SUBTRACT:
9447 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9448 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9449 break;
9451 case WINED3D_TOP_ADD_SMOOTH:
9452 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9453 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9454 break;
9456 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9457 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9458 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9459 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9460 break;
9462 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9463 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9464 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9465 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9466 break;
9468 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9469 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9470 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9471 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9472 break;
9474 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9475 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9476 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9477 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9478 break;
9480 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9481 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9482 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9483 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9484 break;
9486 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9487 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9488 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9489 break;
9491 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9492 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9493 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9494 break;
9496 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9497 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9498 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9499 break;
9500 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9501 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9502 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9503 break;
9505 case WINED3D_TOP_BUMPENVMAP:
9506 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9507 /* These are handled in the first pass, nothing to do. */
9508 break;
9510 case WINED3D_TOP_DOTPRODUCT3:
9511 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9512 dstreg, dstmask, arg1, arg2, dstmask);
9513 break;
9515 case WINED3D_TOP_MULTIPLY_ADD:
9516 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9517 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9518 break;
9520 case WINED3D_TOP_LERP:
9521 /* MSDN isn't quite right here. */
9522 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9523 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9524 break;
9526 default:
9527 FIXME("Unhandled operation %#x.\n", op);
9528 break;
9532 /* Context activation is done by the caller. */
9533 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9534 const struct ffp_frag_settings *settings, const struct wined3d_context_gl *context_gl)
9536 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9537 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9538 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9539 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9540 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
9541 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9542 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9543 UINT lowest_disabled_stage;
9544 GLuint shader_id;
9545 DWORD arg0, arg1, arg2;
9546 unsigned int stage;
9548 string_buffer_clear(buffer);
9550 /* Find out which textures are read */
9551 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES; ++stage)
9553 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9554 break;
9556 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9557 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9558 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9560 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9561 || (stage == 0 && settings->color_key_enabled))
9562 tex_map |= 1u << stage;
9563 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9564 tfactor_used = TRUE;
9565 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9566 tempreg_used = TRUE;
9567 if (settings->op[stage].tmp_dst)
9568 tempreg_used = TRUE;
9569 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9570 tss_const_map |= 1u << stage;
9572 switch (settings->op[stage].cop)
9574 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9575 lum_map |= 1u << stage;
9576 /* fall through */
9577 case WINED3D_TOP_BUMPENVMAP:
9578 bump_map |= 1u << stage;
9579 /* fall through */
9580 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9581 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9582 tex_map |= 1u << stage;
9583 break;
9585 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9586 tfactor_used = TRUE;
9587 break;
9589 default:
9590 break;
9593 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9594 continue;
9596 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9597 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9598 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9600 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9601 tex_map |= 1u << stage;
9602 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9603 tfactor_used = TRUE;
9604 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9605 tempreg_used = TRUE;
9606 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9607 tss_const_map |= 1u << stage;
9609 lowest_disabled_stage = stage;
9611 shader_glsl_add_version_declaration(buffer, gl_info);
9613 if (shader_glsl_use_explicit_attrib_location(gl_info))
9614 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9615 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9616 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9617 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9618 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9620 if (!use_legacy_fragment_output(gl_info))
9622 shader_addline(buffer, "vec4 ps_out[1];\n");
9623 if (shader_glsl_use_explicit_attrib_location(gl_info))
9624 shader_addline(buffer, "layout(location = 0) ");
9625 shader_addline(buffer, "out vec4 color_out0;\n");
9628 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9629 shader_addline(buffer, "vec4 ret;\n");
9630 if (tempreg_used || settings->sRGB_write)
9631 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9632 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9634 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES; ++stage)
9636 const char *sampler_type;
9638 if (tss_const_map & (1u << stage))
9639 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9641 if (!(tex_map & (1u << stage)))
9642 continue;
9644 switch (settings->op[stage].tex_type)
9646 case WINED3D_GL_RES_TYPE_TEX_1D:
9647 sampler_type = "1D";
9648 break;
9649 case WINED3D_GL_RES_TYPE_TEX_2D:
9650 sampler_type = "2D";
9651 break;
9652 case WINED3D_GL_RES_TYPE_TEX_3D:
9653 sampler_type = "3D";
9654 break;
9655 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9656 sampler_type = "Cube";
9657 break;
9658 case WINED3D_GL_RES_TYPE_TEX_RECT:
9659 sampler_type = "2DRect";
9660 break;
9661 default:
9662 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9663 sampler_type = NULL;
9664 break;
9666 if (sampler_type)
9668 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9669 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
9670 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9673 shader_addline(buffer, "vec4 tex%u;\n", stage);
9675 if (!(bump_map & (1u << stage)))
9676 continue;
9677 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9679 if (!(lum_map & (1u << stage)))
9680 continue;
9681 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9682 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9684 if (tfactor_used)
9685 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9686 if (settings->color_key_enabled)
9687 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9688 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9690 if (settings->sRGB_write)
9692 shader_addline(buffer, "const vec4 srgb_const0 = ");
9693 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
9694 shader_addline(buffer, ";\n");
9695 shader_addline(buffer, "const vec4 srgb_const1 = ");
9696 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
9697 shader_addline(buffer, ";\n");
9700 shader_addline(buffer, "uniform struct\n{\n");
9701 shader_addline(buffer, " vec4 color;\n");
9702 shader_addline(buffer, " float density;\n");
9703 shader_addline(buffer, " float end;\n");
9704 shader_addline(buffer, " float scale;\n");
9705 shader_addline(buffer, "} ffp_fog;\n");
9707 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9708 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9710 if (legacy_syntax)
9712 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9713 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9714 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9715 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9716 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9718 else
9720 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9721 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9722 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9723 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_FFP_TEXTURES);
9724 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9727 shader_addline(buffer, "void main()\n{\n");
9729 if (legacy_syntax)
9731 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9732 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9735 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES; ++stage)
9737 if (tex_map & (1u << stage))
9739 if (settings->pointsprite)
9740 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9741 else if (settings->texcoords_initialized & (1u << stage))
9742 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9743 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9744 else
9745 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9749 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9750 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9752 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9753 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9755 /* Generate texture sampling instructions */
9756 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9758 const char *texture_function, *coord_mask;
9759 BOOL proj;
9761 if (!(tex_map & (1u << stage)))
9762 continue;
9764 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9766 proj = FALSE;
9768 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9769 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9771 proj = TRUE;
9773 else
9775 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9776 proj = TRUE;
9779 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9780 proj = FALSE;
9782 switch (settings->op[stage].tex_type)
9784 case WINED3D_GL_RES_TYPE_TEX_1D:
9785 texture_function = "texture1D";
9786 coord_mask = "x";
9787 break;
9788 case WINED3D_GL_RES_TYPE_TEX_2D:
9789 texture_function = "texture2D";
9790 coord_mask = "xy";
9791 break;
9792 case WINED3D_GL_RES_TYPE_TEX_3D:
9793 texture_function = "texture3D";
9794 coord_mask = "xyz";
9795 break;
9796 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9797 texture_function = "textureCube";
9798 coord_mask = "xyz";
9799 break;
9800 case WINED3D_GL_RES_TYPE_TEX_RECT:
9801 texture_function = "texture2DRect";
9802 coord_mask = "xy";
9803 break;
9804 default:
9805 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9806 texture_function = "";
9807 coord_mask = "xyzw";
9808 proj = FALSE;
9809 break;
9811 if (!legacy_syntax)
9812 texture_function = "texture";
9814 if (stage > 0
9815 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9816 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9818 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9820 /* With projective textures, texbem only divides the static
9821 * texture coordinate, not the displacement, so multiply the
9822 * displacement with the dividing parameter before passing it to
9823 * TXP. */
9824 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9826 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9828 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9829 stage, stage);
9830 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9832 else
9834 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9835 stage, stage);
9836 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9839 else
9841 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9844 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ret.%s%s);\n",
9845 stage, texture_function, proj ? "Proj" : "", stage, coord_mask, proj ? "w" : "");
9847 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9848 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9849 stage, stage - 1, stage - 1, stage - 1);
9851 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9853 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9854 stage, texture_function, proj ? "Proj" : "", stage, stage);
9856 else
9858 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].%s%s);\n",
9859 stage, texture_function, proj ? "Proj" : "", stage, stage, coord_mask, proj ? "w" : "");
9862 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9863 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9864 settings->op[stage].color_fixup);
9867 if (settings->color_key_enabled)
9868 shader_glsl_generate_colour_key_test(buffer, "tex0", "color_key[0]", "color_key[1]");
9870 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9872 /* Generate the main shader */
9873 for (stage = 0; stage < WINED3D_MAX_FFP_TEXTURES; ++stage)
9875 BOOL op_equal;
9877 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9878 break;
9880 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9881 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9882 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9883 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9884 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9885 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9886 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9887 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9888 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9889 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9890 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9891 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9892 else
9893 op_equal = settings->op[stage].aop == settings->op[stage].cop
9894 && settings->op[stage].carg0 == settings->op[stage].aarg0
9895 && settings->op[stage].carg1 == settings->op[stage].aarg1
9896 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9898 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9900 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9901 settings->op[stage].cop, settings->op[stage].carg0,
9902 settings->op[stage].carg1, settings->op[stage].carg2);
9904 else if (op_equal)
9906 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9907 settings->op[stage].cop, settings->op[stage].carg0,
9908 settings->op[stage].carg1, settings->op[stage].carg2);
9910 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9911 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9913 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9914 settings->op[stage].cop, settings->op[stage].carg0,
9915 settings->op[stage].carg1, settings->op[stage].carg2);
9916 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9917 settings->op[stage].aop, settings->op[stage].aarg0,
9918 settings->op[stage].aarg1, settings->op[stage].aarg2);
9922 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9923 get_fragment_output(gl_info));
9925 if (settings->sRGB_write)
9926 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9928 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9930 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9931 if (!use_legacy_fragment_output(gl_info))
9932 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9934 shader_addline(buffer, "}\n");
9936 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9937 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9939 string_buffer_release(&priv->string_buffers, tex_reg_name);
9940 return shader_id;
9943 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9944 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9946 struct glsl_ffp_vertex_shader *shader;
9947 const struct wine_rb_entry *entry;
9949 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9950 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9952 if (!(shader = heap_alloc(sizeof(*shader))))
9953 return NULL;
9955 shader->desc.settings = *settings;
9956 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9957 list_init(&shader->linked_programs);
9958 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9959 ERR("Failed to insert ffp vertex shader.\n");
9961 return shader;
9964 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9965 const struct ffp_frag_settings *args, const struct wined3d_context_gl *context_gl)
9967 struct glsl_ffp_fragment_shader *glsl_desc;
9968 const struct ffp_frag_desc *desc;
9970 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9971 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9973 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9974 return NULL;
9976 glsl_desc->entry.settings = *args;
9977 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context_gl);
9978 list_init(&glsl_desc->linked_programs);
9979 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9981 return glsl_desc;
9985 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9986 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9988 unsigned int i;
9989 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9991 for (i = 0; i < vs_c_count; ++i)
9993 string_buffer_sprintf(name, "vs_c[%u]", i);
9994 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9996 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9998 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10000 string_buffer_sprintf(name, "vs_i[%u]", i);
10001 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10004 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10006 string_buffer_sprintf(name, "vs_b[%u]", i);
10007 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10010 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10011 vs->base_vertex_id_location = GL_EXTCALL(glGetUniformLocation(program_id, "base_vertex_id"));
10013 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
10015 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
10016 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10018 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
10019 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
10020 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
10022 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
10023 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10025 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
10026 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
10027 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
10028 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
10029 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
10030 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
10031 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
10033 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
10034 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10035 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
10036 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10037 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
10038 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10039 string_buffer_sprintf(name, "ffp_light[%u].position", i);
10040 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10041 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
10042 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10043 string_buffer_sprintf(name, "ffp_light[%u].range", i);
10044 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10045 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
10046 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10047 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
10048 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10049 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
10050 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10051 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
10052 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10053 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
10054 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10055 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
10056 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10058 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10059 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10060 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10061 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10062 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10063 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10064 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10066 string_buffer_release(&priv->string_buffers, name);
10069 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10070 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10072 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10075 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10076 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10078 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10081 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10082 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10084 unsigned int i;
10085 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10087 for (i = 0; i < ps_c_count; ++i)
10089 string_buffer_sprintf(name, "ps_c[%u]", i);
10090 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10092 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10094 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10096 string_buffer_sprintf(name, "ps_i[%u]", i);
10097 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10100 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10102 string_buffer_sprintf(name, "ps_b[%u]", i);
10103 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10106 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
10108 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10109 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10110 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10111 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10112 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10113 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10114 string_buffer_sprintf(name, "tss_const%u", i);
10115 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10118 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10119 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10121 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10122 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10123 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10124 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10126 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10128 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10129 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10130 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10132 string_buffer_release(&priv->string_buffers, name);
10135 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10136 const struct wined3d_context_gl *context_gl, struct wined3d_shader *shader)
10138 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10139 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10140 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10141 struct glsl_cs_compiled_shader *gl_shaders;
10142 struct glsl_shader_private *shader_data;
10143 struct glsl_shader_prog_link *entry;
10144 GLuint shader_id, program_id;
10146 if (!(entry = heap_alloc(sizeof(*entry))))
10148 ERR("Out of memory.\n");
10149 return E_OUTOFMEMORY;
10152 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10154 ERR("Failed to allocate backend data.\n");
10155 heap_free(entry);
10156 return E_OUTOFMEMORY;
10158 shader_data = shader->backend_data;
10160 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10162 ERR("Failed to allocate GL shader array.\n");
10163 heap_free(entry);
10164 heap_free(shader->backend_data);
10165 shader->backend_data = NULL;
10166 return E_OUTOFMEMORY;
10168 shader_data->shader_array_size = 1;
10169 gl_shaders = shader_data->gl_shaders.cs;
10171 TRACE("Compiling compute shader %p.\n", shader);
10173 string_buffer_clear(buffer);
10174 shader_id = shader_glsl_generate_compute_shader(context_gl, buffer, &priv->string_buffers, shader);
10175 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10177 program_id = GL_EXTCALL(glCreateProgram());
10178 TRACE("Created new GLSL shader program %u.\n", program_id);
10180 entry->id = program_id;
10181 entry->vs.id = 0;
10182 entry->hs.id = 0;
10183 entry->ds.id = 0;
10184 entry->gs.id = 0;
10185 entry->ps.id = 0;
10186 entry->cs.id = shader_id;
10187 entry->constant_version = 0;
10188 entry->shader_controlled_clip_distances = 0;
10189 entry->ps.np2_fixup_info = NULL;
10190 add_glsl_program_entry(priv, entry);
10192 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10193 GL_EXTCALL(glAttachShader(program_id, shader_id));
10194 checkGLcall("glAttachShader");
10196 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10198 TRACE("Linking GLSL shader program %u.\n", program_id);
10199 GL_EXTCALL(glLinkProgram(program_id));
10200 shader_glsl_validate_link(gl_info, program_id);
10202 GL_EXTCALL(glUseProgram(program_id));
10203 checkGLcall("glUseProgram");
10204 shader_glsl_load_program_resources(context_gl, priv, program_id, shader);
10205 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10207 entry->constant_update_mask = 0;
10209 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10210 checkGLcall("glUseProgram");
10211 return WINED3D_OK;
10214 static GLuint find_glsl_compute_shader(const struct wined3d_context_gl *context_gl,
10215 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10217 struct glsl_shader_private *shader_data;
10219 if (!shader->backend_data)
10221 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10222 if (FAILED(shader_glsl_compile_compute_shader(priv, context_gl, shader)))
10224 ERR("Failed to compile compute shader %p.\n", shader);
10225 return 0;
10228 shader_data = shader->backend_data;
10229 return shader_data->gl_shaders.cs[0].id;
10232 /* Context activation is done by the caller. */
10233 static void set_glsl_compute_shader_program(const struct wined3d_context_gl *context_gl,
10234 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10236 struct glsl_shader_prog_link *entry;
10237 struct wined3d_shader *shader;
10238 struct glsl_program_key key;
10239 GLuint cs_id;
10241 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10242 return;
10244 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10246 WARN("Compute shader is NULL.\n");
10247 ctx_data->glsl_program = NULL;
10248 return;
10251 cs_id = find_glsl_compute_shader(context_gl, priv, shader);
10252 memset(&key, 0, sizeof(key));
10253 key.cs_id = cs_id;
10254 if (!(entry = get_glsl_program_entry(priv, &key)))
10255 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10256 ctx_data->glsl_program = entry;
10259 /* Context activation is done by the caller. */
10260 static void set_glsl_shader_program(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
10261 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10263 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
10264 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10265 const struct wined3d_shader *pre_rasterization_shader;
10266 const struct ps_np2fixup_info *np2fixup_info = NULL;
10267 struct wined3d_shader *hshader, *dshader, *gshader;
10268 struct glsl_shader_prog_link *entry = NULL;
10269 struct wined3d_shader *vshader = NULL;
10270 struct wined3d_shader *pshader = NULL;
10271 GLuint reorder_shader_id = 0;
10272 struct glsl_program_key key;
10273 uint32_t attribs_map;
10274 GLuint program_id;
10275 unsigned int i;
10276 GLuint vs_id = 0;
10277 GLuint hs_id = 0;
10278 GLuint ds_id = 0;
10279 GLuint gs_id = 0;
10280 GLuint ps_id = 0;
10281 struct list *ps_list, *vs_list;
10282 struct wined3d_string_buffer *tmp_name;
10284 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10286 vs_id = ctx_data->glsl_program->vs.id;
10287 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10289 if (use_vs(state))
10290 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10292 else if (use_vs(state))
10294 struct vs_compile_args vs_compile_args;
10296 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10298 find_vs_compile_args(state, vshader, &vs_compile_args, &context_gl->c);
10299 vs_id = find_glsl_vertex_shader(context_gl, priv, vshader, &vs_compile_args);
10300 vs_list = &vshader->linked_programs;
10302 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10304 struct glsl_ffp_vertex_shader *ffp_shader;
10305 struct wined3d_ffp_vs_settings settings;
10307 wined3d_ffp_get_vs_settings(&context_gl->c, state, &settings);
10308 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10309 vs_id = ffp_shader->id;
10310 vs_list = &ffp_shader->linked_programs;
10313 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10314 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10315 hs_id = ctx_data->glsl_program->hs.id;
10316 else if (hshader)
10317 hs_id = find_glsl_hull_shader(context_gl, priv, hshader);
10319 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10320 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10322 ds_id = ctx_data->glsl_program->ds.id;
10324 else if (dshader)
10326 struct ds_compile_args args;
10328 find_ds_compile_args(state, dshader, &args, &context_gl->c);
10329 ds_id = find_glsl_domain_shader(context_gl, priv, dshader, &args);
10332 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10333 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10335 gs_id = ctx_data->glsl_program->gs.id;
10337 else if (gshader)
10339 struct gs_compile_args args;
10341 find_gs_compile_args(state, gshader, &args, &context_gl->c);
10342 gs_id = find_glsl_geometry_shader(context_gl, priv, gshader, &args);
10345 /* A pixel shader is not used when rasterization is disabled. */
10346 if (is_rasterization_disabled(gshader))
10348 ps_id = 0;
10349 ps_list = NULL;
10351 else if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10353 ps_id = ctx_data->glsl_program->ps.id;
10354 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10356 if (use_ps(state))
10357 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10359 else if (use_ps(state))
10361 struct ps_compile_args ps_compile_args;
10362 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10363 find_ps_compile_args(state, pshader, context_gl->c.stream_info.position_transformed,
10364 &ps_compile_args, &context_gl->c);
10365 ps_id = find_glsl_fragment_shader(context_gl, &priv->shader_buffer, &priv->string_buffers,
10366 pshader, &ps_compile_args, &np2fixup_info);
10367 ps_list = &pshader->linked_programs;
10369 else if (priv->fragment_pipe == &glsl_fragment_pipe
10370 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10372 struct glsl_ffp_fragment_shader *ffp_shader;
10373 struct ffp_frag_settings settings;
10375 wined3d_ffp_get_fs_settings(&context_gl->c, state, &settings, FALSE);
10376 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context_gl);
10377 ps_id = ffp_shader->id;
10378 ps_list = &ffp_shader->linked_programs;
10381 key.vs_id = vs_id;
10382 key.hs_id = hs_id;
10383 key.ds_id = ds_id;
10384 key.gs_id = gs_id;
10385 key.ps_id = ps_id;
10386 key.cs_id = 0;
10387 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10389 ctx_data->glsl_program = entry;
10390 return;
10393 /* If we get to this point, then no matching program exists, so we create one */
10394 program_id = GL_EXTCALL(glCreateProgram());
10395 TRACE("Created new GLSL shader program %u.\n", program_id);
10397 /* Create the entry */
10398 entry = heap_alloc(sizeof(*entry));
10399 entry->id = program_id;
10400 entry->vs.id = vs_id;
10401 entry->hs.id = hs_id;
10402 entry->ds.id = ds_id;
10403 entry->gs.id = gs_id;
10404 entry->ps.id = ps_id;
10405 entry->cs.id = 0;
10406 entry->constant_version = 0;
10407 entry->shader_controlled_clip_distances = 0;
10408 entry->ps.np2_fixup_info = np2fixup_info;
10409 /* Add the hash table entry */
10410 add_glsl_program_entry(priv, entry);
10412 /* Set the current program */
10413 ctx_data->glsl_program = entry;
10415 /* Attach GLSL vshader */
10416 if (vs_id)
10418 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10419 GL_EXTCALL(glAttachShader(program_id, vs_id));
10420 checkGLcall("glAttachShader");
10422 list_add_head(vs_list, &entry->vs.shader_entry);
10425 if (vshader)
10427 attribs_map = vshader->reg_maps.input_registers;
10428 if (vshader->reg_maps.shader_version.major < 4)
10430 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10431 state->primitive_type == WINED3D_PT_POINTLIST && vshader->reg_maps.point_size,
10432 d3d_info->emulated_flatshading
10433 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10434 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10435 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10436 checkGLcall("glAttachShader");
10437 /* Flag the reorder function for deletion, it will be freed
10438 * automatically when the program is destroyed. */
10439 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10442 else
10444 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10447 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10449 /* Bind vertex attributes to a corresponding index number to match
10450 * the same index numbers as ARB_vertex_programs (makes loading
10451 * vertex attributes simpler). With this method, we can use the
10452 * exact same code to load the attributes later for both ARB and
10453 * GLSL shaders.
10455 * We have to do this here because we need to know the Program ID
10456 * in order to make the bindings work, and it has to be done prior
10457 * to linking the GLSL program. */
10458 tmp_name = string_buffer_get(&priv->string_buffers);
10459 while (attribs_map)
10461 i = wined3d_bit_scan(&attribs_map);
10462 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10463 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10464 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10466 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10467 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10468 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10469 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10472 checkGLcall("glBindAttribLocation");
10474 if (!use_legacy_fragment_output(gl_info))
10476 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
10478 string_buffer_sprintf(tmp_name, "color_out%u", i);
10479 if (state->blend_state && state->blend_state->dual_source)
10480 GL_EXTCALL(glBindFragDataLocationIndexed(program_id, 0, i, tmp_name->buffer));
10481 else
10482 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10483 checkGLcall("glBindFragDataLocation");
10486 string_buffer_release(&priv->string_buffers, tmp_name);
10489 if (hshader)
10491 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10492 GL_EXTCALL(glAttachShader(program_id, hs_id));
10493 checkGLcall("glAttachShader");
10495 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10498 if (dshader)
10500 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10501 GL_EXTCALL(glAttachShader(program_id, ds_id));
10502 checkGLcall("glAttachShader");
10504 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10507 if (gshader)
10509 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10510 GL_EXTCALL(glAttachShader(program_id, gs_id));
10511 checkGLcall("glAttachShader");
10513 shader_glsl_init_transform_feedback(context_gl, priv, program_id, gshader);
10515 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10518 /* Attach GLSL pshader */
10519 if (ps_id)
10521 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10522 GL_EXTCALL(glAttachShader(program_id, ps_id));
10523 checkGLcall("glAttachShader");
10525 list_add_head(ps_list, &entry->ps.shader_entry);
10528 /* Link the program */
10529 TRACE("Linking GLSL shader program %u.\n", program_id);
10530 GL_EXTCALL(glLinkProgram(program_id));
10531 shader_glsl_validate_link(gl_info, program_id);
10533 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10534 vshader ? vshader->limits->constant_float : 0);
10535 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10536 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10537 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10538 pshader ? pshader->limits->constant_float : 0);
10539 checkGLcall("find glsl program uniform locations");
10541 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10542 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10544 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10545 entry->shader_controlled_clip_distances = 1;
10546 entry->clip_distance_mask = wined3d_mask_from_size(clip_distance_count);
10549 if (needs_legacy_glsl_syntax(gl_info))
10551 if (pshader && pshader->reg_maps.shader_version.major >= 3
10552 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10554 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10555 entry->vs.vertex_color_clamp = GL_FALSE;
10557 else
10559 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10562 else
10564 /* With core profile we never change vertex_color_clamp from
10565 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10566 * glClampColorARB(). */
10567 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10570 /* Set the shader to allow uniform loading on it */
10571 GL_EXTCALL(glUseProgram(program_id));
10572 checkGLcall("glUseProgram");
10574 entry->constant_update_mask = 0;
10575 if (vshader)
10577 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10578 if (vshader->reg_maps.integer_constants)
10579 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10580 if (vshader->reg_maps.boolean_constants)
10581 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10582 if (entry->vs.pos_fixup_location != -1)
10583 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10584 if (entry->vs.base_vertex_id_location != -1)
10585 entry->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
10587 shader_glsl_load_program_resources(context_gl, priv, program_id, vshader);
10589 else
10591 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10592 | WINED3D_SHADER_CONST_FFP_PROJ;
10594 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10596 if (entry->vs.modelview_matrix_location[i] != -1)
10598 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10599 break;
10603 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
10605 if (entry->vs.texture_matrix_location[i] != -1)
10607 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10608 break;
10611 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10612 || entry->vs.material_specular_location != -1
10613 || entry->vs.material_emissive_location != -1
10614 || entry->vs.material_shininess_location != -1)
10615 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10616 if (entry->vs.light_ambient_location != -1)
10617 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10619 if (entry->vs.clip_planes_location != -1)
10620 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10621 if (entry->vs.pointsize_min_location != -1)
10622 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10624 if (hshader)
10625 shader_glsl_load_program_resources(context_gl, priv, program_id, hshader);
10627 if (dshader)
10629 if (entry->ds.pos_fixup_location != -1)
10630 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10632 shader_glsl_load_program_resources(context_gl, priv, program_id, dshader);
10635 if (gshader)
10637 if (entry->gs.pos_fixup_location != -1)
10638 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10640 shader_glsl_load_program_resources(context_gl, priv, program_id, gshader);
10643 if (ps_id)
10645 if (pshader)
10647 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10648 if (pshader->reg_maps.integer_constants)
10649 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10650 if (pshader->reg_maps.boolean_constants)
10651 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10652 if (entry->ps.ycorrection_location != -1)
10653 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10655 shader_glsl_load_program_resources(context_gl, priv, program_id, pshader);
10656 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10658 else
10660 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10662 shader_glsl_load_samplers(&context_gl->c, priv, program_id, NULL);
10665 for (i = 0; i < WINED3D_MAX_FFP_TEXTURES; ++i)
10667 if (entry->ps.bumpenv_mat_location[i] != -1)
10669 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10670 break;
10674 if (entry->ps.fog_color_location != -1)
10675 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10676 if (entry->ps.alpha_test_ref_location != -1)
10677 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10678 if (entry->ps.np2_fixup_location != -1)
10679 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10680 if (entry->ps.color_key_location != -1)
10681 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10685 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10687 struct wined3d_device *device = shader->device;
10688 struct wined3d_context *context;
10690 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10692 context = context_acquire(device, NULL, 0);
10693 shader_glsl_compile_compute_shader(shader_priv, wined3d_context_gl(context), shader);
10694 context_release(context);
10698 /* Context activation is done by the caller. */
10699 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10700 const struct wined3d_state *state)
10702 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10703 struct glsl_context_data *ctx_data = context->shader_backend_data;
10704 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10705 struct shader_glsl_priv *priv = shader_priv;
10706 struct glsl_shader_prog_link *glsl_program;
10707 GLenum current_vertex_color_clamp;
10708 GLuint program_id, prev_id;
10710 priv->vertex_pipe->vp_enable(context, !use_vs(state));
10711 priv->fragment_pipe->fp_enable(context, !use_ps(state));
10713 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10714 set_glsl_shader_program(context_gl, state, priv, ctx_data);
10715 glsl_program = ctx_data->glsl_program;
10717 if (glsl_program)
10719 program_id = glsl_program->id;
10720 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10721 if (glsl_program->shader_controlled_clip_distances)
10722 wined3d_context_gl_enable_clip_distances(context_gl, glsl_program->clip_distance_mask);
10724 else
10726 program_id = 0;
10727 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10730 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10732 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10733 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10735 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10736 checkGLcall("glClampColorARB");
10738 else
10740 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10744 TRACE("Using GLSL program %u.\n", program_id);
10746 if (prev_id != program_id)
10748 GL_EXTCALL(glUseProgram(program_id));
10749 checkGLcall("glUseProgram");
10751 if (glsl_program)
10752 context->constant_update_mask |= glsl_program->constant_update_mask;
10755 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10758 /* Context activation is done by the caller. */
10759 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10760 const struct wined3d_state *state)
10762 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10763 struct glsl_context_data *ctx_data = context->shader_backend_data;
10764 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10765 struct shader_glsl_priv *priv = shader_priv;
10766 GLuint program_id, prev_id;
10768 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10769 set_glsl_compute_shader_program(context_gl, state, priv, ctx_data);
10770 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10772 TRACE("Using GLSL program %u.\n", program_id);
10774 if (prev_id != program_id)
10776 GL_EXTCALL(glUseProgram(program_id));
10777 checkGLcall("glUseProgram");
10780 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10781 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10782 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10783 | (1u << WINED3D_SHADER_TYPE_HULL)
10784 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10787 /* "context" is not necessarily the currently active context. */
10788 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10790 struct glsl_context_data *ctx_data = context->shader_backend_data;
10792 ctx_data->glsl_program = NULL;
10793 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10794 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10795 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10796 | (1u << WINED3D_SHADER_TYPE_HULL)
10797 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10798 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10801 /* Context activation is done by the caller. */
10802 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10804 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10805 struct glsl_context_data *ctx_data = context->shader_backend_data;
10806 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10807 struct shader_glsl_priv *priv = shader_priv;
10809 shader_glsl_invalidate_current_program(context);
10810 GL_EXTCALL(glUseProgram(0));
10811 checkGLcall("glUseProgram");
10813 priv->vertex_pipe->vp_enable(context, FALSE);
10814 priv->fragment_pipe->fp_enable(context, FALSE);
10816 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10818 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10819 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10820 checkGLcall("glClampColorARB");
10824 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10825 const struct glsl_shader_prog_link *program)
10827 const struct glsl_context_data *ctx_data;
10828 struct wined3d_context *context;
10829 unsigned int i;
10831 for (i = 0; i < device->context_count; ++i)
10833 context = device->contexts[i];
10834 ctx_data = context->shader_backend_data;
10836 if (ctx_data->glsl_program == program)
10837 shader_glsl_invalidate_current_program(context);
10841 static void shader_glsl_destroy(struct wined3d_shader *shader)
10843 struct glsl_shader_private *shader_data = shader->backend_data;
10844 struct wined3d_device *device = shader->device;
10845 struct shader_glsl_priv *priv = device->shader_priv;
10846 const struct wined3d_gl_info *gl_info;
10847 const struct list *linked_programs;
10848 struct wined3d_context *context;
10850 if (!shader_data || !shader_data->num_gl_shaders)
10852 heap_free(shader_data);
10853 shader->backend_data = NULL;
10854 return;
10857 context = context_acquire(device, NULL, 0);
10858 gl_info = wined3d_context_gl(context)->gl_info;
10860 TRACE("Deleting linked programs.\n");
10861 linked_programs = &shader->linked_programs;
10862 if (!list_empty(linked_programs))
10864 struct glsl_shader_prog_link *entry, *entry2;
10865 UINT i;
10867 switch (shader->reg_maps.shader_version.type)
10869 case WINED3D_SHADER_TYPE_PIXEL:
10871 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10873 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10875 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10876 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10877 checkGLcall("glDeleteShader");
10879 heap_free(shader_data->gl_shaders.ps);
10881 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10882 struct glsl_shader_prog_link, ps.shader_entry)
10884 shader_glsl_invalidate_contexts_program(device, entry);
10885 delete_glsl_program_entry(priv, gl_info, entry);
10888 break;
10891 case WINED3D_SHADER_TYPE_VERTEX:
10893 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10895 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10897 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10898 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10899 checkGLcall("glDeleteShader");
10901 heap_free(shader_data->gl_shaders.vs);
10903 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10904 struct glsl_shader_prog_link, vs.shader_entry)
10906 shader_glsl_invalidate_contexts_program(device, entry);
10907 delete_glsl_program_entry(priv, gl_info, entry);
10910 break;
10913 case WINED3D_SHADER_TYPE_HULL:
10915 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10917 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10919 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10920 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10921 checkGLcall("glDeleteShader");
10923 heap_free(shader_data->gl_shaders.hs);
10925 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10926 struct glsl_shader_prog_link, hs.shader_entry)
10928 shader_glsl_invalidate_contexts_program(device, entry);
10929 delete_glsl_program_entry(priv, gl_info, entry);
10932 break;
10935 case WINED3D_SHADER_TYPE_DOMAIN:
10937 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10939 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10941 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10942 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10943 checkGLcall("glDeleteShader");
10945 heap_free(shader_data->gl_shaders.ds);
10947 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10948 struct glsl_shader_prog_link, ds.shader_entry)
10950 shader_glsl_invalidate_contexts_program(device, entry);
10951 delete_glsl_program_entry(priv, gl_info, entry);
10954 break;
10957 case WINED3D_SHADER_TYPE_GEOMETRY:
10959 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10961 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10963 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10964 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10965 checkGLcall("glDeleteShader");
10967 heap_free(shader_data->gl_shaders.gs);
10969 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10970 struct glsl_shader_prog_link, gs.shader_entry)
10972 shader_glsl_invalidate_contexts_program(device, entry);
10973 delete_glsl_program_entry(priv, gl_info, entry);
10976 break;
10979 case WINED3D_SHADER_TYPE_COMPUTE:
10981 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10983 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10985 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10986 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10987 checkGLcall("glDeleteShader");
10989 heap_free(shader_data->gl_shaders.cs);
10991 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10992 struct glsl_shader_prog_link, cs.shader_entry)
10994 shader_glsl_invalidate_contexts_program(device, entry);
10995 delete_glsl_program_entry(priv, gl_info, entry);
10998 break;
11001 default:
11002 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
11003 break;
11007 heap_free(shader->backend_data);
11008 shader->backend_data = NULL;
11010 context_release(context);
11013 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
11015 const struct glsl_program_key *k = key;
11016 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
11017 const struct glsl_shader_prog_link, program_lookup_entry);
11018 int ret;
11020 if ((ret = wined3d_uint32_compare(k->vs_id, prog->vs.id)))
11021 return ret;
11022 if ((ret = wined3d_uint32_compare(k->gs_id, prog->gs.id)))
11023 return ret;
11024 if ((ret = wined3d_uint32_compare(k->ps_id, prog->ps.id)))
11025 return ret;
11026 if ((ret = wined3d_uint32_compare(k->hs_id, prog->hs.id)))
11027 return ret;
11028 if ((ret = wined3d_uint32_compare(k->ds_id, prog->ds.id)))
11029 return ret;
11030 if ((ret = wined3d_uint32_compare(k->cs_id, prog->cs.id)))
11031 return ret;
11033 return 0;
11036 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
11038 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
11039 + constant_count * sizeof(*heap->contained)
11040 + constant_count * sizeof(*heap->positions);
11041 void *mem;
11043 if (!(mem = heap_alloc(size)))
11045 ERR("Failed to allocate memory\n");
11046 return FALSE;
11049 heap->entries = mem;
11050 heap->entries[1].version = 0;
11051 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11052 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11053 heap->positions = (unsigned int *)(heap->contained + constant_count);
11054 heap->size = 1;
11056 return TRUE;
11059 static void constant_heap_free(struct constant_heap *heap)
11061 heap_free(heap->entries);
11064 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11065 const struct wined3d_fragment_pipe_ops *fragment_pipe)
11067 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11068 struct fragment_caps fragment_caps;
11069 void *vertex_priv, *fragment_priv;
11070 struct shader_glsl_priv *priv;
11072 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11073 return E_OUTOFMEMORY;
11075 string_buffer_list_init(&priv->string_buffers);
11077 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11079 ERR("Failed to initialize vertex pipe.\n");
11080 heap_free(priv);
11081 return E_FAIL;
11084 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11086 ERR("Failed to initialize fragment pipe.\n");
11087 vertex_pipe->vp_free(device, NULL);
11088 heap_free(priv);
11089 return E_FAIL;
11092 if (!string_buffer_init(&priv->shader_buffer))
11094 ERR("Failed to initialize shader buffer.\n");
11095 goto fail;
11098 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11100 ERR("Failed to allocate memory.\n");
11101 goto fail;
11104 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11106 ERR("Failed to initialize vertex shader constant heap\n");
11107 goto fail;
11110 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11112 ERR("Failed to initialize pixel shader constant heap\n");
11113 goto fail;
11116 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11118 priv->next_constant_version = 1;
11119 priv->vertex_pipe = vertex_pipe;
11120 priv->fragment_pipe = fragment_pipe;
11121 fragment_pipe->get_caps(device->adapter, &fragment_caps);
11122 priv->ffp_proj_control = fragment_caps.proj_control;
11123 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11125 device->vertex_priv = vertex_priv;
11126 device->fragment_priv = fragment_priv;
11127 device->shader_priv = priv;
11129 return WINED3D_OK;
11131 fail:
11132 constant_heap_free(&priv->pconst_heap);
11133 constant_heap_free(&priv->vconst_heap);
11134 heap_free(priv->stack);
11135 string_buffer_free(&priv->shader_buffer);
11136 fragment_pipe->free_private(device, NULL);
11137 vertex_pipe->vp_free(device, NULL);
11138 heap_free(priv);
11139 return E_OUTOFMEMORY;
11142 /* Context activation is done by the caller. */
11143 static void shader_glsl_free(struct wined3d_device *device, struct wined3d_context *context)
11145 struct shader_glsl_priv *priv = device->shader_priv;
11147 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11148 constant_heap_free(&priv->pconst_heap);
11149 constant_heap_free(&priv->vconst_heap);
11150 heap_free(priv->stack);
11151 string_buffer_list_cleanup(&priv->string_buffers);
11152 string_buffer_free(&priv->shader_buffer);
11153 priv->fragment_pipe->free_private(device, context);
11154 priv->vertex_pipe->vp_free(device, context);
11156 heap_free(device->shader_priv);
11157 device->shader_priv = NULL;
11160 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11162 struct glsl_context_data *ctx_data;
11164 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11165 return FALSE;
11166 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11167 context->shader_backend_data = ctx_data;
11168 return TRUE;
11171 static void shader_glsl_free_context_data(struct wined3d_context *context)
11173 heap_free(context->shader_backend_data);
11176 static void shader_glsl_init_context_state(struct wined3d_context *context)
11178 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11179 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11181 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11182 checkGLcall("GL_PROGRAM_POINT_SIZE");
11185 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11187 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11188 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11189 && gl_info->supported[ARB_TEXTURE_SWIZZLE]
11190 && gl_info->supported[EXT_SHADER_INTEGER_MIX];
11192 if (shader_model_4
11193 && gl_info->supported[ARB_COMPUTE_SHADER]
11194 && gl_info->supported[ARB_CULL_DISTANCE]
11195 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11196 && gl_info->supported[ARB_GPU_SHADER5]
11197 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11198 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11199 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11200 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11201 && gl_info->supported[ARB_TESSELLATION_SHADER]
11202 && gl_info->supported[ARB_TEXTURE_GATHER]
11203 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11204 return 5;
11206 if (shader_model_4)
11207 return 4;
11209 /* Support for texldd and texldl instructions in pixel shaders is required
11210 * for SM3. */
11211 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11212 return 3;
11214 return 2;
11217 static void shader_glsl_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
11219 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl_const(adapter)->gl_info;
11220 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11222 TRACE("Shader model %u.\n", shader_model);
11224 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11225 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11226 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11227 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11228 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11229 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11231 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11232 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11234 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11235 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11236 caps->varying_count = gl_info->limits.glsl_varyings;
11238 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11239 * Direct3D minimum requirement.
11241 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11242 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11244 * The problem is that the refrast clamps temporary results in the shader to
11245 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11246 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11247 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11248 * offer a way to query this.
11250 if (shader_model >= 4)
11251 caps->ps_1x_max_value = FLT_MAX;
11252 else
11253 caps->ps_1x_max_value = 1024.0f;
11255 /* Ideally we'd only set caps like sRGB writes here if supported by both
11256 * the shader backend and the fragment pipe, but we can get called before
11257 * shader_glsl_alloc(). */
11258 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11259 | WINED3D_SHADER_CAP_SRGB_WRITE;
11260 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
11261 caps->wined3d_caps |= WINED3D_SHADER_CAP_OUTPUT_INTERPOLATION;
11262 if (shader_glsl_full_ffp_varyings(gl_info))
11263 caps->wined3d_caps |= WINED3D_SHADER_CAP_FULL_FFP_VARYINGS;
11266 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11268 /* We support everything except YUV conversions. */
11269 return !is_complex_fixup(fixup);
11272 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11274 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11275 /* WINED3DSIH_ADD */ shader_glsl_binop,
11276 /* WINED3DSIH_AND */ shader_glsl_binop,
11277 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11278 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11279 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11280 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11281 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11282 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11283 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11284 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11285 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11286 /* WINED3DSIH_BEM */ shader_glsl_bem,
11287 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11288 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11289 /* WINED3DSIH_BREAK */ shader_glsl_break,
11290 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11291 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11292 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11293 /* WINED3DSIH_CALL */ shader_glsl_call,
11294 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11295 /* WINED3DSIH_CASE */ shader_glsl_case,
11296 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11297 /* WINED3DSIH_CND */ shader_glsl_cnd,
11298 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11299 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11300 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11301 /* WINED3DSIH_CRS */ shader_glsl_cross,
11302 /* WINED3DSIH_CUT */ shader_glsl_cut,
11303 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11304 /* WINED3DSIH_DCL */ shader_glsl_nop,
11305 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11306 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11307 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11308 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11309 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11310 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11311 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11312 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11313 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11314 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11315 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11316 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11317 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11318 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11319 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11320 /* WINED3DSIH_DCL_INPUT_PS_SGV */ shader_glsl_nop,
11321 /* WINED3DSIH_DCL_INPUT_PS_SIV */ shader_glsl_nop,
11322 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11323 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11324 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11325 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11326 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11327 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11328 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11329 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11330 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11331 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11332 /* WINED3DSIH_DCL_STREAM */ NULL,
11333 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11334 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11335 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11336 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11337 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11338 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11339 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11340 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11341 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11342 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11343 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11344 /* WINED3DSIH_DEF */ shader_glsl_nop,
11345 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11346 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11347 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11348 /* WINED3DSIH_DIV */ shader_glsl_binop,
11349 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11350 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11351 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11352 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11353 /* WINED3DSIH_DST */ shader_glsl_dst,
11354 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11355 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11356 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11357 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11358 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11359 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11360 /* WINED3DSIH_ELSE */ shader_glsl_else,
11361 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11362 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11363 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11364 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11365 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11366 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11367 /* WINED3DSIH_EQ */ shader_glsl_relop,
11368 /* WINED3DSIH_EVAL_CENTROID */ NULL,
11369 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ shader_glsl_interpolate,
11370 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11371 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11372 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11373 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11374 /* WINED3DSIH_FCALL */ NULL,
11375 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11376 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11377 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11378 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11379 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11380 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11381 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11382 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11383 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11384 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11385 /* WINED3DSIH_GE */ shader_glsl_relop,
11386 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11387 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11388 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11389 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11390 /* WINED3DSIH_IADD */ shader_glsl_binop,
11391 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11392 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11393 /* WINED3DSIH_IF */ shader_glsl_if,
11394 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11395 /* WINED3DSIH_IGE */ shader_glsl_relop,
11396 /* WINED3DSIH_ILT */ shader_glsl_relop,
11397 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11398 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11399 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11400 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11401 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11402 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11403 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11404 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11405 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11406 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11407 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11408 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11409 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11410 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11411 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11412 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11413 /* WINED3DSIH_INE */ shader_glsl_relop,
11414 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11415 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11416 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11417 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11418 /* WINED3DSIH_LABEL */ shader_glsl_label,
11419 /* WINED3DSIH_LD */ shader_glsl_ld,
11420 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11421 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11422 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11423 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11424 /* WINED3DSIH_LIT */ shader_glsl_lit,
11425 /* WINED3DSIH_LOD */ NULL,
11426 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11427 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11428 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11429 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11430 /* WINED3DSIH_LT */ shader_glsl_relop,
11431 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11432 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11433 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11434 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11435 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11436 /* WINED3DSIH_MAD */ shader_glsl_mad,
11437 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11438 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11439 /* WINED3DSIH_MOV */ shader_glsl_mov,
11440 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11441 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11442 /* WINED3DSIH_MUL */ shader_glsl_binop,
11443 /* WINED3DSIH_NE */ shader_glsl_relop,
11444 /* WINED3DSIH_NOP */ shader_glsl_nop,
11445 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11446 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11447 /* WINED3DSIH_OR */ shader_glsl_binop,
11448 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11449 /* WINED3DSIH_POW */ shader_glsl_pow,
11450 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11451 /* WINED3DSIH_REP */ shader_glsl_rep,
11452 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11453 /* WINED3DSIH_RET */ shader_glsl_ret,
11454 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11455 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11456 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11457 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11458 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11459 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11460 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11461 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11462 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11463 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11464 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11465 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11466 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11467 /* WINED3DSIH_SAMPLE_POS */ NULL,
11468 /* WINED3DSIH_SETP */ NULL,
11469 /* WINED3DSIH_SGE */ shader_glsl_compare,
11470 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11471 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11472 /* WINED3DSIH_SLT */ shader_glsl_compare,
11473 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11474 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11475 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11476 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11477 /* WINED3DSIH_SUB */ shader_glsl_binop,
11478 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11479 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11480 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11481 /* WINED3DSIH_TEX */ shader_glsl_tex,
11482 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11483 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11484 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11485 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11486 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11487 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11488 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11489 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11490 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11491 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11492 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11493 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11494 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11495 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11496 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11497 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11498 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11499 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11500 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11501 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11502 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11503 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11504 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11505 /* WINED3DSIH_UGE */ shader_glsl_relop,
11506 /* WINED3DSIH_ULT */ shader_glsl_relop,
11507 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11508 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11509 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11510 /* WINED3DSIH_USHR */ shader_glsl_binop,
11511 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11512 /* WINED3DSIH_XOR */ shader_glsl_binop,
11515 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11516 SHADER_HANDLER hw_fct;
11518 /* Select handler */
11519 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11521 /* Unhandled opcode */
11522 if (!hw_fct)
11524 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11525 return;
11527 hw_fct(ins);
11529 shader_glsl_add_instruction_modifiers(ins);
11532 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11534 struct shader_glsl_priv *priv = shader_priv;
11536 return priv->ffp_proj_control;
11539 static uint64_t shader_glsl_shader_compile(struct wined3d_context *context, const struct wined3d_shader_desc *shader_desc,
11540 enum wined3d_shader_type shader_type)
11542 ERR("Not implemented.\n");
11543 return 0;
11546 const struct wined3d_shader_backend_ops glsl_shader_backend =
11548 shader_glsl_handle_instruction,
11549 shader_glsl_precompile,
11550 shader_glsl_select,
11551 shader_glsl_select_compute,
11552 shader_glsl_disable,
11553 shader_glsl_update_float_vertex_constants,
11554 shader_glsl_update_float_pixel_constants,
11555 shader_glsl_load_constants,
11556 shader_glsl_destroy,
11557 shader_glsl_alloc,
11558 shader_glsl_free,
11559 shader_glsl_allocate_context_data,
11560 shader_glsl_free_context_data,
11561 shader_glsl_init_context_state,
11562 shader_glsl_get_caps,
11563 shader_glsl_color_fixup_supported,
11564 shader_glsl_has_ffp_proj_control,
11565 shader_glsl_shader_compile,
11568 static void glsl_vertex_pipe_vp_enable(const struct wined3d_context *context, BOOL enable) {}
11570 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_adapter *adapter, struct wined3d_vertex_caps *caps)
11572 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl_const(adapter)->gl_info;
11574 caps->xyzrhw = TRUE;
11575 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11576 caps->ffp_generic_attributes = TRUE;
11577 caps->max_active_lights = WINED3D_MAX_ACTIVE_LIGHTS;
11578 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11579 caps->max_vertex_blend_matrix_index = 0;
11580 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11581 | WINED3DVTXPCAPS_MATERIALSOURCE7
11582 | WINED3DVTXPCAPS_VERTEXFOG
11583 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11584 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11585 | WINED3DVTXPCAPS_LOCALVIEWER
11586 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11587 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11588 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11589 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11592 static unsigned int glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_adapter *adapter)
11594 if (wined3d_adapter_gl_const(adapter)->gl_info.supported[WINED3D_GL_LEGACY_CONTEXT])
11595 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11596 return 0;
11599 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11601 struct shader_glsl_priv *priv;
11603 if (shader_backend == &glsl_shader_backend)
11605 priv = shader_priv;
11606 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11607 return priv;
11610 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11612 return NULL;
11615 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *param)
11617 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11618 struct glsl_ffp_vertex_shader, desc.entry);
11619 struct glsl_shader_prog_link *program, *program2;
11620 struct glsl_ffp_destroy_ctx *ctx = param;
11621 const struct wined3d_gl_info *gl_info;
11623 gl_info = ctx->context_gl->gl_info;
11624 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11625 struct glsl_shader_prog_link, vs.shader_entry)
11627 delete_glsl_program_entry(ctx->priv, gl_info, program);
11629 GL_EXTCALL(glDeleteShader(shader->id));
11630 heap_free(shader);
11633 /* Context activation is done by the caller. */
11634 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device, struct wined3d_context *context)
11636 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11637 struct shader_glsl_priv *priv = device->vertex_priv;
11638 struct glsl_ffp_destroy_ctx ctx;
11640 ctx.priv = priv;
11641 ctx.context_gl = context_gl;
11642 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11645 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11646 const struct wined3d_state *state, DWORD state_id) {}
11648 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11649 const struct wined3d_state *state, DWORD state_id)
11651 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11654 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11655 const struct wined3d_state *state, DWORD state_id)
11657 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11658 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11659 BOOL specular = !!(context->stream_info.use_map & (1u << WINED3D_FFP_SPECULAR));
11660 BOOL diffuse = !!(context->stream_info.use_map & (1u << WINED3D_FFP_DIFFUSE));
11661 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11662 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11663 BOOL transformed = context->stream_info.position_transformed;
11664 BOOL wasrhw = context->last_was_rhw;
11665 unsigned int i;
11667 context->last_was_rhw = transformed;
11669 /* If the vertex declaration contains a transformed position attribute,
11670 * the draw uses the fixed function vertex pipeline regardless of any
11671 * vertex shader set by the application. */
11672 if (transformed != wasrhw
11673 || context->stream_info.swizzle_map != context->last_swizzle_map)
11674 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11676 context->last_swizzle_map = context->stream_info.swizzle_map;
11678 if (!use_vs(state))
11680 if (context->last_was_vshader)
11682 if (legacy_clip_planes)
11683 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11684 clipplane(context, state, STATE_CLIPPLANE(i));
11685 else
11686 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11689 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11691 /* Because of settings->texcoords, we have to regenerate the vertex
11692 * shader on a vdecl change if there aren't enough varyings to just
11693 * always output all the texture coordinates.
11695 * Likewise, we have to invalidate the shader when using per-vertex
11696 * colours and diffuse/specular attribute presence changes, or when
11697 * normal presence changes. */
11698 if (!shader_glsl_full_ffp_varyings(gl_info) || (state->render_states[WINED3D_RS_COLORVERTEX]
11699 && (diffuse != context->last_was_diffuse || specular != context->last_was_specular))
11700 || normal != context->last_was_normal)
11701 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11703 if (use_ps(state)
11704 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11705 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11706 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11708 else
11710 if (!context->last_was_vshader)
11712 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11713 if (legacy_clip_planes)
11714 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11715 clipplane(context, state, STATE_CLIPPLANE(i));
11716 else
11717 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11721 context->last_was_vshader = use_vs(state);
11722 context->last_was_diffuse = diffuse;
11723 context->last_was_specular = specular;
11724 context->last_was_normal = normal;
11727 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11728 const struct wined3d_state *state, DWORD state_id)
11730 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11731 /* Different vertex shaders potentially require a different vertex attributes setup. */
11732 if (!isStateDirty(context, STATE_VDECL))
11733 context_apply_state(context, state, STATE_VDECL);
11736 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11737 const struct wined3d_state *state, DWORD state_id)
11739 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11740 * winding) are defined in Hull Shaders, while in GLSL those are
11741 * specified in Tessellation Evaluation Shaders. */
11742 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11744 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11745 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11748 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11749 const struct wined3d_state *state, DWORD state_id)
11751 struct glsl_context_data *ctx_data = context->shader_backend_data;
11752 BOOL rasterization_disabled;
11754 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11755 if (ctx_data->rasterization_disabled != rasterization_disabled)
11756 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11757 ctx_data->rasterization_disabled = rasterization_disabled;
11759 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11760 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11761 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11762 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11763 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11766 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11767 const struct wined3d_state *state, DWORD state_id)
11769 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11770 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11771 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11772 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11773 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11774 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11775 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11778 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11779 const struct wined3d_state *state, DWORD state_id)
11781 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11784 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11785 const struct wined3d_state *state, DWORD state_id)
11787 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11790 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11792 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11793 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11794 unsigned int k;
11796 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11797 | WINED3D_SHADER_CONST_FFP_LIGHTS
11798 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11800 if (needs_legacy_glsl_syntax(gl_info))
11802 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11804 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11805 clipplane(context, state, STATE_CLIPPLANE(k));
11808 else
11810 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11814 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11815 const struct wined3d_state *state, DWORD state_id)
11817 /* Table fog behavior depends on the projection matrix. */
11818 if (state->render_states[WINED3D_RS_FOGENABLE]
11819 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11820 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11821 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11824 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11825 const struct wined3d_state *state, DWORD state_id)
11827 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11828 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11829 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11830 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11831 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11832 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11835 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11836 const struct wined3d_state *state, DWORD state_id)
11838 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11841 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11842 const struct wined3d_state *state, DWORD state_id)
11844 DWORD sampler = state_id - STATE_SAMPLER(0);
11845 const struct wined3d_texture *texture = state->textures[sampler];
11846 BOOL np2;
11848 if (!texture)
11849 return;
11851 if (sampler >= WINED3D_MAX_FFP_TEXTURES)
11852 return;
11854 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11855 || context->lastWasPow2Texture & (1u << sampler))
11857 if (np2)
11858 context->lastWasPow2Texture |= 1u << sampler;
11859 else
11860 context->lastWasPow2Texture &= ~(1u << sampler);
11862 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11866 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11867 const struct wined3d_state *state, DWORD state_id)
11869 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11872 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11873 const struct wined3d_state *state, DWORD state_id)
11875 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11878 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11879 const struct wined3d_state *state, DWORD state_id)
11881 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11884 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11885 const struct wined3d_state *state, DWORD state_id)
11887 if (!use_vs(state))
11888 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11891 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11892 const struct wined3d_state *state, DWORD state_id)
11894 static unsigned int once;
11896 if (state->primitive_type == WINED3D_PT_POINTLIST
11897 && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11898 FIXME("Non-point sprite points not supported in core profile.\n");
11901 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11902 const struct wined3d_state *state, DWORD state_id)
11904 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11907 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11908 const struct wined3d_state *state, DWORD state_id)
11910 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11911 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11912 UINT index = state_id - STATE_CLIPPLANE(0);
11914 if (index >= gl_info->limits.user_clip_distances)
11915 return;
11917 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11920 static const struct wined3d_state_entry_template glsl_vertex_pipe_vp_states[] =
11922 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11923 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11924 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11925 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11926 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11927 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11928 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11929 /* Clip planes */
11930 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11931 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11932 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11933 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11934 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11935 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11936 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11937 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11938 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11939 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11940 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11941 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11942 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11943 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11944 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11945 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11946 /* Lights */
11947 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11948 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11949 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11950 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11951 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11952 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11953 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11954 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11955 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11956 /* Viewport */
11957 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11958 /* Transform states */
11959 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11960 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11961 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11962 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11963 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11964 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11965 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11966 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11967 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11968 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11969 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11970 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11971 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11972 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11973 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11974 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11975 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11976 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11977 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11978 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11979 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11980 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11981 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11982 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11983 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11984 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11985 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11986 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11987 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11988 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11989 /* Fog */
11990 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11991 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11992 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11993 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11994 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11995 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11996 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11997 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11998 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11999 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12000 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12001 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12002 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12003 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12004 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12005 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
12006 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
12007 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
12008 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
12009 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
12010 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
12011 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
12012 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12013 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12014 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
12015 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
12016 /* NP2 texture matrix fixups. They are not needed if
12017 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
12018 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
12019 * matrix. */
12020 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12021 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12022 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12023 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12024 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12025 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12026 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12027 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12028 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12029 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12030 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12031 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12032 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12033 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12034 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12035 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12036 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12037 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12038 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12039 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12040 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12041 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12042 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12043 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12044 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12045 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
12046 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
12047 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
12050 /* TODO:
12051 * - Implement vertex tweening. */
12052 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
12054 glsl_vertex_pipe_vp_enable,
12055 glsl_vertex_pipe_vp_get_caps,
12056 glsl_vertex_pipe_vp_get_emul_mask,
12057 glsl_vertex_pipe_vp_alloc,
12058 glsl_vertex_pipe_vp_free,
12059 glsl_vertex_pipe_vp_states,
12062 static void glsl_fragment_pipe_enable(const struct wined3d_context *context, BOOL enable)
12064 /* Nothing to do. */
12067 static void glsl_fragment_pipe_get_caps(const struct wined3d_adapter *adapter, struct fragment_caps *caps)
12069 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl_const(adapter)->gl_info;
12071 memset(caps, 0, sizeof(*caps));
12072 caps->proj_control = true;
12073 caps->srgb_write = true;
12074 caps->color_key = true;
12075 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12076 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12077 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12078 | WINED3DTEXOPCAPS_SELECTARG1
12079 | WINED3DTEXOPCAPS_SELECTARG2
12080 | WINED3DTEXOPCAPS_MODULATE4X
12081 | WINED3DTEXOPCAPS_MODULATE2X
12082 | WINED3DTEXOPCAPS_MODULATE
12083 | WINED3DTEXOPCAPS_ADDSIGNED2X
12084 | WINED3DTEXOPCAPS_ADDSIGNED
12085 | WINED3DTEXOPCAPS_ADD
12086 | WINED3DTEXOPCAPS_SUBTRACT
12087 | WINED3DTEXOPCAPS_ADDSMOOTH
12088 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12089 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12090 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12091 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12092 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12093 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12094 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12095 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12096 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12097 | WINED3DTEXOPCAPS_DOTPRODUCT3
12098 | WINED3DTEXOPCAPS_MULTIPLYADD
12099 | WINED3DTEXOPCAPS_LERP
12100 | WINED3DTEXOPCAPS_BUMPENVMAP
12101 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12102 caps->max_blend_stages = WINED3D_MAX_FFP_TEXTURES;
12103 caps->max_textures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], WINED3D_MAX_FFP_TEXTURES);
12106 static unsigned int glsl_fragment_pipe_get_emul_mask(const struct wined3d_adapter *adapter)
12108 if (wined3d_adapter_gl_const(adapter)->gl_info.supported[WINED3D_GL_LEGACY_CONTEXT])
12109 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12110 return 0;
12113 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12115 struct shader_glsl_priv *priv;
12117 if (shader_backend == &glsl_shader_backend)
12119 priv = shader_priv;
12120 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12121 return priv;
12124 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12126 return NULL;
12129 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *param)
12131 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12132 struct glsl_ffp_fragment_shader, entry.entry);
12133 struct glsl_shader_prog_link *program, *program2;
12134 struct glsl_ffp_destroy_ctx *ctx = param;
12135 const struct wined3d_gl_info *gl_info;
12137 gl_info = ctx->context_gl->gl_info;
12138 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12139 struct glsl_shader_prog_link, ps.shader_entry)
12141 delete_glsl_program_entry(ctx->priv, gl_info, program);
12143 GL_EXTCALL(glDeleteShader(shader->id));
12144 heap_free(shader);
12147 /* Context activation is done by the caller. */
12148 static void glsl_fragment_pipe_free(struct wined3d_device *device, struct wined3d_context *context)
12150 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12151 struct shader_glsl_priv *priv = device->fragment_priv;
12152 struct glsl_ffp_destroy_ctx ctx;
12154 ctx.priv = priv;
12155 ctx.context_gl = context_gl;
12156 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12159 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12160 const struct wined3d_state *state, DWORD state_id)
12162 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12165 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12166 const struct wined3d_state *state, DWORD state_id)
12168 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12171 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12172 const struct wined3d_state *state, DWORD state_id)
12174 BOOL use_vshader = use_vs(state);
12175 enum fogsource new_source;
12176 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12177 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12179 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12181 if (!state->render_states[WINED3D_RS_FOGENABLE])
12182 return;
12184 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12186 if (use_vshader)
12187 new_source = FOGSOURCE_VS;
12188 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12189 new_source = FOGSOURCE_COORD;
12190 else
12191 new_source = FOGSOURCE_FFP;
12193 else
12195 new_source = FOGSOURCE_FFP;
12198 if (new_source != context->fog_source || fogstart == fogend)
12200 context->fog_source = new_source;
12201 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12205 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12206 const struct wined3d_state *state, DWORD state_id)
12208 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12210 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12211 if (!shader_glsl_full_ffp_varyings(gl_info))
12212 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12214 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12215 glsl_fragment_pipe_fog(context, state, state_id);
12218 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12219 const struct wined3d_state *state, DWORD state_id)
12221 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12223 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12224 if (!shader_glsl_full_ffp_varyings(gl_info))
12225 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12228 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12229 const struct wined3d_state *state, DWORD state_id)
12231 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12234 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12235 const struct wined3d_state *state, DWORD state_id)
12237 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12240 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12241 const struct wined3d_state *state, DWORD state_id)
12243 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12244 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12245 float ref = wined3d_alpha_ref(state);
12247 if (func)
12249 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12250 checkGLcall("glAlphaFunc");
12254 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12255 const struct wined3d_state *state, DWORD state_id)
12257 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12260 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12261 const struct wined3d_state *state, DWORD state_id)
12263 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12265 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12267 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12268 checkGLcall("glEnable(GL_ALPHA_TEST)");
12270 else
12272 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12273 checkGLcall("glDisable(GL_ALPHA_TEST)");
12277 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12278 const struct wined3d_state *state, DWORD state_id)
12280 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12283 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12284 const struct wined3d_state *state, DWORD state_id)
12286 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12289 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12290 const struct wined3d_state *state, DWORD state_id)
12292 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12295 static const struct wined3d_state_entry_template glsl_fragment_pipe_state_template[] =
12297 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12298 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12299 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12300 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12301 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12302 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12303 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12304 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12305 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12306 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12307 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12308 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12309 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12310 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12311 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12312 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12313 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12314 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12315 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12316 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12317 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12318 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12319 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12320 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12321 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12322 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12323 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12324 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12325 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12326 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12327 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12328 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12329 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12330 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12331 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12332 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12333 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12334 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12335 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12336 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12337 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12338 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12339 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12340 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12341 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12342 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12343 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12344 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12345 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12346 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12347 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12348 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12349 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12350 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12351 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12352 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12353 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12354 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12355 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12356 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12357 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12358 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12359 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12360 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12361 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12362 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12363 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12364 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12365 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12366 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12367 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12368 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12369 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12370 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12371 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12372 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12373 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12374 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12375 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12376 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12377 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12378 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12379 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12380 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12381 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12382 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12383 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12384 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12385 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12386 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12387 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12388 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12389 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12390 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12391 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12392 {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 },
12393 {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 },
12394 {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 },
12395 {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 },
12396 {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 },
12397 {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 },
12398 {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 },
12399 {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 },
12400 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12401 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12402 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12403 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12404 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12405 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12406 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12407 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12408 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12409 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12410 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12411 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12412 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12415 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12417 return TRUE;
12420 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12424 const struct wined3d_fragment_pipe_ops glsl_fragment_pipe =
12426 glsl_fragment_pipe_enable,
12427 glsl_fragment_pipe_get_caps,
12428 glsl_fragment_pipe_get_emul_mask,
12429 glsl_fragment_pipe_alloc,
12430 glsl_fragment_pipe_free,
12431 glsl_fragment_pipe_alloc_context_data,
12432 glsl_fragment_pipe_free_context_data,
12433 shader_glsl_color_fixup_supported,
12434 glsl_fragment_pipe_state_template,
12437 struct glsl_blitter_args
12439 GLenum texture_type;
12440 struct color_fixup_desc fixup;
12441 unsigned short use_colour_key;
12444 struct glsl_blitter_program
12446 struct wine_rb_entry entry;
12447 struct glsl_blitter_args args;
12448 GLuint id;
12451 struct wined3d_glsl_blitter
12453 struct wined3d_blitter blitter;
12454 struct wined3d_string_buffer_list string_buffers;
12455 struct wine_rb_tree programs;
12456 GLuint palette_texture;
12459 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12461 const struct glsl_blitter_args *a = key;
12462 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12464 return memcmp(a, b, sizeof(*a));
12467 /* Context activation is done by the caller. */
12468 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12470 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12471 struct wined3d_context_gl *context_gl = ctx;
12472 const struct wined3d_gl_info *gl_info;
12474 gl_info = context_gl->gl_info;
12475 GL_EXTCALL(glDeleteProgram(program->id));
12476 checkGLcall("glDeleteProgram()");
12477 heap_free(program);
12480 /* Context activation is done by the caller. */
12481 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12483 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12484 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12485 struct wined3d_glsl_blitter *glsl_blitter;
12486 struct wined3d_blitter *next;
12488 if ((next = blitter->next))
12489 next->ops->blitter_destroy(next, context);
12491 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12493 if (glsl_blitter->palette_texture)
12494 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12496 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context_gl);
12497 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12499 heap_free(glsl_blitter);
12502 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12503 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12504 const char *output, const char *tex_type, const char *swizzle)
12506 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12507 shader_addline(buffer, "\nvoid main()\n{\n");
12508 /* The alpha-component contains the palette index. */
12509 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12510 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12511 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12512 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12513 * the middle. */
12514 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12515 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12516 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12517 if (args->use_colour_key)
12518 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12519 shader_addline(buffer, "}\n");
12522 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12523 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12524 const char *tex_type)
12526 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12527 char chroma, luminance;
12528 const char *tex;
12530 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12531 * macropixel, giving effectively 16 bits per pixel. The color consists of
12532 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12533 * luminance values, one for each single pixel it contains, and one U and
12534 * one V value shared between both pixels.
12536 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12537 * component contains the luminance and alpha the chroma. With UYVY it is
12538 * vice versa. Thus take the format into account when generating the read
12539 * swizzles
12541 * Reading the Y value is straightforward - just sample the texture. The
12542 * hardware takes care of filtering in the horizontal and vertical
12543 * direction.
12545 * Reading the U and V values is harder. We have to avoid filtering
12546 * horizontally, because that would mix the U and V values of one pixel or
12547 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12548 * get an unfiltered read, regardless of the filtering setting. Vertical
12549 * filtering works automatically though - the U and V values of two rows
12550 * are mixed nicely.
12552 * Apart of avoiding filtering issues, the code has to know which value it
12553 * just read, and where it can find the other one. To determine this, it
12554 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12555 * accordingly.
12557 * Handling horizontal filtering of U and V values requires reading a 2nd
12558 * pair of pixels, extracting U and V and mixing them. This is not
12559 * implemented yet.
12561 * An alternative implementation idea is to load the texture as A8R8G8B8
12562 * texture, with width / 2. This way one read gives all 3 values, finding
12563 * U and V is easy in an unfiltered situation. Finding the luminance on
12564 * the other hand requires finding out if it is an odd or even pixel. The
12565 * real drawback of this approach is filtering. This would have to be
12566 * emulated completely in the shader, reading up two 2 packed pixels in up
12567 * to 2 rows and interpolating both horizontally and vertically. Beyond
12568 * that it would require adjustments to the texture handling code to deal
12569 * with the width scaling. */
12571 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12573 chroma = 'x';
12574 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12576 else
12578 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12579 luminance = 'x';
12582 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12584 /* First we have to read the chroma values. This means we need at least
12585 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12586 * unmodified chroma, we have to rid ourselves of the filtering when we
12587 * sample the texture. */
12588 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12589 /* We must not allow filtering between pixel x and x+1, this would mix U
12590 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12591 * center is at 0.5, so add 0.5. */
12592 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12593 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12595 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12596 * and 0.75 for the even and odd pixels respectively. */
12597 /* Put the value into either of the chroma values. */
12598 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12599 shader_addline(buffer, " if (even)\n");
12600 shader_addline(buffer, " chroma.y = luminance;\n");
12601 shader_addline(buffer, " else\n");
12602 shader_addline(buffer, " chroma.x = luminance;\n");
12604 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12605 * current one. Otherwise, sample the left pixel. */
12606 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12607 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12609 /* Put the value into the other chroma. */
12610 shader_addline(buffer, " if (even)\n");
12611 shader_addline(buffer, " chroma.x = luminance;\n");
12612 shader_addline(buffer, " else\n");
12613 shader_addline(buffer, " chroma.y = luminance;\n");
12615 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12616 * the current one and lerp the two U and V values. */
12618 /* This gives the correctly filtered luminance value. */
12619 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12622 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12623 const struct wined3d_gl_info *gl_info, const char *tex_type)
12625 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12626 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12628 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12629 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12630 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12631 * have only half the pitch of the luminance plane, the packing into the
12632 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12633 * luminance data it looks approximately like this:
12635 * +----------------------------------+----
12636 * | |
12637 * | |
12638 * | |
12639 * | |
12640 * | | 2
12641 * | LUMINANCE | -
12642 * | | 3
12643 * | |
12644 * | |
12645 * | |
12646 * | |
12647 * +----------------+-----------------+----
12648 * | | |
12649 * | V even rows | V odd rows |
12650 * | | | 1
12651 * +----------------+------------------ -
12652 * | | | 3
12653 * | U even rows | U odd rows |
12654 * | | |
12655 * +----------------+-----------------+----
12656 * | | |
12657 * | 0.5 | 0.5 |
12659 * So it appears as if there are 4 chroma images, but in fact the odd rows
12660 * in the chroma images are in the same row as the even ones. So it is
12661 * kinda tricky to read. */
12663 /* First sample the chroma values. */
12664 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12665 /* The chroma planes have only half the width. */
12666 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12668 /* The first value is between 2/3 and 5/6 of the texture's height, so
12669 * scale+bias the coordinate. Also read the right side of the image when
12670 * reading odd lines.
12672 * Don't forget to clamp the y values in into the range, otherwise we'll
12673 * get filtering bleeding. */
12675 /* Read odd lines from the right side (add 0.5 to the x coordinate). Keep
12676 * in mind that each line of the chroma plane corresponds to 2 lines of the
12677 * resulting image. */
12678 shader_addline(buffer, " if (fract(texcoord.y * size.y * 0.25) >= 0.5)\n");
12679 shader_addline(buffer, " texcoord.x += 0.5;\n");
12681 /* Clamp, keep the half pixel origin in mind. */
12682 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12683 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12685 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12687 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12688 * 6/6th No need to clamp because we're just reusing the already clamped
12689 * value from above. */
12690 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12691 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12693 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12694 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12695 * values from bleeding into the sampled luminance values due to
12696 * filtering. */
12697 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12698 /* Multiply the y coordinate by 2/3 and clamp it. */
12699 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12700 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12703 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12704 const struct wined3d_gl_info *gl_info, const char *tex_type)
12706 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12707 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12709 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12710 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12711 * effective bitdepth is 12 bits per pixel. If the whole texture is
12712 * interpreted as luminance data it looks approximately like this:
12714 * +----------------------------------+----
12715 * | |
12716 * | |
12717 * | |
12718 * | |
12719 * | | 2
12720 * | LUMINANCE | -
12721 * | | 3
12722 * | |
12723 * | |
12724 * | |
12725 * | |
12726 * +----------------------------------+----
12727 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12728 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12729 * | | 1
12730 * | | -
12731 * | | 3
12732 * | |
12733 * | |
12734 * +----------------------------------+---- */
12736 /* First sample the chroma values. */
12737 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12738 /* We only have half the number of chroma pixels. */
12739 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12740 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12742 /* We must not allow filtering horizontally, this would mix U and V.
12743 * Vertical filtering is ok. However, bear in mind that the pixel center
12744 * is at 0.5, so add 0.5. */
12746 /* Convert to non-normalised coordinates so we can find the individual
12747 * pixel. */
12748 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12749 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12750 * 0.5 to hit the center of the pixel. Then convert back to normalised
12751 * coordinates. */
12752 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12753 /* Clamp, keep the half pixel origin in mind. */
12754 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12756 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12757 /* Add 1.0 / size.x to sample the adjacent texel. */
12758 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12759 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12761 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12762 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12763 * values from bleeding into the sampled luminance values due to
12764 * filtering. */
12765 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12766 /* Multiply the y coordinate by 2/3 and clamp it. */
12767 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12768 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12771 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12772 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12773 const char *output, const char *tex_type, const char *swizzle)
12775 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12777 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12778 shader_addline(buffer, "float luminance;\n");
12779 shader_addline(buffer, "vec2 texcoord;\n");
12780 shader_addline(buffer, "vec2 chroma;\n");
12781 shader_addline(buffer, "uniform vec2 size;\n");
12783 shader_addline(buffer, "\nvoid main()\n{\n");
12785 switch (complex_fixup)
12787 case COMPLEX_FIXUP_UYVY:
12788 case COMPLEX_FIXUP_YUY2:
12789 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12790 break;
12792 case COMPLEX_FIXUP_YV12:
12793 gen_yv12_read(buffer, gl_info, tex_type);
12794 break;
12796 case COMPLEX_FIXUP_NV12:
12797 gen_nv12_read(buffer, gl_info, tex_type);
12798 break;
12800 case COMPLEX_FIXUP_YUV:
12801 /* With APPLE_rgb_422, things are much simpler. The only thing we
12802 * have to do here is Y'CbCr to RGB conversion. */
12803 shader_addline(buffer, " vec3 yuv = vec3(texture%s(sampler, out_texcoord.xy));\n",
12804 needs_legacy_glsl_syntax(gl_info) ? tex_type : "");
12805 shader_addline(buffer, " luminance = yuv.y;\n");
12806 shader_addline(buffer, " chroma = yuv.xz;\n");
12807 break;
12809 default:
12810 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12811 string_buffer_free(buffer);
12812 return;
12815 /* Calculate the final result. Formula is taken from
12816 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12817 * ranges from -0.5 to 0.5. */
12818 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12820 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12821 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12822 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12823 if (args->use_colour_key)
12824 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12826 shader_addline(buffer, "}\n");
12829 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12830 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12831 const char *output, const char *tex_type, const char *swizzle)
12833 shader_addline(buffer, "\nvoid main()\n{\n");
12834 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12835 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12836 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12837 if (args->use_colour_key)
12838 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12839 shader_addline(buffer, "}\n");
12842 /* Context activation is done by the caller. */
12843 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12844 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12846 static const struct
12848 GLenum texture_target;
12849 const char texture_type[7];
12850 const char texcoord_swizzle[4];
12852 texture_data[] =
12854 {GL_TEXTURE_2D, "2D", "xy"},
12855 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12856 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12858 static const char vshader_main[] =
12859 "\n"
12860 "void main()\n"
12861 "{\n"
12862 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12863 " out_texcoord = texcoord;\n"
12864 "}\n";
12865 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12866 struct wined3d_string_buffer *buffer, *output;
12867 GLuint program, vshader_id, fshader_id;
12868 const char *tex_type = NULL, *swizzle = NULL, *ptr;
12869 unsigned int i;
12870 GLint loc;
12872 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12874 if (args->texture_type == texture_data[i].texture_target)
12876 tex_type = texture_data[i].texture_type;
12877 swizzle = texture_data[i].texcoord_swizzle;
12878 break;
12881 if (i == ARRAY_SIZE(texture_data))
12883 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12884 return 0;
12887 program = GL_EXTCALL(glCreateProgram());
12889 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12891 buffer = string_buffer_get(&blitter->string_buffers);
12892 shader_glsl_add_version_declaration(buffer, gl_info);
12893 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12894 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12895 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12896 shader_addline(buffer, vshader_main);
12898 ptr = buffer->buffer;
12899 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12900 GL_EXTCALL(glAttachShader(program, vshader_id));
12901 GL_EXTCALL(glDeleteShader(vshader_id));
12903 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12905 string_buffer_clear(buffer);
12906 shader_glsl_add_version_declaration(buffer, gl_info);
12907 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12908 if (args->use_colour_key)
12910 shader_addline(buffer, "uniform struct\n{\n");
12911 shader_addline(buffer, " vec4 low, high;\n");
12912 shader_addline(buffer, "} colour_key;\n");
12914 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12915 /* TODO: Declare the out variable with the correct type (and put it in the
12916 * blitter args). */
12917 if (!use_legacy_fragment_output(gl_info))
12918 shader_addline(buffer, "out vec4 ps_out[1];\n");
12920 output = string_buffer_get(&blitter->string_buffers);
12921 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12923 switch (complex_fixup)
12925 case COMPLEX_FIXUP_P8:
12926 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12927 break;
12928 case COMPLEX_FIXUP_YUY2:
12929 case COMPLEX_FIXUP_UYVY:
12930 case COMPLEX_FIXUP_YV12:
12931 case COMPLEX_FIXUP_NV12:
12932 case COMPLEX_FIXUP_YUV:
12933 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12934 break;
12935 case COMPLEX_FIXUP_NONE:
12936 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12939 string_buffer_release(&blitter->string_buffers, output);
12941 ptr = buffer->buffer;
12942 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12943 string_buffer_release(&blitter->string_buffers, buffer);
12944 GL_EXTCALL(glAttachShader(program, fshader_id));
12945 GL_EXTCALL(glDeleteShader(fshader_id));
12947 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12948 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12950 if (!use_legacy_fragment_output(gl_info))
12951 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12953 GL_EXTCALL(glCompileShader(vshader_id));
12954 print_glsl_info_log(gl_info, vshader_id, FALSE);
12955 GL_EXTCALL(glCompileShader(fshader_id));
12956 print_glsl_info_log(gl_info, fshader_id, FALSE);
12957 GL_EXTCALL(glLinkProgram(program));
12958 shader_glsl_validate_link(gl_info, program);
12960 GL_EXTCALL(glUseProgram(program));
12961 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12962 GL_EXTCALL(glUniform1i(loc, 0));
12963 if (complex_fixup == COMPLEX_FIXUP_P8)
12965 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12966 GL_EXTCALL(glUniform1i(loc, 1));
12969 return program;
12972 /* Context activation is done by the caller. */
12973 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12974 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl)
12976 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12977 const struct wined3d_palette *palette;
12979 palette = texture_gl->t.swapchain ? texture_gl->t.swapchain->palette : NULL;
12981 if (!blitter->palette_texture)
12982 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12984 wined3d_context_gl_active_texture(context_gl, gl_info, 1);
12985 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12986 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12987 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12988 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12990 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
12991 checkGLcall("glBindBuffer");
12993 if (palette)
12995 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12996 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12998 else
13000 static const DWORD black;
13002 FIXME("P8 texture loaded without a palette.\n");
13003 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
13004 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
13007 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
13010 /* Context activation is done by the caller. */
13011 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
13012 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl, BOOL use_colour_key)
13014 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13015 struct glsl_blitter_program *program;
13016 struct glsl_blitter_args args;
13017 struct wine_rb_entry *entry;
13019 memset(&args, 0, sizeof(args));
13020 args.texture_type = texture_gl->target;
13021 args.fixup = texture_gl->t.resource.format->color_fixup;
13022 args.use_colour_key = use_colour_key;
13024 if ((entry = wine_rb_get(&blitter->programs, &args)))
13025 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
13027 if (!(program = heap_alloc(sizeof(*program))))
13029 ERR("Failed to allocate blitter program memory.\n");
13030 return NULL;
13033 program->args = args;
13034 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
13036 WARN("Failed to generate blitter program.\n");
13037 heap_free(program);
13038 return NULL;
13041 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
13043 ERR("Failed to store blitter program.\n");
13044 GL_EXTCALL(glDeleteProgram(program->id));
13045 heap_free(program);
13046 return NULL;
13049 return program;
13052 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
13053 const struct wined3d_texture_gl *src_texture, DWORD src_location,
13054 const struct wined3d_texture_gl *dst_texture, DWORD dst_location)
13056 const struct wined3d_resource *src_resource = &src_texture->t.resource;
13057 const struct wined3d_resource *dst_resource = &dst_texture->t.resource;
13058 const struct wined3d_format *src_format = src_resource->format;
13059 const struct wined3d_format *dst_format = dst_resource->format;
13060 bool src_ds, dst_ds;
13061 BOOL decompress;
13063 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13065 src_ds = src_format->depth_size || src_format->stencil_size;
13066 dst_ds = dst_format->depth_size || dst_format->stencil_size;
13067 /* We could in principle support raw depth/stencil <-> colour blits as
13068 * well in some cases, but note that typeless formats like e.g.
13069 * R16_TYPELESS may use a normalised GL format for depth/stencil
13070 * resources, and a floating-point format for colour resources, */
13071 if (src_ds && dst_ds)
13072 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13073 else if (!src_ds && !dst_ds)
13074 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13077 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY
13078 && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13080 TRACE("Unsupported blit_op %#x.\n", blit_op);
13081 return FALSE;
13084 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13085 return FALSE;
13087 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13088 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13090 TRACE("Multi-sample source textures not supported.\n");
13091 return FALSE;
13094 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13096 TRACE("Destination resource does not have GPU access.\n");
13097 return FALSE;
13100 /* We don't necessarily want to blit from resources without
13101 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13102 * compressed textures. */
13103 decompress = (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
13104 && !(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED);
13105 if (!decompress && !(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13107 TRACE("Source resource does not have GPU access.\n");
13108 return FALSE;
13111 if (!is_identity_fixup(dst_format->color_fixup)
13112 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13114 TRACE("Destination fixups are not supported.\n");
13115 return FALSE;
13118 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
13119 && !((dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
13120 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
13122 TRACE("Destination texture is not FBO attachable.\n");
13123 return FALSE;
13126 TRACE("Returning supported.\n");
13127 return TRUE;
13130 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13131 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13132 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13133 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13134 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
13135 const struct wined3d_format *resolve_format)
13137 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
13138 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
13139 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
13140 struct wined3d_device *device = dst_texture->resource.device;
13141 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13142 struct wined3d_texture *staging_texture = NULL;
13143 struct wined3d_glsl_blitter *glsl_blitter;
13144 struct wined3d_color_key alpha_test_key;
13145 struct glsl_blitter_program *program;
13146 struct wined3d_blitter *next;
13147 unsigned int src_level;
13148 GLint location;
13149 RECT s, d;
13151 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
13152 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
13153 "colour_key %p, filter %s, resolve format %p.\n",
13154 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13155 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13156 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
13158 if (!glsl_blitter_supported(op, context, src_texture_gl, src_location, dst_texture_gl, dst_location))
13160 if (!(next = blitter->next))
13162 ERR("No blitter to handle blit op %#x.\n", op);
13163 return dst_location;
13166 TRACE("Forwarding to blitter %p.\n", next);
13167 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13168 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
13169 resolve_format);
13172 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13174 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13176 struct wined3d_resource_desc desc;
13177 struct wined3d_box upload_box;
13178 HRESULT hr;
13180 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13182 src_level = src_sub_resource_idx % src_texture->level_count;
13183 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13184 desc.format = src_texture->resource.format->id;
13185 desc.multisample_type = src_texture->resource.multisample_type;
13186 desc.multisample_quality = src_texture->resource.multisample_quality;
13187 desc.usage = WINED3DUSAGE_PRIVATE;
13188 desc.bind_flags = 0;
13189 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13190 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13191 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13192 desc.depth = 1;
13193 desc.size = 0;
13195 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13196 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13198 ERR("Failed to create staging texture, hr %#lx.\n", hr);
13199 return dst_location;
13202 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13203 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13204 src_texture, src_sub_resource_idx, &upload_box);
13206 src_texture = staging_texture;
13207 src_texture_gl = wined3d_texture_gl(src_texture);
13208 src_sub_resource_idx = 0;
13210 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13211 && (src_texture->sub_resources[src_sub_resource_idx].locations
13212 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13213 && !wined3d_resource_is_offscreen(&src_texture->resource))
13216 /* Without FBO blits transferring from the drawable to the texture is
13217 * expensive, because we have to flip the data in sysmem. Since we can
13218 * flip in the blitter, we don't actually need that flip anyway. So we
13219 * use the surface's texture as scratch texture, and flip the source
13220 * rectangle instead. */
13221 texture2d_load_fb_texture(src_texture_gl, src_sub_resource_idx, FALSE, context);
13223 s = *src_rect;
13224 src_level = src_sub_resource_idx % src_texture->level_count;
13225 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13226 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13227 src_rect = &s;
13229 else
13231 wined3d_texture_load(src_texture, context, FALSE);
13234 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
13235 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13236 else
13237 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13239 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
13240 wined3d_context_gl_apply_blit_state(context_gl, device);
13242 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13244 d = *dst_rect;
13245 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
13246 dst_rect = &d;
13249 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13251 const struct wined3d_format *f = src_texture->resource.format;
13253 alpha_test_key.color_space_low_value = 0;
13254 alpha_test_key.color_space_high_value = ~(wined3d_mask_from_size(f->alpha_size) << f->alpha_offset);
13255 colour_key = &alpha_test_key;
13257 else if (op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY)
13259 colour_key = NULL;
13262 if (!(program = glsl_blitter_get_program(glsl_blitter, context_gl, src_texture_gl, !!colour_key)))
13264 ERR("Failed to get blitter program.\n");
13265 return dst_location;
13267 GL_EXTCALL(glUseProgram(program->id));
13268 switch (get_complex_fixup(program->args.fixup))
13270 case COMPLEX_FIXUP_P8:
13271 glsl_blitter_upload_palette(glsl_blitter, context_gl, src_texture_gl);
13272 break;
13274 case COMPLEX_FIXUP_YUY2:
13275 case COMPLEX_FIXUP_UYVY:
13276 case COMPLEX_FIXUP_YV12:
13277 case COMPLEX_FIXUP_NV12:
13278 case COMPLEX_FIXUP_YUV:
13279 src_level = src_sub_resource_idx % src_texture->level_count;
13280 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13281 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13282 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13283 break;
13285 default:
13286 break;
13288 if (colour_key)
13290 struct wined3d_color float_key[2];
13292 wined3d_format_get_float_color_key(src_texture->resource.format, colour_key, float_key);
13293 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.low"));
13294 GL_EXTCALL(glUniform4fv(location, 1, &float_key[0].r));
13295 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.high"));
13296 GL_EXTCALL(glUniform4fv(location, 1, &float_key[1].r));
13298 wined3d_context_gl_draw_shaded_quad(context_gl, src_texture_gl, src_sub_resource_idx, src_rect, dst_rect, filter);
13299 GL_EXTCALL(glUseProgram(0));
13301 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13302 gl_info->gl_ops.gl.p_glFlush();
13304 if (staging_texture)
13305 wined3d_texture_decref(staging_texture);
13307 return dst_location;
13310 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13311 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13312 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *color, float depth, unsigned int stencil)
13314 struct wined3d_blitter *next;
13316 if ((next = blitter->next))
13317 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13318 clear_rects, draw_rect, flags, color, depth, stencil);
13321 static const struct wined3d_blitter_ops glsl_blitter_ops =
13323 glsl_blitter_destroy,
13324 glsl_blitter_clear,
13325 glsl_blitter_blit,
13328 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13329 const struct wined3d_device *device)
13331 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl(device->adapter)->gl_info;
13332 struct wined3d_glsl_blitter *blitter;
13334 if (device->shader_backend != &glsl_shader_backend)
13335 return NULL;
13337 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13338 return NULL;
13340 if (!(blitter = heap_alloc(sizeof(*blitter))))
13342 ERR("Failed to allocate blitter.\n");
13343 return NULL;
13346 TRACE("Created blitter %p.\n", blitter);
13348 blitter->blitter.ops = &glsl_blitter_ops;
13349 blitter->blitter.next = *next;
13350 string_buffer_list_init(&blitter->string_buffers);
13351 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13352 blitter->palette_texture = 0;
13353 *next = &blitter->blitter;
13355 return *next;