wined3d: Do not set last_was_pshader from the GLSL fragment pipe.
[wine.git] / dlls / wined3d / glsl_shader.c
blob7ed45da29f5e197e072d4c8e8e9f9e19c70c95be
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_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_TEXTURES];
219 GLint bumpenv_lum_scale_location[WINED3D_MAX_TEXTURES];
220 GLint bumpenv_lum_offset_location[WINED3D_MAX_TEXTURES];
221 GLint tss_constant_location[WINED3D_MAX_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_TEXTURES * 4 : (WINED3D_MAX_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_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_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;
1646 DWORD parallel_point_count = 0;
1648 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1650 if (!state->light_state.lights[i])
1651 continue;
1653 switch (state->light_state.lights[i]->OriginalParms.type)
1655 case WINED3D_LIGHT_POINT:
1656 ++point_count;
1657 break;
1658 case WINED3D_LIGHT_SPOT:
1659 ++spot_count;
1660 break;
1661 case WINED3D_LIGHT_DIRECTIONAL:
1662 ++directional_count;
1663 break;
1664 case WINED3D_LIGHT_PARALLELPOINT:
1665 ++parallel_point_count;
1666 break;
1667 default:
1668 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
1669 break;
1672 point_idx = 0;
1673 spot_idx = point_idx + point_count;
1674 directional_idx = spot_idx + spot_count;
1675 parallel_point_idx = directional_idx + directional_count;
1677 shader_glsl_ffp_vertex_lightambient_uniform(context_gl, state, prog);
1678 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1680 const struct wined3d_light_info *light_info = state->light_state.lights[i];
1681 unsigned int idx;
1683 if (!light_info)
1684 continue;
1686 switch (light_info->OriginalParms.type)
1688 case WINED3D_LIGHT_POINT:
1689 idx = point_idx++;
1690 break;
1691 case WINED3D_LIGHT_SPOT:
1692 idx = spot_idx++;
1693 break;
1694 case WINED3D_LIGHT_DIRECTIONAL:
1695 idx = directional_idx++;
1696 break;
1697 case WINED3D_LIGHT_PARALLELPOINT:
1698 idx = parallel_point_idx++;
1699 break;
1700 default:
1701 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1702 continue;
1704 shader_glsl_ffp_vertex_light_uniform(context_gl, state, idx, light_info, prog);
1708 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1709 shader_glsl_load_constants_f(pshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_PS_F],
1710 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1712 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1713 shader_glsl_load_constants_i(pshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_PS_I],
1714 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1716 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1717 shader_glsl_load_constants_b(pshader, context_gl, device->push_constants[WINED3D_PUSH_CONSTANTS_PS_B],
1718 prog->ps.uniform_b_locations, pshader->reg_maps.boolean_constants);
1720 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1722 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1724 if (prog->ps.bumpenv_mat_location[i] == -1)
1725 continue;
1727 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1728 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1730 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1732 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1733 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1734 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1735 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1739 checkGLcall("bump env uniforms");
1742 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1744 const struct wined3d_vec4 correction_params =
1746 /* Position is relative to the framebuffer, not the viewport. */
1747 context->render_offscreen ? 0.0f : (float)state->fb.render_targets[0]->height,
1748 context->render_offscreen ? 1.0f : -1.0f,
1749 0.0f,
1750 0.0f,
1753 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1756 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1757 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1758 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1759 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1761 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1763 struct wined3d_color color;
1765 if (prog->ps.tex_factor_location != -1)
1767 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1768 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1771 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1772 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1773 else
1774 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1776 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1778 if (prog->ps.tss_constant_location[i] == -1)
1779 continue;
1781 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1782 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1785 checkGLcall("fixed function uniforms");
1788 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1789 shader_glsl_load_fog_uniform(context_gl, state, prog);
1791 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1793 float ref = wined3d_alpha_ref(state);
1795 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1796 checkGLcall("alpha test emulation uniform");
1799 if (priv->next_constant_version == UINT_MAX)
1801 TRACE("Max constant version reached, resetting to 0.\n");
1802 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1803 priv->next_constant_version = 1;
1805 else
1807 prog->constant_version = priv->next_constant_version++;
1811 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1813 struct constant_entry *entries = heap->entries;
1814 unsigned int *positions = heap->positions;
1815 unsigned int heap_idx, parent_idx;
1817 if (!heap->contained[idx])
1819 heap_idx = heap->size++;
1820 heap->contained[idx] = TRUE;
1822 else
1824 heap_idx = positions[idx];
1827 while (heap_idx > 1)
1829 parent_idx = heap_idx >> 1;
1831 if (new_version <= entries[parent_idx].version) break;
1833 entries[heap_idx] = entries[parent_idx];
1834 positions[entries[parent_idx].idx] = heap_idx;
1835 heap_idx = parent_idx;
1838 entries[heap_idx].version = new_version;
1839 entries[heap_idx].idx = idx;
1840 positions[idx] = heap_idx;
1843 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1845 struct shader_glsl_priv *priv = device->shader_priv;
1846 struct constant_heap *heap = &priv->vconst_heap;
1847 UINT i;
1849 for (i = start; i < count + start; ++i)
1851 update_heap_entry(heap, i, priv->next_constant_version);
1855 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1857 struct shader_glsl_priv *priv = device->shader_priv;
1858 struct constant_heap *heap = &priv->pconst_heap;
1859 UINT i;
1861 for (i = start; i < count + start; ++i)
1863 update_heap_entry(heap, i, priv->next_constant_version);
1867 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1869 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1870 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1871 if(shader_major > 3) return ret;
1873 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1874 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1875 return ret;
1878 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1880 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
1883 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1885 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
1886 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
1889 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
1891 return shader_glsl_get_version(gl_info) >= 150;
1894 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1896 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1899 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1900 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1902 va_list args;
1903 int ret;
1905 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1906 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1907 for (;;)
1909 va_start(args, format);
1910 ret = shader_vaddline(buffer, format, args);
1911 va_end(args);
1912 if (!ret)
1913 return;
1914 if (!string_buffer_resize(buffer, ret))
1915 return;
1919 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1920 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1922 va_list args;
1923 int ret;
1925 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1926 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1927 for (;;)
1929 va_start(args, format);
1930 ret = shader_vaddline(buffer, format, args);
1931 va_end(args);
1932 if (!ret)
1933 return;
1934 if (!string_buffer_resize(buffer, ret))
1935 return;
1939 static const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
1941 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
1944 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
1946 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
1949 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
1951 switch (mode)
1953 case WINED3DSIM_CONSTANT:
1954 return "flat ";
1955 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
1956 return "noperspective ";
1957 default:
1958 FIXME("Unhandled interpolation mode %#x.\n", mode);
1959 case WINED3DSIM_NONE:
1960 case WINED3DSIM_LINEAR:
1961 return "";
1965 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
1966 const uint32_t *packed_interpolation_mode, unsigned int register_idx)
1968 return wined3d_extract_bits(packed_interpolation_mode,
1969 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
1972 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
1973 struct wined3d_string_buffer *buffer, unsigned int element_count,
1974 const uint32_t *interpolation_mode, BOOL unroll)
1976 enum wined3d_shader_interpolation_mode mode;
1977 unsigned int i;
1979 if (shader_glsl_use_interface_blocks(gl_info))
1981 if (unroll)
1983 shader_addline(buffer, "in shader_in_out {\n");
1984 for (i = 0; i < element_count; ++i)
1986 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
1987 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
1989 shader_addline(buffer, "} shader_in;\n");
1991 else
1993 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
1996 else
1998 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2002 static BOOL needs_interpolation_qualifiers_for_shader_outputs(const struct wined3d_gl_info *gl_info)
2004 /* In GLSL 4.40+ it is fine to specify interpolation qualifiers only in
2005 * fragment shaders. In older GLSL versions interpolation qualifiers must
2006 * match between shader stages. */
2007 return gl_info->glsl_version < MAKEDWORD_VERSION(4, 40);
2010 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
2011 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
2012 const uint32_t *interpolation_mode)
2014 enum wined3d_shader_interpolation_mode mode;
2015 unsigned int i;
2017 if (shader_glsl_use_interface_blocks(gl_info))
2019 if (rasterizer_setup)
2021 shader_addline(buffer, "out shader_in_out {\n");
2022 for (i = 0; i < element_count; ++i)
2024 const char *interpolation_qualifiers = "";
2025 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2027 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2028 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2030 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2032 shader_addline(buffer, "} shader_out;\n");
2034 else
2036 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2039 else
2041 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2045 static BOOL use_legacy_fragment_output(const struct wined3d_gl_info *gl_info)
2047 /* Technically 1.30 does support user-defined fragment shader outputs but
2048 * we might not have glBindFragDataLocation() available (i.e. GL version
2049 * might be < 3.0). */
2050 return gl_info->glsl_version <= MAKEDWORD_VERSION(1, 30);
2053 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2055 return use_legacy_fragment_output(gl_info) ? "gl_FragData" : "ps_out";
2058 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2060 switch (primitive_type)
2062 case WINED3D_PT_POINTLIST:
2063 return "points";
2065 case WINED3D_PT_LINELIST:
2066 return "lines";
2068 case WINED3D_PT_LINESTRIP:
2069 return "line_strip";
2071 case WINED3D_PT_TRIANGLELIST:
2072 return "triangles";
2074 case WINED3D_PT_TRIANGLESTRIP:
2075 return "triangle_strip";
2077 case WINED3D_PT_LINELIST_ADJ:
2078 return "lines_adjacency";
2080 case WINED3D_PT_TRIANGLELIST_ADJ:
2081 return "triangles_adjacency";
2083 default:
2084 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2085 return "";
2089 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2091 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2092 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2093 DWORD input_reg_used = shader->u.ps.input_reg_used;
2094 unsigned int i;
2096 if (reg_maps->shader_version.major < 3)
2097 return input_reg_used & (1u << idx);
2099 for (i = 0; i < input_signature->element_count; ++i)
2101 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2103 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2104 continue;
2106 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2107 && input->semantic_idx == idx)
2108 return input_reg_used & (1u << input->register_idx);
2110 return FALSE;
2113 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2114 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2116 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2118 if (version->major >= 4)
2119 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2120 else
2121 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2124 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2125 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2126 unsigned int index)
2128 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2129 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2130 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2131 index, scalar_type, scalar_type, index);
2134 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2135 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2137 unsigned int index = e->register_idx;
2138 enum wined3d_component_type type;
2140 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2142 shader_addline(buffer, "uniform int base_vertex_id;\n");
2143 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID - base_vertex_id), 0.0, 0.0, 0.0);\n", index);
2144 return;
2146 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2148 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2149 index);
2150 return;
2152 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2153 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2155 if (shader_glsl_use_explicit_attrib_location(gl_info))
2156 shader_addline(buffer, "layout(location = %u) ", index);
2158 type = e->component_type;
2159 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2161 FIXME("Unhandled type %#x.\n", type);
2162 type = WINED3D_TYPE_FLOAT;
2164 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2165 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2166 else
2167 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2168 component_type_info[type].glsl_vector_type,
2169 component_type_info[type].glsl_scalar_type, index);
2172 /** Generate the variable & register declarations for the GLSL output target */
2173 static void shader_generate_glsl_declarations(const struct wined3d_context_gl *context_gl,
2174 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2175 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2177 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2178 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2179 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2180 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2181 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2182 unsigned int uniform_block_base, uniform_block_count;
2183 enum wined3d_shader_resource_type resource_type;
2184 const struct wined3d_shader_lconst *lconst;
2185 const char *prefix;
2186 unsigned int i;
2187 uint32_t map;
2189 if (wined3d_settings.strict_shader_math)
2190 shader_addline(buffer, "#pragma optionNV(fastmath off)\n");
2192 prefix = shader_glsl_get_prefix(version->type);
2194 /* Prototype the subroutines */
2195 map = reg_maps->labels;
2196 while (map)
2198 i = wined3d_bit_scan(&map);
2199 shader_addline(buffer, "void subroutine%u();\n", i);
2202 if (version->type != WINED3D_SHADER_TYPE_PIXEL && version->type != WINED3D_SHADER_TYPE_COMPUTE)
2204 if (version->type == WINED3D_SHADER_TYPE_HULL)
2205 shader_addline(buffer, "out gl_PerVertex { invariant vec4 gl_Position; } gl_out[];\n");
2206 else
2207 shader_addline(buffer, "invariant gl_Position;\n");
2210 /* Declare the constants (aka uniforms) */
2211 if (shader->limits->constant_float > 0)
2213 unsigned max_constantsF;
2215 /* Unless the shader uses indirect addressing, always declare the
2216 * maximum array size and ignore that we need some uniforms privately.
2217 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2218 * and immediate values, still declare VC[256]. If the shader needs
2219 * more uniforms than we have it won't work in any case. If it uses
2220 * less, the compiler will figure out which uniforms are really used
2221 * and strip them out. This allows a shader to use c255 on a dx9 card,
2222 * as long as it doesn't also use all the other constants.
2224 * If the shader uses indirect addressing the compiler must assume
2225 * that all declared uniforms are used. In this case, declare only the
2226 * amount that we're assured to have.
2228 * Thus we run into problems in these two cases:
2229 * 1) The shader really uses more uniforms than supported.
2230 * 2) The shader uses indirect addressing, less constants than
2231 * supported, but uses a constant index > #supported consts. */
2232 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2234 /* No indirect addressing here. */
2235 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2237 else
2239 if (reg_maps->usesrelconstF)
2241 /* Subtract the other potential uniforms from the max
2242 * available (bools, ints, and 1 row of projection matrix).
2243 * Subtract another uniform for immediate values, which have
2244 * to be loaded via uniform by the driver as well. The shader
2245 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2246 * shader code, so one vec4 should be enough. (Unfortunately
2247 * the Nvidia driver doesn't store 128 and -128 in one float).
2249 * Writing gl_ClipVertex requires one uniform for each
2250 * clipplane as well. */
2251 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2252 if (vs_args->clip_enabled)
2253 max_constantsF -= gl_info->limits.user_clip_distances;
2254 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2255 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2256 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2257 * for now take this into account when calculating the number of available constants
2259 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2260 /* Set by driver quirks in directx.c */
2261 max_constantsF -= gl_info->reserved_glsl_constants;
2263 if (max_constantsF < shader->limits->constant_float)
2265 static unsigned int once;
2267 if (!once++)
2268 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2269 " it may not render correctly.\n");
2270 else
2271 WARN("The hardware does not support enough uniform components to run this shader.\n");
2274 else
2276 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2279 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2280 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2283 /* Always declare the full set of constants, the compiler can remove the
2284 * unused ones because d3d doesn't (yet) support indirect int and bool
2285 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2286 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2287 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2289 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2290 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2292 /* Declare immediate constant buffer */
2293 if (reg_maps->icb)
2294 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2296 /* Declare constant buffers */
2297 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2298 &uniform_block_base, &uniform_block_count);
2299 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2301 if (reg_maps->cb_sizes[i])
2303 shader_addline(buffer, "layout(std140");
2304 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2305 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2306 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2307 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2311 /* Declare texture samplers */
2312 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2314 struct wined3d_shader_sampler_map_entry *entry;
2315 const char *sampler_type_prefix, *sampler_type;
2316 BOOL shadow_sampler, tex_rect;
2318 entry = &reg_maps->sampler_map.entries[i];
2320 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2322 ERR("Invalid resource index %u.\n", entry->resource_idx);
2323 continue;
2326 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2328 case WINED3D_DATA_FLOAT:
2329 case WINED3D_DATA_UNORM:
2330 case WINED3D_DATA_SNORM:
2331 sampler_type_prefix = "";
2332 break;
2334 case WINED3D_DATA_INT:
2335 sampler_type_prefix = "i";
2336 break;
2338 case WINED3D_DATA_UINT:
2339 sampler_type_prefix = "u";
2340 break;
2342 default:
2343 sampler_type_prefix = "";
2344 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2345 break;
2348 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2349 resource_type = version->type == WINED3D_SHADER_TYPE_PIXEL
2350 ? pixelshader_get_resource_type(reg_maps, entry->resource_idx, ps_args->tex_types)
2351 : reg_maps->resource_info[entry->resource_idx].type;
2353 switch (resource_type)
2355 case WINED3D_SHADER_RESOURCE_BUFFER:
2356 sampler_type = "samplerBuffer";
2357 break;
2359 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2360 if (shadow_sampler)
2361 sampler_type = "sampler1DShadow";
2362 else
2363 sampler_type = "sampler1D";
2364 break;
2366 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2367 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2368 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2369 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2370 if (shadow_sampler)
2372 if (tex_rect)
2373 sampler_type = "sampler2DRectShadow";
2374 else
2375 sampler_type = "sampler2DShadow";
2377 else
2379 if (tex_rect)
2380 sampler_type = "sampler2DRect";
2381 else
2382 sampler_type = "sampler2D";
2384 break;
2386 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2387 if (shadow_sampler)
2388 FIXME("Unsupported 3D shadow sampler.\n");
2389 sampler_type = "sampler3D";
2390 break;
2392 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2393 if (shadow_sampler)
2394 sampler_type = "samplerCubeShadow";
2395 else
2396 sampler_type = "samplerCube";
2397 break;
2399 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2400 if (shadow_sampler)
2401 sampler_type = "sampler1DArrayShadow";
2402 else
2403 sampler_type = "sampler1DArray";
2404 break;
2406 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2407 if (shadow_sampler)
2408 sampler_type = "sampler2DArrayShadow";
2409 else
2410 sampler_type = "sampler2DArray";
2411 break;
2413 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2414 if (shadow_sampler)
2415 sampler_type = "samplerCubeArrayShadow";
2416 else
2417 sampler_type = "samplerCubeArray";
2418 break;
2420 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2421 sampler_type = "sampler2DMS";
2422 break;
2424 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2425 sampler_type = "sampler2DMSArray";
2426 break;
2428 default:
2429 sampler_type = "unsupported_sampler";
2430 FIXME("Unhandled resource type %#x.\n", resource_type);
2431 break;
2434 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2435 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, version, entry->bind_idx);
2436 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2437 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2440 /* Declare images */
2441 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2443 const char *image_type_prefix, *image_type, *read_format;
2445 if (!reg_maps->uav_resource_info[i].type)
2446 continue;
2448 switch (reg_maps->uav_resource_info[i].data_type)
2450 case WINED3D_DATA_FLOAT:
2451 case WINED3D_DATA_UNORM:
2452 case WINED3D_DATA_SNORM:
2453 image_type_prefix = "";
2454 read_format = "r32f";
2455 break;
2457 case WINED3D_DATA_INT:
2458 image_type_prefix = "i";
2459 read_format = "r32i";
2460 break;
2462 case WINED3D_DATA_UINT:
2463 image_type_prefix = "u";
2464 read_format = "r32ui";
2465 break;
2467 default:
2468 image_type_prefix = "";
2469 read_format = "";
2470 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2471 break;
2474 switch (reg_maps->uav_resource_info[i].type)
2476 case WINED3D_SHADER_RESOURCE_BUFFER:
2477 image_type = "imageBuffer";
2478 break;
2480 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2481 image_type = "image1D";
2482 break;
2484 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2485 image_type = "image2D";
2486 break;
2488 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2489 image_type = "image3D";
2490 break;
2492 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2493 image_type = "image1DArray";
2494 break;
2496 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2497 image_type = "image2DArray";
2498 break;
2500 default:
2501 image_type = "unsupported_image";
2502 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2503 break;
2506 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2507 shader_addline(buffer, "layout(binding = %u)\n", i);
2508 if (reg_maps->uav_read_mask & (1u << i))
2509 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2510 read_format, image_type_prefix, image_type, prefix, i);
2511 else
2512 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2513 image_type_prefix, image_type, prefix, i);
2515 if (reg_maps->uav_counter_mask & (1u << i))
2516 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2517 i, prefix, i);
2520 /* Declare address variables */
2521 map = reg_maps->address;
2522 while (map)
2524 i = wined3d_bit_scan(&map);
2525 shader_addline(buffer, "ivec4 A%u;\n", i);
2528 /* Declare output register temporaries */
2529 if (shader->limits->packed_output)
2530 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2532 /* Declare temporary variables */
2533 if (reg_maps->temporary_count)
2535 for (i = 0; i < reg_maps->temporary_count; ++i)
2536 shader_addline(buffer, "vec4 R%u;\n", i);
2538 else if (version->major < 4)
2540 map = reg_maps->temporary;
2541 while (map)
2543 i = wined3d_bit_scan(&map);
2544 shader_addline(buffer, "vec4 R%u;\n", i);
2548 /* Declare indexable temporary variables */
2549 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2551 if (idx_temp_reg->component_count != 4)
2552 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2553 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2556 /* Declare loop registers aLx */
2557 if (version->major < 4)
2559 for (i = 0; i < reg_maps->loop_depth; ++i)
2561 shader_addline(buffer, "int aL%u;\n", i);
2562 shader_addline(buffer, "int tmpInt%u;\n", i);
2566 /* Temporary variables for matrix operations */
2567 shader_addline(buffer, "vec4 tmp0;\n");
2568 shader_addline(buffer, "vec4 tmp1;\n");
2569 if (gl_info->supported[ARB_GPU_SHADER5])
2570 shader_addline(buffer, "precise vec4 tmp_precise[2];\n");
2571 else
2572 shader_addline(buffer, "/* precise */ vec4 tmp_precise[2];\n");
2574 if (!shader->load_local_constsF)
2576 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2578 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2579 shader_glsl_append_imm_vec(buffer, (const float *)lconst->value, ARRAY_SIZE(lconst->value), gl_info);
2580 shader_addline(buffer, ";\n");
2585 /* Prototypes */
2586 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
2587 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2588 enum wined3d_data_type data_type);
2590 /** Used for opcode modifiers - They multiply the result by the specified amount */
2591 static const char * const shift_glsl_tab[] = {
2592 "", /* 0 (none) */
2593 "2.0 * ", /* 1 (x2) */
2594 "4.0 * ", /* 2 (x4) */
2595 "8.0 * ", /* 3 (x8) */
2596 "16.0 * ", /* 4 (x16) */
2597 "32.0 * ", /* 5 (x32) */
2598 "", /* 6 (x64) */
2599 "", /* 7 (x128) */
2600 "", /* 8 (d256) */
2601 "", /* 9 (d128) */
2602 "", /* 10 (d64) */
2603 "", /* 11 (d32) */
2604 "0.0625 * ", /* 12 (d16) */
2605 "0.125 * ", /* 13 (d8) */
2606 "0.25 * ", /* 14 (d4) */
2607 "0.5 * " /* 15 (d2) */
2610 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2611 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2612 const char *in_reg, const char *in_regswizzle, char *out_str)
2614 switch (src_modifier)
2616 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2617 case WINED3DSPSM_DW:
2618 case WINED3DSPSM_NONE:
2619 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2620 break;
2621 case WINED3DSPSM_NEG:
2622 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2623 break;
2624 case WINED3DSPSM_NOT:
2625 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2626 break;
2627 case WINED3DSPSM_BIAS:
2628 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2629 break;
2630 case WINED3DSPSM_BIASNEG:
2631 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2632 break;
2633 case WINED3DSPSM_SIGN:
2634 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2635 break;
2636 case WINED3DSPSM_SIGNNEG:
2637 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2638 break;
2639 case WINED3DSPSM_COMP:
2640 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2641 break;
2642 case WINED3DSPSM_X2:
2643 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2644 break;
2645 case WINED3DSPSM_X2NEG:
2646 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2647 break;
2648 case WINED3DSPSM_ABS:
2649 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2650 break;
2651 case WINED3DSPSM_ABSNEG:
2652 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2653 break;
2654 default:
2655 FIXME("Unhandled modifier %u\n", src_modifier);
2656 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2660 static void shader_glsl_fixup_scalar_register_variable(struct wined3d_string_buffer *register_name,
2661 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2663 /* The ARB_shading_language_420pack extension allows swizzle operations on
2664 * scalars. */
2665 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2666 string_buffer_sprintf(register_name, "%s", glsl_variable);
2667 else
2668 string_buffer_sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2671 /** Writes the GLSL variable name that corresponds to the register that the
2672 * DX opcode parameter is trying to access */
2673 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2674 enum wined3d_data_type data_type, struct wined3d_string_buffer *register_name,
2675 BOOL *is_swizzled, const struct wined3d_shader_context *ctx)
2677 /* oPos, oFog and oPts in D3D */
2678 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2680 const struct wined3d_shader *shader = ctx->shader;
2681 const struct wined3d_shader_reg_maps *reg_maps = ctx->reg_maps;
2682 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2683 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2684 const char *prefix = shader_glsl_get_prefix(version->type);
2685 const struct wined3d_gl_info *gl_info = priv->gl_info;
2686 struct glsl_src_param rel_param0, rel_param1;
2688 if (reg->idx[0].offset != ~0u && reg->idx[0].rel_addr)
2689 shader_glsl_add_src_param_ext(ctx, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0,
2690 &rel_param0, reg->idx[0].rel_addr->reg.data_type);
2691 if (reg->idx[1].offset != ~0u && reg->idx[1].rel_addr)
2692 shader_glsl_add_src_param_ext(ctx, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0,
2693 &rel_param1, reg->idx[1].rel_addr->reg.data_type);
2694 if (is_swizzled)
2695 *is_swizzled = FALSE;
2697 switch (reg->type)
2699 case WINED3DSPR_TEMP:
2700 string_buffer_sprintf(register_name, "R%u", reg->idx[0].offset);
2701 break;
2703 case WINED3DSPR_INPUT:
2704 case WINED3DSPR_INCONTROLPOINT:
2705 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2707 if (reg->idx[0].rel_addr)
2708 FIXME("VS3 input registers relative addressing.\n");
2709 if (is_swizzled && priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2710 *is_swizzled = TRUE;
2711 if (reg->idx[0].rel_addr)
2713 string_buffer_sprintf(register_name, "%s_in[%s + %u]",
2714 prefix, rel_param0.param_str, reg->idx[0].offset);
2716 else
2718 string_buffer_sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2720 break;
2723 if (version->type == WINED3D_SHADER_TYPE_HULL
2724 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2725 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2727 if (reg->idx[0].rel_addr)
2729 if (reg->idx[1].rel_addr)
2730 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2731 rel_param0.param_str, reg->idx[0].offset,
2732 rel_param1.param_str, reg->idx[1].offset);
2733 else
2734 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2735 rel_param0.param_str, reg->idx[0].offset,
2736 reg->idx[1].offset);
2738 else if (reg->idx[1].rel_addr)
2739 string_buffer_sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2740 rel_param1.param_str, reg->idx[1].offset);
2741 else
2742 string_buffer_sprintf(register_name, "shader_in[%u].reg[%u]",
2743 reg->idx[0].offset, reg->idx[1].offset);
2744 break;
2747 /* pixel shaders >= 3.0 */
2748 if (version->major >= 3)
2750 unsigned int idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2751 unsigned int in_count = vec4_varyings(version->major, gl_info);
2753 if (reg->idx[0].rel_addr)
2755 /* Removing a + 0 would be an obvious optimization, but
2756 * OS X doesn't see the NOP operation there. */
2757 if (idx)
2759 if (needs_legacy_glsl_syntax(gl_info)
2760 && shader->u.ps.declared_in_count > in_count)
2762 string_buffer_sprintf(register_name,
2763 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2764 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2765 prefix, rel_param0.param_str, idx);
2767 else
2769 string_buffer_sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2772 else
2774 if (needs_legacy_glsl_syntax(gl_info)
2775 && shader->u.ps.declared_in_count > in_count)
2777 string_buffer_sprintf(register_name,
2778 "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2779 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2780 prefix, rel_param0.param_str);
2782 else
2784 string_buffer_sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2788 else
2790 if (idx == in_count)
2791 string_buffer_sprintf(register_name, "gl_Color");
2792 else if (idx == in_count + 1)
2793 string_buffer_sprintf(register_name, "gl_SecondaryColor");
2794 else
2795 string_buffer_sprintf(register_name, "%s_in[%u]", prefix, idx);
2798 else
2800 if (!reg->idx[0].offset)
2801 string_buffer_sprintf(register_name, "ffp_varying_diffuse");
2802 else
2803 string_buffer_sprintf(register_name, "ffp_varying_specular");
2804 break;
2806 break;
2808 case WINED3DSPR_CONST:
2810 /* Relative addressing */
2811 if (reg->idx[0].rel_addr)
2813 if (wined3d_settings.check_float_constants)
2814 string_buffer_sprintf(register_name,
2815 "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2816 rel_param0.param_str, reg->idx[0].offset,
2817 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2818 prefix, rel_param0.param_str, reg->idx[0].offset);
2819 else if (reg->idx[0].offset)
2820 string_buffer_sprintf(register_name, "%s_c[%s + %u]",
2821 prefix, rel_param0.param_str, reg->idx[0].offset);
2822 else
2823 string_buffer_sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2825 else
2827 if (shader_constant_is_local(shader, reg->idx[0].offset))
2828 string_buffer_sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2829 else
2830 string_buffer_sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2833 break;
2835 case WINED3DSPR_CONSTINT:
2836 string_buffer_sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2837 break;
2839 case WINED3DSPR_CONSTBOOL:
2840 string_buffer_sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2841 break;
2843 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2844 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2845 string_buffer_sprintf(register_name, "T%u", reg->idx[0].offset);
2846 else
2847 string_buffer_sprintf(register_name, "A%u", reg->idx[0].offset);
2848 break;
2850 case WINED3DSPR_LOOP:
2851 string_buffer_sprintf(register_name, "aL%u", ctx->state->current_loop_reg - 1);
2852 break;
2854 case WINED3DSPR_SAMPLER:
2855 string_buffer_sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2856 break;
2858 case WINED3DSPR_COLOROUT:
2859 if (reg->idx[0].offset >= gl_info->limits.buffers)
2860 WARN("Write to render target %u, only %d supported.\n",
2861 reg->idx[0].offset, gl_info->limits.buffers);
2863 string_buffer_sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2864 break;
2866 case WINED3DSPR_RASTOUT:
2867 string_buffer_sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2868 break;
2870 case WINED3DSPR_DEPTHOUT:
2871 case WINED3DSPR_DEPTHOUTGE:
2872 case WINED3DSPR_DEPTHOUTLE:
2873 string_buffer_sprintf(register_name, "gl_FragDepth");
2874 break;
2876 case WINED3DSPR_ATTROUT:
2877 if (!reg->idx[0].offset)
2878 string_buffer_sprintf(register_name, "%s_out[8]", prefix);
2879 else
2880 string_buffer_sprintf(register_name, "%s_out[9]", prefix);
2881 break;
2883 case WINED3DSPR_TEXCRDOUT:
2884 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2885 if (reg->idx[0].rel_addr)
2886 string_buffer_sprintf(register_name, "%s_out[%s + %u]",
2887 prefix, rel_param0.param_str, reg->idx[0].offset);
2888 else
2889 string_buffer_sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2890 break;
2892 case WINED3DSPR_MISCTYPE:
2893 if (!reg->idx[0].offset)
2895 /* vPos */
2896 string_buffer_sprintf(register_name, "vpos");
2898 else if (reg->idx[0].offset == 1)
2900 /* Note that gl_FrontFacing is a bool, while vFace is
2901 * a float for which the sign determines front/back */
2902 string_buffer_sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2904 else
2906 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2907 string_buffer_sprintf(register_name, "unrecognized_register");
2909 break;
2911 case WINED3DSPR_IMMCONST:
2912 switch (reg->immconst_type)
2914 case WINED3D_IMMCONST_SCALAR:
2915 switch (data_type)
2917 case WINED3D_DATA_UNORM:
2918 case WINED3D_DATA_SNORM:
2919 case WINED3D_DATA_FLOAT:
2920 string_buffer_clear(register_name);
2921 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 1, gl_info);
2922 break;
2923 case WINED3D_DATA_INT:
2924 string_buffer_sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2925 break;
2926 case WINED3D_DATA_RESOURCE:
2927 case WINED3D_DATA_SAMPLER:
2928 case WINED3D_DATA_UINT:
2929 string_buffer_sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2930 break;
2931 default:
2932 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2933 break;
2935 break;
2937 case WINED3D_IMMCONST_VEC4:
2938 switch (data_type)
2940 case WINED3D_DATA_UNORM:
2941 case WINED3D_DATA_SNORM:
2942 case WINED3D_DATA_FLOAT:
2943 string_buffer_clear(register_name);
2944 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 4, gl_info);
2945 break;
2946 case WINED3D_DATA_INT:
2947 string_buffer_sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2948 reg->u.immconst_data[0], reg->u.immconst_data[1],
2949 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2950 break;
2951 case WINED3D_DATA_RESOURCE:
2952 case WINED3D_DATA_SAMPLER:
2953 case WINED3D_DATA_UINT:
2954 string_buffer_sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2955 reg->u.immconst_data[0], reg->u.immconst_data[1],
2956 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2957 break;
2958 default:
2959 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2960 break;
2962 break;
2964 default:
2965 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2966 string_buffer_sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2968 break;
2970 case WINED3DSPR_CONSTBUFFER:
2971 if (reg->idx[1].rel_addr)
2972 string_buffer_sprintf(register_name, "%s_cb%u[%s + %u]",
2973 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2974 else
2975 string_buffer_sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2976 break;
2978 case WINED3DSPR_IMMCONSTBUFFER:
2979 if (reg->idx[0].rel_addr)
2980 string_buffer_sprintf(register_name, "%s_icb[%s + %u]",
2981 prefix, rel_param0.param_str, reg->idx[0].offset);
2982 else
2983 string_buffer_sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2984 break;
2986 case WINED3DSPR_PRIMID:
2987 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2988 string_buffer_sprintf(register_name, "gl_PrimitiveIDIn");
2989 else
2990 string_buffer_sprintf(register_name, "gl_PrimitiveID");
2991 break;
2993 case WINED3DSPR_IDXTEMP:
2994 if (reg->idx[1].rel_addr)
2995 string_buffer_sprintf(register_name, "X%u[%s + %u]",
2996 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2997 else
2998 string_buffer_sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2999 break;
3001 case WINED3DSPR_LOCALTHREADINDEX:
3002 shader_glsl_fixup_scalar_register_variable(register_name,
3003 "int(gl_LocalInvocationIndex)", gl_info);
3004 break;
3006 case WINED3DSPR_GSINSTID:
3007 case WINED3DSPR_OUTPOINTID:
3008 shader_glsl_fixup_scalar_register_variable(register_name,
3009 "gl_InvocationID", gl_info);
3010 break;
3012 case WINED3DSPR_THREADID:
3013 string_buffer_sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
3014 break;
3016 case WINED3DSPR_THREADGROUPID:
3017 string_buffer_sprintf(register_name, "ivec3(gl_WorkGroupID)");
3018 break;
3020 case WINED3DSPR_LOCALTHREADID:
3021 string_buffer_sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3022 break;
3024 case WINED3DSPR_FORKINSTID:
3025 case WINED3DSPR_JOININSTID:
3026 shader_glsl_fixup_scalar_register_variable(register_name,
3027 "phase_instance_id", gl_info);
3028 break;
3030 case WINED3DSPR_TESSCOORD:
3031 string_buffer_sprintf(register_name, "gl_TessCoord");
3032 break;
3034 case WINED3DSPR_OUTCONTROLPOINT:
3035 if (reg->idx[0].rel_addr)
3037 if (reg->idx[1].rel_addr)
3038 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3039 rel_param0.param_str, reg->idx[0].offset,
3040 rel_param1.param_str, reg->idx[1].offset);
3041 else
3042 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3043 rel_param0.param_str, reg->idx[0].offset,
3044 reg->idx[1].offset);
3046 else if (reg->idx[1].rel_addr)
3048 string_buffer_sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3049 reg->idx[0].offset, rel_param1.param_str,
3050 reg->idx[1].offset);
3052 else
3054 string_buffer_sprintf(register_name, "shader_out[%u].reg[%u]",
3055 reg->idx[0].offset, reg->idx[1].offset);
3057 break;
3059 case WINED3DSPR_PATCHCONST:
3060 if (version->type == WINED3D_SHADER_TYPE_HULL)
3061 string_buffer_sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3062 else
3063 string_buffer_sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3064 break;
3066 case WINED3DSPR_COVERAGE:
3067 string_buffer_sprintf(register_name, "gl_SampleMaskIn[0]");
3068 break;
3070 case WINED3DSPR_SAMPLEMASK:
3071 string_buffer_sprintf(register_name, "sample_mask");
3072 break;
3074 default:
3075 FIXME("Unhandled register type %#x.\n", reg->type);
3076 string_buffer_sprintf(register_name, "unrecognised_register");
3077 break;
3081 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3083 *str++ = '.';
3084 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3085 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3086 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3087 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3088 *str = '\0';
3091 /* Get the GLSL write mask for the destination register */
3092 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3094 DWORD mask = param->write_mask;
3096 if (shader_is_scalar(&param->reg))
3098 mask = WINED3DSP_WRITEMASK_0;
3099 *write_mask = '\0';
3101 else
3103 shader_glsl_write_mask_to_str(mask, write_mask);
3106 return mask;
3109 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3111 unsigned int size = 0;
3113 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3114 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3115 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3116 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3118 return size;
3121 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3122 unsigned int component_idx)
3124 /* swizzle bits fields: wwzzyyxx */
3125 return (swizzle >> (2 * component_idx)) & 0x3;
3128 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3130 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3131 * but addressed as "rgba". To fix this we need to swap the register's x
3132 * and z components. */
3133 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3134 unsigned int i;
3136 *str++ = '.';
3137 for (i = 0; i < 4; ++i)
3139 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3140 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3142 *str = '\0';
3145 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3146 BOOL fixup, DWORD mask, char *swizzle_str)
3148 if (shader_is_scalar(&param->reg))
3149 *swizzle_str = '\0';
3150 else
3151 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3154 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3155 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type, unsigned int size)
3157 if (dst_data_type == WINED3D_DATA_UNORM || dst_data_type == WINED3D_DATA_SNORM)
3158 dst_data_type = WINED3D_DATA_FLOAT;
3159 if (src_data_type == WINED3D_DATA_UNORM || src_data_type == WINED3D_DATA_SNORM)
3160 src_data_type = WINED3D_DATA_FLOAT;
3162 if (dst_data_type == src_data_type)
3164 string_buffer_sprintf(dst_param, "%s", src_param);
3165 return;
3168 if (src_data_type == WINED3D_DATA_FLOAT)
3170 switch (dst_data_type)
3172 case WINED3D_DATA_INT:
3173 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3174 return;
3175 case WINED3D_DATA_RESOURCE:
3176 case WINED3D_DATA_SAMPLER:
3177 case WINED3D_DATA_UINT:
3178 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3179 return;
3180 default:
3181 break;
3185 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3187 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3188 return;
3191 if (src_data_type == WINED3D_DATA_INT)
3193 switch (dst_data_type)
3195 case WINED3D_DATA_FLOAT:
3196 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3197 return;
3198 case WINED3D_DATA_UINT:
3199 if (size == 1)
3200 string_buffer_sprintf(dst_param, "uint(%s)", src_param);
3201 else
3202 string_buffer_sprintf(dst_param, "uvec%u(%s)", size, src_param);
3203 return;
3204 default:
3205 break;
3209 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3210 string_buffer_sprintf(dst_param, "%s", src_param);
3213 /* From a given parameter token, generate the corresponding GLSL string.
3214 * Also, return the actual register name and swizzle in case the
3215 * caller needs this information as well. */
3216 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
3217 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3218 enum wined3d_data_type data_type)
3220 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3221 struct wined3d_string_buffer *param_str = string_buffer_get(priv->string_buffers);
3222 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3223 enum wined3d_data_type param_data_type;
3224 BOOL is_color = FALSE;
3225 char swizzle_str[6];
3226 unsigned int size;
3228 glsl_src->param_str[0] = '\0';
3229 swizzle_str[0] = '\0';
3231 shader_glsl_get_register_name(&wined3d_src->reg, data_type, reg_name, &is_color, ctx);
3232 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3234 switch (wined3d_src->reg.type)
3236 case WINED3DSPR_IMMCONST:
3237 param_data_type = data_type;
3238 size = wined3d_src->reg.immconst_type == WINED3D_IMMCONST_SCALAR ? 1 : 4;
3239 break;
3240 case WINED3DSPR_FORKINSTID:
3241 case WINED3DSPR_GSINSTID:
3242 case WINED3DSPR_JOININSTID:
3243 case WINED3DSPR_LOCALTHREADINDEX:
3244 case WINED3DSPR_OUTPOINTID:
3245 case WINED3DSPR_PRIMID:
3246 param_data_type = WINED3D_DATA_INT;
3247 size = 1;
3248 break;
3249 case WINED3DSPR_LOCALTHREADID:
3250 case WINED3DSPR_THREADGROUPID:
3251 case WINED3DSPR_THREADID:
3252 param_data_type = WINED3D_DATA_INT;
3253 size = 3;
3254 break;
3255 default:
3256 param_data_type = WINED3D_DATA_FLOAT;
3257 size = 4;
3258 break;
3261 shader_glsl_sprintf_cast(param_str, reg_name->buffer, data_type, param_data_type, size);
3262 shader_glsl_gen_modifier(wined3d_src->modifiers, param_str->buffer, swizzle_str, glsl_src->param_str);
3264 string_buffer_release(priv->string_buffers, reg_name);
3265 string_buffer_release(priv->string_buffers, param_str);
3268 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3269 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3271 shader_glsl_add_src_param_ext(ins->ctx, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3274 /* From a given parameter token, generate the corresponding GLSL string.
3275 * Also, return the actual register name and swizzle in case the
3276 * caller needs this information as well. */
3277 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3278 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3280 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3281 struct wined3d_string_buffer *reg_name;
3282 size_t len;
3284 glsl_dst->mask_str[0] = '\0';
3286 reg_name = string_buffer_get(priv->string_buffers);
3287 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type, reg_name, NULL, ins->ctx);
3288 len = min(reg_name->content_size, ARRAY_SIZE(glsl_dst->reg_name) - 1);
3289 memcpy(glsl_dst->reg_name, reg_name->buffer, len);
3290 glsl_dst->reg_name[len] = '\0';
3291 string_buffer_release(priv->string_buffers, reg_name);
3293 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3296 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3297 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3298 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3299 unsigned int dst_idx, enum wined3d_data_type data_type)
3301 struct glsl_dst_param glsl_dst;
3302 DWORD mask;
3304 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3306 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3307 sprintf(glsl_dst.reg_name, "tmp_precise[%u]", dst_idx);
3309 switch (data_type)
3311 case WINED3D_DATA_FLOAT:
3312 case WINED3D_DATA_UNORM:
3313 case WINED3D_DATA_SNORM:
3314 shader_addline(buffer, "%s%s = %s(",
3315 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3316 break;
3317 case WINED3D_DATA_INT:
3318 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3319 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3320 break;
3321 case WINED3D_DATA_RESOURCE:
3322 case WINED3D_DATA_SAMPLER:
3323 case WINED3D_DATA_UINT:
3324 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3325 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3326 break;
3327 default:
3328 FIXME("Unhandled data type %#x.\n", data_type);
3329 shader_addline(buffer, "%s%s = %s(",
3330 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3331 break;
3335 return mask;
3338 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3339 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3341 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3344 /** Process GLSL instruction modifiers */
3345 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3347 const struct wined3d_shader_dst_param *dst;
3348 struct glsl_dst_param dst_param;
3349 DWORD modifiers;
3350 unsigned int i;
3352 for (i = 0; i < ins->dst_count; ++i)
3354 if ((dst = &ins->dst[i])->reg.type == WINED3DSPR_NULL)
3355 continue;
3357 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3359 shader_glsl_add_dst_param(ins, dst, &dst_param);
3360 shader_addline(ins->ctx->buffer, "%s%s = tmp_precise[%u]%s;\n",
3361 dst_param.reg_name, dst_param.mask_str, i, dst_param.mask_str);
3364 if (!(modifiers = dst->modifiers))
3365 continue;
3367 shader_glsl_add_dst_param(ins, dst, &dst_param);
3369 if (modifiers & WINED3DSPDM_SATURATE)
3371 /* _SAT means to clamp the value of the register to between 0 and 1 */
3372 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3373 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3376 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3378 FIXME("_centroid modifier not handled\n");
3381 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3383 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3388 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3390 switch (op)
3392 case WINED3D_SHADER_REL_OP_GT: return ">";
3393 case WINED3D_SHADER_REL_OP_EQ: return "==";
3394 case WINED3D_SHADER_REL_OP_GE: return ">=";
3395 case WINED3D_SHADER_REL_OP_LT: return "<";
3396 case WINED3D_SHADER_REL_OP_NE: return "!=";
3397 case WINED3D_SHADER_REL_OP_LE: return "<=";
3398 default:
3399 FIXME("Unrecognized operator %#x.\n", op);
3400 return "(\?\?)";
3404 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3406 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3409 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3410 unsigned int *coord_size, unsigned int *deriv_size)
3412 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3413 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3415 *coord_size = resource_type_info[resource_type].coord_size;
3416 *deriv_size = *coord_size;
3417 if (is_array)
3418 --(*deriv_size);
3421 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3422 DWORD resource_idx, DWORD sampler_idx, uint32_t flags, struct glsl_sample_function *sample_function)
3424 enum wined3d_shader_resource_type resource_type;
3425 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3426 const struct wined3d_gl_info *gl_info = priv->gl_info;
3427 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3428 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3429 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3430 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3431 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3432 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3433 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3434 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3435 const char *base = "texture", *type_part = "", *suffix = "";
3436 unsigned int coord_size, deriv_size;
3438 resource_type = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3439 ? pixelshader_get_resource_type(ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
3440 : ctx->reg_maps->resource_info[resource_idx].type;
3442 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3444 if (resource_type >= ARRAY_SIZE(resource_type_info))
3446 ERR("Unexpected resource type %#x.\n", resource_type);
3447 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3450 /* Note that there's no such thing as a projected cube texture. */
3451 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3452 projected = FALSE;
3454 if (needs_legacy_glsl_syntax(gl_info))
3456 if (shadow)
3457 base = "shadow";
3459 type_part = resource_type_info[resource_type].type_part;
3460 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3461 type_part = "2DRect";
3462 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3463 FIXME("Unhandled resource type %#x.\n", resource_type);
3465 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3467 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3468 suffix = "ARB";
3469 else
3470 FIXME("Unsupported grad function.\n");
3474 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3476 static const uint32_t texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3477 if (flags & ~texel_fetch_flags)
3478 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3480 base = "texelFetch";
3481 type_part = "";
3484 sample_function->name = string_buffer_get(priv->string_buffers);
3485 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3486 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3488 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3489 if (shadow)
3490 ++coord_size;
3491 sample_function->offset_size = offset ? deriv_size : 0;
3492 sample_function->coord_mask = wined3d_mask_from_size(coord_size);
3493 sample_function->deriv_mask = wined3d_mask_from_size(deriv_size);
3494 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3497 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3498 struct glsl_sample_function *sample_function)
3500 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3502 string_buffer_release(priv->string_buffers, sample_function->name);
3505 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3506 BOOL sign_fixup, enum fixup_channel_source channel_source)
3508 switch(channel_source)
3510 case CHANNEL_SOURCE_ZERO:
3511 strcat(arguments, "0.0");
3512 break;
3514 case CHANNEL_SOURCE_ONE:
3515 strcat(arguments, "1.0");
3516 break;
3518 case CHANNEL_SOURCE_X:
3519 strcat(arguments, reg_name);
3520 strcat(arguments, ".x");
3521 break;
3523 case CHANNEL_SOURCE_Y:
3524 strcat(arguments, reg_name);
3525 strcat(arguments, ".y");
3526 break;
3528 case CHANNEL_SOURCE_Z:
3529 strcat(arguments, reg_name);
3530 strcat(arguments, ".z");
3531 break;
3533 case CHANNEL_SOURCE_W:
3534 strcat(arguments, reg_name);
3535 strcat(arguments, ".w");
3536 break;
3538 default:
3539 FIXME("Unhandled channel source %#x\n", channel_source);
3540 strcat(arguments, "undefined");
3541 break;
3544 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3547 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3548 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3550 unsigned int mask_size, remaining;
3551 DWORD fixup_mask = 0;
3552 char arguments[256];
3553 char mask_str[6];
3555 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3556 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3557 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3558 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3559 if (!(mask &= fixup_mask))
3560 return;
3562 if (is_complex_fixup(fixup))
3564 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3565 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3566 return;
3569 shader_glsl_write_mask_to_str(mask, mask_str);
3570 mask_size = shader_glsl_get_write_mask_size(mask);
3572 arguments[0] = '\0';
3573 remaining = mask_size;
3574 if (mask & WINED3DSP_WRITEMASK_0)
3576 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3577 if (--remaining) strcat(arguments, ", ");
3579 if (mask & WINED3DSP_WRITEMASK_1)
3581 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3582 if (--remaining) strcat(arguments, ", ");
3584 if (mask & WINED3DSP_WRITEMASK_2)
3586 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3587 if (--remaining) strcat(arguments, ", ");
3589 if (mask & WINED3DSP_WRITEMASK_3)
3591 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3592 if (--remaining) strcat(arguments, ", ");
3595 if (mask_size > 1)
3596 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3597 else
3598 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3601 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3603 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3604 struct wined3d_string_buffer *reg_name;
3606 reg_name = string_buffer_get(priv->string_buffers);
3607 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, NULL, ins->ctx);
3608 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name->buffer, ins->dst[0].write_mask, fixup);
3609 string_buffer_release(priv->string_buffers, reg_name);
3612 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3613 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3614 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3615 const char *coord_reg_fmt, ...)
3617 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3618 char dst_swizzle[6];
3619 struct color_fixup_desc fixup;
3620 BOOL np2_fixup = FALSE;
3621 va_list args;
3622 int ret;
3624 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3626 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3627 * We actually rely on it for vertex shaders and SM4+. */
3628 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3630 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3631 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3633 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3634 np2_fixup = TRUE;
3636 else
3638 fixup = COLOR_FIXUP_IDENTITY;
3641 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, sample_function->data_type);
3643 if (sample_function->output_single_component)
3644 shader_addline(ins->ctx->buffer, "vec4(");
3646 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3647 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3649 for (;;)
3651 va_start(args, coord_reg_fmt);
3652 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3653 va_end(args);
3654 if (!ret)
3655 break;
3656 if (!string_buffer_resize(ins->ctx->buffer, ret))
3657 break;
3660 if (np2_fixup)
3662 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3663 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3665 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3667 case 1:
3668 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3669 idx >> 1, (idx % 2) ? "z" : "x");
3670 break;
3671 case 2:
3672 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3673 idx >> 1, (idx % 2) ? "zw" : "xy");
3674 break;
3675 case 3:
3676 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3677 idx >> 1, (idx % 2) ? "zw" : "xy");
3678 break;
3679 case 4:
3680 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3681 idx >> 1, (idx % 2) ? "zw" : "xy");
3682 break;
3685 if (dx && dy)
3686 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3687 else if (bias)
3688 shader_addline(ins->ctx->buffer, ", %s", bias);
3689 if (sample_function->offset_size)
3691 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3692 shader_addline(ins->ctx->buffer, ", ");
3693 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3695 shader_addline(ins->ctx->buffer, ")");
3697 if (sample_function->output_single_component)
3698 shader_addline(ins->ctx->buffer, ")");
3700 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3702 if (!is_identity_fixup(fixup))
3703 shader_glsl_color_correction(ins, fixup);
3706 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3708 /* Write the final position.
3710 * OpenGL coordinates specify the center of the pixel while D3D coords
3711 * specify the corner. The offsets are stored in z and w in
3712 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3713 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3714 * a MAD. */
3715 if (use_viewport_index)
3717 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3718 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3720 else
3722 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3723 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3726 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3727 * in utils.c
3729 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3730 * shaders are run before the homogeneous divide, so we have to take the w
3731 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3732 * z = z * 2 - w. */
3733 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3736 /*****************************************************************************
3737 * Begin processing individual instruction opcodes
3738 ****************************************************************************/
3740 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3742 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3743 struct glsl_src_param src0_param;
3744 struct glsl_src_param src1_param;
3745 DWORD write_mask;
3746 const char *op;
3748 /* Determine the GLSL operator to use based on the opcode */
3749 switch (ins->handler_idx)
3751 case WINED3DSIH_ADD: op = "+"; break;
3752 case WINED3DSIH_AND: op = "&"; break;
3753 case WINED3DSIH_DIV: op = "/"; break;
3754 case WINED3DSIH_IADD: op = "+"; break;
3755 case WINED3DSIH_ISHL: op = "<<"; break;
3756 case WINED3DSIH_ISHR: op = ">>"; break;
3757 case WINED3DSIH_MUL: op = "*"; break;
3758 case WINED3DSIH_OR: op = "|"; break;
3759 case WINED3DSIH_SUB: op = "-"; break;
3760 case WINED3DSIH_USHR: op = ">>"; break;
3761 case WINED3DSIH_XOR: op = "^"; break;
3762 default:
3763 op = "<unhandled operator>";
3764 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3765 break;
3768 write_mask = shader_glsl_append_dst(buffer, ins);
3769 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3770 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3771 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3774 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3776 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3777 struct glsl_src_param src0_param;
3778 struct glsl_src_param src1_param;
3779 unsigned int mask_size;
3780 DWORD write_mask;
3781 const char *op;
3783 write_mask = shader_glsl_append_dst(buffer, ins);
3784 mask_size = shader_glsl_get_write_mask_size(write_mask);
3785 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3786 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3788 if (mask_size > 1)
3790 switch (ins->handler_idx)
3792 case WINED3DSIH_EQ: op = "equal"; break;
3793 case WINED3DSIH_IEQ: op = "equal"; break;
3794 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3795 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3796 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3797 case WINED3DSIH_LT: op = "lessThan"; break;
3798 case WINED3DSIH_ILT: op = "lessThan"; break;
3799 case WINED3DSIH_ULT: op = "lessThan"; break;
3800 case WINED3DSIH_NE: op = "notEqual"; break;
3801 case WINED3DSIH_INE: op = "notEqual"; break;
3802 default:
3803 op = "<unhandled operator>";
3804 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3805 break;
3808 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3809 mask_size, op, src0_param.param_str, src1_param.param_str);
3811 else
3813 switch (ins->handler_idx)
3815 case WINED3DSIH_EQ: op = "=="; break;
3816 case WINED3DSIH_IEQ: op = "=="; break;
3817 case WINED3DSIH_GE: op = ">="; break;
3818 case WINED3DSIH_IGE: op = ">="; break;
3819 case WINED3DSIH_UGE: op = ">="; break;
3820 case WINED3DSIH_LT: op = "<"; break;
3821 case WINED3DSIH_ILT: op = "<"; break;
3822 case WINED3DSIH_ULT: op = "<"; break;
3823 case WINED3DSIH_NE: op = "!="; break;
3824 case WINED3DSIH_INE: op = "!="; break;
3825 default:
3826 op = "<unhandled operator>";
3827 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3828 break;
3831 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3832 src0_param.param_str, op, src1_param.param_str);
3836 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3838 struct glsl_src_param src_param;
3839 DWORD write_mask;
3840 const char *op;
3842 switch (ins->handler_idx)
3844 case WINED3DSIH_INEG: op = "-"; break;
3845 case WINED3DSIH_NOT: op = "~"; break;
3846 default:
3847 op = "<unhandled operator>";
3848 ERR("Unhandled opcode %s.\n",
3849 debug_d3dshaderinstructionhandler(ins->handler_idx));
3850 break;
3853 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3854 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3855 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3858 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3860 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3861 struct glsl_src_param src0_param;
3862 struct glsl_src_param src1_param;
3863 DWORD write_mask;
3865 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3866 * If not, we can emulate it. */
3867 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3868 FIXME("64-bit integer multiplies not implemented.\n");
3870 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3872 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3873 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3874 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3876 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3877 src0_param.param_str, src1_param.param_str);
3881 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3883 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3884 struct glsl_src_param src0_param, src1_param;
3885 DWORD write_mask;
3887 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3889 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3891 char dst_mask[6];
3893 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3894 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3895 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3896 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3897 dst_mask, src0_param.param_str, src1_param.param_str);
3899 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3900 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3901 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3902 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3904 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
3905 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3907 else
3909 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3910 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3911 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3912 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3915 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3917 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3918 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3919 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3920 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3924 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3925 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3927 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3928 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3929 const struct wined3d_gl_info *gl_info = priv->gl_info;
3930 struct glsl_src_param src0_param;
3931 DWORD write_mask;
3933 write_mask = shader_glsl_append_dst(buffer, ins);
3934 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3936 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3937 * shader versions WINED3DSIO_MOVA is used for this. */
3938 if (ins->ctx->reg_maps->shader_version.major == 1
3939 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3940 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3942 /* This is a simple floor() */
3943 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3944 if (mask_size > 1) {
3945 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3946 } else {
3947 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3950 else if (ins->handler_idx == WINED3DSIH_MOVA)
3952 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3954 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3956 if (mask_size > 1)
3957 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3958 else
3959 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3961 else
3963 if (mask_size > 1)
3964 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3965 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3966 else
3967 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3968 src0_param.param_str, src0_param.param_str);
3971 else
3973 shader_addline(buffer, "%s);\n", src0_param.param_str);
3977 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3978 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3980 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3981 struct glsl_src_param src0_param;
3982 struct glsl_src_param src1_param;
3983 DWORD dst_write_mask, src_write_mask;
3984 unsigned int dst_size;
3986 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3987 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3989 /* dp4 works on vec4, dp3 on vec3, etc. */
3990 if (ins->handler_idx == WINED3DSIH_DP4)
3991 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3992 else if (ins->handler_idx == WINED3DSIH_DP3)
3993 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3994 else
3995 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3997 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3998 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
4000 if (dst_size > 1) {
4001 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
4002 } else {
4003 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
4007 /* Note that this instruction has some restrictions. The destination write mask
4008 * can't contain the w component, and the source swizzles have to be .xyzw */
4009 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
4011 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4012 struct glsl_src_param src0_param;
4013 struct glsl_src_param src1_param;
4014 char dst_mask[6];
4016 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4017 shader_glsl_append_dst(ins->ctx->buffer, ins);
4018 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4019 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4020 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4023 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4025 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4027 if (!stream)
4028 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4029 else
4030 FIXME("Unhandled primitive stream %u.\n", stream);
4033 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4034 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4035 * GLSL uses the value as-is. */
4036 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4038 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4039 struct glsl_src_param src0_param;
4040 struct glsl_src_param src1_param;
4041 DWORD dst_write_mask;
4042 unsigned int dst_size;
4044 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4045 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4047 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4048 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4050 if (dst_size > 1)
4052 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4053 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4055 else
4057 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4058 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4062 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4063 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4065 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4066 bool y_correction = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4067 ? priv->cur_ps_args->y_correction : false;
4068 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4069 struct glsl_src_param src_param;
4070 const char *instruction;
4071 DWORD write_mask;
4072 unsigned i;
4074 /* Determine the GLSL function to use based on the opcode */
4075 /* TODO: Possibly make this a table for faster lookups */
4076 switch (ins->handler_idx)
4078 case WINED3DSIH_ABS: instruction = "abs"; break;
4079 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4080 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4081 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4082 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4083 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4084 case WINED3DSIH_DSY: instruction = y_correction ? "ycorrection.y * dFdy" : "dFdy"; break;
4085 case WINED3DSIH_DSY_COARSE: instruction = y_correction ? "ycorrection.y * dFdyCoarse" : "dFdyCoarse"; break;
4086 case WINED3DSIH_DSY_FINE: instruction = y_correction ? "ycorrection.y * dFdyFine" : "dFdyFine"; break;
4087 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4088 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4089 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4090 case WINED3DSIH_FRC: instruction = "fract"; break;
4091 case WINED3DSIH_IMAX: instruction = "max"; break;
4092 case WINED3DSIH_IMIN: instruction = "min"; break;
4093 case WINED3DSIH_MAX: instruction = "max"; break;
4094 case WINED3DSIH_MIN: instruction = "min"; break;
4095 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4096 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4097 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4098 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4099 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4100 case WINED3DSIH_UMAX: instruction = "max"; break;
4101 case WINED3DSIH_UMIN: instruction = "min"; break;
4102 default: instruction = "";
4103 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4104 break;
4107 write_mask = shader_glsl_append_dst(buffer, ins);
4109 /* In D3D bits are numbered from the most significant bit. */
4110 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4111 shader_addline(buffer, "31 - ");
4112 shader_addline(buffer, "%s(", instruction);
4114 if (ins->src_count)
4116 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4117 shader_addline(buffer, "%s", src_param.param_str);
4118 for (i = 1; i < ins->src_count; ++i)
4120 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4121 shader_addline(buffer, ", %s", src_param.param_str);
4125 shader_addline(buffer, "));\n");
4128 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4130 struct wined3d_shader_dst_param dst;
4131 struct glsl_src_param src;
4132 DWORD write_mask;
4133 const char *fmt;
4134 unsigned int i;
4136 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4137 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4139 dst = ins->dst[0];
4140 for (i = 0; i < 4; ++i)
4142 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4143 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4144 &dst, 0, dst.reg.data_type)))
4145 continue;
4147 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4148 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4152 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4154 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4155 struct wined3d_shader_dst_param dst;
4156 struct glsl_src_param src[4];
4157 const char *instruction;
4158 BOOL tmp_dst = FALSE;
4159 char mask_char[6];
4160 unsigned int i, j;
4161 DWORD write_mask;
4163 switch (ins->handler_idx)
4165 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4166 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4167 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4168 default:
4169 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4170 return;
4173 for (i = 0; i < ins->src_count; ++i)
4175 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4176 && ins->dst[0].reg.type == ins->src[i].reg.type)
4177 tmp_dst = TRUE;
4180 dst = ins->dst[0];
4181 for (i = 0; i < 4; ++i)
4183 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4184 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4185 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4186 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4187 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, 0, dst.reg.data_type)))
4188 continue;
4190 for (j = 0; j < ins->src_count; ++j)
4191 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4192 shader_addline(buffer, "%s(", instruction);
4193 for (j = 0; j < ins->src_count - 2; ++j)
4194 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4195 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4198 if (tmp_dst)
4200 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
4201 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4202 shader_addline(buffer, "tmp0%s);\n", mask_char);
4206 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4208 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4210 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4211 struct glsl_src_param src_param;
4212 unsigned int mask_size;
4213 DWORD write_mask;
4214 char dst_mask[6];
4216 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4217 mask_size = shader_glsl_get_write_mask_size(write_mask);
4218 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4220 if (mask_size > 3)
4221 shader_addline(buffer, "tmp0.x = dot(vec3(%s), vec3(%s));\n",
4222 src_param.param_str, src_param.param_str);
4223 else
4224 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4225 src_param.param_str, src_param.param_str);
4226 shader_glsl_append_dst(buffer, ins);
4228 shader_addline(buffer, "tmp0.x == 0.0 ? %s : (%s * inversesqrt(tmp0.x)));\n",
4229 src_param.param_str, src_param.param_str);
4232 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4234 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4235 ins->ctx->reg_maps->shader_version.minor);
4236 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4237 struct glsl_src_param src0_param;
4238 const char *prefix, *suffix;
4239 unsigned int dst_size;
4240 DWORD dst_write_mask;
4242 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4243 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4245 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4246 dst_write_mask = WINED3DSP_WRITEMASK_3;
4248 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4250 switch (ins->handler_idx)
4252 case WINED3DSIH_EXP:
4253 case WINED3DSIH_EXPP:
4254 prefix = "exp2(";
4255 suffix = ")";
4256 break;
4258 case WINED3DSIH_LOG:
4259 case WINED3DSIH_LOGP:
4260 prefix = "log2(abs(";
4261 suffix = "))";
4262 break;
4264 case WINED3DSIH_RCP:
4265 prefix = "1.0 / ";
4266 suffix = "";
4267 break;
4269 case WINED3DSIH_RSQ:
4270 prefix = "inversesqrt(abs(";
4271 suffix = "))";
4272 break;
4274 default:
4275 prefix = "";
4276 suffix = "";
4277 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4278 break;
4281 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4282 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4283 else
4284 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4287 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4288 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4289 * dst.x = 2^(floor(src))
4290 * dst.y = src - floor(src)
4291 * dst.z = 2^src (partial precision is allowed, but optional)
4292 * dst.w = 1.0;
4293 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4294 * dst = 2^src; (partial precision is allowed, but optional)
4296 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4298 if (ins->ctx->reg_maps->shader_version.major < 2)
4300 struct glsl_src_param src_param;
4301 char dst_mask[6];
4303 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4305 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4306 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4307 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4308 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4310 shader_glsl_append_dst(ins->ctx->buffer, ins);
4311 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4312 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4313 return;
4316 shader_glsl_scalar_op(ins);
4319 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4320 const char *vector_constructor, const char *scalar_constructor)
4322 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4323 struct glsl_src_param src_param;
4324 unsigned int mask_size;
4325 DWORD write_mask;
4327 write_mask = shader_glsl_append_dst(buffer, ins);
4328 mask_size = shader_glsl_get_write_mask_size(write_mask);
4329 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4331 if (mask_size > 1)
4332 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4333 else
4334 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4337 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4339 shader_glsl_cast(ins, "ivec", "int");
4342 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4344 shader_glsl_cast(ins, "uvec", "uint");
4347 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4349 shader_glsl_cast(ins, "vec", "float");
4352 /** Process signed comparison opcodes in GLSL. */
4353 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4355 struct glsl_src_param src0_param;
4356 struct glsl_src_param src1_param;
4357 DWORD write_mask;
4358 unsigned int mask_size;
4360 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4361 mask_size = shader_glsl_get_write_mask_size(write_mask);
4362 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4363 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4365 if (mask_size > 1) {
4366 const char *compare;
4368 switch(ins->handler_idx)
4370 case WINED3DSIH_SLT: compare = "lessThan"; break;
4371 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4372 default: compare = "";
4373 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4376 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4377 src0_param.param_str, src1_param.param_str);
4378 } else {
4379 switch(ins->handler_idx)
4381 case WINED3DSIH_SLT:
4382 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4383 * to return 0.0 but step returns 1.0 because step is not < x
4384 * An alternative is a bvec compare padded with an unused second component.
4385 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4386 * issue. Playing with not() is not possible either because not() does not accept
4387 * a scalar.
4389 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4390 src0_param.param_str, src1_param.param_str);
4391 break;
4392 case WINED3DSIH_SGE:
4393 /* Here we can use the step() function and safe a conditional */
4394 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4395 break;
4396 default:
4397 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4403 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4405 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4406 struct wined3d_shader_dst_param dst[2];
4407 struct glsl_src_param src[3];
4408 unsigned int i, j, k;
4409 char mask_char[6];
4410 DWORD write_mask;
4411 BOOL tmp_dst[2];
4413 for (i = 0; i < ins->dst_count; ++i)
4415 tmp_dst[i] = FALSE;
4416 for (j = 0; j < ins->src_count; ++j)
4418 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4419 && ins->dst[i].reg.type == ins->src[j].reg.type)
4420 tmp_dst[i] = TRUE;
4424 dst[0] = ins->dst[0];
4425 dst[1] = ins->dst[1];
4426 for (i = 0; i < 4; ++i)
4428 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4430 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4431 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4432 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4433 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], j, dst[j].reg.data_type)))
4434 continue;
4436 for (k = 0; k < ARRAY_SIZE(src); ++k)
4437 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4439 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4440 src[0].param_str, src[1].param_str, src[2].param_str);
4444 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4446 if (tmp_dst[i])
4448 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4449 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], i, ins->dst[i].reg.data_type);
4450 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4455 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4457 const char *condition_prefix, *condition_suffix;
4458 struct wined3d_shader_dst_param dst;
4459 struct glsl_src_param src0_param;
4460 struct glsl_src_param src1_param;
4461 struct glsl_src_param src2_param;
4462 BOOL temp_destination = FALSE;
4463 DWORD cmp_channel = 0;
4464 unsigned int i, j;
4465 char mask_char[6];
4466 DWORD write_mask;
4468 switch (ins->handler_idx)
4470 case WINED3DSIH_CMP:
4471 condition_prefix = "";
4472 condition_suffix = " >= 0.0";
4473 break;
4475 case WINED3DSIH_CND:
4476 condition_prefix = "";
4477 condition_suffix = " > 0.5";
4478 break;
4480 case WINED3DSIH_MOVC:
4481 condition_prefix = "bool(";
4482 condition_suffix = ")";
4483 break;
4485 default:
4486 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4487 condition_prefix = "<unhandled prefix>";
4488 condition_suffix = "<unhandled suffix>";
4489 break;
4492 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4494 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4495 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4496 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4497 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4499 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4500 condition_prefix, src0_param.param_str, condition_suffix,
4501 src1_param.param_str, src2_param.param_str);
4502 return;
4505 dst = ins->dst[0];
4507 /* Splitting the instruction up in multiple lines imposes a problem:
4508 * The first lines may overwrite source parameters of the following lines.
4509 * Deal with that by using a temporary destination register if needed. */
4510 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4511 && ins->src[0].reg.type == dst.reg.type)
4512 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4513 && ins->src[1].reg.type == dst.reg.type)
4514 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4515 && ins->src[2].reg.type == dst.reg.type))
4516 temp_destination = TRUE;
4518 /* Cycle through all source0 channels. */
4519 for (i = 0; i < 4; ++i)
4521 write_mask = 0;
4522 /* Find the destination channels which use the current source0 channel. */
4523 for (j = 0; j < 4; ++j)
4525 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4527 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4528 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4531 dst.write_mask = ins->dst[0].write_mask & write_mask;
4533 if (temp_destination)
4535 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4536 continue;
4537 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4539 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type)))
4540 continue;
4542 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4543 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4544 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4546 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4547 condition_prefix, src0_param.param_str, condition_suffix,
4548 src1_param.param_str, src2_param.param_str);
4551 if (temp_destination)
4553 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4554 shader_glsl_append_dst(ins->ctx->buffer, ins);
4555 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4559 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4560 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4561 * the compare is done per component of src0. */
4562 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4564 struct glsl_src_param src0_param;
4565 struct glsl_src_param src1_param;
4566 struct glsl_src_param src2_param;
4567 DWORD write_mask;
4568 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4569 ins->ctx->reg_maps->shader_version.minor);
4571 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4573 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4574 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4575 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4576 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4578 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4579 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4580 else
4581 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4582 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4583 return;
4586 shader_glsl_conditional_move(ins);
4589 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4590 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4592 struct glsl_src_param src0_param;
4593 struct glsl_src_param src1_param;
4594 struct glsl_src_param src2_param;
4595 DWORD write_mask;
4597 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4598 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4599 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4600 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4601 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4602 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4605 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4606 Vertex shaders to GLSL codes */
4607 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4609 int i;
4610 int nComponents = 0;
4611 struct wined3d_shader_dst_param tmp_dst = {{0}};
4612 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4613 struct wined3d_shader_instruction tmp_ins;
4615 memset(&tmp_ins, 0, sizeof(tmp_ins));
4617 /* Set constants for the temporary argument */
4618 tmp_ins.ctx = ins->ctx;
4619 tmp_ins.dst_count = 1;
4620 tmp_ins.dst = &tmp_dst;
4621 tmp_ins.src_count = 2;
4622 tmp_ins.src = tmp_src;
4624 switch(ins->handler_idx)
4626 case WINED3DSIH_M4x4:
4627 nComponents = 4;
4628 tmp_ins.handler_idx = WINED3DSIH_DP4;
4629 break;
4630 case WINED3DSIH_M4x3:
4631 nComponents = 3;
4632 tmp_ins.handler_idx = WINED3DSIH_DP4;
4633 break;
4634 case WINED3DSIH_M3x4:
4635 nComponents = 4;
4636 tmp_ins.handler_idx = WINED3DSIH_DP3;
4637 break;
4638 case WINED3DSIH_M3x3:
4639 nComponents = 3;
4640 tmp_ins.handler_idx = WINED3DSIH_DP3;
4641 break;
4642 case WINED3DSIH_M3x2:
4643 nComponents = 2;
4644 tmp_ins.handler_idx = WINED3DSIH_DP3;
4645 break;
4646 default:
4647 break;
4650 tmp_dst = ins->dst[0];
4651 tmp_src[0] = ins->src[0];
4652 tmp_src[1] = ins->src[1];
4653 for (i = 0; i < nComponents; ++i)
4655 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4656 shader_glsl_dot(&tmp_ins);
4657 ++tmp_src[1].reg.idx[0].offset;
4662 The LRP instruction performs a component-wise linear interpolation
4663 between the second and third operands using the first operand as the
4664 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4665 This is equivalent to mix(src2, src1, src0);
4667 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4669 struct glsl_src_param src0_param;
4670 struct glsl_src_param src1_param;
4671 struct glsl_src_param src2_param;
4672 DWORD write_mask;
4674 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4676 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4677 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4678 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4680 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4681 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4684 /** Process the WINED3DSIO_LIT instruction in GLSL:
4685 * dst.x = dst.w = 1.0
4686 * dst.y = (src0.x > 0) ? src0.x
4687 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4688 * where src.w is clamped at +- 128
4690 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4692 struct glsl_src_param src0_param;
4693 struct glsl_src_param src1_param;
4694 struct glsl_src_param src3_param;
4695 char dst_mask[6];
4697 shader_glsl_append_dst(ins->ctx->buffer, ins);
4698 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4700 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4701 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4702 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4704 /* The sdk specifies the instruction like this
4705 * dst.x = 1.0;
4706 * if(src.x > 0.0) dst.y = src.x
4707 * else dst.y = 0.0.
4708 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4709 * else dst.z = 0.0;
4710 * dst.w = 1.0;
4711 * (where power = src.w clamped between -128 and 128)
4713 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4714 * dst.x = 1.0 ... No further explanation needed
4715 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4716 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4717 * dst.w = 1.0. ... Nothing fancy.
4719 * So we still have one conditional in there. So do this:
4720 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4722 * 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),
4723 * 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.
4724 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4726 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4727 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4728 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4730 shader_addline(ins->ctx->buffer,
4731 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4732 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4733 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4734 src0_param.param_str, src3_param.param_str, dst_mask);
4737 /** Process the WINED3DSIO_DST instruction in GLSL:
4738 * dst.x = 1.0
4739 * dst.y = src0.x * src0.y
4740 * dst.z = src0.z
4741 * dst.w = src1.w
4743 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4745 struct glsl_src_param src0y_param;
4746 struct glsl_src_param src0z_param;
4747 struct glsl_src_param src1y_param;
4748 struct glsl_src_param src1w_param;
4749 char dst_mask[6];
4751 shader_glsl_append_dst(ins->ctx->buffer, ins);
4752 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4754 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4755 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4756 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4757 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4759 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4760 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4763 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4764 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4765 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4767 * dst.x = cos(src0.?)
4768 * dst.y = sin(src0.?)
4769 * dst.z = dst.z
4770 * dst.w = dst.w
4772 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4774 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4775 struct glsl_src_param src0_param;
4776 DWORD write_mask;
4778 if (ins->ctx->reg_maps->shader_version.major < 4)
4780 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4782 write_mask = shader_glsl_append_dst(buffer, ins);
4783 switch (write_mask)
4785 case WINED3DSP_WRITEMASK_0:
4786 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4787 break;
4789 case WINED3DSP_WRITEMASK_1:
4790 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4791 break;
4793 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4794 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4795 src0_param.param_str, src0_param.param_str);
4796 break;
4798 default:
4799 ERR("Write mask should be .x, .y or .xy\n");
4800 break;
4803 return;
4806 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4809 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4811 char dst_mask[6];
4813 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4814 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4815 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4817 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4818 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4819 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4821 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4822 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4824 else
4826 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4827 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4828 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4831 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4833 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4834 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4835 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4839 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4840 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4841 * generate invalid code
4843 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4845 struct glsl_src_param src0_param;
4846 DWORD write_mask;
4848 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4849 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4851 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4854 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4855 * Start a for() loop where src1.y is the initial value of aL,
4856 * increment aL by src1.z for a total of src1.x iterations.
4857 * Need to use a temporary variable for this operation.
4859 /* FIXME: I don't think nested loops will work correctly this way. */
4860 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4862 struct wined3d_shader_parser_state *state = ins->ctx->state;
4863 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4864 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4865 const struct wined3d_shader *shader = ins->ctx->shader;
4866 const struct wined3d_shader_lconst *constant;
4867 struct wined3d_string_buffer *reg_name;
4868 const unsigned int *control_values = NULL;
4870 if (ins->ctx->reg_maps->shader_version.major < 4)
4872 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4873 * class hardware doesn't support real varying indexing, but Microsoft
4874 * designed this feature for Shader model 2.x+. If the loop control is
4875 * known at compile time, the GLSL compiler can unroll the loop, and
4876 * replace indirect addressing with direct addressing. */
4877 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4879 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4881 if (constant->idx == ins->src[1].reg.idx[0].offset)
4883 control_values = constant->value;
4884 break;
4889 if (control_values)
4891 struct wined3d_shader_loop_control loop_control;
4892 loop_control.count = control_values[0];
4893 loop_control.start = control_values[1];
4894 loop_control.step = (int)control_values[2];
4896 if (loop_control.step > 0)
4898 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4899 state->current_loop_depth, loop_control.start,
4900 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4901 state->current_loop_depth, loop_control.step);
4903 else if (loop_control.step < 0)
4905 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4906 state->current_loop_depth, loop_control.start,
4907 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4908 state->current_loop_depth, loop_control.step);
4910 else
4912 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4913 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4914 state->current_loop_depth, loop_control.count,
4915 state->current_loop_depth);
4918 else
4920 reg_name = string_buffer_get(priv->string_buffers);
4921 shader_glsl_get_register_name(&ins->src[1].reg, ins->src[1].reg.data_type, reg_name, NULL, ins->ctx);
4923 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4924 state->current_loop_depth, state->current_loop_reg, reg_name->buffer,
4925 state->current_loop_depth, reg_name->buffer,
4926 state->current_loop_depth, state->current_loop_reg, reg_name->buffer);
4928 string_buffer_release(priv->string_buffers, reg_name);
4932 ++state->current_loop_reg;
4934 else
4936 shader_addline(buffer, "for (;;)\n{\n");
4939 ++state->current_loop_depth;
4942 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4944 struct wined3d_shader_parser_state *state = ins->ctx->state;
4946 shader_addline(ins->ctx->buffer, "}\n");
4948 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4950 --state->current_loop_depth;
4951 --state->current_loop_reg;
4954 if (ins->handler_idx == WINED3DSIH_ENDREP)
4956 --state->current_loop_depth;
4960 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4962 struct wined3d_shader_parser_state *state = ins->ctx->state;
4963 const struct wined3d_shader *shader = ins->ctx->shader;
4964 const struct wined3d_shader_lconst *constant;
4965 struct glsl_src_param src0_param;
4966 const unsigned int *control_values = NULL;
4968 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4969 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4971 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4973 if (constant->idx == ins->src[0].reg.idx[0].offset)
4975 control_values = constant->value;
4976 break;
4981 if (control_values)
4983 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4984 state->current_loop_depth, state->current_loop_depth,
4985 control_values[0], state->current_loop_depth);
4987 else
4989 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4990 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4991 state->current_loop_depth, state->current_loop_depth,
4992 src0_param.param_str, state->current_loop_depth);
4995 ++state->current_loop_depth;
4998 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
5000 struct glsl_src_param src0_param;
5002 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5003 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
5006 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
5008 struct glsl_src_param src0_param;
5010 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5011 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
5014 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
5016 shader_addline(ins->ctx->buffer, "default:\n");
5019 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
5021 struct glsl_src_param src_param;
5022 const char *condition;
5024 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5025 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5026 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
5029 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5031 shader_glsl_generate_condition(ins);
5032 shader_addline(ins->ctx->buffer, "{\n");
5035 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5037 struct glsl_src_param src0_param;
5038 struct glsl_src_param src1_param;
5040 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5041 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5043 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5044 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5047 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5049 shader_addline(ins->ctx->buffer, "} else {\n");
5052 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5054 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5055 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5056 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5058 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5059 if (!priv->gl_info->supported[ARB_CLIP_CONTROL])
5060 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5062 if (!stream)
5063 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5064 else
5065 FIXME("Unhandled primitive stream %u.\n", stream);
5068 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5070 shader_addline(ins->ctx->buffer, "break;\n");
5073 /* FIXME: According to MSDN the compare is done per component. */
5074 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5076 struct glsl_src_param src0_param;
5077 struct glsl_src_param src1_param;
5079 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5080 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5082 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5083 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5086 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5088 const char *op;
5090 switch (ins->handler_idx)
5092 case WINED3DSIH_BREAKP:
5093 op = "break;";
5094 break;
5095 case WINED3DSIH_CONTINUEP:
5096 op = "continue;";
5097 break;
5098 case WINED3DSIH_RETP:
5099 op = "return;";
5100 break;
5101 default:
5102 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5103 return;
5106 shader_glsl_generate_condition(ins);
5107 if (ins->handler_idx == WINED3DSIH_RETP)
5109 shader_addline(ins->ctx->buffer, "{\n");
5110 shader_glsl_generate_shader_epilogue(ins->ctx);
5112 shader_addline(ins->ctx->buffer, " %s\n", op);
5113 if (ins->handler_idx == WINED3DSIH_RETP)
5114 shader_addline(ins->ctx->buffer, "}\n");
5117 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5119 shader_addline(ins->ctx->buffer, "continue;\n");
5122 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5124 shader_addline(ins->ctx->buffer, "}\n");
5125 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5127 /* Subroutines appear at the end of the shader. */
5128 ins->ctx->state->in_subroutine = TRUE;
5131 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5133 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5136 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5138 struct glsl_src_param src1_param;
5140 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5141 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5142 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5145 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5147 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5149 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5151 shader_glsl_generate_shader_epilogue(ins->ctx);
5152 shader_addline(ins->ctx->buffer, "return;\n");
5156 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5158 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5159 ins->ctx->reg_maps->shader_version.minor);
5160 struct glsl_sample_function sample_function;
5161 DWORD sample_flags = 0;
5162 unsigned int resource_idx;
5163 DWORD mask = 0, swizzle;
5164 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5165 enum wined3d_shader_resource_type resource_type;
5167 /* 1.0-1.4: Use destination register as sampler source.
5168 * 2.0+: Use provided sampler source. */
5169 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5170 resource_idx = ins->dst[0].reg.idx[0].offset;
5171 else
5172 resource_idx = ins->src[1].reg.idx[0].offset;
5174 resource_type = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
5175 ? pixelshader_get_resource_type(ins->ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
5176 : ins->ctx->reg_maps->resource_info[resource_idx].type;
5178 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5180 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5181 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5183 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5184 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5186 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5187 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5189 case WINED3D_TTFF_COUNT1:
5190 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5191 break;
5192 case WINED3D_TTFF_COUNT2:
5193 mask = WINED3DSP_WRITEMASK_1;
5194 break;
5195 case WINED3D_TTFF_COUNT3:
5196 mask = WINED3DSP_WRITEMASK_2;
5197 break;
5198 case WINED3D_TTFF_COUNT4:
5199 case WINED3D_TTFF_DISABLE:
5200 mask = WINED3DSP_WRITEMASK_3;
5201 break;
5205 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5207 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5209 if (src_mod == WINED3DSPSM_DZ) {
5210 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5211 mask = WINED3DSP_WRITEMASK_2;
5212 } else if (src_mod == WINED3DSPSM_DW) {
5213 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5214 mask = WINED3DSP_WRITEMASK_3;
5217 else
5219 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5220 && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5222 /* ps 2.0 texldp instruction always divides by the fourth component. */
5223 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5224 mask = WINED3DSP_WRITEMASK_3;
5228 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5229 mask |= sample_function.coord_mask;
5230 sample_function.coord_mask = mask;
5232 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5233 else swizzle = ins->src[1].swizzle;
5235 /* 1.0-1.3: Use destination register as coordinate source.
5236 1.4+: Use provided coordinate source register. */
5237 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5239 char coord_mask[6];
5240 shader_glsl_write_mask_to_str(mask, coord_mask);
5241 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5242 "T%u%s", resource_idx, coord_mask);
5244 else
5246 struct glsl_src_param coord_param;
5247 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5248 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5250 struct glsl_src_param bias;
5251 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5252 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5253 NULL, "%s", coord_param.param_str);
5254 } else {
5255 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5256 "%s", coord_param.param_str);
5259 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5262 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5264 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5265 const struct wined3d_gl_info *gl_info = priv->gl_info;
5266 struct glsl_src_param coord_param, dx_param, dy_param;
5267 struct glsl_sample_function sample_function;
5268 DWORD sampler_idx;
5269 DWORD swizzle = ins->src[1].swizzle;
5271 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5273 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5274 shader_glsl_tex(ins);
5275 return;
5278 sampler_idx = ins->src[1].reg.idx[0].offset;
5280 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5281 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5282 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5283 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5285 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5286 NULL, NULL, "%s", coord_param.param_str);
5287 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5290 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5292 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5293 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5294 const struct wined3d_gl_info *gl_info = priv->gl_info;
5295 struct glsl_src_param coord_param, lod_param;
5296 struct glsl_sample_function sample_function;
5297 DWORD swizzle = ins->src[1].swizzle;
5298 DWORD sampler_idx;
5300 sampler_idx = ins->src[1].reg.idx[0].offset;
5302 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5303 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5305 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5307 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5308 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5310 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5311 * However, the NVIDIA drivers allow them in fragment shaders as well,
5312 * even without the appropriate extension. */
5313 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5315 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5316 "%s", coord_param.param_str);
5317 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5320 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5321 unsigned int resource_idx, unsigned int sampler_idx)
5323 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5324 unsigned int i;
5326 for (i = 0; i < sampler_map->count; ++i)
5328 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5329 return entries[i].bind_idx;
5332 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5334 return ~0u;
5337 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5339 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5340 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5341 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5342 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5343 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5344 struct glsl_src_param structure_idx, offset, data, data2;
5345 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5346 enum wined3d_shader_resource_type resource_type;
5347 struct wined3d_string_buffer *address;
5348 enum wined3d_data_type data_type;
5349 unsigned int resource_idx, stride;
5350 const char *op, *resource;
5351 DWORD coord_mask;
5352 BOOL is_tgsm;
5354 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5355 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5356 if (is_tgsm)
5358 if (resource_idx >= reg_maps->tgsm_count)
5360 ERR("Invalid TGSM index %u.\n", resource_idx);
5361 return;
5363 resource = "g";
5364 data_type = WINED3D_DATA_UINT;
5365 coord_mask = 1;
5366 stride = reg_maps->tgsm[resource_idx].stride;
5368 else
5370 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5372 ERR("Invalid UAV index %u.\n", resource_idx);
5373 return;
5375 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5376 if (resource_type >= ARRAY_SIZE(resource_type_info))
5378 ERR("Unexpected resource type %#x.\n", resource_type);
5379 return;
5381 resource = "image";
5382 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5383 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5384 stride = reg_maps->uav_resource_info[resource_idx].stride;
5387 switch (ins->handler_idx)
5389 case WINED3DSIH_ATOMIC_AND:
5390 case WINED3DSIH_IMM_ATOMIC_AND:
5391 if (is_tgsm)
5392 op = "atomicAnd";
5393 else
5394 op = "imageAtomicAnd";
5395 break;
5396 case WINED3DSIH_ATOMIC_CMP_STORE:
5397 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5398 if (is_tgsm)
5399 op = "atomicCompSwap";
5400 else
5401 op = "imageAtomicCompSwap";
5402 break;
5403 case WINED3DSIH_ATOMIC_IADD:
5404 case WINED3DSIH_IMM_ATOMIC_IADD:
5405 if (is_tgsm)
5406 op = "atomicAdd";
5407 else
5408 op = "imageAtomicAdd";
5409 break;
5410 case WINED3DSIH_ATOMIC_IMAX:
5411 case WINED3DSIH_IMM_ATOMIC_IMAX:
5412 if (is_tgsm)
5413 op = "atomicMax";
5414 else
5415 op = "imageAtomicMax";
5416 if (data_type != WINED3D_DATA_INT)
5418 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5419 return;
5421 break;
5422 case WINED3DSIH_ATOMIC_IMIN:
5423 case WINED3DSIH_IMM_ATOMIC_IMIN:
5424 if (is_tgsm)
5425 op = "atomicMin";
5426 else
5427 op = "imageAtomicMin";
5428 if (data_type != WINED3D_DATA_INT)
5430 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5431 return;
5433 break;
5434 case WINED3DSIH_ATOMIC_OR:
5435 case WINED3DSIH_IMM_ATOMIC_OR:
5436 if (is_tgsm)
5437 op = "atomicOr";
5438 else
5439 op = "imageAtomicOr";
5440 break;
5441 case WINED3DSIH_ATOMIC_UMAX:
5442 case WINED3DSIH_IMM_ATOMIC_UMAX:
5443 if (is_tgsm)
5444 op = "atomicMax";
5445 else
5446 op = "imageAtomicMax";
5447 if (data_type != WINED3D_DATA_UINT)
5449 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5450 return;
5452 break;
5453 case WINED3DSIH_ATOMIC_UMIN:
5454 case WINED3DSIH_IMM_ATOMIC_UMIN:
5455 if (is_tgsm)
5456 op = "atomicMin";
5457 else
5458 op = "imageAtomicMin";
5459 if (data_type != WINED3D_DATA_UINT)
5461 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5462 return;
5464 break;
5465 case WINED3DSIH_ATOMIC_XOR:
5466 case WINED3DSIH_IMM_ATOMIC_XOR:
5467 if (is_tgsm)
5468 op = "atomicXor";
5469 else
5470 op = "imageAtomicXor";
5471 break;
5472 case WINED3DSIH_IMM_ATOMIC_EXCH:
5473 if (is_tgsm)
5474 op = "atomicExchange";
5475 else
5476 op = "imageAtomicExchange";
5477 break;
5478 default:
5479 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5480 return;
5483 address = string_buffer_get(priv->string_buffers);
5484 if (stride)
5486 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5487 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5488 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5490 else
5492 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5493 string_buffer_sprintf(address, "%s", offset.param_str);
5494 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5495 shader_addline(address, "/ 4");
5498 if (is_imm_instruction)
5499 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5501 if (is_tgsm)
5502 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5503 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5504 else
5505 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5506 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5508 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5509 shader_addline(buffer, "%s", data.param_str);
5510 if (ins->src_count >= 3)
5512 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5513 shader_addline(buffer, ", %s", data2.param_str);
5516 if (is_imm_instruction)
5517 shader_addline(buffer, ")");
5518 shader_addline(buffer, ");\n");
5520 string_buffer_release(priv->string_buffers, address);
5523 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5525 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5526 const char *op;
5528 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5529 op = "atomicCounterIncrement";
5530 else
5531 op = "atomicCounterDecrement";
5533 shader_glsl_append_dst(ins->ctx->buffer, ins);
5534 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5537 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5539 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5540 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5541 enum wined3d_shader_resource_type resource_type;
5542 struct glsl_src_param image_coord_param;
5543 enum wined3d_data_type data_type;
5544 DWORD coord_mask, write_mask;
5545 unsigned int uav_idx;
5546 char dst_swizzle[6];
5548 uav_idx = ins->src[1].reg.idx[0].offset;
5549 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5551 ERR("Invalid UAV index %u.\n", uav_idx);
5552 return;
5554 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5555 if (resource_type >= ARRAY_SIZE(resource_type_info))
5557 ERR("Unexpected resource type %#x.\n", resource_type);
5558 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5560 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5561 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5563 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5564 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5566 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5567 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5568 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5571 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5573 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5574 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5575 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5576 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5577 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5578 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5579 struct glsl_src_param structure_idx, offset;
5580 struct wined3d_string_buffer *address;
5581 struct wined3d_shader_dst_param dst;
5582 const char *function, *resource;
5584 resource_idx = src->reg.idx[0].offset;
5585 if (src->reg.type == WINED3DSPR_RESOURCE)
5587 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5589 ERR("Invalid resource index %u.\n", resource_idx);
5590 return;
5592 stride = reg_maps->resource_info[resource_idx].stride;
5593 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5594 function = "texelFetch";
5595 resource = "sampler";
5597 else if (src->reg.type == WINED3DSPR_UAV)
5599 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5601 ERR("Invalid UAV index %u.\n", resource_idx);
5602 return;
5604 stride = reg_maps->uav_resource_info[resource_idx].stride;
5605 bind_idx = resource_idx;
5606 function = "imageLoad";
5607 resource = "image";
5609 else
5611 if (resource_idx >= reg_maps->tgsm_count)
5613 ERR("Invalid TGSM index %u.\n", resource_idx);
5614 return;
5616 stride = reg_maps->tgsm[resource_idx].stride;
5617 bind_idx = resource_idx;
5618 function = NULL;
5619 resource = "g";
5622 address = string_buffer_get(priv->string_buffers);
5623 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5625 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5626 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5628 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5629 shader_addline(address, "%s / 4", offset.param_str);
5631 dst = ins->dst[0];
5632 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5634 /* The instruction is split into multiple lines. The first lines may
5635 * overwrite source parameters of the following lines. */
5636 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5637 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5640 for (i = 0; i < 4; ++i)
5642 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5643 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type))
5644 continue;
5646 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5647 if (function)
5648 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5649 function, prefix, resource, bind_idx, address->buffer, swizzle);
5650 else
5651 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5652 prefix, resource, bind_idx, address->buffer, swizzle);
5655 string_buffer_release(priv->string_buffers, address);
5658 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5660 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5661 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5662 struct glsl_src_param image_coord_param, image_data_param;
5663 enum wined3d_shader_resource_type resource_type;
5664 enum wined3d_data_type data_type;
5665 unsigned int uav_idx;
5666 DWORD coord_mask;
5668 uav_idx = ins->dst[0].reg.idx[0].offset;
5669 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5671 ERR("Invalid UAV index %u.\n", uav_idx);
5672 return;
5674 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5675 if (resource_type >= ARRAY_SIZE(resource_type_info))
5677 ERR("Unexpected resource type %#x.\n", resource_type);
5678 return;
5680 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5681 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5683 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5684 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5685 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5686 shader_glsl_get_prefix(version->type), uav_idx,
5687 image_coord_param.param_str, image_data_param.param_str);
5690 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5692 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5693 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5694 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5695 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5696 struct glsl_src_param structure_idx, offset, data;
5697 unsigned int i, resource_idx, stride, src_idx = 0;
5698 struct wined3d_string_buffer *address;
5699 DWORD write_mask;
5700 BOOL is_tgsm;
5702 resource_idx = ins->dst[0].reg.idx[0].offset;
5703 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5704 if (is_tgsm)
5706 if (resource_idx >= reg_maps->tgsm_count)
5708 ERR("Invalid TGSM index %u.\n", resource_idx);
5709 return;
5711 stride = reg_maps->tgsm[resource_idx].stride;
5713 else
5715 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5717 ERR("Invalid UAV index %u.\n", resource_idx);
5718 return;
5720 stride = reg_maps->uav_resource_info[resource_idx].stride;
5723 address = string_buffer_get(priv->string_buffers);
5724 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5726 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5727 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5729 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5730 shader_addline(address, "%s / 4", offset.param_str);
5732 for (i = 0; i < 4; ++i)
5734 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5735 continue;
5737 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5739 if (is_tgsm)
5740 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5741 prefix, resource_idx, address->buffer, i, data.param_str);
5742 else
5743 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5744 prefix, resource_idx, address->buffer, i, data.param_str);
5747 string_buffer_release(priv->string_buffers, address);
5750 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5752 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5753 unsigned int sync_flags = ins->flags;
5755 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5757 shader_addline(buffer, "barrier();\n");
5758 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5761 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5763 shader_addline(buffer, "memoryBarrierShared();\n");
5764 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5767 if (sync_flags & WINED3DSSF_GLOBAL_UAV)
5769 shader_addline(buffer, "memoryBarrier();\n");
5770 sync_flags &= ~WINED3DSSF_GLOBAL_UAV;
5773 if (sync_flags)
5774 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5777 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5778 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5780 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5781 unsigned int idx = reg->idx[0].offset;
5783 if (reg->type == WINED3DSPR_RESOURCE)
5785 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5787 ERR("Invalid resource index %u.\n", idx);
5788 return NULL;
5790 return &reg_maps->resource_info[idx];
5793 if (reg->type == WINED3DSPR_UAV)
5795 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5797 ERR("Invalid UAV index %u.\n", idx);
5798 return NULL;
5800 return &reg_maps->uav_resource_info[idx];
5803 FIXME("Unhandled register type %#x.\n", reg->type);
5804 return NULL;
5807 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5809 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5810 const struct wined3d_shader_resource_info *resource_info;
5811 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5812 unsigned int resource_idx;
5813 char dst_swizzle[6];
5814 DWORD write_mask;
5816 write_mask = shader_glsl_append_dst(buffer, ins);
5817 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5819 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5820 return;
5821 resource_idx = ins->src[0].reg.idx[0].offset;
5823 shader_addline(buffer, "ivec2(");
5824 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5826 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5827 resource_idx, WINED3D_SAMPLER_DEFAULT);
5828 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5830 else
5832 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5834 if (resource_info->stride)
5835 shader_addline(buffer, " / %u", resource_info->stride);
5836 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5837 shader_addline(buffer, " * 4");
5838 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5841 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5843 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5844 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5847 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5849 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5852 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5854 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5855 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5856 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5857 const struct wined3d_gl_info *gl_info = priv->gl_info;
5858 enum wined3d_shader_resource_type resource_type;
5859 enum wined3d_shader_register_type reg_type;
5860 unsigned int resource_idx, bind_idx, i;
5861 enum wined3d_data_type dst_data_type;
5862 struct glsl_src_param lod_param;
5863 BOOL supports_mipmaps;
5864 char dst_swizzle[6];
5865 DWORD write_mask;
5867 dst_data_type = ins->dst[0].reg.data_type;
5868 if (ins->flags == WINED3DSI_RESINFO_UINT)
5869 dst_data_type = WINED3D_DATA_UINT;
5870 else if (ins->flags)
5871 FIXME("Unhandled flags %#x.\n", ins->flags);
5873 reg_type = ins->src[1].reg.type;
5874 resource_idx = ins->src[1].reg.idx[0].offset;
5875 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5876 if (reg_type == WINED3DSPR_RESOURCE)
5878 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5879 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5880 resource_idx, WINED3D_SAMPLER_DEFAULT);
5882 else
5884 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5885 bind_idx = resource_idx;
5888 if (resource_type >= ARRAY_SIZE(resource_type_info))
5890 ERR("Unexpected resource type %#x.\n", resource_type);
5891 return;
5894 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, dst_data_type);
5895 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5897 if (dst_data_type == WINED3D_DATA_UINT)
5898 shader_addline(buffer, "uvec4(");
5899 else
5900 shader_addline(buffer, "vec4(");
5902 if (reg_type == WINED3DSPR_RESOURCE)
5904 shader_addline(buffer, "textureSize(%s_sampler%u",
5905 shader_glsl_get_prefix(version->type), bind_idx);
5907 else
5909 shader_addline(buffer, "imageSize(%s_image%u",
5910 shader_glsl_get_prefix(version->type), bind_idx);
5913 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5914 if (supports_mipmaps)
5915 shader_addline(buffer, ", %s", lod_param.param_str);
5916 shader_addline(buffer, "), ");
5918 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5919 shader_addline(buffer, "0, ");
5921 if (supports_mipmaps)
5923 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5925 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5926 shader_glsl_get_prefix(version->type), bind_idx);
5928 else
5930 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
5931 shader_addline(buffer, "1");
5934 else
5936 shader_addline(buffer, "1");
5939 shader_addline(buffer, ")%s);\n", dst_swizzle);
5942 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
5944 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5945 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5946 const struct wined3d_gl_info *gl_info = priv->gl_info;
5947 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5948 const struct wined3d_shader_dst_param *dst = ins->dst;
5949 const struct wined3d_shader_src_param *src = ins->src;
5950 enum wined3d_shader_resource_type resource_type;
5951 enum wined3d_data_type dst_data_type;
5952 unsigned int resource_idx, bind_idx;
5953 char dst_swizzle[6];
5954 DWORD write_mask;
5956 dst_data_type = dst->reg.data_type;
5957 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
5958 dst_data_type = WINED3D_DATA_UINT;
5959 else if (ins->flags)
5960 FIXME("Unhandled flags %#x.\n", ins->flags);
5962 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, 0, dst_data_type);
5963 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
5965 if (dst_data_type == WINED3D_DATA_UINT)
5966 shader_addline(buffer, "uvec4(");
5967 else
5968 shader_addline(buffer, "vec4(");
5970 if (src->reg.type == WINED3DSPR_RASTERIZER)
5972 if (gl_info->supported[ARB_SAMPLE_SHADING])
5974 shader_addline(buffer, "gl_NumSamples");
5976 else
5978 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
5979 shader_addline(buffer, "1");
5982 else
5984 resource_idx = src->reg.idx[0].offset;
5985 resource_type = reg_maps->resource_info[resource_idx].type;
5986 if (resource_type >= ARRAY_SIZE(resource_type_info))
5988 ERR("Unexpected resource type %#x.\n", resource_type);
5989 return;
5991 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5993 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
5995 shader_addline(buffer, "textureSamples(%s_sampler%u)",
5996 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
5998 else
6000 FIXME("textureSamples() is not supported.\n");
6001 shader_addline(buffer, "1");
6005 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
6008 static void shader_glsl_interpolate(const struct wined3d_shader_instruction *ins)
6010 const struct wined3d_shader_src_param *input = &ins->src[0];
6011 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6012 struct glsl_src_param sample_param;
6013 char dst_swizzle[6];
6014 DWORD write_mask;
6016 write_mask = shader_glsl_append_dst(buffer, ins);
6017 shader_glsl_get_swizzle(input, FALSE, write_mask, dst_swizzle);
6019 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &sample_param);
6020 shader_addline(buffer, "interpolateAtSample(shader_in.reg%u, %s)%s);\n",
6021 input->reg.idx[0].offset, sample_param.param_str, dst_swizzle);
6024 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6026 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6027 struct glsl_src_param coord_param, lod_param, sample_param;
6028 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6029 struct glsl_sample_function sample_function;
6030 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6031 BOOL has_lod_param;
6033 if (wined3d_shader_instruction_has_texel_offset(ins))
6034 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6036 resource_idx = ins->src[1].reg.idx[0].offset;
6037 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6039 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6041 ERR("Invalid resource index %u.\n", resource_idx);
6042 return;
6044 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6046 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6047 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6048 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6049 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6050 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6052 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6053 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6054 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6056 else
6058 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6059 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6060 "%s", coord_param.param_str);
6062 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6065 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6067 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6068 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6069 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6070 struct glsl_sample_function sample_function;
6071 DWORD flags = 0;
6073 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6074 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6075 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6076 flags |= WINED3D_GLSL_SAMPLE_LOD;
6077 if (wined3d_shader_instruction_has_texel_offset(ins))
6078 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6080 resource_idx = ins->src[1].reg.idx[0].offset;
6081 sampler_idx = ins->src[2].reg.idx[0].offset;
6083 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6084 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6086 switch (ins->handler_idx)
6088 case WINED3DSIH_SAMPLE:
6089 break;
6090 case WINED3DSIH_SAMPLE_B:
6091 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6092 lod_param_str = lod_param.param_str;
6093 break;
6094 case WINED3DSIH_SAMPLE_GRAD:
6095 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6096 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6097 dx_param_str = dx_param.param_str;
6098 dy_param_str = dy_param.param_str;
6099 break;
6100 case WINED3DSIH_SAMPLE_LOD:
6101 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6102 lod_param_str = lod_param.param_str;
6103 break;
6104 default:
6105 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6106 break;
6109 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6110 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6111 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6112 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6115 /* Unless EXT_texture_shadow_lod is available, GLSL doesn't provide a function
6116 * to sample from level zero with depth comparison for array textures and cube
6117 * textures. We use textureGrad*() to implement sample_c_lz in that case. */
6118 static void shader_glsl_gen_sample_c_lz_emulation(const struct wined3d_shader_instruction *ins,
6119 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6120 unsigned int coord_size, const char *coord_param, const char *ref_param)
6122 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6123 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6124 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6125 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6126 char dst_swizzle[6];
6128 WARN("Emitting textureGrad() for sample_c_lz.\n");
6130 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6131 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, sample_function->data_type);
6132 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6133 sample_function->offset_size ? "Offset" : "",
6134 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6135 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6136 if (sample_function->offset_size)
6138 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6139 shader_addline(buffer, ", ");
6140 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6142 shader_addline(buffer, "))%s);\n", dst_swizzle);
6145 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6147 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6148 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6149 const struct wined3d_shader_resource_info *resource_info;
6150 const struct wined3d_gl_info *gl_info = priv->gl_info;
6151 struct glsl_src_param coord_param, compare_param;
6152 struct glsl_sample_function sample_function;
6153 const char *lod_param = NULL;
6154 unsigned int coord_size;
6155 DWORD flags = 0;
6157 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6159 lod_param = "0";
6160 flags |= WINED3D_GLSL_SAMPLE_LOD;
6163 if (wined3d_shader_instruction_has_texel_offset(ins))
6164 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6166 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6167 return;
6168 resource_idx = ins->src[1].reg.idx[0].offset;
6169 sampler_idx = ins->src[2].reg.idx[0].offset;
6171 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6172 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6173 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6174 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6175 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6176 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6177 && !gl_info->supported[EXT_TEXTURE_SHADOW_LOD]
6178 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6179 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6181 shader_glsl_gen_sample_c_lz_emulation(ins, sampler_bind_idx, &sample_function,
6182 coord_size, coord_param.param_str, compare_param.param_str);
6184 else
6186 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6187 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6188 coord_size, coord_param.param_str, compare_param.param_str);
6190 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6193 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6195 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6196 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6197 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6198 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6199 struct glsl_src_param coord_param, compare_param, offset_param;
6200 const struct wined3d_gl_info *gl_info = priv->gl_info;
6201 const struct wined3d_shader_resource_info *resource_info;
6202 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6203 unsigned int coord_size, offset_size;
6204 char dst_swizzle[6];
6205 BOOL has_offset;
6207 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6209 FIXME("OpenGL implementation does not support textureGather.\n");
6210 return;
6213 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6214 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6215 || wined3d_shader_instruction_has_texel_offset(ins);
6217 resource_param_idx =
6218 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6219 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6220 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6221 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6222 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6224 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6225 return;
6227 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6229 ERR("Unexpected resource type %#x.\n", resource_info->type);
6230 return;
6232 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6234 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6235 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, resource_info->data_type);
6237 shader_glsl_add_src_param(ins, &ins->src[0], wined3d_mask_from_size(coord_size), &coord_param);
6239 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6240 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6241 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6243 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6244 shader_addline(buffer, ", %s", compare_param.param_str);
6246 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6248 shader_glsl_add_src_param(ins, &ins->src[1], wined3d_mask_from_size(offset_size), &offset_param);
6249 shader_addline(buffer, ", %s", offset_param.param_str);
6251 else if (has_offset)
6253 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6254 shader_addline(buffer, ", ");
6255 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6257 if (component_idx)
6258 shader_addline(buffer, ", %u", component_idx);
6260 shader_addline(buffer, ")%s);\n", dst_swizzle);
6263 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6265 /* FIXME: Make this work for more than just 2D textures */
6266 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6267 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6269 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6271 char dst_mask[6];
6273 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6274 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6275 ins->dst[0].reg.idx[0].offset, dst_mask);
6277 else
6279 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6280 unsigned int reg = ins->src[0].reg.idx[0].offset;
6281 char dst_swizzle[6];
6283 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6285 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6287 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6288 struct glsl_src_param div_param;
6289 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6291 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6293 if (mask_size > 1)
6294 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6295 else
6296 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6298 else
6300 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6305 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6306 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6307 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6308 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6310 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6311 unsigned int sampler_idx = ins->dst[0].reg.idx[0].offset;
6312 struct glsl_sample_function sample_function;
6313 struct glsl_src_param src0_param;
6314 UINT mask_size;
6316 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6318 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6319 * scalar, and projected sampling would require 4.
6321 * It is a dependent read - not valid with conditional NP2 textures
6323 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6324 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6326 switch(mask_size)
6328 case 1:
6329 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6330 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6331 break;
6333 case 2:
6334 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6335 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6336 break;
6338 case 3:
6339 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6340 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6341 break;
6343 default:
6344 FIXME("Unexpected mask size %u\n", mask_size);
6345 break;
6347 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6350 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6351 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6352 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6354 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6355 unsigned int dstreg = ins->dst[0].reg.idx[0].offset;
6356 struct glsl_src_param src0_param;
6357 DWORD dst_mask;
6358 unsigned int mask_size;
6360 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6361 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6362 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6364 if (mask_size > 1) {
6365 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6366 } else {
6367 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6371 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6372 * Calculate the depth as dst.x / dst.y */
6373 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6375 struct glsl_dst_param dst_param;
6377 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6379 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6380 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6381 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6382 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6383 * >= 1.0 or < 0.0
6385 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6386 dst_param.reg_name, dst_param.reg_name);
6389 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6390 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6391 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6392 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6394 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6396 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6397 unsigned int dstreg = ins->dst[0].reg.idx[0].offset;
6398 struct glsl_src_param src0_param;
6400 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6402 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6403 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6406 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6407 * Calculate the 1st of a 2-row matrix multiplication. */
6408 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6410 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6411 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6412 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6413 struct glsl_src_param src0_param;
6415 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6416 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6419 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6420 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6421 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6423 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6424 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6425 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6426 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6427 struct glsl_src_param src0_param;
6429 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6430 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6431 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6434 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6436 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6437 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6438 struct glsl_sample_function sample_function;
6439 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6440 struct glsl_src_param src0_param;
6442 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6443 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6445 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6447 /* Sample the texture using the calculated coordinates */
6448 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6449 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6452 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6453 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6454 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6456 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6457 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6458 struct glsl_sample_function sample_function;
6459 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6460 struct glsl_src_param src0_param;
6462 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6463 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6465 /* Dependent read, not valid with conditional NP2 */
6466 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6468 /* Sample the texture using the calculated coordinates */
6469 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6470 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6472 tex_mx->current_row = 0;
6475 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6476 * Perform the 3rd row of a 3x3 matrix multiply */
6477 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6479 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6480 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6481 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6482 struct glsl_src_param src0_param;
6483 char dst_mask[6];
6485 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6487 shader_glsl_append_dst(ins->ctx->buffer, ins);
6488 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6489 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6491 tex_mx->current_row = 0;
6494 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6495 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6496 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6498 struct glsl_src_param src0_param;
6499 struct glsl_src_param src1_param;
6500 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6501 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6502 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6503 struct glsl_sample_function sample_function;
6504 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6505 char coord_mask[6];
6507 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6508 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6510 /* Perform the last matrix multiply operation */
6511 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6512 /* Reflection calculation */
6513 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6515 /* Dependent read, not valid with conditional NP2 */
6516 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6517 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6519 /* Sample the texture */
6520 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6521 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6522 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6524 tex_mx->current_row = 0;
6527 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6528 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6529 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6531 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6532 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6533 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6534 struct glsl_sample_function sample_function;
6535 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6536 struct glsl_src_param src0_param;
6537 char coord_mask[6];
6539 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6541 /* Perform the last matrix multiply operation */
6542 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6544 /* Construct the eye-ray vector from w coordinates */
6545 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6546 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6547 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6549 /* Dependent read, not valid with conditional NP2 */
6550 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6551 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6553 /* Sample the texture using the calculated coordinates */
6554 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6555 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6556 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6558 tex_mx->current_row = 0;
6561 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6562 * Apply a fake bump map transform.
6563 * texbem is pshader <= 1.3 only, this saves a few version checks
6565 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6567 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6568 struct glsl_sample_function sample_function;
6569 struct glsl_src_param coord_param;
6570 unsigned int sampler_idx;
6571 DWORD mask;
6572 DWORD flags;
6573 char coord_mask[6];
6575 sampler_idx = ins->dst[0].reg.idx[0].offset;
6576 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6577 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6579 /* Dependent read, not valid with conditional NP2 */
6580 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6581 mask = sample_function.coord_mask;
6583 shader_glsl_write_mask_to_str(mask, coord_mask);
6585 /* With projected textures, texbem only divides the static texture coord,
6586 * not the displacement, so we can't let GL handle this. */
6587 if (flags & WINED3D_PSARGS_PROJECTED)
6589 DWORD div_mask=0;
6590 char coord_div_mask[3];
6591 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6593 case WINED3D_TTFF_COUNT1:
6594 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6595 break;
6596 case WINED3D_TTFF_COUNT2:
6597 div_mask = WINED3DSP_WRITEMASK_1;
6598 break;
6599 case WINED3D_TTFF_COUNT3:
6600 div_mask = WINED3DSP_WRITEMASK_2;
6601 break;
6602 case WINED3D_TTFF_COUNT4:
6603 case WINED3D_TTFF_DISABLE:
6604 div_mask = WINED3DSP_WRITEMASK_3;
6605 break;
6607 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6608 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6611 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6613 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6614 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6615 coord_param.param_str, coord_mask);
6617 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6619 struct glsl_src_param luminance_param;
6620 struct glsl_dst_param dst_param;
6622 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6623 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6625 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6626 dst_param.reg_name, dst_param.mask_str,
6627 luminance_param.param_str, sampler_idx, sampler_idx);
6629 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6632 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6634 unsigned int sampler_idx = ins->dst[0].reg.idx[0].offset;
6635 struct glsl_src_param src0_param, src1_param;
6637 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6638 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6640 shader_glsl_append_dst(ins->ctx->buffer, ins);
6641 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6642 src0_param.param_str, sampler_idx, src1_param.param_str);
6645 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6646 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6647 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6649 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6650 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6651 struct glsl_sample_function sample_function;
6652 struct wined3d_string_buffer *reg_name;
6654 reg_name = string_buffer_get(priv->string_buffers);
6655 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6657 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6658 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6659 "%s.wx", reg_name->buffer);
6660 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6662 string_buffer_release(priv->string_buffers, reg_name);
6665 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6666 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6667 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
6669 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6670 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6671 struct glsl_sample_function sample_function;
6672 struct wined3d_string_buffer *reg_name;
6674 reg_name = string_buffer_get(priv->string_buffers);
6675 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6677 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6678 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6679 "%s.yz", reg_name->buffer);
6680 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6682 string_buffer_release(priv->string_buffers, reg_name);
6685 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
6686 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6687 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6689 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6690 struct glsl_sample_function sample_function;
6691 struct glsl_src_param src0_param;
6693 /* Dependent read, not valid with conditional NP2 */
6694 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6695 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6697 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6698 "%s", src0_param.param_str);
6699 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6702 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6703 * If any of the first 3 components are < 0, discard this pixel */
6704 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6706 if (ins->ctx->reg_maps->shader_version.major >= 4)
6708 shader_glsl_generate_condition(ins);
6709 shader_addline(ins->ctx->buffer, " discard;\n");
6711 else
6713 struct glsl_dst_param dst_param;
6715 /* The argument is a destination parameter, and no writemasks are allowed */
6716 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6718 /* 2.0 shaders compare all 4 components in texkill. */
6719 if (ins->ctx->reg_maps->shader_version.major >= 2)
6720 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6721 /* 1.x shaders only compare the first 3 components, probably due to
6722 * the nature of the texkill instruction as a tex* instruction, and
6723 * phase, which kills all .w components. Even if all 4 components are
6724 * defined, only the first 3 are used. */
6725 else
6726 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6730 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6731 * dst = dot2(src0, src1) + src2 */
6732 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6734 struct glsl_src_param src0_param;
6735 struct glsl_src_param src1_param;
6736 struct glsl_src_param src2_param;
6737 DWORD write_mask;
6738 unsigned int mask_size;
6740 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6741 mask_size = shader_glsl_get_write_mask_size(write_mask);
6743 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6744 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6745 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6747 if (mask_size > 1) {
6748 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6749 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6750 } else {
6751 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6752 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6756 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6757 const struct wined3d_shader_signature *input_signature,
6758 const struct wined3d_shader_reg_maps *reg_maps,
6759 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6761 unsigned int i;
6763 for (i = 0; i < input_signature->element_count; ++i)
6765 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6766 const char *semantic_name;
6767 UINT semantic_idx;
6768 char reg_mask[6];
6770 /* Unused */
6771 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6772 continue;
6774 semantic_name = input->semantic_name;
6775 semantic_idx = input->semantic_idx;
6776 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6778 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6780 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6782 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6783 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6785 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6787 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6789 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6791 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6792 input->register_idx, reg_mask);
6794 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6796 if (gl_info->supported[ARB_SAMPLE_SHADING])
6797 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6798 input->register_idx, reg_mask);
6799 else
6800 FIXME("ARB_sample_shading is not supported.\n");
6802 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6804 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6805 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6806 input->register_idx, reg_mask);
6807 else
6808 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6810 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6812 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6813 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6814 input->register_idx, reg_mask);
6815 else
6816 FIXME("ARB_viewport_array is not supported.\n");
6818 else
6820 if (input->sysval_semantic)
6821 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6822 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6823 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6824 shader_glsl_shader_input_name(gl_info),
6825 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6828 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6830 if (args->pointsprite)
6831 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6832 shader->u.ps.input_reg_map[input->register_idx]);
6833 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6834 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6835 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6836 needs_legacy_glsl_syntax(gl_info)
6837 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6838 else
6839 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6840 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6842 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6844 if (!semantic_idx)
6845 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6846 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6847 else if (semantic_idx == 1)
6848 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6849 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6850 else
6851 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6852 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6854 else
6856 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6857 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6862 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6864 struct glsl_program_key key;
6866 key.vs_id = entry->vs.id;
6867 key.hs_id = entry->hs.id;
6868 key.ds_id = entry->ds.id;
6869 key.gs_id = entry->gs.id;
6870 key.ps_id = entry->ps.id;
6871 key.cs_id = entry->cs.id;
6873 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6875 ERR("Failed to insert program entry.\n");
6879 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6880 const struct glsl_program_key *key)
6882 struct wine_rb_entry *entry;
6884 entry = wine_rb_get(&priv->program_lookup, key);
6885 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6888 /* Context activation is done by the caller. */
6889 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6890 struct glsl_shader_prog_link *entry)
6892 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6894 GL_EXTCALL(glDeleteProgram(entry->id));
6895 if (entry->vs.id)
6896 list_remove(&entry->vs.shader_entry);
6897 if (entry->hs.id)
6898 list_remove(&entry->hs.shader_entry);
6899 if (entry->ds.id)
6900 list_remove(&entry->ds.shader_entry);
6901 if (entry->gs.id)
6902 list_remove(&entry->gs.shader_entry);
6903 if (entry->ps.id)
6904 list_remove(&entry->ps.shader_entry);
6905 if (entry->cs.id)
6906 list_remove(&entry->cs.shader_entry);
6907 heap_free(entry);
6910 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6911 const struct wined3d_gl_info *gl_info, const unsigned int *map,
6912 const struct wined3d_shader_signature *input_signature,
6913 const struct wined3d_shader_reg_maps *reg_maps_in,
6914 const struct wined3d_shader_signature *output_signature,
6915 const struct wined3d_shader_reg_maps *reg_maps_out)
6917 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6918 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6919 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6920 unsigned int in_count = vec4_varyings(3, gl_info);
6921 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6922 unsigned int in_idx;
6923 DWORD *set = NULL;
6924 unsigned int i, j;
6925 char reg_mask[6];
6927 set = heap_calloc(max_varyings, sizeof(*set));
6929 for (i = 0; i < input_signature->element_count; ++i)
6931 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6933 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6934 continue;
6936 in_idx = map[input->register_idx];
6937 /* Declared, but not read register */
6938 if (in_idx == ~0u)
6939 continue;
6940 if (in_idx >= max_varyings)
6942 FIXME("More input varyings declared than supported, expect issues.\n");
6943 continue;
6946 if (in_idx == in_count)
6947 string_buffer_sprintf(destination, "gl_FrontColor");
6948 else if (in_idx == in_count + 1)
6949 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6950 else
6951 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6953 if (!set[in_idx])
6954 set[in_idx] = ~0u;
6956 for (j = 0; j < output_signature->element_count; ++j)
6958 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6959 DWORD mask;
6961 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6962 || input->semantic_idx != output->semantic_idx
6963 || strcmp(input->semantic_name, output->semantic_name)
6964 || !(mask = input->mask & output->mask))
6965 continue;
6967 if (set[in_idx] == ~0u)
6968 set[in_idx] = 0;
6969 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6970 shader_glsl_write_mask_to_str(mask, reg_mask);
6972 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6973 destination->buffer, reg_mask, output->register_idx, reg_mask);
6977 for (i = 0; i < max_varyings; ++i)
6979 unsigned int size;
6981 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6982 continue;
6984 if (set[i] == ~0u)
6985 set[i] = 0;
6987 size = 0;
6988 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6989 reg_mask[size++] = 'x';
6990 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6991 reg_mask[size++] = 'y';
6992 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6993 reg_mask[size++] = 'z';
6994 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6995 reg_mask[size++] = 'w';
6996 reg_mask[size] = '\0';
6998 if (i == in_count)
6999 string_buffer_sprintf(destination, "gl_FrontColor");
7000 else if (i == in_count + 1)
7001 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
7002 else
7003 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
7005 if (size == 1)
7006 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
7007 else
7008 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
7011 heap_free(set);
7012 string_buffer_release(&priv->string_buffers, destination);
7015 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
7016 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
7017 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
7018 BOOL rasterizer_setup)
7020 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7021 char reg_mask[6];
7022 unsigned int i;
7024 for (i = 0; i < output_signature->element_count; ++i)
7026 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7028 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7029 continue;
7031 if (output->stream_idx)
7032 continue;
7034 if (output->register_idx >= input_count)
7035 continue;
7037 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7039 shader_addline(buffer,
7040 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7041 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7045 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7046 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7048 unsigned int i, clip_or_cull_index;
7049 const char *name;
7050 char reg_mask[6];
7052 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7053 /* Assign consecutive indices starting from 0. */
7054 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7055 for (i = 0; i < 4; ++i)
7057 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7058 continue;
7060 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7061 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7062 name, clip_or_cull_index, element->register_idx, reg_mask);
7063 ++clip_or_cull_index;
7067 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7068 const struct wined3d_gl_info *gl_info, const unsigned int *map,
7069 const struct wined3d_shader_signature *input_signature,
7070 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7071 const struct wined3d_shader_signature *output_signature,
7072 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7074 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7075 const char *semantic_name;
7076 unsigned int semantic_idx;
7077 char reg_mask[6];
7078 unsigned int i;
7080 /* First, sort out position and point size system values. */
7081 for (i = 0; i < output_signature->element_count; ++i)
7083 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7085 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7086 continue;
7088 if (output->stream_idx)
7089 continue;
7091 semantic_name = output->semantic_name;
7092 semantic_idx = output->semantic_idx;
7093 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7095 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7097 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7098 reg_mask, output->register_idx, reg_mask);
7100 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7102 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7103 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7105 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7107 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7108 output->register_idx, reg_mask);
7110 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7112 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7113 output->register_idx, reg_mask);
7115 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7117 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7119 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7121 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7123 else if (output->sysval_semantic)
7125 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7129 /* Then, setup the pixel shader input. */
7130 if (reg_maps_out->shader_version.major < 4)
7131 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7132 output_signature, reg_maps_out);
7133 else
7134 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7137 /* Context activation is done by the caller. */
7138 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7139 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7140 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7142 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7143 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7144 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7145 const char *semantic_name;
7146 UINT semantic_idx;
7147 char reg_mask[6];
7148 unsigned int i;
7149 GLuint ret;
7151 string_buffer_clear(buffer);
7153 shader_glsl_add_version_declaration(buffer, gl_info);
7155 shader_addline(buffer, "invariant gl_Position;\n");
7157 if (per_vertex_point_size)
7159 shader_addline(buffer, "uniform struct\n{\n");
7160 shader_addline(buffer, " float size_min;\n");
7161 shader_addline(buffer, " float size_max;\n");
7162 shader_addline(buffer, "} ffp_point;\n");
7165 if (ps_major < 3)
7167 DWORD colors_written_mask[2] = {0};
7168 DWORD texcoords_written_mask[WINED3D_MAX_TEXTURES] = {0};
7170 if (!legacy_syntax)
7172 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7173 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7174 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7175 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7178 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7180 for (i = 0; i < vs->output_signature.element_count; ++i)
7182 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7183 DWORD write_mask;
7185 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7186 continue;
7188 semantic_name = output->semantic_name;
7189 semantic_idx = output->semantic_idx;
7190 write_mask = output->mask;
7191 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7193 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7195 if (legacy_syntax)
7196 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7197 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7198 else
7199 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7200 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7202 colors_written_mask[semantic_idx] = write_mask;
7204 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7206 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7207 reg_mask, output->register_idx, reg_mask);
7209 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7211 if (semantic_idx < WINED3D_MAX_TEXTURES)
7213 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7214 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7215 semantic_idx, reg_mask, output->register_idx, reg_mask);
7216 texcoords_written_mask[semantic_idx] = write_mask;
7219 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7221 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7222 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7224 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7226 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7227 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7228 output->register_idx, reg_mask[1]);
7232 for (i = 0; i < 2; ++i)
7234 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7236 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7237 if (!i)
7238 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7239 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7240 reg_mask, reg_mask);
7241 else
7242 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7243 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7244 reg_mask, reg_mask);
7247 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
7249 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7250 continue;
7252 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7254 if (!shader_glsl_full_ffp_varyings(gl_info) && !texcoords_written_mask[i])
7255 continue;
7257 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7258 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7259 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7263 else
7265 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7267 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7268 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7269 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7270 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7273 shader_addline(buffer, "}\n");
7275 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7276 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7277 shader_glsl_compile(gl_info, ret, buffer->buffer);
7279 return ret;
7282 static void shader_glsl_generate_stream_output_setup(struct wined3d_string_buffer *buffer,
7283 const struct wined3d_shader *shader)
7285 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
7286 unsigned int i, register_idx, component_idx;
7288 shader_addline(buffer, "out shader_in_out\n{\n");
7289 for (i = 0; i < so_desc->element_count; ++i)
7291 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7293 if (e->stream_idx)
7295 FIXME("Unhandled stream %u.\n", e->stream_idx);
7296 continue;
7298 if (!e->semantic_name)
7299 continue;
7300 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7301 continue;
7303 if (component_idx || e->component_count != 4)
7305 if (e->component_count == 1)
7306 shader_addline(buffer, "float");
7307 else
7308 shader_addline(buffer, "vec%u", e->component_count);
7310 shader_addline(buffer, " reg%u_%u_%u;\n",
7311 register_idx, component_idx, component_idx + e->component_count - 1);
7313 else
7315 shader_addline(buffer, "vec4 reg%u;\n", register_idx);
7318 shader_addline(buffer, "} shader_out;\n");
7320 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7321 shader->limits->packed_output);
7322 for (i = 0; i < so_desc->element_count; ++i)
7324 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7326 if (e->stream_idx)
7328 FIXME("Unhandled stream %u.\n", e->stream_idx);
7329 continue;
7331 if (!e->semantic_name)
7332 continue;
7333 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7334 continue;
7336 if (component_idx || e->component_count != 4)
7338 DWORD write_mask;
7339 char str_mask[6];
7341 write_mask = wined3d_mask_from_size(e->component_count) << component_idx;
7342 shader_glsl_write_mask_to_str(write_mask, str_mask);
7343 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7344 register_idx, component_idx, component_idx + e->component_count - 1,
7345 register_idx, str_mask);
7347 else
7349 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n", register_idx, register_idx);
7352 shader_addline(buffer, "}\n");
7355 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7356 const struct wined3d_shader *shader, unsigned int input_count,
7357 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const uint32_t *interpolation_mode)
7359 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7360 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7362 if (rasterizer_setup)
7363 input_count = min(vec4_varyings(4, gl_info), input_count);
7365 if (input_count)
7366 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7368 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7369 prefix, shader->limits->packed_output);
7371 if (rasterizer_setup)
7372 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7373 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7374 else
7375 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7376 &shader->reg_maps, "shader_out", rasterizer_setup);
7378 shader_addline(buffer, "}\n");
7381 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7382 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7383 const char *reg_mask)
7385 if (!constant->sysval_semantic)
7387 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7388 return;
7391 switch (constant->sysval_semantic)
7393 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7394 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7395 case WINED3D_SV_TESS_FACTOR_LINEDET:
7396 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7397 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7398 break;
7399 case WINED3D_SV_TESS_FACTOR_QUADINT:
7400 case WINED3D_SV_TESS_FACTOR_TRIINT:
7401 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7402 break;
7403 default:
7404 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7405 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7409 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7410 const struct wined3d_shader_signature *signature, BOOL input_setup)
7412 unsigned int i, register_count, user_constant_index, user_constant_count;
7414 register_count = user_constant_count = 0;
7415 for (i = 0; i < signature->element_count; ++i)
7417 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7418 register_count = max(constant->register_idx + 1, register_count);
7419 if (!constant->sysval_semantic)
7420 ++user_constant_count;
7423 if (user_constant_count)
7424 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7425 input_setup ? "in" : "out", user_constant_count);
7426 if (input_setup)
7427 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7429 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7430 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7432 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7433 char reg_mask[6];
7435 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7437 if (input_setup)
7438 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7439 else
7440 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7442 shader_addline(buffer, " = ");
7444 if (input_setup)
7445 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7446 else
7447 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7449 shader_addline(buffer, ";\n");
7451 shader_addline(buffer, "}\n");
7454 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7455 const struct wined3d_gl_info *gl_info)
7457 const char *output = get_fragment_output(gl_info);
7459 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7460 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7461 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7462 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7463 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7464 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7467 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7468 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7470 const char *output = get_fragment_output(gl_info);
7472 switch (mode)
7474 case WINED3D_FFP_PS_FOG_OFF:
7475 return;
7477 case WINED3D_FFP_PS_FOG_LINEAR:
7478 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7479 break;
7481 case WINED3D_FFP_PS_FOG_EXP:
7482 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7483 break;
7485 case WINED3D_FFP_PS_FOG_EXP2:
7486 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7487 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7488 break;
7490 default:
7491 ERR("Invalid fog mode %#x.\n", mode);
7492 return;
7495 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7496 output, output);
7499 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7500 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7502 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7503 * flipping all the operators here, just negate the comparison below. */
7504 static const char * const comparison_operator[] =
7506 "", /* WINED3D_CMP_NEVER */
7507 "<", /* WINED3D_CMP_LESS */
7508 "==", /* WINED3D_CMP_EQUAL */
7509 "<=", /* WINED3D_CMP_LESSEQUAL */
7510 ">", /* WINED3D_CMP_GREATER */
7511 "!=", /* WINED3D_CMP_NOTEQUAL */
7512 ">=", /* WINED3D_CMP_GREATEREQUAL */
7513 "" /* WINED3D_CMP_ALWAYS */
7516 if (alpha_func == WINED3D_CMP_ALWAYS)
7517 return;
7519 if (alpha_func != WINED3D_CMP_NEVER)
7520 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7521 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7522 shader_addline(buffer, " discard;\n");
7525 static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *buffer,
7526 const char *colour, const char *colour_key_low, const char *colour_key_high)
7528 shader_addline(buffer, "if (all(greaterThanEqual(%s, %s)) && all(lessThan(%s, %s)))\n",
7529 colour, colour_key_low, colour, colour_key_high);
7530 shader_addline(buffer, " discard;\n");
7533 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7534 const struct wined3d_gl_info *gl_info)
7536 if (gl_info->supported[ARB_CULL_DISTANCE])
7537 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7538 if (gl_info->supported[ARB_GPU_SHADER5])
7539 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7540 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7541 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7542 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7543 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7544 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7545 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7546 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7547 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7548 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7549 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7550 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7551 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7552 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7553 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7554 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7555 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7556 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7557 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7558 if (gl_info->supported[ARB_TEXTURE_GATHER])
7559 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7560 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7561 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7562 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7563 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7564 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7565 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7566 if (gl_info->supported[EXT_GPU_SHADER4])
7567 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7568 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7569 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7570 if (gl_info->supported[EXT_TEXTURE_SHADOW_LOD])
7571 shader_addline(buffer, "#extension GL_EXT_texture_shadow_lod : enable\n");
7574 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7575 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7576 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7578 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7579 struct wined3d_string_buffer *src, *assignment;
7580 enum wined3d_data_type dst_data_type;
7581 const char *swizzle;
7582 unsigned int i;
7584 if (output_signature->element_count)
7586 src = string_buffer_get(string_buffers);
7587 assignment = string_buffer_get(string_buffers);
7588 for (i = 0; i < output_signature->element_count; ++i)
7590 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7592 /* register_idx is set to ~0u for non-color outputs. */
7593 if (output->register_idx == ~0u)
7594 continue;
7595 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7597 FIXME("Unhandled component type %#x.\n", output->component_type);
7598 continue;
7600 dst_data_type = component_type_info[output->component_type].data_type;
7601 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7602 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7603 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT, 4);
7604 swizzle = args->rt_alpha_swizzle & (1u << output->semantic_idx) ? ".argb" : "";
7605 shader_addline(buffer, "%s%s;\n", assignment->buffer, swizzle);
7607 string_buffer_release(string_buffers, src);
7608 string_buffer_release(string_buffers, assignment);
7610 else
7612 uint32_t mask = shader->reg_maps.rt_mask;
7614 while (mask)
7616 i = wined3d_bit_scan(&mask);
7617 swizzle = args->rt_alpha_swizzle & (1u << i) ? ".argb" : "";
7618 shader_addline(buffer, "color_out%u = ps_out[%u]%s;\n", i, i, swizzle);
7623 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7624 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7625 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7627 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7629 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7630 if (reg_maps->shader_version.major < 2)
7631 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7633 if (args->srgb_correction)
7634 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7636 /* SM < 3 does not replace the fog stage. */
7637 if (reg_maps->shader_version.major < 3)
7638 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7640 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7642 if (reg_maps->sample_mask)
7643 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7645 if (!use_legacy_fragment_output(gl_info))
7646 shader_glsl_generate_color_output(buffer, gl_info, shader, args, string_buffers);
7649 /* Context activation is done by the caller. */
7650 static GLuint shader_glsl_generate_fragment_shader(const struct wined3d_context_gl *context_gl,
7651 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7652 const struct wined3d_shader *shader, const struct ps_compile_args *args,
7653 struct ps_np2fixup_info *np2fixup_info)
7655 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7656 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7657 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7658 const char *prefix = shader_glsl_get_prefix(version->type);
7659 BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7660 unsigned int i, extra_constants_needed = 0;
7661 struct shader_glsl_ctx_priv priv_ctx;
7662 GLuint shader_id;
7663 uint32_t map;
7665 memset(&priv_ctx, 0, sizeof(priv_ctx));
7666 priv_ctx.gl_info = gl_info;
7667 priv_ctx.cur_ps_args = args;
7668 priv_ctx.cur_np2fixup_info = np2fixup_info;
7669 priv_ctx.string_buffers = string_buffers;
7671 shader_glsl_add_version_declaration(buffer, gl_info);
7673 shader_glsl_enable_extensions(buffer, gl_info);
7674 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7675 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7676 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7677 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7678 if (shader_glsl_use_explicit_attrib_location(gl_info))
7679 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7680 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7681 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7682 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7683 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7684 if (gl_info->supported[ARB_SAMPLE_SHADING])
7685 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7686 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7687 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7688 /* The spec says that it doesn't have to be explicitly enabled, but the
7689 * nvidia drivers write a warning if we don't do so. */
7690 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7691 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7693 /* Base Declarations */
7694 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7696 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7698 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7699 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7700 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7701 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7704 /* Declare uniforms for NP2 texcoord fixup:
7705 * This is NOT done inside the loop that declares the texture samplers
7706 * since the NP2 fixup code is currently only used for the GeforceFX
7707 * series and when forcing the ARB_npot extension off. Modern cards just
7708 * skip the code anyway, so put it inside a separate loop. */
7709 if (args->np2_fixup)
7711 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7712 unsigned int cur = 0;
7714 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7715 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7716 * samplerNP2Fixup stores texture dimensions and is updated through
7717 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7719 for (i = 0; i < shader->limits->sampler; ++i)
7721 enum wined3d_shader_resource_type resource_type;
7723 resource_type = pixelshader_get_resource_type(reg_maps, i, args->tex_types);
7725 if (!resource_type || !(args->np2_fixup & (1u << i)))
7726 continue;
7728 if (resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7730 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7731 continue;
7734 fixup->idx[i] = cur++;
7737 fixup->num_consts = (cur + 1) >> 1;
7738 fixup->active = args->np2_fixup;
7739 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7742 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7744 shader_addline(buffer, "uniform struct\n{\n");
7745 shader_addline(buffer, " vec4 color;\n");
7746 shader_addline(buffer, " float density;\n");
7747 shader_addline(buffer, " float end;\n");
7748 shader_addline(buffer, " float scale;\n");
7749 shader_addline(buffer, "} ffp_fog;\n");
7751 if (legacy_syntax)
7753 if (glsl_is_color_reg_read(shader, 0))
7754 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7755 if (glsl_is_color_reg_read(shader, 1))
7756 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7757 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7758 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7760 else
7762 if (glsl_is_color_reg_read(shader, 0))
7763 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7764 if (glsl_is_color_reg_read(shader, 1))
7765 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7766 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7767 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7768 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7772 if (version->major >= 3)
7774 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7776 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7777 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7778 shader->u.ps.interpolation_mode, version->major >= 4);
7779 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7782 map = reg_maps->bumpmat;
7783 while (map)
7785 i = wined3d_bit_scan(&map);
7786 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7788 if (reg_maps->luminanceparams & (1u << i))
7790 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7791 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7792 extra_constants_needed++;
7795 extra_constants_needed++;
7798 if (args->srgb_correction)
7800 shader_addline(buffer, "const vec4 srgb_const0 = ");
7801 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
7802 shader_addline(buffer, ";\n");
7803 shader_addline(buffer, "const vec4 srgb_const1 = ");
7804 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
7805 shader_addline(buffer, ";\n");
7807 if ((reg_maps->usesdsy && wined3d_settings.offscreen_rendering_mode != ORM_FBO)
7808 || (reg_maps->vpos && !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]))
7810 ++extra_constants_needed;
7811 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7813 if (reg_maps->vpos)
7815 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7817 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7818 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7819 args->y_correction ? "origin_upper_left, " : "");
7820 else if (args->y_correction)
7821 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7823 shader_addline(buffer, "vec4 vpos;\n");
7826 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7827 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7829 if (!use_legacy_fragment_output(gl_info))
7831 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7833 if (args->dual_source_blend)
7834 shader_addline(buffer, "vec4 ps_out[2];\n");
7835 else
7836 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7837 if (output_signature->element_count)
7839 for (i = 0; i < output_signature->element_count; ++i)
7841 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7843 if (output->register_idx == ~0u)
7844 continue;
7845 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7847 FIXME("Unhandled component type %#x.\n", output->component_type);
7848 continue;
7850 if (shader_glsl_use_explicit_attrib_location(gl_info))
7852 if (args->dual_source_blend)
7853 shader_addline(buffer, "layout(location = 0, index = %u) ", output->semantic_idx);
7854 else
7855 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7857 shader_addline(buffer, "out %s4 color_out%u;\n",
7858 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7861 else
7863 uint32_t mask = reg_maps->rt_mask;
7865 while (mask)
7867 i = wined3d_bit_scan(&mask);
7868 if (shader_glsl_use_explicit_attrib_location(gl_info))
7870 if (args->dual_source_blend)
7871 shader_addline(buffer, "layout(location = 0, index = %u) ", i);
7872 else
7873 shader_addline(buffer, "layout(location = %u) ", i);
7875 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7880 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7881 FIXME("Insufficient uniforms to run this shader.\n");
7883 if (shader->u.ps.force_early_depth_stencil)
7884 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7886 shader_addline(buffer, "void main()\n{\n");
7888 if (reg_maps->sample_mask)
7889 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7891 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7892 * add approximately 0.5. This causes off-by-one problems as spotted by
7893 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7894 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7895 * causes precision troubles when we just subtract 0.5.
7897 * To deal with that, just floor() the position. This will eliminate the
7898 * fraction on all cards.
7900 * TODO: Test how this behaves with multisampling.
7902 * An advantage of floor is that it works even if the driver doesn't add
7903 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7904 * to return in gl_FragCoord, even though coordinates specify the pixel
7905 * centers instead of the pixel corners. This code will behave correctly
7906 * on drivers that returns integer values. */
7907 if (reg_maps->vpos)
7909 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7910 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7911 else if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7912 shader_addline(buffer,
7913 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7914 else
7915 shader_addline(buffer,
7916 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7919 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7921 unsigned int i;
7923 if (legacy_syntax)
7925 if (glsl_is_color_reg_read(shader, 0))
7926 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7927 if (glsl_is_color_reg_read(shader, 1))
7928 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7931 map = reg_maps->texcoord;
7932 while (map)
7934 i = wined3d_bit_scan(&map);
7935 if (args->pointsprite)
7936 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7937 else if (args->texcoords_initialized & (1u << i))
7938 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7939 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7940 else
7941 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7942 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7945 if (legacy_syntax)
7946 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7949 /* Pack 3.0 inputs */
7950 if (reg_maps->shader_version.major >= 3)
7951 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7952 reg_maps->shader_version.major >= 4);
7954 /* Base Shader Body */
7955 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7956 return 0;
7958 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7959 if (reg_maps->shader_version.major < 4)
7960 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
7962 shader_addline(buffer, "}\n");
7964 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7965 TRACE("Compiling shader object %u.\n", shader_id);
7966 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7968 return shader_id;
7971 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7972 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7973 const struct vs_compile_args *args)
7975 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7976 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7977 unsigned int i;
7979 /* Unpack outputs. */
7980 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7982 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7983 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7984 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7985 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7987 if (reg_maps->shader_version.major < 3)
7989 if (args->fog_src == VS_FOG_Z)
7990 shader_addline(buffer, "%s = gl_Position.z;\n",
7991 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7992 else if (!reg_maps->fog)
7993 shader_addline(buffer, "%s = 0.0;\n",
7994 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7997 /* We always store the clipplanes without y inversion. */
7998 if (args->clip_enabled)
8000 if (legacy_syntax)
8001 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
8002 else
8003 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
8004 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
8007 if (args->point_size && !args->per_vertex_point_size)
8008 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
8010 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8011 shader_glsl_fixup_position(buffer, FALSE);
8014 /* Context activation is done by the caller. */
8015 static GLuint shader_glsl_generate_vertex_shader(const struct wined3d_context_gl *context_gl,
8016 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
8018 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8019 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8020 const struct wined3d_shader_version *version = &reg_maps->shader_version;
8021 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8022 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8023 struct shader_glsl_ctx_priv priv_ctx;
8024 GLuint shader_id;
8025 unsigned int i;
8027 memset(&priv_ctx, 0, sizeof(priv_ctx));
8028 priv_ctx.gl_info = gl_info;
8029 priv_ctx.cur_vs_args = args;
8030 priv_ctx.string_buffers = string_buffers;
8032 shader_glsl_add_version_declaration(buffer, gl_info);
8034 shader_glsl_enable_extensions(buffer, gl_info);
8035 if (gl_info->supported[ARB_DRAW_INSTANCED])
8036 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8037 if (shader_glsl_use_explicit_attrib_location(gl_info))
8038 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8039 if (gl_info->supported[ARB_SHADER_VIEWPORT_LAYER_ARRAY])
8040 shader_addline(buffer, "#extension GL_ARB_shader_viewport_layer_array : enable\n");
8042 /* Base Declarations */
8043 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8045 for (i = 0; i < shader->input_signature.element_count; ++i)
8046 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8048 if (args->point_size && !args->per_vertex_point_size)
8050 shader_addline(buffer, "uniform struct\n{\n");
8051 shader_addline(buffer, " float size;\n");
8052 shader_addline(buffer, " float size_min;\n");
8053 shader_addline(buffer, " float size_max;\n");
8054 shader_addline(buffer, "} ffp_point;\n");
8057 if (!needs_legacy_glsl_syntax(gl_info))
8059 if (args->clip_enabled)
8060 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8062 if (version->major < 3)
8064 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8065 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8066 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
8067 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8071 if (version->major < 4)
8072 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8074 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8075 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8077 if (reg_maps->shader_version.major >= 4)
8078 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8079 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8081 shader_addline(buffer, "void main()\n{\n");
8083 if (reg_maps->input_rel_addressing)
8085 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8086 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8087 for (i = 0; i < shader->input_signature.element_count; ++i)
8089 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8090 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8094 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8095 return 0;
8097 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8098 if (reg_maps->shader_version.major < 4)
8099 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8101 shader_addline(buffer, "}\n");
8103 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8104 TRACE("Compiling shader object %u.\n", shader_id);
8105 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8107 return shader_id;
8110 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8111 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8113 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8114 char reg_mask[6];
8115 unsigned int i;
8117 for (i = 0; i < output_signature->element_count; ++i)
8119 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8121 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8122 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8123 output->register_idx, reg_mask, output->register_idx, reg_mask);
8127 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8128 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8129 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8130 const char *phase_name, unsigned phase_idx)
8132 unsigned int i;
8133 HRESULT hr;
8135 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8136 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8137 for (i = 0; i < phase->temporary_count; ++i)
8138 shader_addline(buffer, "vec4 R%u;\n", i);
8139 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8140 shader_addline(buffer, "}\n");
8141 return hr;
8144 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8145 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8147 if (phase->instance_count)
8149 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8150 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8151 shader_addline(buffer, "}\n");
8153 else
8155 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8159 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context_gl *context_gl,
8160 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8162 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8163 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8164 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8165 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8166 const struct wined3d_hull_shader *hs = &shader->u.hs;
8167 const struct wined3d_shader_phase *phase;
8168 struct shader_glsl_ctx_priv priv_ctx;
8169 GLuint shader_id;
8170 unsigned int i;
8172 memset(&priv_ctx, 0, sizeof(priv_ctx));
8173 priv_ctx.gl_info = gl_info;
8174 priv_ctx.string_buffers = string_buffers;
8176 shader_glsl_add_version_declaration(buffer, gl_info);
8178 shader_glsl_enable_extensions(buffer, gl_info);
8179 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8181 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8183 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8185 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8186 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8188 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8190 if (hs->phases.control_point)
8192 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8193 shader->limits->packed_output);
8194 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8195 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8196 shader_addline(buffer, "}\n");
8199 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8200 if ((phase = hs->phases.control_point))
8202 for (i = 0; i < phase->temporary_count; ++i)
8203 shader_addline(buffer, "vec4 R%u;\n", i);
8204 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8205 return 0;
8206 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8208 else
8210 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8212 shader_addline(buffer, "}\n");
8214 for (i = 0; i < hs->phases.fork_count; ++i)
8216 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8217 &hs->phases.fork[i], "fork", i)))
8218 return 0;
8221 for (i = 0; i < hs->phases.join_count; ++i)
8223 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8224 &hs->phases.join[i], "join", i)))
8225 return 0;
8228 shader_addline(buffer, "void main()\n{\n");
8229 shader_addline(buffer, "hs_control_point_phase();\n");
8230 if (reg_maps->vocp)
8231 shader_addline(buffer, "barrier();\n");
8232 for (i = 0; i < hs->phases.fork_count; ++i)
8233 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8234 for (i = 0; i < hs->phases.join_count; ++i)
8235 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8236 shader_addline(buffer, "setup_patch_constant_output();\n");
8237 shader_addline(buffer, "}\n");
8239 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8240 TRACE("Compiling shader object %u.\n", shader_id);
8241 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8243 return shader_id;
8246 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8247 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8248 const struct ds_compile_args *args)
8250 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8252 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8253 shader_glsl_fixup_position(buffer, FALSE);
8256 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context_gl *context_gl,
8257 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8259 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8260 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8261 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8262 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8263 struct shader_glsl_ctx_priv priv_ctx;
8264 GLuint shader_id;
8266 memset(&priv_ctx, 0, sizeof(priv_ctx));
8267 priv_ctx.gl_info = gl_info;
8268 priv_ctx.cur_ds_args = args;
8269 priv_ctx.string_buffers = string_buffers;
8271 shader_glsl_add_version_declaration(buffer, gl_info);
8273 shader_glsl_enable_extensions(buffer, gl_info);
8274 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8276 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8278 shader_addline(buffer, "layout(");
8279 switch (shader->u.ds.tessellator_domain)
8281 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8282 shader_addline(buffer, "isolines");
8283 break;
8284 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8285 shader_addline(buffer, "quads");
8286 break;
8287 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8288 shader_addline(buffer, "triangles");
8289 break;
8291 switch (args->tessellator_output_primitive)
8293 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8294 if (args->render_offscreen)
8295 shader_addline(buffer, ", ccw");
8296 else
8297 shader_addline(buffer, ", cw");
8298 break;
8299 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8300 if (args->render_offscreen)
8301 shader_addline(buffer, ", cw");
8302 else
8303 shader_addline(buffer, ", ccw");
8304 break;
8305 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8306 shader_addline(buffer, ", point_mode");
8307 break;
8308 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8309 break;
8311 switch (args->tessellator_partitioning)
8313 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8314 shader_addline(buffer, ", fractional_odd_spacing");
8315 break;
8316 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8317 shader_addline(buffer, ", fractional_even_spacing");
8318 break;
8319 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8320 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8321 shader_addline(buffer, ", equal_spacing");
8322 break;
8324 shader_addline(buffer, ") in;\n");
8326 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8328 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8329 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8331 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8332 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8333 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8335 shader_addline(buffer, "void main()\n{\n");
8336 shader_addline(buffer, "setup_patch_constant_input();\n");
8338 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8339 return 0;
8341 shader_addline(buffer, "}\n");
8343 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8344 TRACE("Compiling shader object %u.\n", shader_id);
8345 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8347 return shader_id;
8350 /* Context activation is done by the caller. */
8351 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context_gl *context_gl,
8352 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8354 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8355 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8356 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8357 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8358 const struct wined3d_shader_signature_element *output;
8359 enum wined3d_primitive_type primitive_type;
8360 struct shader_glsl_ctx_priv priv_ctx;
8361 unsigned int max_vertices;
8362 unsigned int i, j;
8363 GLuint shader_id;
8365 memset(&priv_ctx, 0, sizeof(priv_ctx));
8366 priv_ctx.gl_info = gl_info;
8367 priv_ctx.string_buffers = string_buffers;
8369 shader_glsl_add_version_declaration(buffer, gl_info);
8371 shader_glsl_enable_extensions(buffer, gl_info);
8373 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8375 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8376 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8377 if (shader->u.gs.instance_count > 1)
8378 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8379 shader_addline(buffer, ") in;\n");
8381 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8382 if (!(max_vertices = shader->u.gs.vertices_out))
8384 switch (args->primitive_type)
8386 case WINED3D_PT_POINTLIST:
8387 max_vertices = 1;
8388 break;
8389 case WINED3D_PT_LINELIST:
8390 max_vertices = 2;
8391 break;
8392 case WINED3D_PT_TRIANGLELIST:
8393 max_vertices = 3;
8394 break;
8395 default:
8396 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8397 break;
8400 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8401 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8402 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8404 if (!gl_info->supported[ARB_CLIP_CONTROL])
8406 shader_addline(buffer, "uniform vec4 pos_fixup");
8407 if (reg_maps->viewport_array)
8408 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8409 shader_addline(buffer, ";\n");
8412 if (is_rasterization_disabled(shader))
8414 shader_glsl_generate_stream_output_setup(buffer, shader);
8416 else
8418 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8419 gl_info, TRUE, args->interpolation_mode);
8422 shader_addline(buffer, "void main()\n{\n");
8423 if (shader->function)
8425 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8426 return 0;
8428 else
8430 for (i = 0; i < max_vertices; ++i)
8432 for (j = 0; j < shader->output_signature.element_count; ++j)
8434 output = &shader->output_signature.elements[j];
8435 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8436 output->register_idx, i, output->register_idx);
8438 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8439 if (!gl_info->supported[ARB_CLIP_CONTROL])
8440 shader_glsl_fixup_position(buffer, FALSE);
8441 shader_addline(buffer, "EmitVertex();\n");
8444 shader_addline(buffer, "}\n");
8446 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8447 TRACE("Compiling shader object %u.\n", shader_id);
8448 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8450 return shader_id;
8453 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8455 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8456 const struct wined3d_gl_info *gl_info = priv->gl_info;
8457 struct wined3d_string_buffer *buffer = ctx->buffer;
8458 const struct wined3d_shader *shader = ctx->shader;
8460 switch (shader->reg_maps.shader_version.type)
8462 case WINED3D_SHADER_TYPE_PIXEL:
8463 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8464 break;
8465 case WINED3D_SHADER_TYPE_VERTEX:
8466 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8467 break;
8468 case WINED3D_SHADER_TYPE_DOMAIN:
8469 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8470 break;
8471 case WINED3D_SHADER_TYPE_GEOMETRY:
8472 case WINED3D_SHADER_TYPE_COMPUTE:
8473 break;
8474 default:
8475 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8476 break;
8480 /* Context activation is done by the caller. */
8481 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context_gl *context_gl,
8482 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8483 const struct wined3d_shader *shader)
8485 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8486 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8487 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8488 struct shader_glsl_ctx_priv priv_ctx;
8489 GLuint shader_id;
8490 unsigned int i;
8492 memset(&priv_ctx, 0, sizeof(priv_ctx));
8493 priv_ctx.gl_info = gl_info;
8494 priv_ctx.string_buffers = string_buffers;
8496 shader_glsl_add_version_declaration(buffer, gl_info);
8498 shader_glsl_enable_extensions(buffer, gl_info);
8499 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8501 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8503 for (i = 0; i < reg_maps->tgsm_count; ++i)
8505 if (reg_maps->tgsm[i].size)
8506 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8509 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8510 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8512 shader_addline(buffer, "void main()\n{\n");
8513 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8514 shader_addline(buffer, "}\n");
8516 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8517 TRACE("Compiling shader object %u.\n", shader_id);
8518 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8520 return shader_id;
8523 static GLuint find_glsl_fragment_shader(const struct wined3d_context_gl *context_gl,
8524 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8525 struct wined3d_shader *shader,
8526 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8528 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8529 struct glsl_shader_private *shader_data;
8530 struct ps_np2fixup_info *np2fixup;
8531 UINT i;
8532 DWORD new_size;
8533 GLuint ret;
8535 if (!shader->backend_data)
8537 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8539 ERR("Failed to allocate backend data.\n");
8540 return 0;
8543 shader_data = shader->backend_data;
8544 gl_shaders = shader_data->gl_shaders.ps;
8546 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8547 * so a linear search is more performant than a hashmap or a binary search
8548 * (cache coherency etc)
8550 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8552 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8554 if (args->np2_fixup)
8555 *np2fixup_info = &gl_shaders[i].np2fixup;
8556 return gl_shaders[i].id;
8560 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8561 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8563 if (shader_data->num_gl_shaders)
8565 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8566 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8568 else
8570 new_array = heap_alloc(sizeof(*gl_shaders));
8571 new_size = 1;
8574 if(!new_array) {
8575 ERR("Out of memory\n");
8576 return 0;
8578 shader_data->gl_shaders.ps = new_array;
8579 shader_data->shader_array_size = new_size;
8580 gl_shaders = new_array;
8583 gl_shaders[shader_data->num_gl_shaders].args = *args;
8585 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8586 memset(np2fixup, 0, sizeof(*np2fixup));
8587 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8589 string_buffer_clear(buffer);
8590 ret = shader_glsl_generate_fragment_shader(context_gl, buffer, string_buffers, shader, args, np2fixup);
8591 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8593 return ret;
8596 static BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8597 const DWORD use_map)
8599 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8600 return FALSE;
8601 if ((stored->clip_enabled) != new->clip_enabled)
8602 return FALSE;
8603 if (stored->point_size != new->point_size)
8604 return FALSE;
8605 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8606 return FALSE;
8607 if (stored->flatshading != new->flatshading)
8608 return FALSE;
8609 if (stored->next_shader_type != new->next_shader_type)
8610 return FALSE;
8611 if (stored->next_shader_input_count != new->next_shader_input_count)
8612 return FALSE;
8613 if (stored->fog_src != new->fog_src)
8614 return FALSE;
8615 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8618 static GLuint find_glsl_vertex_shader(const struct wined3d_context_gl *context_gl,
8619 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct vs_compile_args *args)
8621 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8622 uint32_t use_map = context_gl->c.stream_info.use_map;
8623 struct glsl_shader_private *shader_data;
8624 unsigned int i, new_size;
8625 GLuint ret;
8627 if (!shader->backend_data)
8629 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8631 ERR("Failed to allocate backend data.\n");
8632 return 0;
8635 shader_data = shader->backend_data;
8636 gl_shaders = shader_data->gl_shaders.vs;
8638 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8639 * so a linear search is more performant than a hashmap or a binary search
8640 * (cache coherency etc)
8642 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8644 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8645 return gl_shaders[i].id;
8648 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8650 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8652 if (shader_data->num_gl_shaders)
8654 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8655 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8657 else
8659 new_array = heap_alloc(sizeof(*gl_shaders));
8660 new_size = 1;
8663 if(!new_array) {
8664 ERR("Out of memory\n");
8665 return 0;
8667 shader_data->gl_shaders.vs = new_array;
8668 shader_data->shader_array_size = new_size;
8669 gl_shaders = new_array;
8672 gl_shaders[shader_data->num_gl_shaders].args = *args;
8674 string_buffer_clear(&priv->shader_buffer);
8675 ret = shader_glsl_generate_vertex_shader(context_gl, priv, shader, args);
8676 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8678 return ret;
8681 static GLuint find_glsl_hull_shader(const struct wined3d_context_gl *context_gl,
8682 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8684 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8685 struct glsl_shader_private *shader_data;
8686 unsigned int new_size;
8687 GLuint ret;
8689 if (!shader->backend_data)
8691 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8693 ERR("Failed to allocate backend data.\n");
8694 return 0;
8697 shader_data = shader->backend_data;
8698 gl_shaders = shader_data->gl_shaders.hs;
8700 if (shader_data->num_gl_shaders > 0)
8702 assert(shader_data->num_gl_shaders == 1);
8703 return gl_shaders[0].id;
8706 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8708 assert(!shader_data->gl_shaders.hs);
8709 new_size = 1;
8710 if (!(new_array = heap_alloc(sizeof(*new_array))))
8712 ERR("Failed to allocate GL shaders array.\n");
8713 return 0;
8715 shader_data->gl_shaders.hs = new_array;
8716 shader_data->shader_array_size = new_size;
8717 gl_shaders = new_array;
8719 string_buffer_clear(&priv->shader_buffer);
8720 ret = shader_glsl_generate_hull_shader(context_gl, priv, shader);
8721 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8723 return ret;
8726 static GLuint find_glsl_domain_shader(const struct wined3d_context_gl *context_gl,
8727 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8729 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8730 struct glsl_shader_private *shader_data;
8731 unsigned int i, new_size;
8732 GLuint ret;
8734 if (!shader->backend_data)
8736 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8738 ERR("Failed to allocate backend data.\n");
8739 return 0;
8742 shader_data = shader->backend_data;
8743 gl_shaders = shader_data->gl_shaders.ds;
8745 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8747 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8748 return gl_shaders[i].id;
8751 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8753 if (shader_data->num_gl_shaders)
8755 new_size = shader_data->shader_array_size + 1;
8756 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8758 else
8760 new_array = heap_alloc(sizeof(*new_array));
8761 new_size = 1;
8764 if (!new_array)
8766 ERR("Failed to allocate GL shaders array.\n");
8767 return 0;
8769 shader_data->gl_shaders.ds = new_array;
8770 shader_data->shader_array_size = new_size;
8771 gl_shaders = new_array;
8773 string_buffer_clear(&priv->shader_buffer);
8774 ret = shader_glsl_generate_domain_shader(context_gl, priv, shader, args);
8775 gl_shaders[shader_data->num_gl_shaders].args = *args;
8776 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8778 return ret;
8781 static GLuint find_glsl_geometry_shader(const struct wined3d_context_gl *context_gl,
8782 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8784 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8785 struct glsl_shader_private *shader_data;
8786 unsigned int i, new_size;
8787 GLuint ret;
8789 if (!shader->backend_data)
8791 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8793 ERR("Failed to allocate backend data.\n");
8794 return 0;
8797 shader_data = shader->backend_data;
8798 gl_shaders = shader_data->gl_shaders.gs;
8800 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8802 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8803 return gl_shaders[i].id;
8806 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8808 if (shader_data->num_gl_shaders)
8810 new_size = shader_data->shader_array_size + 1;
8811 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8813 else
8815 new_array = heap_alloc(sizeof(*new_array));
8816 new_size = 1;
8819 if (!new_array)
8821 ERR("Failed to allocate GL shaders array.\n");
8822 return 0;
8824 shader_data->gl_shaders.gs = new_array;
8825 shader_data->shader_array_size = new_size;
8826 gl_shaders = new_array;
8828 string_buffer_clear(&priv->shader_buffer);
8829 ret = shader_glsl_generate_geometry_shader(context_gl, priv, shader, args);
8830 gl_shaders[shader_data->num_gl_shaders].args = *args;
8831 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8833 return ret;
8836 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8838 switch (mcs)
8840 case WINED3D_MCS_MATERIAL:
8841 return material;
8842 case WINED3D_MCS_COLOR1:
8843 return "ffp_attrib_diffuse";
8844 case WINED3D_MCS_COLOR2:
8845 return "ffp_attrib_specular";
8846 default:
8847 ERR("Invalid material color source %#x.\n", mcs);
8848 return "<invalid>";
8852 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8853 const struct wined3d_ffp_vs_settings *settings, unsigned int idx, BOOL legacy_lighting)
8855 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8856 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8857 if (settings->localviewer)
8858 shader_addline(buffer, "t = dot(normal, ffp_normalize(dir - ffp_normalize(ec_pos.xyz)));\n");
8859 else
8860 shader_addline(buffer, "t = dot(normal, ffp_normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8861 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0%s) specular +="
8862 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n",
8863 legacy_lighting ? " && ffp_material.shininess > 0.0" : "", idx);
8866 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8867 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8869 const char *diffuse, *specular, *emissive, *ambient;
8870 unsigned int i, idx;
8872 if (!settings->lighting)
8874 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8875 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8876 return;
8879 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8880 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8881 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8882 shader_addline(buffer, "vec3 dir, dst;\n");
8883 shader_addline(buffer, "float att, t;\n");
8885 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8886 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8887 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8888 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8890 idx = 0;
8891 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8893 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8894 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8895 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8896 shader_addline(buffer, "dst.x = 1.0;\n");
8897 if (legacy_lighting)
8899 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8900 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8901 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8903 else
8905 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8907 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8908 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8909 if (!legacy_lighting)
8910 shader_addline(buffer, "att = 1.0 / att;\n");
8911 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8912 if (!settings->normal)
8914 shader_addline(buffer, "}\n");
8915 continue;
8917 shader_addline(buffer, "dir = ffp_normalize(dir);\n");
8918 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8919 shader_addline(buffer, "}\n");
8922 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8924 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8925 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8926 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8927 shader_addline(buffer, "dst.x = 1.0;\n");
8928 if (legacy_lighting)
8930 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8931 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8932 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8934 else
8936 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8938 shader_addline(buffer, "dir = ffp_normalize(dir);\n");
8939 shader_addline(buffer, "t = dot(-dir, ffp_normalize(ffp_light[%u].direction));\n", idx);
8940 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8941 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8942 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8943 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8944 idx, idx, idx, idx);
8945 if (legacy_lighting)
8946 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8947 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8948 idx, idx, idx);
8949 else
8950 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8951 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8952 idx, idx, idx);
8953 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8954 if (!settings->normal)
8956 shader_addline(buffer, "}\n");
8957 continue;
8959 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8960 shader_addline(buffer, "}\n");
8963 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8965 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8966 if (!settings->normal)
8967 continue;
8968 shader_addline(buffer, "att = 1.0;\n");
8969 shader_addline(buffer, "dir = ffp_normalize(ffp_light[%u].direction.xyz);\n", idx);
8970 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8973 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8975 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8976 if (!settings->normal)
8977 continue;
8978 shader_addline(buffer, "att = 1.0;\n");
8979 shader_addline(buffer, "dir = ffp_normalize(ffp_light[%u].position.xyz);\n", idx);
8980 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8983 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8984 ambient, diffuse, emissive);
8985 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8986 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8989 /* Context activation is done by the caller. */
8990 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8991 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8993 static const struct attrib_info
8995 const char type[6];
8996 const char name[24];
8998 attrib_info[] =
9000 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
9001 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
9002 /* TODO: Indexed vertex blending */
9003 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
9004 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
9005 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
9006 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
9007 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
9009 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9010 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9011 BOOL output_legacy_fogcoord = legacy_syntax;
9012 BOOL legacy_lighting = priv->legacy_lighting;
9013 GLuint shader_obj;
9014 unsigned int i;
9016 string_buffer_clear(buffer);
9018 shader_glsl_add_version_declaration(buffer, gl_info);
9020 if (shader_glsl_use_explicit_attrib_location(gl_info))
9021 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9023 shader_addline(buffer, "invariant gl_Position;\n");
9025 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
9027 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
9029 if (shader_glsl_use_explicit_attrib_location(gl_info))
9030 shader_addline(buffer, "layout(location = %u) ", i);
9031 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
9033 shader_addline(buffer, "\n");
9035 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9036 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9037 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9038 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", WINED3D_MAX_TEXTURES);
9040 shader_addline(buffer, "uniform struct\n{\n");
9041 shader_addline(buffer, " vec4 emissive;\n");
9042 shader_addline(buffer, " vec4 ambient;\n");
9043 shader_addline(buffer, " vec4 diffuse;\n");
9044 shader_addline(buffer, " vec4 specular;\n");
9045 shader_addline(buffer, " float shininess;\n");
9046 shader_addline(buffer, "} ffp_material;\n");
9048 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9049 shader_addline(buffer, "uniform struct\n{\n");
9050 shader_addline(buffer, " vec4 diffuse;\n");
9051 shader_addline(buffer, " vec4 specular;\n");
9052 shader_addline(buffer, " vec4 ambient;\n");
9053 shader_addline(buffer, " vec4 position;\n");
9054 shader_addline(buffer, " vec3 direction;\n");
9055 shader_addline(buffer, " float range;\n");
9056 shader_addline(buffer, " float falloff;\n");
9057 shader_addline(buffer, " float c_att;\n");
9058 shader_addline(buffer, " float l_att;\n");
9059 shader_addline(buffer, " float q_att;\n");
9060 shader_addline(buffer, " float cos_htheta;\n");
9061 shader_addline(buffer, " float cos_hphi;\n");
9062 shader_addline(buffer, "} ffp_light[%u];\n", WINED3D_MAX_ACTIVE_LIGHTS);
9064 if (settings->point_size)
9066 shader_addline(buffer, "uniform struct\n{\n");
9067 shader_addline(buffer, " float size;\n");
9068 shader_addline(buffer, " float size_min;\n");
9069 shader_addline(buffer, " float size_max;\n");
9070 shader_addline(buffer, " float c_att;\n");
9071 shader_addline(buffer, " float l_att;\n");
9072 shader_addline(buffer, " float q_att;\n");
9073 shader_addline(buffer, "} ffp_point;\n");
9076 if (legacy_syntax)
9078 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9079 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9080 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9081 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9083 else
9085 if (settings->clipping)
9086 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9088 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9089 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9090 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9091 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9094 shader_addline(buffer, "\nvec3 ffp_normalize(vec3 n)\n{\n");
9095 shader_addline(buffer, " float lensq = dot(n, n);\n");
9096 shader_addline(buffer, " return lensq == 0.0 ? n : (n * inversesqrt(lensq));\n");
9097 shader_addline(buffer, "}\n");
9099 shader_addline(buffer, "\nvoid main()\n{\n");
9100 shader_addline(buffer, "float m;\n");
9101 shader_addline(buffer, "vec3 r;\n");
9103 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9105 if (attrib_info[i].name[0])
9106 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9107 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9109 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9111 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9112 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9113 && settings->texcoords & (1u << i))
9114 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9117 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9119 if (settings->transformed)
9121 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9122 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9123 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9125 else
9127 for (i = 0; i < settings->vertexblends; ++i)
9128 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9130 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9131 for (i = 0; i < settings->vertexblends + 1; ++i)
9132 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9134 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9135 if (settings->clipping)
9137 if (legacy_syntax)
9138 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9139 else
9140 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9141 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9143 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9146 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9147 if (settings->normal)
9149 if (!settings->vertexblends)
9151 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9153 else
9155 for (i = 0; i < settings->vertexblends + 1; ++i)
9156 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9159 if (settings->normalize)
9160 shader_addline(buffer, "normal = ffp_normalize(normal);\n");
9163 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9164 if (legacy_syntax)
9166 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9167 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9169 else
9171 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9172 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9175 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9177 BOOL output_legacy_texcoord = legacy_syntax;
9179 switch (settings->texgen[i] & 0xffff0000)
9181 case WINED3DTSS_TCI_PASSTHRU:
9182 if (settings->texcoords & (1u << i))
9183 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9184 i, i, i);
9185 else if (shader_glsl_full_ffp_varyings(gl_info))
9186 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9187 else
9188 output_legacy_texcoord = FALSE;
9189 break;
9191 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9192 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9193 break;
9195 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9196 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9197 break;
9199 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9200 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9201 " * vec4(reflect(ffp_normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9202 break;
9204 case WINED3DTSS_TCI_SPHEREMAP:
9205 shader_addline(buffer, "r = reflect(ffp_normalize(ec_pos.xyz), normal);\n");
9206 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9207 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9208 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9209 break;
9211 default:
9212 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9213 break;
9215 if (output_legacy_texcoord)
9216 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9219 switch (settings->fog_mode)
9221 case WINED3D_FFP_VS_FOG_OFF:
9222 output_legacy_fogcoord = FALSE;
9223 break;
9225 case WINED3D_FFP_VS_FOG_FOGCOORD:
9226 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9227 break;
9229 case WINED3D_FFP_VS_FOG_RANGE:
9230 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9231 break;
9233 case WINED3D_FFP_VS_FOG_DEPTH:
9234 if (settings->ortho_fog)
9236 if (gl_info->supported[ARB_CLIP_CONTROL])
9237 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9238 else
9239 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9240 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9242 else if (settings->transformed)
9244 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9246 else
9248 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9250 break;
9252 default:
9253 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9254 break;
9256 if (output_legacy_fogcoord)
9257 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9259 if (settings->point_size)
9261 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9262 " + ffp_point.l_att * length(ec_pos.xyz)"
9263 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9264 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9265 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9268 shader_addline(buffer, "}\n");
9270 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9271 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9273 return shader_obj;
9276 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9277 unsigned int argnum, unsigned int stage, DWORD arg)
9279 const char *ret;
9281 if (arg == ARG_UNUSED)
9282 return "<unused arg>";
9284 switch (arg & WINED3DTA_SELECTMASK)
9286 case WINED3DTA_DIFFUSE:
9287 ret = "ffp_varying_diffuse";
9288 break;
9290 case WINED3DTA_CURRENT:
9291 ret = "ret";
9292 break;
9294 case WINED3DTA_TEXTURE:
9295 switch (stage)
9297 case 0: ret = "tex0"; break;
9298 case 1: ret = "tex1"; break;
9299 case 2: ret = "tex2"; break;
9300 case 3: ret = "tex3"; break;
9301 case 4: ret = "tex4"; break;
9302 case 5: ret = "tex5"; break;
9303 case 6: ret = "tex6"; break;
9304 case 7: ret = "tex7"; break;
9305 default:
9306 ret = "<invalid texture>";
9307 break;
9309 break;
9311 case WINED3DTA_TFACTOR:
9312 ret = "tex_factor";
9313 break;
9315 case WINED3DTA_SPECULAR:
9316 ret = "ffp_varying_specular";
9317 break;
9319 case WINED3DTA_TEMP:
9320 ret = "temp_reg";
9321 break;
9323 case WINED3DTA_CONSTANT:
9324 switch (stage)
9326 case 0: ret = "tss_const0"; break;
9327 case 1: ret = "tss_const1"; break;
9328 case 2: ret = "tss_const2"; break;
9329 case 3: ret = "tss_const3"; break;
9330 case 4: ret = "tss_const4"; break;
9331 case 5: ret = "tss_const5"; break;
9332 case 6: ret = "tss_const6"; break;
9333 case 7: ret = "tss_const7"; break;
9334 default:
9335 ret = "<invalid constant>";
9336 break;
9338 break;
9340 default:
9341 return "<unhandled arg>";
9344 if (arg & WINED3DTA_COMPLEMENT)
9346 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9347 if (argnum == 0)
9348 ret = "arg0";
9349 else if (argnum == 1)
9350 ret = "arg1";
9351 else if (argnum == 2)
9352 ret = "arg2";
9355 if (arg & WINED3DTA_ALPHAREPLICATE)
9357 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9358 if (argnum == 0)
9359 ret = "arg0";
9360 else if (argnum == 1)
9361 ret = "arg1";
9362 else if (argnum == 2)
9363 ret = "arg2";
9366 return ret;
9369 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9370 BOOL alpha, BOOL tmp_dst, unsigned int op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9372 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9374 if (color && alpha)
9375 dstmask = "";
9376 else if (color)
9377 dstmask = ".xyz";
9378 else
9379 dstmask = ".w";
9381 dstreg = tmp_dst ? "temp_reg" : "ret";
9383 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9384 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9385 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9387 switch (op)
9389 case WINED3D_TOP_DISABLE:
9390 break;
9392 case WINED3D_TOP_SELECT_ARG1:
9393 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9394 break;
9396 case WINED3D_TOP_SELECT_ARG2:
9397 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9398 break;
9400 case WINED3D_TOP_MODULATE:
9401 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9402 break;
9404 case WINED3D_TOP_MODULATE_4X:
9405 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9406 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9407 break;
9409 case WINED3D_TOP_MODULATE_2X:
9410 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9411 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9412 break;
9414 case WINED3D_TOP_ADD:
9415 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9416 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9417 break;
9419 case WINED3D_TOP_ADD_SIGNED:
9420 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9421 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9422 break;
9424 case WINED3D_TOP_ADD_SIGNED_2X:
9425 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9426 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9427 break;
9429 case WINED3D_TOP_SUBTRACT:
9430 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9431 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9432 break;
9434 case WINED3D_TOP_ADD_SMOOTH:
9435 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9436 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9437 break;
9439 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9440 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9441 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9442 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9443 break;
9445 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9446 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9447 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9448 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9449 break;
9451 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9452 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9453 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9454 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9455 break;
9457 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9458 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9459 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9460 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9461 break;
9463 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9464 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9465 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9466 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9467 break;
9469 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9470 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9471 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9472 break;
9474 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9475 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9476 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9477 break;
9479 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9480 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9481 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9482 break;
9483 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9484 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9485 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9486 break;
9488 case WINED3D_TOP_BUMPENVMAP:
9489 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9490 /* These are handled in the first pass, nothing to do. */
9491 break;
9493 case WINED3D_TOP_DOTPRODUCT3:
9494 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9495 dstreg, dstmask, arg1, arg2, dstmask);
9496 break;
9498 case WINED3D_TOP_MULTIPLY_ADD:
9499 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9500 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9501 break;
9503 case WINED3D_TOP_LERP:
9504 /* MSDN isn't quite right here. */
9505 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9506 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9507 break;
9509 default:
9510 FIXME("Unhandled operation %#x.\n", op);
9511 break;
9515 /* Context activation is done by the caller. */
9516 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9517 const struct ffp_frag_settings *settings, const struct wined3d_context_gl *context_gl)
9519 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9520 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9521 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9522 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9523 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
9524 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9525 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9526 UINT lowest_disabled_stage;
9527 GLuint shader_id;
9528 DWORD arg0, arg1, arg2;
9529 unsigned int stage;
9531 string_buffer_clear(buffer);
9533 /* Find out which textures are read */
9534 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9536 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9537 break;
9539 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9540 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9541 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9543 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9544 || (stage == 0 && settings->color_key_enabled))
9545 tex_map |= 1u << stage;
9546 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9547 tfactor_used = TRUE;
9548 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9549 tempreg_used = TRUE;
9550 if (settings->op[stage].tmp_dst)
9551 tempreg_used = TRUE;
9552 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9553 tss_const_map |= 1u << stage;
9555 switch (settings->op[stage].cop)
9557 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9558 lum_map |= 1u << stage;
9559 /* fall through */
9560 case WINED3D_TOP_BUMPENVMAP:
9561 bump_map |= 1u << stage;
9562 /* fall through */
9563 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9564 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9565 tex_map |= 1u << stage;
9566 break;
9568 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9569 tfactor_used = TRUE;
9570 break;
9572 default:
9573 break;
9576 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9577 continue;
9579 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9580 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9581 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9583 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9584 tex_map |= 1u << stage;
9585 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9586 tfactor_used = TRUE;
9587 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9588 tempreg_used = TRUE;
9589 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9590 tss_const_map |= 1u << stage;
9592 lowest_disabled_stage = stage;
9594 shader_glsl_add_version_declaration(buffer, gl_info);
9596 if (shader_glsl_use_explicit_attrib_location(gl_info))
9597 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9598 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9599 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9600 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9601 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9603 if (!use_legacy_fragment_output(gl_info))
9605 shader_addline(buffer, "vec4 ps_out[1];\n");
9606 if (shader_glsl_use_explicit_attrib_location(gl_info))
9607 shader_addline(buffer, "layout(location = 0) ");
9608 shader_addline(buffer, "out vec4 color_out0;\n");
9611 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9612 shader_addline(buffer, "vec4 ret;\n");
9613 if (tempreg_used || settings->sRGB_write)
9614 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9615 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9617 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9619 const char *sampler_type;
9621 if (tss_const_map & (1u << stage))
9622 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9624 if (!(tex_map & (1u << stage)))
9625 continue;
9627 switch (settings->op[stage].tex_type)
9629 case WINED3D_GL_RES_TYPE_TEX_1D:
9630 sampler_type = "1D";
9631 break;
9632 case WINED3D_GL_RES_TYPE_TEX_2D:
9633 sampler_type = "2D";
9634 break;
9635 case WINED3D_GL_RES_TYPE_TEX_3D:
9636 sampler_type = "3D";
9637 break;
9638 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9639 sampler_type = "Cube";
9640 break;
9641 case WINED3D_GL_RES_TYPE_TEX_RECT:
9642 sampler_type = "2DRect";
9643 break;
9644 default:
9645 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9646 sampler_type = NULL;
9647 break;
9649 if (sampler_type)
9651 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9652 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
9653 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9656 shader_addline(buffer, "vec4 tex%u;\n", stage);
9658 if (!(bump_map & (1u << stage)))
9659 continue;
9660 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9662 if (!(lum_map & (1u << stage)))
9663 continue;
9664 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9665 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9667 if (tfactor_used)
9668 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9669 if (settings->color_key_enabled)
9670 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9671 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9673 if (settings->sRGB_write)
9675 shader_addline(buffer, "const vec4 srgb_const0 = ");
9676 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
9677 shader_addline(buffer, ";\n");
9678 shader_addline(buffer, "const vec4 srgb_const1 = ");
9679 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
9680 shader_addline(buffer, ";\n");
9683 shader_addline(buffer, "uniform struct\n{\n");
9684 shader_addline(buffer, " vec4 color;\n");
9685 shader_addline(buffer, " float density;\n");
9686 shader_addline(buffer, " float end;\n");
9687 shader_addline(buffer, " float scale;\n");
9688 shader_addline(buffer, "} ffp_fog;\n");
9690 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9691 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9693 if (legacy_syntax)
9695 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9696 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9697 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9698 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9699 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9701 else
9703 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9704 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9705 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9706 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9707 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9710 shader_addline(buffer, "void main()\n{\n");
9712 if (legacy_syntax)
9714 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9715 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9718 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9720 if (tex_map & (1u << stage))
9722 if (settings->pointsprite)
9723 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9724 else if (settings->texcoords_initialized & (1u << stage))
9725 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9726 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9727 else
9728 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9732 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9733 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9735 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9736 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9738 /* Generate texture sampling instructions */
9739 for (stage = 0; stage < WINED3D_MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9741 const char *texture_function, *coord_mask;
9742 BOOL proj;
9744 if (!(tex_map & (1u << stage)))
9745 continue;
9747 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9749 proj = FALSE;
9751 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9752 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9754 proj = TRUE;
9756 else
9758 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9759 proj = TRUE;
9762 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9763 proj = FALSE;
9765 switch (settings->op[stage].tex_type)
9767 case WINED3D_GL_RES_TYPE_TEX_1D:
9768 texture_function = "texture1D";
9769 coord_mask = "x";
9770 break;
9771 case WINED3D_GL_RES_TYPE_TEX_2D:
9772 texture_function = "texture2D";
9773 coord_mask = "xy";
9774 break;
9775 case WINED3D_GL_RES_TYPE_TEX_3D:
9776 texture_function = "texture3D";
9777 coord_mask = "xyz";
9778 break;
9779 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9780 texture_function = "textureCube";
9781 coord_mask = "xyz";
9782 break;
9783 case WINED3D_GL_RES_TYPE_TEX_RECT:
9784 texture_function = "texture2DRect";
9785 coord_mask = "xy";
9786 break;
9787 default:
9788 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9789 texture_function = "";
9790 coord_mask = "xyzw";
9791 proj = FALSE;
9792 break;
9794 if (!legacy_syntax)
9795 texture_function = "texture";
9797 if (stage > 0
9798 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9799 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9801 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9803 /* With projective textures, texbem only divides the static
9804 * texture coordinate, not the displacement, so multiply the
9805 * displacement with the dividing parameter before passing it to
9806 * TXP. */
9807 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9809 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9811 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9812 stage, stage);
9813 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9815 else
9817 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9818 stage, stage);
9819 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9822 else
9824 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9827 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ret.%s%s);\n",
9828 stage, texture_function, proj ? "Proj" : "", stage, coord_mask, proj ? "w" : "");
9830 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9831 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9832 stage, stage - 1, stage - 1, stage - 1);
9834 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9836 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9837 stage, texture_function, proj ? "Proj" : "", stage, stage);
9839 else
9841 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].%s%s);\n",
9842 stage, texture_function, proj ? "Proj" : "", stage, stage, coord_mask, proj ? "w" : "");
9845 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9846 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9847 settings->op[stage].color_fixup);
9850 if (settings->color_key_enabled)
9851 shader_glsl_generate_colour_key_test(buffer, "tex0", "color_key[0]", "color_key[1]");
9853 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9855 /* Generate the main shader */
9856 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9858 BOOL op_equal;
9860 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9861 break;
9863 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9864 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9865 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9866 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9867 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9868 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9869 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9870 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9871 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9872 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9873 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9874 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9875 else
9876 op_equal = settings->op[stage].aop == settings->op[stage].cop
9877 && settings->op[stage].carg0 == settings->op[stage].aarg0
9878 && settings->op[stage].carg1 == settings->op[stage].aarg1
9879 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9881 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9883 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9884 settings->op[stage].cop, settings->op[stage].carg0,
9885 settings->op[stage].carg1, settings->op[stage].carg2);
9887 else if (op_equal)
9889 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9890 settings->op[stage].cop, settings->op[stage].carg0,
9891 settings->op[stage].carg1, settings->op[stage].carg2);
9893 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9894 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9896 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9897 settings->op[stage].cop, settings->op[stage].carg0,
9898 settings->op[stage].carg1, settings->op[stage].carg2);
9899 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9900 settings->op[stage].aop, settings->op[stage].aarg0,
9901 settings->op[stage].aarg1, settings->op[stage].aarg2);
9905 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9906 get_fragment_output(gl_info));
9908 if (settings->sRGB_write)
9909 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9911 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9913 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9914 if (!use_legacy_fragment_output(gl_info))
9915 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9917 shader_addline(buffer, "}\n");
9919 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9920 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9922 string_buffer_release(&priv->string_buffers, tex_reg_name);
9923 return shader_id;
9926 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9927 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9929 struct glsl_ffp_vertex_shader *shader;
9930 const struct wine_rb_entry *entry;
9932 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9933 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9935 if (!(shader = heap_alloc(sizeof(*shader))))
9936 return NULL;
9938 shader->desc.settings = *settings;
9939 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9940 list_init(&shader->linked_programs);
9941 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9942 ERR("Failed to insert ffp vertex shader.\n");
9944 return shader;
9947 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9948 const struct ffp_frag_settings *args, const struct wined3d_context_gl *context_gl)
9950 struct glsl_ffp_fragment_shader *glsl_desc;
9951 const struct ffp_frag_desc *desc;
9953 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9954 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9956 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9957 return NULL;
9959 glsl_desc->entry.settings = *args;
9960 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context_gl);
9961 list_init(&glsl_desc->linked_programs);
9962 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9964 return glsl_desc;
9968 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9969 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9971 unsigned int i;
9972 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9974 for (i = 0; i < vs_c_count; ++i)
9976 string_buffer_sprintf(name, "vs_c[%u]", i);
9977 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9979 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9981 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9983 string_buffer_sprintf(name, "vs_i[%u]", i);
9984 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9987 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9989 string_buffer_sprintf(name, "vs_b[%u]", i);
9990 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9993 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9994 vs->base_vertex_id_location = GL_EXTCALL(glGetUniformLocation(program_id, "base_vertex_id"));
9996 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9998 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9999 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10001 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
10002 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
10003 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10005 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
10006 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10008 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
10009 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
10010 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
10011 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
10012 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
10013 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
10014 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
10016 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
10017 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10018 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
10019 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10020 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
10021 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10022 string_buffer_sprintf(name, "ffp_light[%u].position", i);
10023 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10024 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
10025 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10026 string_buffer_sprintf(name, "ffp_light[%u].range", i);
10027 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10028 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
10029 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10030 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
10031 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10032 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
10033 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10034 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
10035 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10036 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
10037 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10038 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
10039 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10041 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10042 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10043 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10044 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10045 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10046 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10047 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10049 string_buffer_release(&priv->string_buffers, name);
10052 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10053 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10055 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10058 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10059 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10061 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10064 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10065 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10067 unsigned int i;
10068 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10070 for (i = 0; i < ps_c_count; ++i)
10072 string_buffer_sprintf(name, "ps_c[%u]", i);
10073 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10075 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10077 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10079 string_buffer_sprintf(name, "ps_i[%u]", i);
10080 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10083 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10085 string_buffer_sprintf(name, "ps_b[%u]", i);
10086 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10089 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10091 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10092 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10093 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10094 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10095 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10096 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10097 string_buffer_sprintf(name, "tss_const%u", i);
10098 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10101 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10102 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10104 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10105 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10106 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10107 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10109 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10111 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10112 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10113 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10115 string_buffer_release(&priv->string_buffers, name);
10118 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10119 const struct wined3d_context_gl *context_gl, struct wined3d_shader *shader)
10121 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10122 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10123 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10124 struct glsl_cs_compiled_shader *gl_shaders;
10125 struct glsl_shader_private *shader_data;
10126 struct glsl_shader_prog_link *entry;
10127 GLuint shader_id, program_id;
10129 if (!(entry = heap_alloc(sizeof(*entry))))
10131 ERR("Out of memory.\n");
10132 return E_OUTOFMEMORY;
10135 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10137 ERR("Failed to allocate backend data.\n");
10138 heap_free(entry);
10139 return E_OUTOFMEMORY;
10141 shader_data = shader->backend_data;
10143 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10145 ERR("Failed to allocate GL shader array.\n");
10146 heap_free(entry);
10147 heap_free(shader->backend_data);
10148 shader->backend_data = NULL;
10149 return E_OUTOFMEMORY;
10151 shader_data->shader_array_size = 1;
10152 gl_shaders = shader_data->gl_shaders.cs;
10154 TRACE("Compiling compute shader %p.\n", shader);
10156 string_buffer_clear(buffer);
10157 shader_id = shader_glsl_generate_compute_shader(context_gl, buffer, &priv->string_buffers, shader);
10158 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10160 program_id = GL_EXTCALL(glCreateProgram());
10161 TRACE("Created new GLSL shader program %u.\n", program_id);
10163 entry->id = program_id;
10164 entry->vs.id = 0;
10165 entry->hs.id = 0;
10166 entry->ds.id = 0;
10167 entry->gs.id = 0;
10168 entry->ps.id = 0;
10169 entry->cs.id = shader_id;
10170 entry->constant_version = 0;
10171 entry->shader_controlled_clip_distances = 0;
10172 entry->ps.np2_fixup_info = NULL;
10173 add_glsl_program_entry(priv, entry);
10175 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10176 GL_EXTCALL(glAttachShader(program_id, shader_id));
10177 checkGLcall("glAttachShader");
10179 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10181 TRACE("Linking GLSL shader program %u.\n", program_id);
10182 GL_EXTCALL(glLinkProgram(program_id));
10183 shader_glsl_validate_link(gl_info, program_id);
10185 GL_EXTCALL(glUseProgram(program_id));
10186 checkGLcall("glUseProgram");
10187 shader_glsl_load_program_resources(context_gl, priv, program_id, shader);
10188 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10190 entry->constant_update_mask = 0;
10192 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10193 checkGLcall("glUseProgram");
10194 return WINED3D_OK;
10197 static GLuint find_glsl_compute_shader(const struct wined3d_context_gl *context_gl,
10198 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10200 struct glsl_shader_private *shader_data;
10202 if (!shader->backend_data)
10204 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10205 if (FAILED(shader_glsl_compile_compute_shader(priv, context_gl, shader)))
10207 ERR("Failed to compile compute shader %p.\n", shader);
10208 return 0;
10211 shader_data = shader->backend_data;
10212 return shader_data->gl_shaders.cs[0].id;
10215 /* Context activation is done by the caller. */
10216 static void set_glsl_compute_shader_program(const struct wined3d_context_gl *context_gl,
10217 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10219 struct glsl_shader_prog_link *entry;
10220 struct wined3d_shader *shader;
10221 struct glsl_program_key key;
10222 GLuint cs_id;
10224 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10225 return;
10227 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10229 WARN("Compute shader is NULL.\n");
10230 ctx_data->glsl_program = NULL;
10231 return;
10234 cs_id = find_glsl_compute_shader(context_gl, priv, shader);
10235 memset(&key, 0, sizeof(key));
10236 key.cs_id = cs_id;
10237 if (!(entry = get_glsl_program_entry(priv, &key)))
10238 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10239 ctx_data->glsl_program = entry;
10242 /* Context activation is done by the caller. */
10243 static void set_glsl_shader_program(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
10244 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10246 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
10247 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10248 const struct wined3d_shader *pre_rasterization_shader;
10249 const struct ps_np2fixup_info *np2fixup_info = NULL;
10250 struct wined3d_shader *hshader, *dshader, *gshader;
10251 struct glsl_shader_prog_link *entry = NULL;
10252 struct wined3d_shader *vshader = NULL;
10253 struct wined3d_shader *pshader = NULL;
10254 GLuint reorder_shader_id = 0;
10255 struct glsl_program_key key;
10256 uint32_t attribs_map;
10257 GLuint program_id;
10258 unsigned int i;
10259 GLuint vs_id = 0;
10260 GLuint hs_id = 0;
10261 GLuint ds_id = 0;
10262 GLuint gs_id = 0;
10263 GLuint ps_id = 0;
10264 struct list *ps_list, *vs_list;
10265 struct wined3d_string_buffer *tmp_name;
10267 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10269 vs_id = ctx_data->glsl_program->vs.id;
10270 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10272 if (use_vs(state))
10273 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10275 else if (use_vs(state))
10277 struct vs_compile_args vs_compile_args;
10279 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10281 find_vs_compile_args(state, vshader, &vs_compile_args, &context_gl->c);
10282 vs_id = find_glsl_vertex_shader(context_gl, priv, vshader, &vs_compile_args);
10283 vs_list = &vshader->linked_programs;
10285 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10287 struct glsl_ffp_vertex_shader *ffp_shader;
10288 struct wined3d_ffp_vs_settings settings;
10290 wined3d_ffp_get_vs_settings(&context_gl->c, state, &settings);
10291 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10292 vs_id = ffp_shader->id;
10293 vs_list = &ffp_shader->linked_programs;
10296 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10297 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10298 hs_id = ctx_data->glsl_program->hs.id;
10299 else if (hshader)
10300 hs_id = find_glsl_hull_shader(context_gl, priv, hshader);
10302 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10303 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10305 ds_id = ctx_data->glsl_program->ds.id;
10307 else if (dshader)
10309 struct ds_compile_args args;
10311 find_ds_compile_args(state, dshader, &args, &context_gl->c);
10312 ds_id = find_glsl_domain_shader(context_gl, priv, dshader, &args);
10315 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10316 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10318 gs_id = ctx_data->glsl_program->gs.id;
10320 else if (gshader)
10322 struct gs_compile_args args;
10324 find_gs_compile_args(state, gshader, &args, &context_gl->c);
10325 gs_id = find_glsl_geometry_shader(context_gl, priv, gshader, &args);
10328 /* A pixel shader is not used when rasterization is disabled. */
10329 if (is_rasterization_disabled(gshader))
10331 ps_id = 0;
10332 ps_list = NULL;
10334 else if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10336 ps_id = ctx_data->glsl_program->ps.id;
10337 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10339 if (use_ps(state))
10340 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10342 else if (use_ps(state))
10344 struct ps_compile_args ps_compile_args;
10345 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10346 find_ps_compile_args(state, pshader, context_gl->c.stream_info.position_transformed,
10347 &ps_compile_args, &context_gl->c);
10348 ps_id = find_glsl_fragment_shader(context_gl, &priv->shader_buffer, &priv->string_buffers,
10349 pshader, &ps_compile_args, &np2fixup_info);
10350 ps_list = &pshader->linked_programs;
10352 else if (priv->fragment_pipe == &glsl_fragment_pipe
10353 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10355 struct glsl_ffp_fragment_shader *ffp_shader;
10356 struct ffp_frag_settings settings;
10358 wined3d_ffp_get_fs_settings(&context_gl->c, state, &settings, FALSE);
10359 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context_gl);
10360 ps_id = ffp_shader->id;
10361 ps_list = &ffp_shader->linked_programs;
10364 key.vs_id = vs_id;
10365 key.hs_id = hs_id;
10366 key.ds_id = ds_id;
10367 key.gs_id = gs_id;
10368 key.ps_id = ps_id;
10369 key.cs_id = 0;
10370 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10372 ctx_data->glsl_program = entry;
10373 return;
10376 /* If we get to this point, then no matching program exists, so we create one */
10377 program_id = GL_EXTCALL(glCreateProgram());
10378 TRACE("Created new GLSL shader program %u.\n", program_id);
10380 /* Create the entry */
10381 entry = heap_alloc(sizeof(*entry));
10382 entry->id = program_id;
10383 entry->vs.id = vs_id;
10384 entry->hs.id = hs_id;
10385 entry->ds.id = ds_id;
10386 entry->gs.id = gs_id;
10387 entry->ps.id = ps_id;
10388 entry->cs.id = 0;
10389 entry->constant_version = 0;
10390 entry->shader_controlled_clip_distances = 0;
10391 entry->ps.np2_fixup_info = np2fixup_info;
10392 /* Add the hash table entry */
10393 add_glsl_program_entry(priv, entry);
10395 /* Set the current program */
10396 ctx_data->glsl_program = entry;
10398 /* Attach GLSL vshader */
10399 if (vs_id)
10401 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10402 GL_EXTCALL(glAttachShader(program_id, vs_id));
10403 checkGLcall("glAttachShader");
10405 list_add_head(vs_list, &entry->vs.shader_entry);
10408 if (vshader)
10410 attribs_map = vshader->reg_maps.input_registers;
10411 if (vshader->reg_maps.shader_version.major < 4)
10413 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10414 state->primitive_type == WINED3D_PT_POINTLIST && vshader->reg_maps.point_size,
10415 d3d_info->emulated_flatshading
10416 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10417 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10418 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10419 checkGLcall("glAttachShader");
10420 /* Flag the reorder function for deletion, it will be freed
10421 * automatically when the program is destroyed. */
10422 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10425 else
10427 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10430 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10432 /* Bind vertex attributes to a corresponding index number to match
10433 * the same index numbers as ARB_vertex_programs (makes loading
10434 * vertex attributes simpler). With this method, we can use the
10435 * exact same code to load the attributes later for both ARB and
10436 * GLSL shaders.
10438 * We have to do this here because we need to know the Program ID
10439 * in order to make the bindings work, and it has to be done prior
10440 * to linking the GLSL program. */
10441 tmp_name = string_buffer_get(&priv->string_buffers);
10442 while (attribs_map)
10444 i = wined3d_bit_scan(&attribs_map);
10445 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10446 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10447 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10449 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10450 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10451 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10452 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10455 checkGLcall("glBindAttribLocation");
10457 if (!use_legacy_fragment_output(gl_info))
10459 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
10461 string_buffer_sprintf(tmp_name, "color_out%u", i);
10462 if (state->blend_state && state->blend_state->dual_source)
10463 GL_EXTCALL(glBindFragDataLocationIndexed(program_id, 0, i, tmp_name->buffer));
10464 else
10465 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10466 checkGLcall("glBindFragDataLocation");
10469 string_buffer_release(&priv->string_buffers, tmp_name);
10472 if (hshader)
10474 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10475 GL_EXTCALL(glAttachShader(program_id, hs_id));
10476 checkGLcall("glAttachShader");
10478 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10481 if (dshader)
10483 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10484 GL_EXTCALL(glAttachShader(program_id, ds_id));
10485 checkGLcall("glAttachShader");
10487 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10490 if (gshader)
10492 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10493 GL_EXTCALL(glAttachShader(program_id, gs_id));
10494 checkGLcall("glAttachShader");
10496 shader_glsl_init_transform_feedback(context_gl, priv, program_id, gshader);
10498 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10501 /* Attach GLSL pshader */
10502 if (ps_id)
10504 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10505 GL_EXTCALL(glAttachShader(program_id, ps_id));
10506 checkGLcall("glAttachShader");
10508 list_add_head(ps_list, &entry->ps.shader_entry);
10511 /* Link the program */
10512 TRACE("Linking GLSL shader program %u.\n", program_id);
10513 GL_EXTCALL(glLinkProgram(program_id));
10514 shader_glsl_validate_link(gl_info, program_id);
10516 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10517 vshader ? vshader->limits->constant_float : 0);
10518 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10519 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10520 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10521 pshader ? pshader->limits->constant_float : 0);
10522 checkGLcall("find glsl program uniform locations");
10524 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10525 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10527 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10528 entry->shader_controlled_clip_distances = 1;
10529 entry->clip_distance_mask = wined3d_mask_from_size(clip_distance_count);
10532 if (needs_legacy_glsl_syntax(gl_info))
10534 if (pshader && pshader->reg_maps.shader_version.major >= 3
10535 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10537 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10538 entry->vs.vertex_color_clamp = GL_FALSE;
10540 else
10542 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10545 else
10547 /* With core profile we never change vertex_color_clamp from
10548 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10549 * glClampColorARB(). */
10550 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10553 /* Set the shader to allow uniform loading on it */
10554 GL_EXTCALL(glUseProgram(program_id));
10555 checkGLcall("glUseProgram");
10557 entry->constant_update_mask = 0;
10558 if (vshader)
10560 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10561 if (vshader->reg_maps.integer_constants)
10562 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10563 if (vshader->reg_maps.boolean_constants)
10564 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10565 if (entry->vs.pos_fixup_location != -1)
10566 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10567 if (entry->vs.base_vertex_id_location != -1)
10568 entry->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
10570 shader_glsl_load_program_resources(context_gl, priv, program_id, vshader);
10572 else
10574 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10575 | WINED3D_SHADER_CONST_FFP_PROJ;
10577 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10579 if (entry->vs.modelview_matrix_location[i] != -1)
10581 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10582 break;
10586 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10588 if (entry->vs.texture_matrix_location[i] != -1)
10590 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10591 break;
10594 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10595 || entry->vs.material_specular_location != -1
10596 || entry->vs.material_emissive_location != -1
10597 || entry->vs.material_shininess_location != -1)
10598 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10599 if (entry->vs.light_ambient_location != -1)
10600 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10602 if (entry->vs.clip_planes_location != -1)
10603 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10604 if (entry->vs.pointsize_min_location != -1)
10605 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10607 if (hshader)
10608 shader_glsl_load_program_resources(context_gl, priv, program_id, hshader);
10610 if (dshader)
10612 if (entry->ds.pos_fixup_location != -1)
10613 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10615 shader_glsl_load_program_resources(context_gl, priv, program_id, dshader);
10618 if (gshader)
10620 if (entry->gs.pos_fixup_location != -1)
10621 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10623 shader_glsl_load_program_resources(context_gl, priv, program_id, gshader);
10626 if (ps_id)
10628 if (pshader)
10630 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10631 if (pshader->reg_maps.integer_constants)
10632 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10633 if (pshader->reg_maps.boolean_constants)
10634 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10635 if (entry->ps.ycorrection_location != -1)
10636 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10638 shader_glsl_load_program_resources(context_gl, priv, program_id, pshader);
10639 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10641 else
10643 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10645 shader_glsl_load_samplers(&context_gl->c, priv, program_id, NULL);
10648 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10650 if (entry->ps.bumpenv_mat_location[i] != -1)
10652 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10653 break;
10657 if (entry->ps.fog_color_location != -1)
10658 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10659 if (entry->ps.alpha_test_ref_location != -1)
10660 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10661 if (entry->ps.np2_fixup_location != -1)
10662 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10663 if (entry->ps.color_key_location != -1)
10664 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10668 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10670 struct wined3d_device *device = shader->device;
10671 struct wined3d_context *context;
10673 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10675 context = context_acquire(device, NULL, 0);
10676 shader_glsl_compile_compute_shader(shader_priv, wined3d_context_gl(context), shader);
10677 context_release(context);
10681 /* Context activation is done by the caller. */
10682 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10683 const struct wined3d_state *state)
10685 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10686 struct glsl_context_data *ctx_data = context->shader_backend_data;
10687 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10688 struct shader_glsl_priv *priv = shader_priv;
10689 struct glsl_shader_prog_link *glsl_program;
10690 GLenum current_vertex_color_clamp;
10691 GLuint program_id, prev_id;
10693 priv->vertex_pipe->vp_enable(context, !use_vs(state));
10694 priv->fragment_pipe->fp_enable(context, !use_ps(state));
10696 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10697 set_glsl_shader_program(context_gl, state, priv, ctx_data);
10698 glsl_program = ctx_data->glsl_program;
10700 if (glsl_program)
10702 program_id = glsl_program->id;
10703 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10704 if (glsl_program->shader_controlled_clip_distances)
10705 wined3d_context_gl_enable_clip_distances(context_gl, glsl_program->clip_distance_mask);
10707 else
10709 program_id = 0;
10710 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10713 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10715 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10716 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10718 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10719 checkGLcall("glClampColorARB");
10721 else
10723 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10727 TRACE("Using GLSL program %u.\n", program_id);
10729 if (prev_id != program_id)
10731 GL_EXTCALL(glUseProgram(program_id));
10732 checkGLcall("glUseProgram");
10734 if (glsl_program)
10735 context->constant_update_mask |= glsl_program->constant_update_mask;
10738 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10741 /* Context activation is done by the caller. */
10742 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10743 const struct wined3d_state *state)
10745 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10746 struct glsl_context_data *ctx_data = context->shader_backend_data;
10747 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10748 struct shader_glsl_priv *priv = shader_priv;
10749 GLuint program_id, prev_id;
10751 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10752 set_glsl_compute_shader_program(context_gl, state, priv, ctx_data);
10753 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10755 TRACE("Using GLSL program %u.\n", program_id);
10757 if (prev_id != program_id)
10759 GL_EXTCALL(glUseProgram(program_id));
10760 checkGLcall("glUseProgram");
10763 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10764 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10765 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10766 | (1u << WINED3D_SHADER_TYPE_HULL)
10767 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10770 /* "context" is not necessarily the currently active context. */
10771 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10773 struct glsl_context_data *ctx_data = context->shader_backend_data;
10775 ctx_data->glsl_program = NULL;
10776 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10777 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10778 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10779 | (1u << WINED3D_SHADER_TYPE_HULL)
10780 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10781 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10784 /* Context activation is done by the caller. */
10785 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10787 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10788 struct glsl_context_data *ctx_data = context->shader_backend_data;
10789 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10790 struct shader_glsl_priv *priv = shader_priv;
10792 shader_glsl_invalidate_current_program(context);
10793 GL_EXTCALL(glUseProgram(0));
10794 checkGLcall("glUseProgram");
10796 priv->vertex_pipe->vp_enable(context, FALSE);
10797 priv->fragment_pipe->fp_enable(context, FALSE);
10799 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10801 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10802 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10803 checkGLcall("glClampColorARB");
10807 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10808 const struct glsl_shader_prog_link *program)
10810 const struct glsl_context_data *ctx_data;
10811 struct wined3d_context *context;
10812 unsigned int i;
10814 for (i = 0; i < device->context_count; ++i)
10816 context = device->contexts[i];
10817 ctx_data = context->shader_backend_data;
10819 if (ctx_data->glsl_program == program)
10820 shader_glsl_invalidate_current_program(context);
10824 static void shader_glsl_destroy(struct wined3d_shader *shader)
10826 struct glsl_shader_private *shader_data = shader->backend_data;
10827 struct wined3d_device *device = shader->device;
10828 struct shader_glsl_priv *priv = device->shader_priv;
10829 const struct wined3d_gl_info *gl_info;
10830 const struct list *linked_programs;
10831 struct wined3d_context *context;
10833 if (!shader_data || !shader_data->num_gl_shaders)
10835 heap_free(shader_data);
10836 shader->backend_data = NULL;
10837 return;
10840 context = context_acquire(device, NULL, 0);
10841 gl_info = wined3d_context_gl(context)->gl_info;
10843 TRACE("Deleting linked programs.\n");
10844 linked_programs = &shader->linked_programs;
10845 if (!list_empty(linked_programs))
10847 struct glsl_shader_prog_link *entry, *entry2;
10848 UINT i;
10850 switch (shader->reg_maps.shader_version.type)
10852 case WINED3D_SHADER_TYPE_PIXEL:
10854 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10856 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10858 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10859 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10860 checkGLcall("glDeleteShader");
10862 heap_free(shader_data->gl_shaders.ps);
10864 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10865 struct glsl_shader_prog_link, ps.shader_entry)
10867 shader_glsl_invalidate_contexts_program(device, entry);
10868 delete_glsl_program_entry(priv, gl_info, entry);
10871 break;
10874 case WINED3D_SHADER_TYPE_VERTEX:
10876 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10878 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10880 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10881 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10882 checkGLcall("glDeleteShader");
10884 heap_free(shader_data->gl_shaders.vs);
10886 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10887 struct glsl_shader_prog_link, vs.shader_entry)
10889 shader_glsl_invalidate_contexts_program(device, entry);
10890 delete_glsl_program_entry(priv, gl_info, entry);
10893 break;
10896 case WINED3D_SHADER_TYPE_HULL:
10898 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10900 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10902 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10903 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10904 checkGLcall("glDeleteShader");
10906 heap_free(shader_data->gl_shaders.hs);
10908 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10909 struct glsl_shader_prog_link, hs.shader_entry)
10911 shader_glsl_invalidate_contexts_program(device, entry);
10912 delete_glsl_program_entry(priv, gl_info, entry);
10915 break;
10918 case WINED3D_SHADER_TYPE_DOMAIN:
10920 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10922 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10924 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10925 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10926 checkGLcall("glDeleteShader");
10928 heap_free(shader_data->gl_shaders.ds);
10930 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10931 struct glsl_shader_prog_link, ds.shader_entry)
10933 shader_glsl_invalidate_contexts_program(device, entry);
10934 delete_glsl_program_entry(priv, gl_info, entry);
10937 break;
10940 case WINED3D_SHADER_TYPE_GEOMETRY:
10942 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10944 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10946 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10947 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10948 checkGLcall("glDeleteShader");
10950 heap_free(shader_data->gl_shaders.gs);
10952 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10953 struct glsl_shader_prog_link, gs.shader_entry)
10955 shader_glsl_invalidate_contexts_program(device, entry);
10956 delete_glsl_program_entry(priv, gl_info, entry);
10959 break;
10962 case WINED3D_SHADER_TYPE_COMPUTE:
10964 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10966 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10968 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10969 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10970 checkGLcall("glDeleteShader");
10972 heap_free(shader_data->gl_shaders.cs);
10974 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10975 struct glsl_shader_prog_link, cs.shader_entry)
10977 shader_glsl_invalidate_contexts_program(device, entry);
10978 delete_glsl_program_entry(priv, gl_info, entry);
10981 break;
10984 default:
10985 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10986 break;
10990 heap_free(shader->backend_data);
10991 shader->backend_data = NULL;
10993 context_release(context);
10996 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10998 const struct glsl_program_key *k = key;
10999 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
11000 const struct glsl_shader_prog_link, program_lookup_entry);
11001 int ret;
11003 if ((ret = wined3d_uint32_compare(k->vs_id, prog->vs.id)))
11004 return ret;
11005 if ((ret = wined3d_uint32_compare(k->gs_id, prog->gs.id)))
11006 return ret;
11007 if ((ret = wined3d_uint32_compare(k->ps_id, prog->ps.id)))
11008 return ret;
11009 if ((ret = wined3d_uint32_compare(k->hs_id, prog->hs.id)))
11010 return ret;
11011 if ((ret = wined3d_uint32_compare(k->ds_id, prog->ds.id)))
11012 return ret;
11013 if ((ret = wined3d_uint32_compare(k->cs_id, prog->cs.id)))
11014 return ret;
11016 return 0;
11019 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
11021 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
11022 + constant_count * sizeof(*heap->contained)
11023 + constant_count * sizeof(*heap->positions);
11024 void *mem;
11026 if (!(mem = heap_alloc(size)))
11028 ERR("Failed to allocate memory\n");
11029 return FALSE;
11032 heap->entries = mem;
11033 heap->entries[1].version = 0;
11034 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11035 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11036 heap->positions = (unsigned int *)(heap->contained + constant_count);
11037 heap->size = 1;
11039 return TRUE;
11042 static void constant_heap_free(struct constant_heap *heap)
11044 heap_free(heap->entries);
11047 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11048 const struct wined3d_fragment_pipe_ops *fragment_pipe)
11050 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11051 struct fragment_caps fragment_caps;
11052 void *vertex_priv, *fragment_priv;
11053 struct shader_glsl_priv *priv;
11055 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11056 return E_OUTOFMEMORY;
11058 string_buffer_list_init(&priv->string_buffers);
11060 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11062 ERR("Failed to initialize vertex pipe.\n");
11063 heap_free(priv);
11064 return E_FAIL;
11067 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11069 ERR("Failed to initialize fragment pipe.\n");
11070 vertex_pipe->vp_free(device, NULL);
11071 heap_free(priv);
11072 return E_FAIL;
11075 if (!string_buffer_init(&priv->shader_buffer))
11077 ERR("Failed to initialize shader buffer.\n");
11078 goto fail;
11081 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11083 ERR("Failed to allocate memory.\n");
11084 goto fail;
11087 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11089 ERR("Failed to initialize vertex shader constant heap\n");
11090 goto fail;
11093 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11095 ERR("Failed to initialize pixel shader constant heap\n");
11096 goto fail;
11099 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11101 priv->next_constant_version = 1;
11102 priv->vertex_pipe = vertex_pipe;
11103 priv->fragment_pipe = fragment_pipe;
11104 fragment_pipe->get_caps(device->adapter, &fragment_caps);
11105 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11106 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11108 device->vertex_priv = vertex_priv;
11109 device->fragment_priv = fragment_priv;
11110 device->shader_priv = priv;
11112 return WINED3D_OK;
11114 fail:
11115 constant_heap_free(&priv->pconst_heap);
11116 constant_heap_free(&priv->vconst_heap);
11117 heap_free(priv->stack);
11118 string_buffer_free(&priv->shader_buffer);
11119 fragment_pipe->free_private(device, NULL);
11120 vertex_pipe->vp_free(device, NULL);
11121 heap_free(priv);
11122 return E_OUTOFMEMORY;
11125 /* Context activation is done by the caller. */
11126 static void shader_glsl_free(struct wined3d_device *device, struct wined3d_context *context)
11128 struct shader_glsl_priv *priv = device->shader_priv;
11130 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11131 constant_heap_free(&priv->pconst_heap);
11132 constant_heap_free(&priv->vconst_heap);
11133 heap_free(priv->stack);
11134 string_buffer_list_cleanup(&priv->string_buffers);
11135 string_buffer_free(&priv->shader_buffer);
11136 priv->fragment_pipe->free_private(device, context);
11137 priv->vertex_pipe->vp_free(device, context);
11139 heap_free(device->shader_priv);
11140 device->shader_priv = NULL;
11143 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11145 struct glsl_context_data *ctx_data;
11147 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11148 return FALSE;
11149 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11150 context->shader_backend_data = ctx_data;
11151 return TRUE;
11154 static void shader_glsl_free_context_data(struct wined3d_context *context)
11156 heap_free(context->shader_backend_data);
11159 static void shader_glsl_init_context_state(struct wined3d_context *context)
11161 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11162 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11164 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11165 checkGLcall("GL_PROGRAM_POINT_SIZE");
11168 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11170 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11171 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11172 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11174 if (shader_model_4
11175 && gl_info->supported[ARB_COMPUTE_SHADER]
11176 && gl_info->supported[ARB_CULL_DISTANCE]
11177 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11178 && gl_info->supported[ARB_GPU_SHADER5]
11179 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11180 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11181 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11182 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11183 && gl_info->supported[ARB_TESSELLATION_SHADER]
11184 && gl_info->supported[ARB_TEXTURE_GATHER]
11185 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11186 return 5;
11188 if (shader_model_4)
11189 return 4;
11191 /* Support for texldd and texldl instructions in pixel shaders is required
11192 * for SM3. */
11193 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11194 return 3;
11196 return 2;
11199 static void shader_glsl_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
11201 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl_const(adapter)->gl_info;
11202 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11204 TRACE("Shader model %u.\n", shader_model);
11206 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11207 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11208 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11209 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11210 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11211 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11213 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11214 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11216 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11217 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11218 caps->varying_count = gl_info->limits.glsl_varyings;
11220 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11221 * Direct3D minimum requirement.
11223 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11224 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11226 * The problem is that the refrast clamps temporary results in the shader to
11227 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11228 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11229 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11230 * offer a way to query this.
11232 if (shader_model >= 4)
11233 caps->ps_1x_max_value = FLT_MAX;
11234 else
11235 caps->ps_1x_max_value = 1024.0f;
11237 /* Ideally we'd only set caps like sRGB writes here if supported by both
11238 * the shader backend and the fragment pipe, but we can get called before
11239 * shader_glsl_alloc(). */
11240 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11241 | WINED3D_SHADER_CAP_SRGB_WRITE;
11242 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
11243 caps->wined3d_caps |= WINED3D_SHADER_CAP_OUTPUT_INTERPOLATION;
11244 if (shader_glsl_full_ffp_varyings(gl_info))
11245 caps->wined3d_caps |= WINED3D_SHADER_CAP_FULL_FFP_VARYINGS;
11248 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11250 /* We support everything except YUV conversions. */
11251 return !is_complex_fixup(fixup);
11254 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11256 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11257 /* WINED3DSIH_ADD */ shader_glsl_binop,
11258 /* WINED3DSIH_AND */ shader_glsl_binop,
11259 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11260 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11261 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11262 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11263 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11264 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11265 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11266 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11267 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11268 /* WINED3DSIH_BEM */ shader_glsl_bem,
11269 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11270 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11271 /* WINED3DSIH_BREAK */ shader_glsl_break,
11272 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11273 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11274 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11275 /* WINED3DSIH_CALL */ shader_glsl_call,
11276 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11277 /* WINED3DSIH_CASE */ shader_glsl_case,
11278 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11279 /* WINED3DSIH_CND */ shader_glsl_cnd,
11280 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11281 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11282 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11283 /* WINED3DSIH_CRS */ shader_glsl_cross,
11284 /* WINED3DSIH_CUT */ shader_glsl_cut,
11285 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11286 /* WINED3DSIH_DCL */ shader_glsl_nop,
11287 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11288 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11289 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11290 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11291 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11292 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11293 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11294 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11295 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11296 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11297 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11298 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11299 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11300 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11301 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11302 /* WINED3DSIH_DCL_INPUT_PS_SGV */ shader_glsl_nop,
11303 /* WINED3DSIH_DCL_INPUT_PS_SIV */ shader_glsl_nop,
11304 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11305 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11306 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11307 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11308 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11309 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11310 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11311 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11312 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11313 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11314 /* WINED3DSIH_DCL_STREAM */ NULL,
11315 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11316 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11317 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11318 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11319 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11320 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11321 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11322 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11323 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11324 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11325 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11326 /* WINED3DSIH_DEF */ shader_glsl_nop,
11327 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11328 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11329 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11330 /* WINED3DSIH_DIV */ shader_glsl_binop,
11331 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11332 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11333 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11334 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11335 /* WINED3DSIH_DST */ shader_glsl_dst,
11336 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11337 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11338 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11339 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11340 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11341 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11342 /* WINED3DSIH_ELSE */ shader_glsl_else,
11343 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11344 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11345 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11346 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11347 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11348 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11349 /* WINED3DSIH_EQ */ shader_glsl_relop,
11350 /* WINED3DSIH_EVAL_CENTROID */ NULL,
11351 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ shader_glsl_interpolate,
11352 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11353 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11354 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11355 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11356 /* WINED3DSIH_FCALL */ NULL,
11357 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11358 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11359 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11360 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11361 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11362 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11363 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11364 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11365 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11366 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11367 /* WINED3DSIH_GE */ shader_glsl_relop,
11368 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11369 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11370 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11371 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11372 /* WINED3DSIH_IADD */ shader_glsl_binop,
11373 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11374 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11375 /* WINED3DSIH_IF */ shader_glsl_if,
11376 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11377 /* WINED3DSIH_IGE */ shader_glsl_relop,
11378 /* WINED3DSIH_ILT */ shader_glsl_relop,
11379 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11380 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11381 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11382 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11383 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11384 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11385 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11386 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11387 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11388 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11389 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11390 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11391 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11392 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11393 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11394 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11395 /* WINED3DSIH_INE */ shader_glsl_relop,
11396 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11397 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11398 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11399 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11400 /* WINED3DSIH_LABEL */ shader_glsl_label,
11401 /* WINED3DSIH_LD */ shader_glsl_ld,
11402 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11403 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11404 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11405 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11406 /* WINED3DSIH_LIT */ shader_glsl_lit,
11407 /* WINED3DSIH_LOD */ NULL,
11408 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11409 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11410 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11411 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11412 /* WINED3DSIH_LT */ shader_glsl_relop,
11413 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11414 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11415 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11416 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11417 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11418 /* WINED3DSIH_MAD */ shader_glsl_mad,
11419 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11420 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11421 /* WINED3DSIH_MOV */ shader_glsl_mov,
11422 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11423 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11424 /* WINED3DSIH_MUL */ shader_glsl_binop,
11425 /* WINED3DSIH_NE */ shader_glsl_relop,
11426 /* WINED3DSIH_NOP */ shader_glsl_nop,
11427 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11428 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11429 /* WINED3DSIH_OR */ shader_glsl_binop,
11430 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11431 /* WINED3DSIH_POW */ shader_glsl_pow,
11432 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11433 /* WINED3DSIH_REP */ shader_glsl_rep,
11434 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11435 /* WINED3DSIH_RET */ shader_glsl_ret,
11436 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11437 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11438 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11439 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11440 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11441 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11442 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11443 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11444 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11445 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11446 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11447 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11448 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11449 /* WINED3DSIH_SAMPLE_POS */ NULL,
11450 /* WINED3DSIH_SETP */ NULL,
11451 /* WINED3DSIH_SGE */ shader_glsl_compare,
11452 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11453 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11454 /* WINED3DSIH_SLT */ shader_glsl_compare,
11455 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11456 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11457 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11458 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11459 /* WINED3DSIH_SUB */ shader_glsl_binop,
11460 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11461 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11462 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11463 /* WINED3DSIH_TEX */ shader_glsl_tex,
11464 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11465 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11466 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11467 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11468 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11469 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11470 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11471 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11472 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11473 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11474 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11475 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11476 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11477 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11478 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11479 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11480 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11481 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11482 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11483 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11484 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11485 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11486 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11487 /* WINED3DSIH_UGE */ shader_glsl_relop,
11488 /* WINED3DSIH_ULT */ shader_glsl_relop,
11489 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11490 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11491 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11492 /* WINED3DSIH_USHR */ shader_glsl_binop,
11493 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11494 /* WINED3DSIH_XOR */ shader_glsl_binop,
11497 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11498 SHADER_HANDLER hw_fct;
11500 /* Select handler */
11501 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11503 /* Unhandled opcode */
11504 if (!hw_fct)
11506 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11507 return;
11509 hw_fct(ins);
11511 shader_glsl_add_instruction_modifiers(ins);
11514 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11516 struct shader_glsl_priv *priv = shader_priv;
11518 return priv->ffp_proj_control;
11521 static uint64_t shader_glsl_shader_compile(struct wined3d_context *context, const struct wined3d_shader_desc *shader_desc,
11522 enum wined3d_shader_type shader_type)
11524 ERR("Not implemented.\n");
11525 return 0;
11528 const struct wined3d_shader_backend_ops glsl_shader_backend =
11530 shader_glsl_handle_instruction,
11531 shader_glsl_precompile,
11532 shader_glsl_select,
11533 shader_glsl_select_compute,
11534 shader_glsl_disable,
11535 shader_glsl_update_float_vertex_constants,
11536 shader_glsl_update_float_pixel_constants,
11537 shader_glsl_load_constants,
11538 shader_glsl_destroy,
11539 shader_glsl_alloc,
11540 shader_glsl_free,
11541 shader_glsl_allocate_context_data,
11542 shader_glsl_free_context_data,
11543 shader_glsl_init_context_state,
11544 shader_glsl_get_caps,
11545 shader_glsl_color_fixup_supported,
11546 shader_glsl_has_ffp_proj_control,
11547 shader_glsl_shader_compile,
11550 static void glsl_vertex_pipe_vp_enable(const struct wined3d_context *context, BOOL enable) {}
11552 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_adapter *adapter, struct wined3d_vertex_caps *caps)
11554 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl_const(adapter)->gl_info;
11556 caps->xyzrhw = TRUE;
11557 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11558 caps->ffp_generic_attributes = TRUE;
11559 caps->max_active_lights = WINED3D_MAX_ACTIVE_LIGHTS;
11560 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11561 caps->max_vertex_blend_matrix_index = 0;
11562 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11563 | WINED3DVTXPCAPS_MATERIALSOURCE7
11564 | WINED3DVTXPCAPS_VERTEXFOG
11565 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11566 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11567 | WINED3DVTXPCAPS_LOCALVIEWER
11568 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11569 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11570 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11571 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11574 static unsigned int glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_adapter *adapter)
11576 if (wined3d_adapter_gl_const(adapter)->gl_info.supported[WINED3D_GL_LEGACY_CONTEXT])
11577 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11578 return 0;
11581 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11583 struct shader_glsl_priv *priv;
11585 if (shader_backend == &glsl_shader_backend)
11587 priv = shader_priv;
11588 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11589 return priv;
11592 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11594 return NULL;
11597 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *param)
11599 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11600 struct glsl_ffp_vertex_shader, desc.entry);
11601 struct glsl_shader_prog_link *program, *program2;
11602 struct glsl_ffp_destroy_ctx *ctx = param;
11603 const struct wined3d_gl_info *gl_info;
11605 gl_info = ctx->context_gl->gl_info;
11606 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11607 struct glsl_shader_prog_link, vs.shader_entry)
11609 delete_glsl_program_entry(ctx->priv, gl_info, program);
11611 GL_EXTCALL(glDeleteShader(shader->id));
11612 heap_free(shader);
11615 /* Context activation is done by the caller. */
11616 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device, struct wined3d_context *context)
11618 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11619 struct shader_glsl_priv *priv = device->vertex_priv;
11620 struct glsl_ffp_destroy_ctx ctx;
11622 ctx.priv = priv;
11623 ctx.context_gl = context_gl;
11624 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11627 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11628 const struct wined3d_state *state, DWORD state_id) {}
11630 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11631 const struct wined3d_state *state, DWORD state_id)
11633 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11636 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11637 const struct wined3d_state *state, DWORD state_id)
11639 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11640 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11641 BOOL specular = !!(context->stream_info.use_map & (1u << WINED3D_FFP_SPECULAR));
11642 BOOL diffuse = !!(context->stream_info.use_map & (1u << WINED3D_FFP_DIFFUSE));
11643 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11644 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11645 BOOL transformed = context->stream_info.position_transformed;
11646 BOOL wasrhw = context->last_was_rhw;
11647 unsigned int i;
11649 context->last_was_rhw = transformed;
11651 /* If the vertex declaration contains a transformed position attribute,
11652 * the draw uses the fixed function vertex pipeline regardless of any
11653 * vertex shader set by the application. */
11654 if (transformed != wasrhw
11655 || context->stream_info.swizzle_map != context->last_swizzle_map)
11656 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11658 context->last_swizzle_map = context->stream_info.swizzle_map;
11660 if (!use_vs(state))
11662 if (context->last_was_vshader)
11664 if (legacy_clip_planes)
11665 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11666 clipplane(context, state, STATE_CLIPPLANE(i));
11667 else
11668 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11671 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11673 /* Because of settings->texcoords, we have to regenerate the vertex
11674 * shader on a vdecl change if there aren't enough varyings to just
11675 * always output all the texture coordinates.
11677 * Likewise, we have to invalidate the shader when using per-vertex
11678 * colours and diffuse/specular attribute presence changes, or when
11679 * normal presence changes. */
11680 if (!shader_glsl_full_ffp_varyings(gl_info) || (state->render_states[WINED3D_RS_COLORVERTEX]
11681 && (diffuse != context->last_was_diffuse || specular != context->last_was_specular))
11682 || normal != context->last_was_normal)
11683 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11685 if (use_ps(state)
11686 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11687 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11688 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11690 else
11692 if (!context->last_was_vshader)
11694 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11695 if (legacy_clip_planes)
11696 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11697 clipplane(context, state, STATE_CLIPPLANE(i));
11698 else
11699 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11703 context->last_was_vshader = use_vs(state);
11704 context->last_was_diffuse = diffuse;
11705 context->last_was_specular = specular;
11706 context->last_was_normal = normal;
11709 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11710 const struct wined3d_state *state, DWORD state_id)
11712 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11713 /* Different vertex shaders potentially require a different vertex attributes setup. */
11714 if (!isStateDirty(context, STATE_VDECL))
11715 context_apply_state(context, state, STATE_VDECL);
11718 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11719 const struct wined3d_state *state, DWORD state_id)
11721 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11722 * winding) are defined in Hull Shaders, while in GLSL those are
11723 * specified in Tessellation Evaluation Shaders. */
11724 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11726 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11727 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11730 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11731 const struct wined3d_state *state, DWORD state_id)
11733 struct glsl_context_data *ctx_data = context->shader_backend_data;
11734 BOOL rasterization_disabled;
11736 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11737 if (ctx_data->rasterization_disabled != rasterization_disabled)
11738 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11739 ctx_data->rasterization_disabled = rasterization_disabled;
11741 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11742 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11743 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11744 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11745 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11748 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11749 const struct wined3d_state *state, DWORD state_id)
11751 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11752 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11753 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11754 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11755 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11756 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11757 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11760 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11761 const struct wined3d_state *state, DWORD state_id)
11763 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11766 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11767 const struct wined3d_state *state, DWORD state_id)
11769 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11772 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11774 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11775 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11776 unsigned int k;
11778 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11779 | WINED3D_SHADER_CONST_FFP_LIGHTS
11780 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11782 if (needs_legacy_glsl_syntax(gl_info))
11784 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11786 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11787 clipplane(context, state, STATE_CLIPPLANE(k));
11790 else
11792 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11796 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11797 const struct wined3d_state *state, DWORD state_id)
11799 /* Table fog behavior depends on the projection matrix. */
11800 if (state->render_states[WINED3D_RS_FOGENABLE]
11801 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11802 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11803 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11806 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11807 const struct wined3d_state *state, DWORD state_id)
11809 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11810 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11811 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11812 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11813 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11814 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11817 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11818 const struct wined3d_state *state, DWORD state_id)
11820 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11823 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11824 const struct wined3d_state *state, DWORD state_id)
11826 DWORD sampler = state_id - STATE_SAMPLER(0);
11827 const struct wined3d_texture *texture = state->textures[sampler];
11828 BOOL np2;
11830 if (!texture)
11831 return;
11833 if (sampler >= WINED3D_MAX_TEXTURES)
11834 return;
11836 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11837 || context->lastWasPow2Texture & (1u << sampler))
11839 if (np2)
11840 context->lastWasPow2Texture |= 1u << sampler;
11841 else
11842 context->lastWasPow2Texture &= ~(1u << sampler);
11844 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11848 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11849 const struct wined3d_state *state, DWORD state_id)
11851 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11854 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11855 const struct wined3d_state *state, DWORD state_id)
11857 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11860 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11861 const struct wined3d_state *state, DWORD state_id)
11863 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11866 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11867 const struct wined3d_state *state, DWORD state_id)
11869 if (!use_vs(state))
11870 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11873 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11874 const struct wined3d_state *state, DWORD state_id)
11876 static unsigned int once;
11878 if (state->primitive_type == WINED3D_PT_POINTLIST
11879 && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11880 FIXME("Non-point sprite points not supported in core profile.\n");
11883 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11884 const struct wined3d_state *state, DWORD state_id)
11886 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11889 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11890 const struct wined3d_state *state, DWORD state_id)
11892 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11893 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11894 UINT index = state_id - STATE_CLIPPLANE(0);
11896 if (index >= gl_info->limits.user_clip_distances)
11897 return;
11899 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11902 static const struct wined3d_state_entry_template glsl_vertex_pipe_vp_states[] =
11904 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11905 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11906 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11907 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11908 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11909 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11910 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11911 /* Clip planes */
11912 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11913 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11914 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11915 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11916 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11917 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11918 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11919 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11920 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11921 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11922 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11923 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11924 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11925 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11926 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11927 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11928 /* Lights */
11929 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11930 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11931 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11932 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11933 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11934 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11935 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11936 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11937 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11938 /* Viewport */
11939 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11940 /* Transform states */
11941 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11942 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11943 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11944 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11945 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11946 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11947 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11948 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11949 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11950 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11951 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11952 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11953 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11954 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11955 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11956 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11957 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11958 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11959 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11960 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11961 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11962 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11963 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11964 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11965 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11966 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11967 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11968 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11969 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11970 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11971 /* Fog */
11972 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11973 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11974 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11975 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11976 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11977 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11978 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11979 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11980 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11981 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11982 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11983 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11984 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11985 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11986 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11987 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11988 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11989 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11990 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11991 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11992 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11993 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11994 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11995 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11996 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11997 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11998 /* NP2 texture matrix fixups. They are not needed if
11999 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
12000 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
12001 * matrix. */
12002 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12003 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12004 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12005 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12006 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12007 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12008 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12009 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12010 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12011 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12012 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12013 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12014 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12015 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12016 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12017 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12018 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12019 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12020 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12021 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12022 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12023 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
12024 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12025 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12026 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12027 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
12028 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
12029 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
12032 /* TODO:
12033 * - Implement vertex tweening. */
12034 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
12036 glsl_vertex_pipe_vp_enable,
12037 glsl_vertex_pipe_vp_get_caps,
12038 glsl_vertex_pipe_vp_get_emul_mask,
12039 glsl_vertex_pipe_vp_alloc,
12040 glsl_vertex_pipe_vp_free,
12041 glsl_vertex_pipe_vp_states,
12044 static void glsl_fragment_pipe_enable(const struct wined3d_context *context, BOOL enable)
12046 /* Nothing to do. */
12049 static void glsl_fragment_pipe_get_caps(const struct wined3d_adapter *adapter, struct fragment_caps *caps)
12051 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl_const(adapter)->gl_info;
12053 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
12054 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
12055 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
12056 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12057 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12058 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12059 | WINED3DTEXOPCAPS_SELECTARG1
12060 | WINED3DTEXOPCAPS_SELECTARG2
12061 | WINED3DTEXOPCAPS_MODULATE4X
12062 | WINED3DTEXOPCAPS_MODULATE2X
12063 | WINED3DTEXOPCAPS_MODULATE
12064 | WINED3DTEXOPCAPS_ADDSIGNED2X
12065 | WINED3DTEXOPCAPS_ADDSIGNED
12066 | WINED3DTEXOPCAPS_ADD
12067 | WINED3DTEXOPCAPS_SUBTRACT
12068 | WINED3DTEXOPCAPS_ADDSMOOTH
12069 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12070 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12071 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12072 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12073 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12074 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12075 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12076 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12077 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12078 | WINED3DTEXOPCAPS_DOTPRODUCT3
12079 | WINED3DTEXOPCAPS_MULTIPLYADD
12080 | WINED3DTEXOPCAPS_LERP
12081 | WINED3DTEXOPCAPS_BUMPENVMAP
12082 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12083 caps->MaxTextureBlendStages = WINED3D_MAX_TEXTURES;
12084 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], WINED3D_MAX_TEXTURES);
12087 static unsigned int glsl_fragment_pipe_get_emul_mask(const struct wined3d_adapter *adapter)
12089 if (wined3d_adapter_gl_const(adapter)->gl_info.supported[WINED3D_GL_LEGACY_CONTEXT])
12090 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12091 return 0;
12094 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12096 struct shader_glsl_priv *priv;
12098 if (shader_backend == &glsl_shader_backend)
12100 priv = shader_priv;
12101 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12102 return priv;
12105 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12107 return NULL;
12110 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *param)
12112 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12113 struct glsl_ffp_fragment_shader, entry.entry);
12114 struct glsl_shader_prog_link *program, *program2;
12115 struct glsl_ffp_destroy_ctx *ctx = param;
12116 const struct wined3d_gl_info *gl_info;
12118 gl_info = ctx->context_gl->gl_info;
12119 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12120 struct glsl_shader_prog_link, ps.shader_entry)
12122 delete_glsl_program_entry(ctx->priv, gl_info, program);
12124 GL_EXTCALL(glDeleteShader(shader->id));
12125 heap_free(shader);
12128 /* Context activation is done by the caller. */
12129 static void glsl_fragment_pipe_free(struct wined3d_device *device, struct wined3d_context *context)
12131 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12132 struct shader_glsl_priv *priv = device->fragment_priv;
12133 struct glsl_ffp_destroy_ctx ctx;
12135 ctx.priv = priv;
12136 ctx.context_gl = context_gl;
12137 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12140 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12141 const struct wined3d_state *state, DWORD state_id)
12143 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12146 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12147 const struct wined3d_state *state, DWORD state_id)
12149 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12152 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12153 const struct wined3d_state *state, DWORD state_id)
12155 BOOL use_vshader = use_vs(state);
12156 enum fogsource new_source;
12157 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12158 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12160 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12162 if (!state->render_states[WINED3D_RS_FOGENABLE])
12163 return;
12165 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12167 if (use_vshader)
12168 new_source = FOGSOURCE_VS;
12169 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12170 new_source = FOGSOURCE_COORD;
12171 else
12172 new_source = FOGSOURCE_FFP;
12174 else
12176 new_source = FOGSOURCE_FFP;
12179 if (new_source != context->fog_source || fogstart == fogend)
12181 context->fog_source = new_source;
12182 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12186 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12187 const struct wined3d_state *state, DWORD state_id)
12189 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12191 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12192 if (!shader_glsl_full_ffp_varyings(gl_info))
12193 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12195 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12196 glsl_fragment_pipe_fog(context, state, state_id);
12199 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12200 const struct wined3d_state *state, DWORD state_id)
12202 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12204 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12205 if (!shader_glsl_full_ffp_varyings(gl_info))
12206 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12209 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12210 const struct wined3d_state *state, DWORD state_id)
12212 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12215 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12216 const struct wined3d_state *state, DWORD state_id)
12218 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12221 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12222 const struct wined3d_state *state, DWORD state_id)
12224 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12225 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12226 float ref = wined3d_alpha_ref(state);
12228 if (func)
12230 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12231 checkGLcall("glAlphaFunc");
12235 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12236 const struct wined3d_state *state, DWORD state_id)
12238 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12241 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12242 const struct wined3d_state *state, DWORD state_id)
12244 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12246 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12248 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12249 checkGLcall("glEnable(GL_ALPHA_TEST)");
12251 else
12253 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12254 checkGLcall("glDisable(GL_ALPHA_TEST)");
12258 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12259 const struct wined3d_state *state, DWORD state_id)
12261 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12264 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12265 const struct wined3d_state *state, DWORD state_id)
12267 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12270 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12271 const struct wined3d_state *state, DWORD state_id)
12273 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12276 static const struct wined3d_state_entry_template glsl_fragment_pipe_state_template[] =
12278 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12279 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12280 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12281 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12282 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12283 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12284 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12285 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12286 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12287 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12288 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12289 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12290 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12291 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12292 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12293 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12294 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12295 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12296 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12297 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12298 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12299 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12300 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12301 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12302 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12303 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12304 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12305 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12306 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12307 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12308 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12309 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12310 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12311 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12312 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12313 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12314 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12315 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12316 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12317 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12318 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12319 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12320 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12321 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12322 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12323 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12324 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12325 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12326 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12327 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12328 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12329 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12330 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12331 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12332 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12333 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12334 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12335 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12336 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12337 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12338 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12339 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12340 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12341 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12342 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12343 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12344 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12345 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12346 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12347 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12348 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12349 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12350 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12351 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12352 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12353 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12354 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12355 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12356 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12357 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12358 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12359 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12360 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12361 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12362 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12363 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12364 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12365 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12366 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12367 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12368 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12369 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12370 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12371 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12372 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12373 {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 },
12374 {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 },
12375 {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 },
12376 {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 },
12377 {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 },
12378 {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 },
12379 {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 },
12380 {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 },
12381 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12382 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12383 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12384 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12385 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12386 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12387 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12388 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12389 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12390 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12391 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12392 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12393 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12396 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12398 return TRUE;
12401 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12405 const struct wined3d_fragment_pipe_ops glsl_fragment_pipe =
12407 glsl_fragment_pipe_enable,
12408 glsl_fragment_pipe_get_caps,
12409 glsl_fragment_pipe_get_emul_mask,
12410 glsl_fragment_pipe_alloc,
12411 glsl_fragment_pipe_free,
12412 glsl_fragment_pipe_alloc_context_data,
12413 glsl_fragment_pipe_free_context_data,
12414 shader_glsl_color_fixup_supported,
12415 glsl_fragment_pipe_state_template,
12418 struct glsl_blitter_args
12420 GLenum texture_type;
12421 struct color_fixup_desc fixup;
12422 unsigned short use_colour_key;
12425 struct glsl_blitter_program
12427 struct wine_rb_entry entry;
12428 struct glsl_blitter_args args;
12429 GLuint id;
12432 struct wined3d_glsl_blitter
12434 struct wined3d_blitter blitter;
12435 struct wined3d_string_buffer_list string_buffers;
12436 struct wine_rb_tree programs;
12437 GLuint palette_texture;
12440 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12442 const struct glsl_blitter_args *a = key;
12443 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12445 return memcmp(a, b, sizeof(*a));
12448 /* Context activation is done by the caller. */
12449 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12451 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12452 struct wined3d_context_gl *context_gl = ctx;
12453 const struct wined3d_gl_info *gl_info;
12455 gl_info = context_gl->gl_info;
12456 GL_EXTCALL(glDeleteProgram(program->id));
12457 checkGLcall("glDeleteProgram()");
12458 heap_free(program);
12461 /* Context activation is done by the caller. */
12462 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12464 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12465 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12466 struct wined3d_glsl_blitter *glsl_blitter;
12467 struct wined3d_blitter *next;
12469 if ((next = blitter->next))
12470 next->ops->blitter_destroy(next, context);
12472 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12474 if (glsl_blitter->palette_texture)
12475 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12477 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context_gl);
12478 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12480 heap_free(glsl_blitter);
12483 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12484 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12485 const char *output, const char *tex_type, const char *swizzle)
12487 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12488 shader_addline(buffer, "\nvoid main()\n{\n");
12489 /* The alpha-component contains the palette index. */
12490 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12491 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12492 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12493 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12494 * the middle. */
12495 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12496 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12497 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12498 if (args->use_colour_key)
12499 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12500 shader_addline(buffer, "}\n");
12503 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12504 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12505 const char *tex_type)
12507 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12508 char chroma, luminance;
12509 const char *tex;
12511 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12512 * macropixel, giving effectively 16 bits per pixel. The color consists of
12513 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12514 * luminance values, one for each single pixel it contains, and one U and
12515 * one V value shared between both pixels.
12517 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12518 * component contains the luminance and alpha the chroma. With UYVY it is
12519 * vice versa. Thus take the format into account when generating the read
12520 * swizzles
12522 * Reading the Y value is straightforward - just sample the texture. The
12523 * hardware takes care of filtering in the horizontal and vertical
12524 * direction.
12526 * Reading the U and V values is harder. We have to avoid filtering
12527 * horizontally, because that would mix the U and V values of one pixel or
12528 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12529 * get an unfiltered read, regardless of the filtering setting. Vertical
12530 * filtering works automatically though - the U and V values of two rows
12531 * are mixed nicely.
12533 * Apart of avoiding filtering issues, the code has to know which value it
12534 * just read, and where it can find the other one. To determine this, it
12535 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12536 * accordingly.
12538 * Handling horizontal filtering of U and V values requires reading a 2nd
12539 * pair of pixels, extracting U and V and mixing them. This is not
12540 * implemented yet.
12542 * An alternative implementation idea is to load the texture as A8R8G8B8
12543 * texture, with width / 2. This way one read gives all 3 values, finding
12544 * U and V is easy in an unfiltered situation. Finding the luminance on
12545 * the other hand requires finding out if it is an odd or even pixel. The
12546 * real drawback of this approach is filtering. This would have to be
12547 * emulated completely in the shader, reading up two 2 packed pixels in up
12548 * to 2 rows and interpolating both horizontally and vertically. Beyond
12549 * that it would require adjustments to the texture handling code to deal
12550 * with the width scaling. */
12552 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12554 chroma = 'x';
12555 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12557 else
12559 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12560 luminance = 'x';
12563 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12565 /* First we have to read the chroma values. This means we need at least
12566 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12567 * unmodified chroma, we have to rid ourselves of the filtering when we
12568 * sample the texture. */
12569 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12570 /* We must not allow filtering between pixel x and x+1, this would mix U
12571 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12572 * center is at 0.5, so add 0.5. */
12573 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12574 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12576 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12577 * and 0.75 for the even and odd pixels respectively. */
12578 /* Put the value into either of the chroma values. */
12579 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12580 shader_addline(buffer, " if (even)\n");
12581 shader_addline(buffer, " chroma.y = luminance;\n");
12582 shader_addline(buffer, " else\n");
12583 shader_addline(buffer, " chroma.x = luminance;\n");
12585 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12586 * current one. Otherwise, sample the left pixel. */
12587 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12588 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12590 /* Put the value into the other chroma. */
12591 shader_addline(buffer, " if (even)\n");
12592 shader_addline(buffer, " chroma.x = luminance;\n");
12593 shader_addline(buffer, " else\n");
12594 shader_addline(buffer, " chroma.y = luminance;\n");
12596 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12597 * the current one and lerp the two U and V values. */
12599 /* This gives the correctly filtered luminance value. */
12600 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12603 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12604 const struct wined3d_gl_info *gl_info, const char *tex_type)
12606 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12607 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12609 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12610 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12611 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12612 * have only half the pitch of the luminance plane, the packing into the
12613 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12614 * luminance data it looks approximately like this:
12616 * +----------------------------------+----
12617 * | |
12618 * | |
12619 * | |
12620 * | |
12621 * | | 2
12622 * | LUMINANCE | -
12623 * | | 3
12624 * | |
12625 * | |
12626 * | |
12627 * | |
12628 * +----------------+-----------------+----
12629 * | | |
12630 * | V even rows | V odd rows |
12631 * | | | 1
12632 * +----------------+------------------ -
12633 * | | | 3
12634 * | U even rows | U odd rows |
12635 * | | |
12636 * +----------------+-----------------+----
12637 * | | |
12638 * | 0.5 | 0.5 |
12640 * So it appears as if there are 4 chroma images, but in fact the odd rows
12641 * in the chroma images are in the same row as the even ones. So it is
12642 * kinda tricky to read. */
12644 /* First sample the chroma values. */
12645 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12646 /* The chroma planes have only half the width. */
12647 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12649 /* The first value is between 2/3 and 5/6 of the texture's height, so
12650 * scale+bias the coordinate. Also read the right side of the image when
12651 * reading odd lines.
12653 * Don't forget to clamp the y values in into the range, otherwise we'll
12654 * get filtering bleeding. */
12656 /* Read odd lines from the right side (add 0.5 to the x coordinate). Keep
12657 * in mind that each line of the chroma plane corresponds to 2 lines of the
12658 * resulting image. */
12659 shader_addline(buffer, " if (fract(texcoord.y * size.y * 0.25) >= 0.5)\n");
12660 shader_addline(buffer, " texcoord.x += 0.5;\n");
12662 /* Clamp, keep the half pixel origin in mind. */
12663 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12664 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12666 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12668 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12669 * 6/6th No need to clamp because we're just reusing the already clamped
12670 * value from above. */
12671 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12672 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12674 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12675 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12676 * values from bleeding into the sampled luminance values due to
12677 * filtering. */
12678 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12679 /* Multiply the y coordinate by 2/3 and clamp it. */
12680 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12681 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12684 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12685 const struct wined3d_gl_info *gl_info, const char *tex_type)
12687 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12688 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12690 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12691 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12692 * effective bitdepth is 12 bits per pixel. If the whole texture is
12693 * interpreted as luminance data it looks approximately like this:
12695 * +----------------------------------+----
12696 * | |
12697 * | |
12698 * | |
12699 * | |
12700 * | | 2
12701 * | LUMINANCE | -
12702 * | | 3
12703 * | |
12704 * | |
12705 * | |
12706 * | |
12707 * +----------------------------------+----
12708 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12709 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12710 * | | 1
12711 * | | -
12712 * | | 3
12713 * | |
12714 * | |
12715 * +----------------------------------+---- */
12717 /* First sample the chroma values. */
12718 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12719 /* We only have half the number of chroma pixels. */
12720 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12721 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12723 /* We must not allow filtering horizontally, this would mix U and V.
12724 * Vertical filtering is ok. However, bear in mind that the pixel center
12725 * is at 0.5, so add 0.5. */
12727 /* Convert to non-normalised coordinates so we can find the individual
12728 * pixel. */
12729 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12730 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12731 * 0.5 to hit the center of the pixel. Then convert back to normalised
12732 * coordinates. */
12733 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12734 /* Clamp, keep the half pixel origin in mind. */
12735 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12737 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12738 /* Add 1.0 / size.x to sample the adjacent texel. */
12739 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12740 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12742 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12743 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12744 * values from bleeding into the sampled luminance values due to
12745 * filtering. */
12746 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12747 /* Multiply the y coordinate by 2/3 and clamp it. */
12748 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12749 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12752 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12753 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12754 const char *output, const char *tex_type, const char *swizzle)
12756 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12758 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12759 shader_addline(buffer, "float luminance;\n");
12760 shader_addline(buffer, "vec2 texcoord;\n");
12761 shader_addline(buffer, "vec2 chroma;\n");
12762 shader_addline(buffer, "uniform vec2 size;\n");
12764 shader_addline(buffer, "\nvoid main()\n{\n");
12766 switch (complex_fixup)
12768 case COMPLEX_FIXUP_UYVY:
12769 case COMPLEX_FIXUP_YUY2:
12770 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12771 break;
12773 case COMPLEX_FIXUP_YV12:
12774 gen_yv12_read(buffer, gl_info, tex_type);
12775 break;
12777 case COMPLEX_FIXUP_NV12:
12778 gen_nv12_read(buffer, gl_info, tex_type);
12779 break;
12781 case COMPLEX_FIXUP_YUV:
12782 /* With APPLE_rgb_422, things are much simpler. The only thing we
12783 * have to do here is Y'CbCr to RGB conversion. */
12784 shader_addline(buffer, " vec3 yuv = vec3(texture%s(sampler, out_texcoord.xy));\n",
12785 needs_legacy_glsl_syntax(gl_info) ? tex_type : "");
12786 shader_addline(buffer, " luminance = yuv.y;\n");
12787 shader_addline(buffer, " chroma = yuv.xz;\n");
12788 break;
12790 default:
12791 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12792 string_buffer_free(buffer);
12793 return;
12796 /* Calculate the final result. Formula is taken from
12797 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12798 * ranges from -0.5 to 0.5. */
12799 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12801 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12802 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12803 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12804 if (args->use_colour_key)
12805 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12807 shader_addline(buffer, "}\n");
12810 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12811 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12812 const char *output, const char *tex_type, const char *swizzle)
12814 shader_addline(buffer, "\nvoid main()\n{\n");
12815 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12816 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12817 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12818 if (args->use_colour_key)
12819 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12820 shader_addline(buffer, "}\n");
12823 /* Context activation is done by the caller. */
12824 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12825 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12827 static const struct
12829 GLenum texture_target;
12830 const char texture_type[7];
12831 const char texcoord_swizzle[4];
12833 texture_data[] =
12835 {GL_TEXTURE_2D, "2D", "xy"},
12836 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12837 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12839 static const char vshader_main[] =
12840 "\n"
12841 "void main()\n"
12842 "{\n"
12843 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12844 " out_texcoord = texcoord;\n"
12845 "}\n";
12846 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12847 struct wined3d_string_buffer *buffer, *output;
12848 GLuint program, vshader_id, fshader_id;
12849 const char *tex_type = NULL, *swizzle = NULL, *ptr;
12850 unsigned int i;
12851 GLint loc;
12853 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12855 if (args->texture_type == texture_data[i].texture_target)
12857 tex_type = texture_data[i].texture_type;
12858 swizzle = texture_data[i].texcoord_swizzle;
12859 break;
12862 if (i == ARRAY_SIZE(texture_data))
12864 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12865 return 0;
12868 program = GL_EXTCALL(glCreateProgram());
12870 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12872 buffer = string_buffer_get(&blitter->string_buffers);
12873 shader_glsl_add_version_declaration(buffer, gl_info);
12874 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12875 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12876 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12877 shader_addline(buffer, vshader_main);
12879 ptr = buffer->buffer;
12880 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12881 GL_EXTCALL(glAttachShader(program, vshader_id));
12882 GL_EXTCALL(glDeleteShader(vshader_id));
12884 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12886 string_buffer_clear(buffer);
12887 shader_glsl_add_version_declaration(buffer, gl_info);
12888 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12889 if (args->use_colour_key)
12891 shader_addline(buffer, "uniform struct\n{\n");
12892 shader_addline(buffer, " vec4 low, high;\n");
12893 shader_addline(buffer, "} colour_key;\n");
12895 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12896 /* TODO: Declare the out variable with the correct type (and put it in the
12897 * blitter args). */
12898 if (!use_legacy_fragment_output(gl_info))
12899 shader_addline(buffer, "out vec4 ps_out[1];\n");
12901 output = string_buffer_get(&blitter->string_buffers);
12902 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12904 switch (complex_fixup)
12906 case COMPLEX_FIXUP_P8:
12907 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12908 break;
12909 case COMPLEX_FIXUP_YUY2:
12910 case COMPLEX_FIXUP_UYVY:
12911 case COMPLEX_FIXUP_YV12:
12912 case COMPLEX_FIXUP_NV12:
12913 case COMPLEX_FIXUP_YUV:
12914 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12915 break;
12916 case COMPLEX_FIXUP_NONE:
12917 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12920 string_buffer_release(&blitter->string_buffers, output);
12922 ptr = buffer->buffer;
12923 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12924 string_buffer_release(&blitter->string_buffers, buffer);
12925 GL_EXTCALL(glAttachShader(program, fshader_id));
12926 GL_EXTCALL(glDeleteShader(fshader_id));
12928 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12929 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12931 if (!use_legacy_fragment_output(gl_info))
12932 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12934 GL_EXTCALL(glCompileShader(vshader_id));
12935 print_glsl_info_log(gl_info, vshader_id, FALSE);
12936 GL_EXTCALL(glCompileShader(fshader_id));
12937 print_glsl_info_log(gl_info, fshader_id, FALSE);
12938 GL_EXTCALL(glLinkProgram(program));
12939 shader_glsl_validate_link(gl_info, program);
12941 GL_EXTCALL(glUseProgram(program));
12942 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12943 GL_EXTCALL(glUniform1i(loc, 0));
12944 if (complex_fixup == COMPLEX_FIXUP_P8)
12946 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12947 GL_EXTCALL(glUniform1i(loc, 1));
12950 return program;
12953 /* Context activation is done by the caller. */
12954 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12955 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl)
12957 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12958 const struct wined3d_palette *palette;
12960 palette = texture_gl->t.swapchain ? texture_gl->t.swapchain->palette : NULL;
12962 if (!blitter->palette_texture)
12963 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12965 wined3d_context_gl_active_texture(context_gl, gl_info, 1);
12966 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12967 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12968 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12969 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12971 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
12972 checkGLcall("glBindBuffer");
12974 if (palette)
12976 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12977 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12979 else
12981 static const DWORD black;
12983 FIXME("P8 texture loaded without a palette.\n");
12984 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
12985 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
12988 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
12991 /* Context activation is done by the caller. */
12992 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
12993 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl, BOOL use_colour_key)
12995 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12996 struct glsl_blitter_program *program;
12997 struct glsl_blitter_args args;
12998 struct wine_rb_entry *entry;
13000 memset(&args, 0, sizeof(args));
13001 args.texture_type = texture_gl->target;
13002 args.fixup = texture_gl->t.resource.format->color_fixup;
13003 args.use_colour_key = use_colour_key;
13005 if ((entry = wine_rb_get(&blitter->programs, &args)))
13006 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
13008 if (!(program = heap_alloc(sizeof(*program))))
13010 ERR("Failed to allocate blitter program memory.\n");
13011 return NULL;
13014 program->args = args;
13015 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
13017 WARN("Failed to generate blitter program.\n");
13018 heap_free(program);
13019 return NULL;
13022 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
13024 ERR("Failed to store blitter program.\n");
13025 GL_EXTCALL(glDeleteProgram(program->id));
13026 heap_free(program);
13027 return NULL;
13030 return program;
13033 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
13034 const struct wined3d_texture_gl *src_texture, DWORD src_location,
13035 const struct wined3d_texture_gl *dst_texture, DWORD dst_location)
13037 const struct wined3d_resource *src_resource = &src_texture->t.resource;
13038 const struct wined3d_resource *dst_resource = &dst_texture->t.resource;
13039 const struct wined3d_format *src_format = src_resource->format;
13040 const struct wined3d_format *dst_format = dst_resource->format;
13041 bool src_ds, dst_ds;
13042 BOOL decompress;
13044 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13046 src_ds = src_format->depth_size || src_format->stencil_size;
13047 dst_ds = dst_format->depth_size || dst_format->stencil_size;
13048 /* We could in principle support raw depth/stencil <-> colour blits as
13049 * well in some cases, but note that typeless formats like e.g.
13050 * R16_TYPELESS may use a normalised GL format for depth/stencil
13051 * resources, and a floating-point format for colour resources, */
13052 if (src_ds && dst_ds)
13053 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13054 else if (!src_ds && !dst_ds)
13055 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13058 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY
13059 && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13061 TRACE("Unsupported blit_op %#x.\n", blit_op);
13062 return FALSE;
13065 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13066 return FALSE;
13068 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13069 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13071 TRACE("Multi-sample source textures not supported.\n");
13072 return FALSE;
13075 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13077 TRACE("Destination resource does not have GPU access.\n");
13078 return FALSE;
13081 /* We don't necessarily want to blit from resources without
13082 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13083 * compressed textures. */
13084 decompress = (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
13085 && !(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED);
13086 if (!decompress && !(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13088 TRACE("Source resource does not have GPU access.\n");
13089 return FALSE;
13092 if (!is_identity_fixup(dst_format->color_fixup)
13093 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13095 TRACE("Destination fixups are not supported.\n");
13096 return FALSE;
13099 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
13100 && !((dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
13101 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
13103 TRACE("Destination texture is not FBO attachable.\n");
13104 return FALSE;
13107 TRACE("Returning supported.\n");
13108 return TRUE;
13111 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13112 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13113 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13114 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13115 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
13116 const struct wined3d_format *resolve_format)
13118 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
13119 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
13120 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
13121 struct wined3d_device *device = dst_texture->resource.device;
13122 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13123 struct wined3d_texture *staging_texture = NULL;
13124 struct wined3d_glsl_blitter *glsl_blitter;
13125 struct wined3d_color_key alpha_test_key;
13126 struct glsl_blitter_program *program;
13127 struct wined3d_blitter *next;
13128 unsigned int src_level;
13129 GLint location;
13130 RECT s, d;
13132 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
13133 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
13134 "colour_key %p, filter %s, resolve format %p.\n",
13135 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13136 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13137 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
13139 if (!glsl_blitter_supported(op, context, src_texture_gl, src_location, dst_texture_gl, dst_location))
13141 if (!(next = blitter->next))
13143 ERR("No blitter to handle blit op %#x.\n", op);
13144 return dst_location;
13147 TRACE("Forwarding to blitter %p.\n", next);
13148 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13149 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
13150 resolve_format);
13153 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13155 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13157 struct wined3d_resource_desc desc;
13158 struct wined3d_box upload_box;
13159 HRESULT hr;
13161 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13163 src_level = src_sub_resource_idx % src_texture->level_count;
13164 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13165 desc.format = src_texture->resource.format->id;
13166 desc.multisample_type = src_texture->resource.multisample_type;
13167 desc.multisample_quality = src_texture->resource.multisample_quality;
13168 desc.usage = WINED3DUSAGE_PRIVATE;
13169 desc.bind_flags = 0;
13170 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13171 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13172 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13173 desc.depth = 1;
13174 desc.size = 0;
13176 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13177 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13179 ERR("Failed to create staging texture, hr %#lx.\n", hr);
13180 return dst_location;
13183 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13184 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13185 src_texture, src_sub_resource_idx, &upload_box);
13187 src_texture = staging_texture;
13188 src_texture_gl = wined3d_texture_gl(src_texture);
13189 src_sub_resource_idx = 0;
13191 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13192 && (src_texture->sub_resources[src_sub_resource_idx].locations
13193 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13194 && !wined3d_resource_is_offscreen(&src_texture->resource))
13197 /* Without FBO blits transferring from the drawable to the texture is
13198 * expensive, because we have to flip the data in sysmem. Since we can
13199 * flip in the blitter, we don't actually need that flip anyway. So we
13200 * use the surface's texture as scratch texture, and flip the source
13201 * rectangle instead. */
13202 texture2d_load_fb_texture(src_texture_gl, src_sub_resource_idx, FALSE, context);
13204 s = *src_rect;
13205 src_level = src_sub_resource_idx % src_texture->level_count;
13206 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13207 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13208 src_rect = &s;
13210 else
13212 wined3d_texture_load(src_texture, context, FALSE);
13215 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
13216 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13217 else
13218 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13220 wined3d_context_gl_apply_blit_state(context_gl, device);
13222 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13224 d = *dst_rect;
13225 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
13226 dst_rect = &d;
13229 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
13231 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13233 const struct wined3d_format *f = src_texture->resource.format;
13235 alpha_test_key.color_space_low_value = 0;
13236 alpha_test_key.color_space_high_value = ~(wined3d_mask_from_size(f->alpha_size) << f->alpha_offset);
13237 colour_key = &alpha_test_key;
13239 else if (op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY)
13241 colour_key = NULL;
13244 if (!(program = glsl_blitter_get_program(glsl_blitter, context_gl, src_texture_gl, !!colour_key)))
13246 ERR("Failed to get blitter program.\n");
13247 return dst_location;
13249 GL_EXTCALL(glUseProgram(program->id));
13250 switch (get_complex_fixup(program->args.fixup))
13252 case COMPLEX_FIXUP_P8:
13253 glsl_blitter_upload_palette(glsl_blitter, context_gl, src_texture_gl);
13254 break;
13256 case COMPLEX_FIXUP_YUY2:
13257 case COMPLEX_FIXUP_UYVY:
13258 case COMPLEX_FIXUP_YV12:
13259 case COMPLEX_FIXUP_NV12:
13260 case COMPLEX_FIXUP_YUV:
13261 src_level = src_sub_resource_idx % src_texture->level_count;
13262 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13263 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13264 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13265 break;
13267 default:
13268 break;
13270 if (colour_key)
13272 struct wined3d_color float_key[2];
13274 wined3d_format_get_float_color_key(src_texture->resource.format, colour_key, float_key);
13275 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.low"));
13276 GL_EXTCALL(glUniform4fv(location, 1, &float_key[0].r));
13277 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.high"));
13278 GL_EXTCALL(glUniform4fv(location, 1, &float_key[1].r));
13280 wined3d_context_gl_draw_shaded_quad(context_gl, src_texture_gl, src_sub_resource_idx, src_rect, dst_rect, filter);
13281 GL_EXTCALL(glUseProgram(0));
13283 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13284 gl_info->gl_ops.gl.p_glFlush();
13286 if (staging_texture)
13287 wined3d_texture_decref(staging_texture);
13289 return dst_location;
13292 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13293 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13294 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *color, float depth, unsigned int stencil)
13296 struct wined3d_blitter *next;
13298 if ((next = blitter->next))
13299 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13300 clear_rects, draw_rect, flags, color, depth, stencil);
13303 static const struct wined3d_blitter_ops glsl_blitter_ops =
13305 glsl_blitter_destroy,
13306 glsl_blitter_clear,
13307 glsl_blitter_blit,
13310 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13311 const struct wined3d_device *device)
13313 const struct wined3d_gl_info *gl_info = &wined3d_adapter_gl(device->adapter)->gl_info;
13314 struct wined3d_glsl_blitter *blitter;
13316 if (device->shader_backend != &glsl_shader_backend)
13317 return NULL;
13319 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13320 return NULL;
13322 if (!(blitter = heap_alloc(sizeof(*blitter))))
13324 ERR("Failed to allocate blitter.\n");
13325 return NULL;
13328 TRACE("Created blitter %p.\n", blitter);
13330 blitter->blitter.ops = &glsl_blitter_ops;
13331 blitter->blitter.next = *next;
13332 string_buffer_list_init(&blitter->string_buffers);
13333 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13334 blitter->palette_texture = 0;
13335 *next = &blitter->blitter;
13337 return *next;