mf/tests: Test input type for WMA decoder DMO.
[wine.git] / dlls / wined3d / glsl_shader.c
blob7dc0de1b596e681aac806aee711e3bf8a4f8c9ac
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"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d);
39 WINE_DECLARE_DEBUG_CHANNEL(winediag);
41 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x01
42 #define WINED3D_GLSL_SAMPLE_LOD 0x02
43 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
44 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
45 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
47 static const struct
49 unsigned int coord_size;
50 unsigned int resinfo_size;
51 const char *type_part;
53 resource_type_info[] =
55 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
56 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
57 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
58 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
59 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
60 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
61 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
62 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
63 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
64 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
65 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
68 static const struct
70 enum wined3d_data_type data_type;
71 const char *glsl_scalar_type;
72 const char *glsl_vector_type;
74 component_type_info[] =
76 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_UNKNOWN */
77 {WINED3D_DATA_UINT, "uint", "uvec"}, /* WINED3D_TYPE_UINT */
78 {WINED3D_DATA_INT, "int", "ivec"}, /* WINED3D_TYPE_INT */
79 {WINED3D_DATA_FLOAT, "float", "vec"}, /* WINED3D_TYPE_FLOAT */
82 struct glsl_dst_param
84 char reg_name[150];
85 char mask_str[6];
88 struct glsl_src_param
90 char param_str[200];
93 struct glsl_sample_function
95 struct wined3d_string_buffer *name;
96 unsigned int coord_mask;
97 unsigned int deriv_mask;
98 enum wined3d_data_type data_type;
99 BOOL output_single_component;
100 unsigned int offset_size;
103 enum heap_node_op
105 HEAP_NODE_TRAVERSE_LEFT,
106 HEAP_NODE_TRAVERSE_RIGHT,
107 HEAP_NODE_POP,
110 struct constant_entry
112 unsigned int idx;
113 unsigned int version;
116 struct constant_heap
118 struct constant_entry *entries;
119 BOOL *contained;
120 unsigned int *positions;
121 unsigned int size;
124 /* GLSL shader private data */
125 struct shader_glsl_priv
127 struct wined3d_string_buffer shader_buffer;
128 struct wined3d_string_buffer_list string_buffers;
129 struct wine_rb_tree program_lookup;
130 struct constant_heap vconst_heap;
131 struct constant_heap pconst_heap;
132 unsigned char *stack;
133 UINT next_constant_version;
135 const struct wined3d_vertex_pipe_ops *vertex_pipe;
136 const struct wined3d_fragment_pipe_ops *fragment_pipe;
137 struct wine_rb_tree ffp_vertex_shaders;
138 struct wine_rb_tree ffp_fragment_shaders;
139 BOOL ffp_proj_control;
140 BOOL legacy_lighting;
143 struct glsl_vs_program
145 struct list shader_entry;
146 GLuint id;
147 GLenum vertex_color_clamp;
148 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
149 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
150 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
151 GLint pos_fixup_location;
152 GLint base_vertex_id_location;
154 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
155 GLint projection_matrix_location;
156 GLint normal_matrix_location;
157 GLint texture_matrix_location[WINED3D_MAX_TEXTURES];
158 GLint material_ambient_location;
159 GLint material_diffuse_location;
160 GLint material_specular_location;
161 GLint material_emissive_location;
162 GLint material_shininess_location;
163 GLint light_ambient_location;
164 struct
166 GLint diffuse;
167 GLint specular;
168 GLint ambient;
169 GLint position;
170 GLint direction;
171 GLint range;
172 GLint falloff;
173 GLint c_att;
174 GLint l_att;
175 GLint q_att;
176 GLint cos_htheta;
177 GLint cos_hphi;
178 } light_location[WINED3D_MAX_ACTIVE_LIGHTS];
179 GLint pointsize_location;
180 GLint pointsize_min_location;
181 GLint pointsize_max_location;
182 GLint pointsize_c_att_location;
183 GLint pointsize_l_att_location;
184 GLint pointsize_q_att_location;
185 GLint clip_planes_location;
188 struct glsl_hs_program
190 struct list shader_entry;
191 GLuint id;
194 struct glsl_ds_program
196 struct list shader_entry;
197 GLuint id;
199 GLint pos_fixup_location;
202 struct glsl_gs_program
204 struct list shader_entry;
205 GLuint id;
207 GLint pos_fixup_location;
210 struct glsl_ps_program
212 struct list shader_entry;
213 GLuint id;
214 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
215 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
216 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
217 GLint bumpenv_mat_location[WINED3D_MAX_TEXTURES];
218 GLint bumpenv_lum_scale_location[WINED3D_MAX_TEXTURES];
219 GLint bumpenv_lum_offset_location[WINED3D_MAX_TEXTURES];
220 GLint tss_constant_location[WINED3D_MAX_TEXTURES];
221 GLint tex_factor_location;
222 GLint specular_enable_location;
223 GLint fog_color_location;
224 GLint fog_density_location;
225 GLint fog_end_location;
226 GLint fog_scale_location;
227 GLint alpha_test_ref_location;
228 GLint ycorrection_location;
229 GLint np2_fixup_location;
230 GLint color_key_location;
231 const struct ps_np2fixup_info *np2_fixup_info;
234 struct glsl_cs_program
236 struct list shader_entry;
237 GLuint id;
240 /* Struct to maintain data about a linked GLSL program */
241 struct glsl_shader_prog_link
243 struct wine_rb_entry program_lookup_entry;
244 struct glsl_vs_program vs;
245 struct glsl_hs_program hs;
246 struct glsl_ds_program ds;
247 struct glsl_gs_program gs;
248 struct glsl_ps_program ps;
249 struct glsl_cs_program cs;
250 GLuint id;
251 DWORD constant_update_mask;
252 unsigned int constant_version;
253 DWORD shader_controlled_clip_distances : 1;
254 DWORD clip_distance_mask : 8; /* WINED3D_MAX_CLIP_DISTANCES, 8 */
255 DWORD padding : 23;
258 struct glsl_program_key
260 GLuint vs_id;
261 GLuint hs_id;
262 GLuint ds_id;
263 GLuint gs_id;
264 GLuint ps_id;
265 GLuint cs_id;
268 struct shader_glsl_ctx_priv
270 const struct wined3d_gl_info *gl_info;
271 const struct vs_compile_args *cur_vs_args;
272 const struct ds_compile_args *cur_ds_args;
273 const struct ps_compile_args *cur_ps_args;
274 struct ps_np2fixup_info *cur_np2fixup_info;
275 struct wined3d_string_buffer_list *string_buffers;
278 struct glsl_context_data
280 struct glsl_shader_prog_link *glsl_program;
281 GLenum vertex_color_clamp;
282 BOOL rasterization_disabled;
285 struct glsl_ps_compiled_shader
287 struct ps_compile_args args;
288 struct ps_np2fixup_info np2fixup;
289 GLuint id;
292 struct glsl_vs_compiled_shader
294 struct vs_compile_args args;
295 GLuint id;
298 struct glsl_hs_compiled_shader
300 GLuint id;
303 struct glsl_ds_compiled_shader
305 struct ds_compile_args args;
306 GLuint id;
309 struct glsl_gs_compiled_shader
311 struct gs_compile_args args;
312 GLuint id;
315 struct glsl_cs_compiled_shader
317 GLuint id;
320 struct glsl_shader_private
322 union
324 struct glsl_vs_compiled_shader *vs;
325 struct glsl_hs_compiled_shader *hs;
326 struct glsl_ds_compiled_shader *ds;
327 struct glsl_gs_compiled_shader *gs;
328 struct glsl_ps_compiled_shader *ps;
329 struct glsl_cs_compiled_shader *cs;
330 } gl_shaders;
331 unsigned int num_gl_shaders, shader_array_size;
334 struct glsl_ffp_vertex_shader
336 struct wined3d_ffp_vs_desc desc;
337 GLuint id;
338 struct list linked_programs;
341 struct glsl_ffp_fragment_shader
343 struct ffp_frag_desc entry;
344 GLuint id;
345 struct list linked_programs;
348 struct glsl_ffp_destroy_ctx
350 struct shader_glsl_priv *priv;
351 const struct wined3d_context_gl *context_gl;
354 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
356 static const char *debug_gl_shader_type(GLenum type)
358 switch (type)
360 #define WINED3D_TO_STR(u) case u: return #u
361 WINED3D_TO_STR(GL_VERTEX_SHADER);
362 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
363 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
364 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
365 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
366 WINED3D_TO_STR(GL_COMPUTE_SHADER);
367 #undef WINED3D_TO_STR
368 default:
369 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
373 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
375 switch (type)
377 case WINED3D_SHADER_TYPE_VERTEX:
378 return "vs";
380 case WINED3D_SHADER_TYPE_HULL:
381 return "hs";
383 case WINED3D_SHADER_TYPE_DOMAIN:
384 return "ds";
386 case WINED3D_SHADER_TYPE_GEOMETRY:
387 return "gs";
389 case WINED3D_SHADER_TYPE_PIXEL:
390 return "ps";
392 case WINED3D_SHADER_TYPE_COMPUTE:
393 return "cs";
395 default:
396 FIXME("Unhandled shader type %#x.\n", type);
397 return "unknown";
401 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info)
403 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 40))
404 return 440;
405 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
406 return 150;
407 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30))
408 return 130;
409 else
410 return 120;
413 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
414 const struct wined3d_gl_info *gl_info)
416 shader_addline(buffer, "#version %u\n", shader_glsl_get_version(gl_info));
419 static unsigned int shader_glsl_full_ffp_varyings(const struct wined3d_gl_info *gl_info)
421 /* On core profile we have to also count diffuse and specular colours and
422 * the fog coordinate. */
423 return gl_info->limits.glsl_varyings >= (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
424 ? WINED3D_MAX_TEXTURES * 4 : (WINED3D_MAX_TEXTURES + 2) * 4 + 1);
427 static void shader_glsl_append_imm_vec(struct wined3d_string_buffer *buffer,
428 const float *values, unsigned int size, const struct wined3d_gl_info *gl_info)
430 const int *int_values = (const int *)values;
431 unsigned int i;
432 char str[17];
434 if (!gl_info->supported[ARB_SHADER_BIT_ENCODING])
436 if (size > 1)
437 shader_addline(buffer, "vec%u(", size);
439 for (i = 0; i < size; ++i)
441 wined3d_ftoa(values[i], str);
442 shader_addline(buffer, i ? ", %s" : "%s", str);
445 if (size > 1)
446 shader_addline(buffer, ")");
448 return;
451 shader_addline(buffer, "intBitsToFloat(");
452 if (size > 1)
453 shader_addline(buffer, "ivec%u(", size);
455 for (i = 0; i < size; ++i)
457 wined3d_ftoa(values[i], str);
458 shader_addline(buffer, i ? ", %#x /* %s */" : "%#x /* %s */", int_values[i], str);
461 if (size > 1)
462 shader_addline(buffer, ")");
463 shader_addline(buffer, ")");
466 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
467 const int *values, unsigned int size)
469 int i;
471 if (!size || size > 4)
473 ERR("Invalid vector size %u.\n", size);
474 return;
477 if (size > 1)
478 shader_addline(buffer, "ivec%u(", size);
480 for (i = 0; i < size; ++i)
481 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
483 if (size > 1)
484 shader_addline(buffer, ")");
487 /* Context activation is done by the caller. */
488 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
490 int length = 0;
491 char *log;
493 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
494 return;
496 if (program)
497 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
498 else
499 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
501 /* A size of 1 is just a null-terminated string, so the log should be bigger than
502 * that if there are errors. */
503 if (length > 1)
505 const char *ptr, *end, *line;
507 log = heap_alloc(length);
508 if (program)
509 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
510 else
511 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
513 ptr = log;
514 /* The info log is supposed to be zero-terminated. Note that at least
515 * some versions of fglrx don't terminate the string properly. The
516 * reported length does include the supposed terminator though, so we
517 * don't care here. */
518 end = &ptr[length - 1];
519 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
521 WARN("Info log received from GLSL shader #%u:\n", id);
522 while ((line = wined3d_get_line(&ptr, end)))
524 WARN(" %.*s", (int)(ptr - line), line);
527 else
529 FIXME("Info log received from GLSL shader #%u:\n", id);
530 while ((line = wined3d_get_line(&ptr, end)))
532 FIXME(" %.*s", (int)(ptr - line), line);
535 heap_free(log);
539 /* Context activation is done by the caller. */
540 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
542 const char *ptr, *end, *line;
544 TRACE("Compiling shader object %u.\n", shader);
546 if (TRACE_ON(d3d_shader))
548 ptr = src;
549 end = ptr + strlen(ptr);
550 while ((line = wined3d_get_line(&ptr, end)))
552 TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
556 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
557 checkGLcall("glShaderSource");
558 GL_EXTCALL(glCompileShader(shader));
559 checkGLcall("glCompileShader");
560 print_glsl_info_log(gl_info, shader, FALSE);
563 /* Context activation is done by the caller. */
564 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
566 GLint i, shader_count, source_size = -1;
567 GLuint *shaders;
568 char *source = NULL;
570 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
571 if (!(shaders = heap_calloc(shader_count, sizeof(*shaders))))
573 ERR("Failed to allocate shader array memory.\n");
574 return;
577 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
578 for (i = 0; i < shader_count; ++i)
580 const char *ptr, *end, *line;
581 GLint tmp;
583 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
585 if (source_size < tmp)
587 heap_free(source);
589 if (!(source = heap_alloc(tmp)))
591 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
592 heap_free(shaders);
593 return;
595 source_size = tmp;
598 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
599 ptr = source;
600 end = &ptr[tmp - 1];
602 FIXME("Shader %u:\n", shaders[i]);
603 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
604 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
605 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
606 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
607 FIXME("\n");
608 while ((line = wined3d_get_line(&ptr, end)))
610 FIXME(" %.*s", (int)(ptr - line), line);
612 FIXME("\n");
615 heap_free(source);
616 heap_free(shaders);
619 /* Context activation is done by the caller. */
620 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
622 GLint tmp;
624 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
625 return;
627 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
628 if (!tmp)
630 FIXME("Program %u link status invalid.\n", program);
631 shader_glsl_dump_program_source(gl_info, program);
634 print_glsl_info_log(gl_info, program, TRUE);
637 static BOOL shader_glsl_use_layout_qualifier(const struct wined3d_gl_info *gl_info)
639 /* Layout qualifiers were introduced in GLSL 1.40. The Nvidia Legacy GPU
640 * driver (series 340.xx) doesn't parse layout qualifiers in older GLSL
641 * versions. */
642 return shader_glsl_get_version(gl_info) >= 140;
645 static BOOL shader_glsl_use_layout_binding_qualifier(const struct wined3d_gl_info *gl_info)
647 return gl_info->supported[ARB_SHADING_LANGUAGE_420PACK] && shader_glsl_use_layout_qualifier(gl_info);
650 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
651 struct shader_glsl_priv *priv, GLuint program_id,
652 const struct wined3d_shader_reg_maps *reg_maps)
654 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
655 struct wined3d_string_buffer *name;
656 unsigned int i, base, count;
657 GLuint block_idx;
659 if (shader_glsl_use_layout_binding_qualifier(gl_info))
660 return;
662 name = string_buffer_get(&priv->string_buffers);
663 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
664 for (i = 0; i < count; ++i)
666 if (!reg_maps->cb_sizes[i])
667 continue;
669 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
670 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
671 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
673 checkGLcall("glUniformBlockBinding");
674 string_buffer_release(&priv->string_buffers, name);
677 /* Context activation is done by the caller. */
678 static void shader_glsl_load_samplers_range(const struct wined3d_gl_info *gl_info,
679 struct shader_glsl_priv *priv, GLuint program_id, const char *prefix,
680 unsigned int base, unsigned int count, const unsigned int *tex_unit_map)
682 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
683 unsigned int i, mapped_unit;
684 GLint name_loc;
686 for (i = 0; i < count; ++i)
688 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
689 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
690 if (name_loc == -1)
691 continue;
693 mapped_unit = tex_unit_map ? tex_unit_map[base + i] : base + i;
694 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
696 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
697 continue;
700 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
701 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
703 checkGLcall("Load sampler bindings");
704 string_buffer_release(&priv->string_buffers, sampler_name);
707 static unsigned int shader_glsl_map_tex_unit(const struct wined3d_context *context,
708 const struct wined3d_shader_version *shader_version, unsigned int sampler_idx)
710 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
711 const unsigned int *tex_unit_map;
712 unsigned int base, count;
714 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
715 if (sampler_idx >= count)
716 return WINED3D_UNMAPPED_STAGE;
717 if (!tex_unit_map)
718 return base + sampler_idx;
719 return tex_unit_map[base + sampler_idx];
722 static void shader_glsl_append_sampler_binding_qualifier(struct wined3d_string_buffer *buffer,
723 const struct wined3d_context *context, const struct wined3d_shader_version *shader_version,
724 unsigned int sampler_idx)
726 unsigned int mapped_unit = shader_glsl_map_tex_unit(context, shader_version, sampler_idx);
727 if (mapped_unit != WINED3D_UNMAPPED_STAGE)
728 shader_addline(buffer, "layout(binding = %u)\n", mapped_unit);
729 else
730 ERR("Unmapped sampler %u.\n", sampler_idx);
733 /* Context activation is done by the caller. */
734 static void shader_glsl_load_samplers(const struct wined3d_context *context,
735 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
737 const struct wined3d_context_gl *context_gl = wined3d_context_gl_const(context);
738 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
739 const struct wined3d_shader_version *shader_version;
740 const unsigned int *tex_unit_map;
741 unsigned int base, count;
742 const char *prefix;
744 if (shader_glsl_use_layout_binding_qualifier(gl_info))
745 return;
747 shader_version = reg_maps ? &reg_maps->shader_version : NULL;
748 prefix = shader_glsl_get_prefix(shader_version ? shader_version->type : WINED3D_SHADER_TYPE_PIXEL);
749 tex_unit_map = wined3d_context_gl_get_tex_unit_mapping(context_gl, shader_version, &base, &count);
750 shader_glsl_load_samplers_range(gl_info, priv, program_id, prefix, base, count, tex_unit_map);
753 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
754 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
756 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
758 if (icb)
760 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
761 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
762 GLint icb_location;
764 string_buffer_sprintf(icb_name, "%s_icb", prefix);
765 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
766 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
767 checkGLcall("Load immediate constant buffer");
769 string_buffer_release(&priv->string_buffers, icb_name);
773 /* Context activation is done by the caller. */
774 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
775 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
777 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
778 struct wined3d_string_buffer *name;
779 GLint location;
780 unsigned int i;
782 if (shader_glsl_use_layout_binding_qualifier(gl_info))
783 return;
785 name = string_buffer_get(&priv->string_buffers);
786 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
788 if (!reg_maps->uav_resource_info[i].type)
789 continue;
791 string_buffer_sprintf(name, "%s_image%u", prefix, i);
792 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
793 if (location == -1)
794 continue;
796 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
797 GL_EXTCALL(glUniform1i(location, i));
799 checkGLcall("Load image bindings");
800 string_buffer_release(&priv->string_buffers, name);
803 /* Context activation is done by the caller. */
804 static void shader_glsl_load_program_resources(const struct wined3d_context_gl *context_gl,
805 struct shader_glsl_priv *priv, GLuint program_id, const struct wined3d_shader *shader)
807 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
809 shader_glsl_init_uniform_block_bindings(context_gl->gl_info, priv, program_id, reg_maps);
810 shader_glsl_load_icb(context_gl->gl_info, priv, program_id, reg_maps);
811 /* Texture unit mapping is set up to be the same each time the shader
812 * program is used so we can hardcode the sampler uniform values. */
813 shader_glsl_load_samplers(&context_gl->c, priv, program_id, reg_maps);
816 static void append_transform_feedback_varying(const char **varyings, unsigned int *varying_count,
817 char **strings, unsigned int *strings_length, struct wined3d_string_buffer *buffer)
819 if (varyings && *strings)
821 char *ptr = *strings;
823 varyings[*varying_count] = ptr;
825 memcpy(ptr, buffer->buffer, buffer->content_size + 1);
826 ptr += buffer->content_size + 1;
828 *strings = ptr;
831 *strings_length += buffer->content_size + 1;
832 ++(*varying_count);
835 static void append_transform_feedback_skip_components(const char **varyings,
836 unsigned int *varying_count, char **strings, unsigned int *strings_length,
837 struct wined3d_string_buffer *buffer, unsigned int component_count)
839 unsigned int j;
841 for (j = 0; j < component_count / 4; ++j)
843 string_buffer_sprintf(buffer, "gl_SkipComponents4");
844 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
846 if (component_count % 4)
848 string_buffer_sprintf(buffer, "gl_SkipComponents%u", component_count % 4);
849 append_transform_feedback_varying(varyings, varying_count, strings, strings_length, buffer);
853 static BOOL shader_glsl_generate_transform_feedback_varyings(struct wined3d_string_buffer *buffer,
854 const char **varyings, unsigned int *varying_count, char *strings, unsigned int *strings_length,
855 GLenum buffer_mode, struct wined3d_shader *shader)
857 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
858 unsigned int buffer_idx, count, length, highest_output_slot, stride;
859 unsigned int i, register_idx, component_idx;
860 BOOL have_varyings_to_record = FALSE;
862 count = length = 0;
863 highest_output_slot = 0;
864 for (buffer_idx = 0; buffer_idx < WINED3D_MAX_STREAM_OUTPUT_BUFFERS; ++buffer_idx)
866 stride = 0;
868 for (i = 0; i < so_desc->element_count; ++i)
870 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
872 highest_output_slot = max(highest_output_slot, e->output_slot);
873 if (e->output_slot != buffer_idx)
874 continue;
876 if (e->stream_idx)
878 FIXME("Unhandled stream %u.\n", e->stream_idx);
879 continue;
882 stride += e->component_count;
884 if (!e->semantic_name)
886 append_transform_feedback_skip_components(varyings, &count,
887 &strings, &length, buffer, e->component_count);
888 continue;
891 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
892 continue;
894 if (component_idx || e->component_count != 4)
896 if (so_desc->rasterizer_stream_idx != WINED3D_NO_RASTERIZER_STREAM)
898 FIXME("Unsupported component range %u-%u.\n", component_idx, e->component_count);
899 append_transform_feedback_skip_components(varyings, &count,
900 &strings, &length, buffer, e->component_count);
901 continue;
904 string_buffer_sprintf(buffer, "shader_in_out.reg%u_%u_%u",
905 register_idx, component_idx, component_idx + e->component_count - 1);
906 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
908 else
910 string_buffer_sprintf(buffer, "shader_in_out.reg%u", register_idx);
911 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
914 have_varyings_to_record = TRUE;
917 if (buffer_idx < so_desc->buffer_stride_count
918 && stride < so_desc->buffer_strides[buffer_idx] / 4)
920 unsigned int component_count = so_desc->buffer_strides[buffer_idx] / 4 - stride;
921 append_transform_feedback_skip_components(varyings, &count,
922 &strings, &length, buffer, component_count);
925 if (highest_output_slot <= buffer_idx)
926 break;
928 if (buffer_mode == GL_INTERLEAVED_ATTRIBS)
930 string_buffer_sprintf(buffer, "gl_NextBuffer");
931 append_transform_feedback_varying(varyings, &count, &strings, &length, buffer);
935 if (varying_count)
936 *varying_count = count;
937 if (strings_length)
938 *strings_length = length;
940 return have_varyings_to_record;
943 static void shader_glsl_init_transform_feedback(const struct wined3d_context_gl *context_gl,
944 struct shader_glsl_priv *priv, GLuint program_id, struct wined3d_shader *shader)
946 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
947 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
948 struct wined3d_string_buffer *buffer;
949 unsigned int i, count, length;
950 const char **varyings;
951 char *strings;
952 GLenum mode;
954 if (!so_desc)
955 return;
957 if (gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
959 mode = GL_INTERLEAVED_ATTRIBS;
961 else
963 unsigned int element_count[WINED3D_MAX_STREAM_OUTPUT_BUFFERS] = {0};
965 for (i = 0; i < so_desc->element_count; ++i)
967 if (!so_desc->elements[i].semantic_name)
969 FIXME("ARB_transform_feedback3 is needed for stream output gaps.\n");
970 return;
972 ++element_count[so_desc->elements[i].output_slot];
975 if (element_count[0] == so_desc->element_count)
977 mode = GL_INTERLEAVED_ATTRIBS;
979 else
981 mode = GL_SEPARATE_ATTRIBS;
982 for (i = 0; i < ARRAY_SIZE(element_count); ++i)
984 if (element_count[i] != 1)
985 break;
987 for (; i < ARRAY_SIZE(element_count); ++i)
989 if (element_count[i])
991 FIXME("Only single element per buffer is allowed in separate mode.\n");
992 return;
998 buffer = string_buffer_get(&priv->string_buffers);
1000 if (!shader_glsl_generate_transform_feedback_varyings(buffer, NULL, &count, NULL, &length, mode, shader))
1002 FIXME("No varyings to record, disabling transform feedback.\n");
1003 shader->u.gs.so_desc = NULL;
1004 string_buffer_release(&priv->string_buffers, buffer);
1005 return;
1008 if (!(varyings = heap_calloc(count, sizeof(*varyings))))
1010 ERR("Out of memory.\n");
1011 string_buffer_release(&priv->string_buffers, buffer);
1012 return;
1014 if (!(strings = heap_calloc(length, sizeof(*strings))))
1016 ERR("Out of memory.\n");
1017 heap_free(varyings);
1018 string_buffer_release(&priv->string_buffers, buffer);
1019 return;
1022 shader_glsl_generate_transform_feedback_varyings(buffer, varyings, NULL, strings, NULL, mode, shader);
1023 GL_EXTCALL(glTransformFeedbackVaryings(program_id, count, varyings, mode));
1024 checkGLcall("glTransformFeedbackVaryings");
1026 heap_free(varyings);
1027 heap_free(strings);
1028 string_buffer_release(&priv->string_buffers, buffer);
1031 /* Context activation is done by the caller. */
1032 static void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
1033 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
1035 unsigned int start = ~0U, end = 0;
1036 int stack_idx = 0;
1037 unsigned int heap_idx = 1;
1038 unsigned int idx;
1040 if (heap->entries[heap_idx].version <= version) return;
1042 idx = heap->entries[heap_idx].idx;
1043 if (constant_locations[idx] != -1)
1044 start = end = idx;
1045 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1047 while (stack_idx >= 0)
1049 /* Note that we fall through to the next case statement. */
1050 switch(stack[stack_idx])
1052 case HEAP_NODE_TRAVERSE_LEFT:
1054 unsigned int left_idx = heap_idx << 1;
1055 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1057 heap_idx = left_idx;
1058 idx = heap->entries[heap_idx].idx;
1059 if (constant_locations[idx] != -1)
1061 if (start > idx)
1062 start = idx;
1063 if (end < idx)
1064 end = idx;
1067 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1068 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1069 break;
1073 case HEAP_NODE_TRAVERSE_RIGHT:
1075 unsigned int right_idx = (heap_idx << 1) + 1;
1076 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1078 heap_idx = right_idx;
1079 idx = heap->entries[heap_idx].idx;
1080 if (constant_locations[idx] != -1)
1082 if (start > idx)
1083 start = idx;
1084 if (end < idx)
1085 end = idx;
1088 stack[stack_idx++] = HEAP_NODE_POP;
1089 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1090 break;
1094 case HEAP_NODE_POP:
1095 heap_idx >>= 1;
1096 --stack_idx;
1097 break;
1100 if (start <= end)
1101 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
1102 checkGLcall("walk_constant_heap()");
1105 /* Context activation is done by the caller. */
1106 static void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
1107 GLint location, const struct wined3d_vec4 *data)
1109 GLfloat clamped_constant[4];
1111 if (location == -1) return;
1113 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
1114 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
1115 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
1116 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
1118 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
1121 /* Context activation is done by the caller. */
1122 static void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
1123 const struct wined3d_vec4 *constants, const GLint *constant_locations,
1124 const struct constant_heap *heap, unsigned char *stack, DWORD version)
1126 int stack_idx = 0;
1127 unsigned int heap_idx = 1;
1128 unsigned int idx;
1130 if (heap->entries[heap_idx].version <= version) return;
1132 idx = heap->entries[heap_idx].idx;
1133 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1134 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1136 while (stack_idx >= 0)
1138 /* Note that we fall through to the next case statement. */
1139 switch(stack[stack_idx])
1141 case HEAP_NODE_TRAVERSE_LEFT:
1143 unsigned int left_idx = heap_idx << 1;
1144 if (left_idx < heap->size && heap->entries[left_idx].version > version)
1146 heap_idx = left_idx;
1147 idx = heap->entries[heap_idx].idx;
1148 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1150 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
1151 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1152 break;
1156 case HEAP_NODE_TRAVERSE_RIGHT:
1158 unsigned int right_idx = (heap_idx << 1) + 1;
1159 if (right_idx < heap->size && heap->entries[right_idx].version > version)
1161 heap_idx = right_idx;
1162 idx = heap->entries[heap_idx].idx;
1163 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
1165 stack[stack_idx++] = HEAP_NODE_POP;
1166 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
1167 break;
1171 case HEAP_NODE_POP:
1172 heap_idx >>= 1;
1173 --stack_idx;
1174 break;
1177 checkGLcall("walk_constant_heap_clamped()");
1180 /* Context activation is done by the caller. */
1181 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1182 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
1183 unsigned char *stack, unsigned int version)
1185 const struct wined3d_shader_lconst *lconst;
1187 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
1188 if (shader->reg_maps.shader_version.major == 1
1189 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
1190 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
1191 else
1192 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
1194 if (!shader->load_local_constsF)
1196 TRACE("No need to load local float constants for this shader.\n");
1197 return;
1200 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
1201 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1203 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
1205 checkGLcall("glUniform4fv()");
1208 /* Context activation is done by the caller. */
1209 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1210 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], uint32_t constants_set)
1212 unsigned int i;
1213 struct list* ptr;
1215 while (constants_set)
1217 /* We found this uniform name in the program - go ahead and send the data */
1218 i = wined3d_bit_scan(&constants_set);
1219 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
1222 /* Load immediate constants */
1223 ptr = list_head(&shader->constantsI);
1224 while (ptr)
1226 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1227 unsigned int idx = lconst->idx;
1228 const GLint *values = (const GLint *)lconst->value;
1230 /* We found this uniform name in the program - go ahead and send the data */
1231 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
1232 ptr = list_next(&shader->constantsI, ptr);
1234 checkGLcall("glUniform4iv()");
1237 /* Context activation is done by the caller. */
1238 static void shader_glsl_load_constants_b(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
1239 const BOOL *constants, const GLint locations[WINED3D_MAX_CONSTS_B], uint32_t constants_set)
1241 unsigned int i;
1242 struct list* ptr;
1244 while (constants_set)
1246 i = wined3d_bit_scan(&constants_set);
1247 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
1250 /* Load immediate constants */
1251 ptr = list_head(&shader->constantsB);
1252 while (ptr)
1254 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
1255 unsigned int idx = lconst->idx;
1256 const GLint *values = (const GLint *)lconst->value;
1258 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
1259 ptr = list_next(&shader->constantsB, ptr);
1261 checkGLcall("glUniform1iv()");
1264 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
1266 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
1269 /* Context activation is done by the caller (state handler). */
1270 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
1271 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1273 struct
1275 float sx, sy;
1277 np2fixup_constants[WINED3D_MAX_FRAGMENT_SAMPLERS];
1278 UINT fixup = ps->np2_fixup_info->active;
1279 UINT i;
1281 for (i = 0; fixup; fixup >>= 1, ++i)
1283 const struct wined3d_texture *tex = state->textures[i];
1284 unsigned char idx = ps->np2_fixup_info->idx[i];
1286 if (!tex)
1288 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
1289 continue;
1292 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
1293 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
1296 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
1299 static void transpose_matrix(struct wined3d_matrix *out, const struct wined3d_matrix *m)
1301 struct wined3d_matrix temp;
1302 unsigned int i, j;
1304 for (i = 0; i < 4; ++i)
1305 for (j = 0; j < 4; ++j)
1306 (&temp._11)[4 * j + i] = (&m->_11)[4 * i + j];
1308 *out = temp;
1311 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context_gl *context_gl,
1312 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1314 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1315 struct wined3d_matrix mv;
1316 float mat[3 * 3];
1318 if (prog->vs.normal_matrix_location == -1)
1319 return;
1321 get_modelview_matrix(&context_gl->c, state, 0, &mv);
1322 compute_normal_matrix(mat, context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING, &mv);
1324 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1325 checkGLcall("glUniformMatrix3fv");
1328 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context_gl *context_gl,
1329 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1331 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1332 struct wined3d_matrix mat;
1334 if (tex >= WINED3D_MAX_TEXTURES)
1335 return;
1336 if (prog->vs.texture_matrix_location[tex] == -1)
1337 return;
1339 get_texture_matrix(&context_gl->c, state, tex, &mat);
1340 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1341 checkGLcall("glUniformMatrix4fv");
1344 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context_gl *context_gl,
1345 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1347 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1349 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1351 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1352 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1354 else
1356 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1358 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1360 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1361 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1362 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1363 checkGLcall("setting FFP material uniforms");
1366 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context_gl *context_gl,
1367 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1369 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1370 struct wined3d_color color;
1372 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1373 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1374 checkGLcall("glUniform3fv");
1377 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context_gl *context_gl,
1378 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1379 struct glsl_shader_prog_link *prog)
1381 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1382 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1383 struct wined3d_vec4 vec4;
1385 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1386 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1387 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1389 switch (light_info->OriginalParms.type)
1391 case WINED3D_LIGHT_POINT:
1392 wined3d_vec4_transform(&vec4, &light_info->position, view);
1393 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1394 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1395 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1396 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1397 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1398 break;
1400 case WINED3D_LIGHT_SPOT:
1401 wined3d_vec4_transform(&vec4, &light_info->position, view);
1402 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1404 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1405 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1407 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1408 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1409 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1410 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1411 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1412 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1413 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1414 break;
1416 case WINED3D_LIGHT_DIRECTIONAL:
1417 wined3d_vec4_transform(&vec4, &light_info->direction, view);
1418 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1419 break;
1421 case WINED3D_LIGHT_PARALLELPOINT:
1422 wined3d_vec4_transform(&vec4, &light_info->position, view);
1423 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1424 break;
1426 default:
1427 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1429 checkGLcall("setting FFP lights uniforms");
1432 static void shader_glsl_pointsize_uniform(const struct wined3d_context_gl *context_gl,
1433 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1435 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1436 float size, att[3];
1437 float min, max;
1439 get_pointsize_minmax(&context_gl->c, state, &min, &max);
1441 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1442 checkGLcall("glUniform1f");
1443 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1444 checkGLcall("glUniform1f");
1446 get_pointsize(&context_gl->c, state, &size, att);
1448 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1449 checkGLcall("glUniform1f");
1450 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1451 checkGLcall("glUniform1f");
1452 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1453 checkGLcall("glUniform1f");
1454 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1455 checkGLcall("glUniform1f");
1458 static void shader_glsl_load_fog_uniform(const struct wined3d_context_gl *context_gl,
1459 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1461 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1462 struct wined3d_color color;
1463 float start, end, scale;
1464 union
1466 DWORD d;
1467 float f;
1468 } tmpvalue;
1470 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1471 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1472 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1473 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1474 get_fog_start_end(&context_gl->c, state, &start, &end);
1475 scale = 1.0f / (end - start);
1476 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1477 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1478 checkGLcall("fog emulation uniforms");
1481 static void shader_glsl_clip_plane_uniform(const struct wined3d_context_gl *context_gl,
1482 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1484 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1485 struct wined3d_matrix matrix;
1486 struct wined3d_vec4 plane;
1488 plane = state->clip_planes[index];
1490 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1491 if (!use_vs(state))
1493 invert_matrix(&matrix, &state->transforms[WINED3D_TS_VIEW]);
1494 transpose_matrix(&matrix, &matrix);
1495 wined3d_vec4_transform(&plane, &plane, &matrix);
1498 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1501 /* Context activation is done by the caller (state handler). */
1502 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1503 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1505 struct wined3d_color float_key[2];
1506 const struct wined3d_texture *texture = state->textures[0];
1508 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1509 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1512 /* Context activation is done by the caller (state handler). */
1513 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1514 const struct wined3d_state *state)
1516 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1517 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1518 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1519 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
1520 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1521 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1522 float position_fixup[4 * WINED3D_MAX_VIEWPORTS];
1523 struct shader_glsl_priv *priv = shader_priv;
1524 unsigned int constant_version;
1525 DWORD update_mask;
1526 int i;
1528 /* No GLSL program set - nothing to do. */
1529 if (!prog)
1530 return;
1532 constant_version = prog->constant_version;
1533 update_mask = context->constant_update_mask & prog->constant_update_mask;
1535 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1536 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1537 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1539 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1540 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1541 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1543 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1544 shader_glsl_load_constants_b(vshader, gl_info, state->vs_consts_b,
1545 prog->vs.uniform_b_locations, vshader->reg_maps.boolean_constants);
1547 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1549 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1550 shader_glsl_clip_plane_uniform(context_gl, state, i, prog);
1553 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1554 shader_glsl_pointsize_uniform(context_gl, state, prog);
1556 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1558 unsigned int fixup_count = state->shader[WINED3D_SHADER_TYPE_GEOMETRY] ?
1559 max(state->viewport_count, 1) : 1;
1560 shader_get_position_fixup(context, state, fixup_count, position_fixup);
1561 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1562 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, fixup_count, position_fixup));
1563 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
1564 GL_EXTCALL(glUniform4fv(prog->ds.pos_fixup_location, 1, position_fixup));
1565 else
1566 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1567 checkGLcall("glUniform4fv");
1570 if (update_mask & WINED3D_SHADER_CONST_BASE_VERTEX_ID)
1572 GL_EXTCALL(glUniform1i(prog->vs.base_vertex_id_location, state->base_vertex_index));
1573 checkGLcall("base vertex id");
1576 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1578 struct wined3d_matrix mat;
1580 get_modelview_matrix(context, state, 0, &mat);
1581 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1582 checkGLcall("glUniformMatrix4fv");
1584 shader_glsl_ffp_vertex_normalmatrix_uniform(context_gl, state, prog);
1587 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1589 struct wined3d_matrix mat;
1591 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1593 if (prog->vs.modelview_matrix_location[i] == -1)
1594 break;
1596 get_modelview_matrix(context, state, i, &mat);
1597 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1598 checkGLcall("glUniformMatrix4fv");
1602 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1604 struct wined3d_matrix projection;
1606 get_projection_matrix(context, state, &projection);
1607 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1608 checkGLcall("glUniformMatrix4fv");
1611 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1613 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1614 shader_glsl_ffp_vertex_texmatrix_uniform(context_gl, state, i, prog);
1617 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1618 shader_glsl_ffp_vertex_material_uniform(context_gl, state, prog);
1620 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1622 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1623 DWORD point_count = 0;
1624 DWORD spot_count = 0;
1625 DWORD directional_count = 0;
1626 DWORD parallel_point_count = 0;
1628 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1630 if (!state->light_state.lights[i])
1631 continue;
1633 switch (state->light_state.lights[i]->OriginalParms.type)
1635 case WINED3D_LIGHT_POINT:
1636 ++point_count;
1637 break;
1638 case WINED3D_LIGHT_SPOT:
1639 ++spot_count;
1640 break;
1641 case WINED3D_LIGHT_DIRECTIONAL:
1642 ++directional_count;
1643 break;
1644 case WINED3D_LIGHT_PARALLELPOINT:
1645 ++parallel_point_count;
1646 break;
1647 default:
1648 FIXME("Unhandled light type %#x.\n", state->light_state.lights[i]->OriginalParms.type);
1649 break;
1652 point_idx = 0;
1653 spot_idx = point_idx + point_count;
1654 directional_idx = spot_idx + spot_count;
1655 parallel_point_idx = directional_idx + directional_count;
1657 shader_glsl_ffp_vertex_lightambient_uniform(context_gl, state, prog);
1658 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
1660 const struct wined3d_light_info *light_info = state->light_state.lights[i];
1661 unsigned int idx;
1663 if (!light_info)
1664 continue;
1666 switch (light_info->OriginalParms.type)
1668 case WINED3D_LIGHT_POINT:
1669 idx = point_idx++;
1670 break;
1671 case WINED3D_LIGHT_SPOT:
1672 idx = spot_idx++;
1673 break;
1674 case WINED3D_LIGHT_DIRECTIONAL:
1675 idx = directional_idx++;
1676 break;
1677 case WINED3D_LIGHT_PARALLELPOINT:
1678 idx = parallel_point_idx++;
1679 break;
1680 default:
1681 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1682 continue;
1684 shader_glsl_ffp_vertex_light_uniform(context_gl, state, idx, light_info, prog);
1688 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1689 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1690 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1692 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1693 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1694 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1696 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1697 shader_glsl_load_constants_b(pshader, gl_info, state->ps_consts_b,
1698 prog->ps.uniform_b_locations, pshader->reg_maps.boolean_constants);
1700 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1702 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1704 if (prog->ps.bumpenv_mat_location[i] == -1)
1705 continue;
1707 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1708 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1710 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1712 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1713 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1714 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1715 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1719 checkGLcall("bump env uniforms");
1722 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1724 const struct wined3d_vec4 correction_params =
1726 /* Position is relative to the framebuffer, not the viewport. */
1727 context->render_offscreen ? 0.0f : (float)state->fb.render_targets[0]->height,
1728 context->render_offscreen ? 1.0f : -1.0f,
1729 0.0f,
1730 0.0f,
1733 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1736 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1737 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1738 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1739 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1741 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1743 struct wined3d_color color;
1745 if (prog->ps.tex_factor_location != -1)
1747 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1748 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1751 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1752 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1753 else
1754 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1756 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
1758 if (prog->ps.tss_constant_location[i] == -1)
1759 continue;
1761 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1762 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1765 checkGLcall("fixed function uniforms");
1768 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1769 shader_glsl_load_fog_uniform(context_gl, state, prog);
1771 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1773 float ref = wined3d_alpha_ref(state);
1775 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1776 checkGLcall("alpha test emulation uniform");
1779 if (priv->next_constant_version == UINT_MAX)
1781 TRACE("Max constant version reached, resetting to 0.\n");
1782 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1783 priv->next_constant_version = 1;
1785 else
1787 prog->constant_version = priv->next_constant_version++;
1791 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1793 struct constant_entry *entries = heap->entries;
1794 unsigned int *positions = heap->positions;
1795 unsigned int heap_idx, parent_idx;
1797 if (!heap->contained[idx])
1799 heap_idx = heap->size++;
1800 heap->contained[idx] = TRUE;
1802 else
1804 heap_idx = positions[idx];
1807 while (heap_idx > 1)
1809 parent_idx = heap_idx >> 1;
1811 if (new_version <= entries[parent_idx].version) break;
1813 entries[heap_idx] = entries[parent_idx];
1814 positions[entries[parent_idx].idx] = heap_idx;
1815 heap_idx = parent_idx;
1818 entries[heap_idx].version = new_version;
1819 entries[heap_idx].idx = idx;
1820 positions[idx] = heap_idx;
1823 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1825 struct shader_glsl_priv *priv = device->shader_priv;
1826 struct constant_heap *heap = &priv->vconst_heap;
1827 UINT i;
1829 for (i = start; i < count + start; ++i)
1831 update_heap_entry(heap, i, priv->next_constant_version);
1835 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1837 struct shader_glsl_priv *priv = device->shader_priv;
1838 struct constant_heap *heap = &priv->pconst_heap;
1839 UINT i;
1841 for (i = start; i < count + start; ++i)
1843 update_heap_entry(heap, i, priv->next_constant_version);
1847 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1849 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1850 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1851 if(shader_major > 3) return ret;
1853 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1854 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1855 return ret;
1858 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1860 return gl_info->glsl_version < MAKEDWORD_VERSION(1, 30);
1863 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1865 return gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION]
1866 && shader_glsl_use_layout_qualifier(gl_info) && !needs_legacy_glsl_syntax(gl_info);
1869 static BOOL shader_glsl_use_interface_blocks(const struct wined3d_gl_info *gl_info)
1871 return shader_glsl_get_version(gl_info) >= 150;
1874 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1876 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1879 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1880 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1882 va_list args;
1883 int ret;
1885 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1886 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1887 for (;;)
1889 va_start(args, format);
1890 ret = shader_vaddline(buffer, format, args);
1891 va_end(args);
1892 if (!ret)
1893 return;
1894 if (!string_buffer_resize(buffer, ret))
1895 return;
1899 static void PRINTF_ATTR(4, 5) declare_out_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" : "out");
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 const char *shader_glsl_shader_input_name(const struct wined3d_gl_info *gl_info)
1921 return shader_glsl_use_interface_blocks(gl_info) ? "shader_in.reg" : "ps_link";
1924 static const char *shader_glsl_shader_output_name(const struct wined3d_gl_info *gl_info)
1926 return shader_glsl_use_interface_blocks(gl_info) ? "shader_out.reg" : "ps_link";
1929 static const char *shader_glsl_interpolation_qualifiers(enum wined3d_shader_interpolation_mode mode)
1931 switch (mode)
1933 case WINED3DSIM_CONSTANT:
1934 return "flat ";
1935 case WINED3DSIM_LINEAR_NOPERSPECTIVE:
1936 return "noperspective ";
1937 default:
1938 FIXME("Unhandled interpolation mode %#x.\n", mode);
1939 case WINED3DSIM_NONE:
1940 case WINED3DSIM_LINEAR:
1941 return "";
1945 static enum wined3d_shader_interpolation_mode wined3d_extract_interpolation_mode(
1946 const uint32_t *packed_interpolation_mode, unsigned int register_idx)
1948 return wined3d_extract_bits(packed_interpolation_mode,
1949 register_idx * WINED3D_PACKED_INTERPOLATION_BIT_COUNT, WINED3D_PACKED_INTERPOLATION_BIT_COUNT);
1952 static void shader_glsl_declare_shader_inputs(const struct wined3d_gl_info *gl_info,
1953 struct wined3d_string_buffer *buffer, unsigned int element_count,
1954 const uint32_t *interpolation_mode, BOOL unroll)
1956 enum wined3d_shader_interpolation_mode mode;
1957 unsigned int i;
1959 if (shader_glsl_use_interface_blocks(gl_info))
1961 if (unroll)
1963 shader_addline(buffer, "in shader_in_out {\n");
1964 for (i = 0; i < element_count; ++i)
1966 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
1967 shader_addline(buffer, " %svec4 reg%u;\n", shader_glsl_interpolation_qualifiers(mode), i);
1969 shader_addline(buffer, "} shader_in;\n");
1971 else
1973 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in;\n", element_count);
1976 else
1978 declare_in_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
1982 static BOOL needs_interpolation_qualifiers_for_shader_outputs(const struct wined3d_gl_info *gl_info)
1984 /* In GLSL 4.40+ it is fine to specify interpolation qualifiers only in
1985 * fragment shaders. In older GLSL versions interpolation qualifiers must
1986 * match between shader stages. */
1987 return gl_info->glsl_version < MAKEDWORD_VERSION(4, 40);
1990 static void shader_glsl_declare_shader_outputs(const struct wined3d_gl_info *gl_info,
1991 struct wined3d_string_buffer *buffer, unsigned int element_count, BOOL rasterizer_setup,
1992 const uint32_t *interpolation_mode)
1994 enum wined3d_shader_interpolation_mode mode;
1995 unsigned int i;
1997 if (shader_glsl_use_interface_blocks(gl_info))
1999 if (rasterizer_setup)
2001 shader_addline(buffer, "out shader_in_out {\n");
2002 for (i = 0; i < element_count; ++i)
2004 const char *interpolation_qualifiers = "";
2005 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
2007 mode = wined3d_extract_interpolation_mode(interpolation_mode, i);
2008 interpolation_qualifiers = shader_glsl_interpolation_qualifiers(mode);
2010 shader_addline(buffer, " %svec4 reg%u;\n", interpolation_qualifiers, i);
2012 shader_addline(buffer, "} shader_out;\n");
2014 else
2016 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out;\n", element_count);
2019 else
2021 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", element_count);
2025 static BOOL use_legacy_fragment_output(const struct wined3d_gl_info *gl_info)
2027 /* Technically 1.30 does support user-defined fragment shader outputs but
2028 * we might not have glBindFragDataLocation() available (i.e. GL version
2029 * might be < 3.0). */
2030 return gl_info->glsl_version <= MAKEDWORD_VERSION(1, 30);
2033 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
2035 return use_legacy_fragment_output(gl_info) ? "gl_FragData" : "ps_out";
2038 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
2040 switch (primitive_type)
2042 case WINED3D_PT_POINTLIST:
2043 return "points";
2045 case WINED3D_PT_LINELIST:
2046 return "lines";
2048 case WINED3D_PT_LINESTRIP:
2049 return "line_strip";
2051 case WINED3D_PT_TRIANGLELIST:
2052 return "triangles";
2054 case WINED3D_PT_TRIANGLESTRIP:
2055 return "triangle_strip";
2057 case WINED3D_PT_LINELIST_ADJ:
2058 return "lines_adjacency";
2060 case WINED3D_PT_TRIANGLELIST_ADJ:
2061 return "triangles_adjacency";
2063 default:
2064 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
2065 return "";
2069 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
2071 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
2072 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
2073 DWORD input_reg_used = shader->u.ps.input_reg_used;
2074 unsigned int i;
2076 if (reg_maps->shader_version.major < 3)
2077 return input_reg_used & (1u << idx);
2079 for (i = 0; i < input_signature->element_count; ++i)
2081 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
2083 if (!(reg_maps->input_registers & (1u << input->register_idx)))
2084 continue;
2086 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
2087 && input->semantic_idx == idx)
2088 return input_reg_used & (1u << input->register_idx);
2090 return FALSE;
2093 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
2094 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
2096 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
2098 if (version->major >= 4)
2099 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
2100 else
2101 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
2104 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
2105 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
2106 unsigned int index)
2108 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
2109 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
2110 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
2111 index, scalar_type, scalar_type, index);
2114 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
2115 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
2117 unsigned int index = e->register_idx;
2118 enum wined3d_component_type type;
2120 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
2122 shader_addline(buffer, "uniform int base_vertex_id;\n");
2123 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID - base_vertex_id), 0.0, 0.0, 0.0);\n", index);
2124 return;
2126 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
2128 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
2129 index);
2130 return;
2132 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
2133 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
2135 if (shader_glsl_use_explicit_attrib_location(gl_info))
2136 shader_addline(buffer, "layout(location = %u) ", index);
2138 type = e->component_type;
2139 if ((unsigned int)type >= ARRAY_SIZE(component_type_info))
2141 FIXME("Unhandled type %#x.\n", type);
2142 type = WINED3D_TYPE_FLOAT;
2144 if (type == WINED3D_TYPE_FLOAT || type == WINED3D_TYPE_UNKNOWN)
2145 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
2146 else
2147 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info,
2148 component_type_info[type].glsl_vector_type,
2149 component_type_info[type].glsl_scalar_type, index);
2152 /** Generate the variable & register declarations for the GLSL output target */
2153 static void shader_generate_glsl_declarations(const struct wined3d_context_gl *context_gl,
2154 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
2155 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
2157 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2158 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
2159 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
2160 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
2161 const struct wined3d_shader_indexable_temp *idx_temp_reg;
2162 unsigned int uniform_block_base, uniform_block_count;
2163 enum wined3d_shader_resource_type resource_type;
2164 const struct wined3d_shader_lconst *lconst;
2165 const char *prefix;
2166 unsigned int i;
2167 uint32_t map;
2169 if (wined3d_settings.strict_shader_math)
2170 shader_addline(buffer, "#pragma optionNV(fastmath off)\n");
2172 prefix = shader_glsl_get_prefix(version->type);
2174 /* Prototype the subroutines */
2175 map = reg_maps->labels;
2176 while (map)
2178 i = wined3d_bit_scan(&map);
2179 shader_addline(buffer, "void subroutine%u();\n", i);
2182 if (version->type != WINED3D_SHADER_TYPE_PIXEL && version->type != WINED3D_SHADER_TYPE_COMPUTE)
2184 if (version->type == WINED3D_SHADER_TYPE_HULL)
2185 shader_addline(buffer, "out gl_PerVertex { invariant vec4 gl_Position; } gl_out[];\n");
2186 else
2187 shader_addline(buffer, "invariant gl_Position;\n");
2190 /* Declare the constants (aka uniforms) */
2191 if (shader->limits->constant_float > 0)
2193 unsigned max_constantsF;
2195 /* Unless the shader uses indirect addressing, always declare the
2196 * maximum array size and ignore that we need some uniforms privately.
2197 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
2198 * and immediate values, still declare VC[256]. If the shader needs
2199 * more uniforms than we have it won't work in any case. If it uses
2200 * less, the compiler will figure out which uniforms are really used
2201 * and strip them out. This allows a shader to use c255 on a dx9 card,
2202 * as long as it doesn't also use all the other constants.
2204 * If the shader uses indirect addressing the compiler must assume
2205 * that all declared uniforms are used. In this case, declare only the
2206 * amount that we're assured to have.
2208 * Thus we run into problems in these two cases:
2209 * 1) The shader really uses more uniforms than supported.
2210 * 2) The shader uses indirect addressing, less constants than
2211 * supported, but uses a constant index > #supported consts. */
2212 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2214 /* No indirect addressing here. */
2215 max_constantsF = gl_info->limits.glsl_ps_float_constants;
2217 else
2219 if (reg_maps->usesrelconstF)
2221 /* Subtract the other potential uniforms from the max
2222 * available (bools, ints, and 1 row of projection matrix).
2223 * Subtract another uniform for immediate values, which have
2224 * to be loaded via uniform by the driver as well. The shader
2225 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2226 * shader code, so one vec4 should be enough. (Unfortunately
2227 * the Nvidia driver doesn't store 128 and -128 in one float).
2229 * Writing gl_ClipVertex requires one uniform for each
2230 * clipplane as well. */
2231 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2232 if (vs_args->clip_enabled)
2233 max_constantsF -= gl_info->limits.user_clip_distances;
2234 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2235 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2236 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2237 * for now take this into account when calculating the number of available constants
2239 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2240 /* Set by driver quirks in directx.c */
2241 max_constantsF -= gl_info->reserved_glsl_constants;
2243 if (max_constantsF < shader->limits->constant_float)
2245 static unsigned int once;
2247 if (!once++)
2248 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2249 " it may not render correctly.\n");
2250 else
2251 WARN("The hardware does not support enough uniform components to run this shader.\n");
2254 else
2256 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2259 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2260 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2263 /* Always declare the full set of constants, the compiler can remove the
2264 * unused ones because d3d doesn't (yet) support indirect int and bool
2265 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2266 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2267 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2269 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2270 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2272 /* Declare immediate constant buffer */
2273 if (reg_maps->icb)
2274 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2276 /* Declare constant buffers */
2277 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, version->type,
2278 &uniform_block_base, &uniform_block_count);
2279 for (i = 0; i < min(uniform_block_count, WINED3D_MAX_CBS); ++i)
2281 if (reg_maps->cb_sizes[i])
2283 shader_addline(buffer, "layout(std140");
2284 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2285 shader_addline(buffer, ", binding = %u", uniform_block_base + i);
2286 shader_addline(buffer, ") uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2287 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2291 /* Declare texture samplers */
2292 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2294 struct wined3d_shader_sampler_map_entry *entry;
2295 const char *sampler_type_prefix, *sampler_type;
2296 BOOL shadow_sampler, tex_rect;
2298 entry = &reg_maps->sampler_map.entries[i];
2300 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2302 ERR("Invalid resource index %u.\n", entry->resource_idx);
2303 continue;
2306 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2308 case WINED3D_DATA_FLOAT:
2309 case WINED3D_DATA_UNORM:
2310 case WINED3D_DATA_SNORM:
2311 sampler_type_prefix = "";
2312 break;
2314 case WINED3D_DATA_INT:
2315 sampler_type_prefix = "i";
2316 break;
2318 case WINED3D_DATA_UINT:
2319 sampler_type_prefix = "u";
2320 break;
2322 default:
2323 sampler_type_prefix = "";
2324 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2325 break;
2328 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2329 resource_type = version->type == WINED3D_SHADER_TYPE_PIXEL
2330 ? pixelshader_get_resource_type(reg_maps, entry->resource_idx, ps_args->tex_types)
2331 : reg_maps->resource_info[entry->resource_idx].type;
2333 switch (resource_type)
2335 case WINED3D_SHADER_RESOURCE_BUFFER:
2336 sampler_type = "samplerBuffer";
2337 break;
2339 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2340 if (shadow_sampler)
2341 sampler_type = "sampler1DShadow";
2342 else
2343 sampler_type = "sampler1D";
2344 break;
2346 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2347 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2348 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2349 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2350 if (shadow_sampler)
2352 if (tex_rect)
2353 sampler_type = "sampler2DRectShadow";
2354 else
2355 sampler_type = "sampler2DShadow";
2357 else
2359 if (tex_rect)
2360 sampler_type = "sampler2DRect";
2361 else
2362 sampler_type = "sampler2D";
2364 break;
2366 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2367 if (shadow_sampler)
2368 FIXME("Unsupported 3D shadow sampler.\n");
2369 sampler_type = "sampler3D";
2370 break;
2372 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2373 if (shadow_sampler)
2374 sampler_type = "samplerCubeShadow";
2375 else
2376 sampler_type = "samplerCube";
2377 break;
2379 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2380 if (shadow_sampler)
2381 sampler_type = "sampler1DArrayShadow";
2382 else
2383 sampler_type = "sampler1DArray";
2384 break;
2386 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2387 if (shadow_sampler)
2388 sampler_type = "sampler2DArrayShadow";
2389 else
2390 sampler_type = "sampler2DArray";
2391 break;
2393 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2394 if (shadow_sampler)
2395 sampler_type = "samplerCubeArrayShadow";
2396 else
2397 sampler_type = "samplerCubeArray";
2398 break;
2400 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMS:
2401 sampler_type = "sampler2DMS";
2402 break;
2404 case WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY:
2405 sampler_type = "sampler2DMSArray";
2406 break;
2408 default:
2409 sampler_type = "unsupported_sampler";
2410 FIXME("Unhandled resource type %#x.\n", resource_type);
2411 break;
2414 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2415 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, version, entry->bind_idx);
2416 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2417 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2420 /* Declare images */
2421 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2423 const char *image_type_prefix, *image_type, *read_format;
2425 if (!reg_maps->uav_resource_info[i].type)
2426 continue;
2428 switch (reg_maps->uav_resource_info[i].data_type)
2430 case WINED3D_DATA_FLOAT:
2431 case WINED3D_DATA_UNORM:
2432 case WINED3D_DATA_SNORM:
2433 image_type_prefix = "";
2434 read_format = "r32f";
2435 break;
2437 case WINED3D_DATA_INT:
2438 image_type_prefix = "i";
2439 read_format = "r32i";
2440 break;
2442 case WINED3D_DATA_UINT:
2443 image_type_prefix = "u";
2444 read_format = "r32ui";
2445 break;
2447 default:
2448 image_type_prefix = "";
2449 read_format = "";
2450 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2451 break;
2454 switch (reg_maps->uav_resource_info[i].type)
2456 case WINED3D_SHADER_RESOURCE_BUFFER:
2457 image_type = "imageBuffer";
2458 break;
2460 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2461 image_type = "image1D";
2462 break;
2464 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2465 image_type = "image2D";
2466 break;
2468 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2469 image_type = "image3D";
2470 break;
2472 case WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY:
2473 image_type = "image1DArray";
2474 break;
2476 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2477 image_type = "image2DArray";
2478 break;
2480 default:
2481 image_type = "unsupported_image";
2482 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2483 break;
2486 if (shader_glsl_use_layout_binding_qualifier(gl_info))
2487 shader_addline(buffer, "layout(binding = %u)\n", i);
2488 if (reg_maps->uav_read_mask & (1u << i))
2489 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2490 read_format, image_type_prefix, image_type, prefix, i);
2491 else
2492 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2493 image_type_prefix, image_type, prefix, i);
2495 if (reg_maps->uav_counter_mask & (1u << i))
2496 shader_addline(buffer, "layout(binding = %u) uniform atomic_uint %s_counter%u;\n",
2497 i, prefix, i);
2500 /* Declare address variables */
2501 map = reg_maps->address;
2502 while (map)
2504 i = wined3d_bit_scan(&map);
2505 shader_addline(buffer, "ivec4 A%u;\n", i);
2508 /* Declare output register temporaries */
2509 if (shader->limits->packed_output)
2510 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2512 /* Declare temporary variables */
2513 if (reg_maps->temporary_count)
2515 for (i = 0; i < reg_maps->temporary_count; ++i)
2516 shader_addline(buffer, "vec4 R%u;\n", i);
2518 else if (version->major < 4)
2520 map = reg_maps->temporary;
2521 while (map)
2523 i = wined3d_bit_scan(&map);
2524 shader_addline(buffer, "vec4 R%u;\n", i);
2528 /* Declare indexable temporary variables */
2529 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2531 if (idx_temp_reg->component_count != 4)
2532 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2533 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2536 /* Declare loop registers aLx */
2537 if (version->major < 4)
2539 for (i = 0; i < reg_maps->loop_depth; ++i)
2541 shader_addline(buffer, "int aL%u;\n", i);
2542 shader_addline(buffer, "int tmpInt%u;\n", i);
2546 /* Temporary variables for matrix operations */
2547 shader_addline(buffer, "vec4 tmp0;\n");
2548 shader_addline(buffer, "vec4 tmp1;\n");
2549 if (gl_info->supported[ARB_GPU_SHADER5])
2550 shader_addline(buffer, "precise vec4 tmp_precise[2];\n");
2551 else
2552 shader_addline(buffer, "/* precise */ vec4 tmp_precise[2];\n");
2554 if (!shader->load_local_constsF)
2556 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2558 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2559 shader_glsl_append_imm_vec(buffer, (const float *)lconst->value, ARRAY_SIZE(lconst->value), gl_info);
2560 shader_addline(buffer, ";\n");
2565 /* Prototypes */
2566 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
2567 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2568 enum wined3d_data_type data_type);
2570 /** Used for opcode modifiers - They multiply the result by the specified amount */
2571 static const char * const shift_glsl_tab[] = {
2572 "", /* 0 (none) */
2573 "2.0 * ", /* 1 (x2) */
2574 "4.0 * ", /* 2 (x4) */
2575 "8.0 * ", /* 3 (x8) */
2576 "16.0 * ", /* 4 (x16) */
2577 "32.0 * ", /* 5 (x32) */
2578 "", /* 6 (x64) */
2579 "", /* 7 (x128) */
2580 "", /* 8 (d256) */
2581 "", /* 9 (d128) */
2582 "", /* 10 (d64) */
2583 "", /* 11 (d32) */
2584 "0.0625 * ", /* 12 (d16) */
2585 "0.125 * ", /* 13 (d8) */
2586 "0.25 * ", /* 14 (d4) */
2587 "0.5 * " /* 15 (d2) */
2590 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2591 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2592 const char *in_reg, const char *in_regswizzle, char *out_str)
2594 switch (src_modifier)
2596 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2597 case WINED3DSPSM_DW:
2598 case WINED3DSPSM_NONE:
2599 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2600 break;
2601 case WINED3DSPSM_NEG:
2602 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2603 break;
2604 case WINED3DSPSM_NOT:
2605 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2606 break;
2607 case WINED3DSPSM_BIAS:
2608 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2609 break;
2610 case WINED3DSPSM_BIASNEG:
2611 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2612 break;
2613 case WINED3DSPSM_SIGN:
2614 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2615 break;
2616 case WINED3DSPSM_SIGNNEG:
2617 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2618 break;
2619 case WINED3DSPSM_COMP:
2620 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2621 break;
2622 case WINED3DSPSM_X2:
2623 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2624 break;
2625 case WINED3DSPSM_X2NEG:
2626 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2627 break;
2628 case WINED3DSPSM_ABS:
2629 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2630 break;
2631 case WINED3DSPSM_ABSNEG:
2632 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2633 break;
2634 default:
2635 FIXME("Unhandled modifier %u\n", src_modifier);
2636 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2640 static void shader_glsl_fixup_scalar_register_variable(struct wined3d_string_buffer *register_name,
2641 const char *glsl_variable, const struct wined3d_gl_info *gl_info)
2643 /* The ARB_shading_language_420pack extension allows swizzle operations on
2644 * scalars. */
2645 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2646 string_buffer_sprintf(register_name, "%s", glsl_variable);
2647 else
2648 string_buffer_sprintf(register_name, "ivec2(%s, 0)", glsl_variable);
2651 /** Writes the GLSL variable name that corresponds to the register that the
2652 * DX opcode parameter is trying to access */
2653 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2654 enum wined3d_data_type data_type, struct wined3d_string_buffer *register_name,
2655 BOOL *is_swizzled, const struct wined3d_shader_context *ctx)
2657 /* oPos, oFog and oPts in D3D */
2658 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2660 const struct wined3d_shader *shader = ctx->shader;
2661 const struct wined3d_shader_reg_maps *reg_maps = ctx->reg_maps;
2662 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2663 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2664 const char *prefix = shader_glsl_get_prefix(version->type);
2665 const struct wined3d_gl_info *gl_info = priv->gl_info;
2666 struct glsl_src_param rel_param0, rel_param1;
2668 if (reg->idx[0].offset != ~0u && reg->idx[0].rel_addr)
2669 shader_glsl_add_src_param_ext(ctx, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0,
2670 &rel_param0, reg->idx[0].rel_addr->reg.data_type);
2671 if (reg->idx[1].offset != ~0u && reg->idx[1].rel_addr)
2672 shader_glsl_add_src_param_ext(ctx, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0,
2673 &rel_param1, reg->idx[1].rel_addr->reg.data_type);
2674 if (is_swizzled)
2675 *is_swizzled = FALSE;
2677 switch (reg->type)
2679 case WINED3DSPR_TEMP:
2680 string_buffer_sprintf(register_name, "R%u", reg->idx[0].offset);
2681 break;
2683 case WINED3DSPR_INPUT:
2684 case WINED3DSPR_INCONTROLPOINT:
2685 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2687 if (reg->idx[0].rel_addr)
2688 FIXME("VS3 input registers relative addressing.\n");
2689 if (is_swizzled && priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2690 *is_swizzled = TRUE;
2691 if (reg->idx[0].rel_addr)
2693 string_buffer_sprintf(register_name, "%s_in[%s + %u]",
2694 prefix, rel_param0.param_str, reg->idx[0].offset);
2696 else
2698 string_buffer_sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2700 break;
2703 if (version->type == WINED3D_SHADER_TYPE_HULL
2704 || version->type == WINED3D_SHADER_TYPE_DOMAIN
2705 || version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2707 if (reg->idx[0].rel_addr)
2709 if (reg->idx[1].rel_addr)
2710 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%s + %u]",
2711 rel_param0.param_str, reg->idx[0].offset,
2712 rel_param1.param_str, reg->idx[1].offset);
2713 else
2714 string_buffer_sprintf(register_name, "shader_in[%s + %u].reg[%u]",
2715 rel_param0.param_str, reg->idx[0].offset,
2716 reg->idx[1].offset);
2718 else if (reg->idx[1].rel_addr)
2719 string_buffer_sprintf(register_name, "shader_in[%u].reg[%s + %u]", reg->idx[0].offset,
2720 rel_param1.param_str, reg->idx[1].offset);
2721 else
2722 string_buffer_sprintf(register_name, "shader_in[%u].reg[%u]",
2723 reg->idx[0].offset, reg->idx[1].offset);
2724 break;
2727 /* pixel shaders >= 3.0 */
2728 if (version->major >= 3)
2730 unsigned int idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2731 unsigned int in_count = vec4_varyings(version->major, gl_info);
2733 if (reg->idx[0].rel_addr)
2735 /* Removing a + 0 would be an obvious optimization, but
2736 * OS X doesn't see the NOP operation there. */
2737 if (idx)
2739 if (needs_legacy_glsl_syntax(gl_info)
2740 && shader->u.ps.declared_in_count > in_count)
2742 string_buffer_sprintf(register_name,
2743 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2744 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2745 prefix, rel_param0.param_str, idx);
2747 else
2749 string_buffer_sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2752 else
2754 if (needs_legacy_glsl_syntax(gl_info)
2755 && shader->u.ps.declared_in_count > in_count)
2757 string_buffer_sprintf(register_name,
2758 "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2759 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2760 prefix, rel_param0.param_str);
2762 else
2764 string_buffer_sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2768 else
2770 if (idx == in_count)
2771 string_buffer_sprintf(register_name, "gl_Color");
2772 else if (idx == in_count + 1)
2773 string_buffer_sprintf(register_name, "gl_SecondaryColor");
2774 else
2775 string_buffer_sprintf(register_name, "%s_in[%u]", prefix, idx);
2778 else
2780 if (!reg->idx[0].offset)
2781 string_buffer_sprintf(register_name, "ffp_varying_diffuse");
2782 else
2783 string_buffer_sprintf(register_name, "ffp_varying_specular");
2784 break;
2786 break;
2788 case WINED3DSPR_CONST:
2790 /* Relative addressing */
2791 if (reg->idx[0].rel_addr)
2793 if (wined3d_settings.check_float_constants)
2794 string_buffer_sprintf(register_name,
2795 "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2796 rel_param0.param_str, reg->idx[0].offset,
2797 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2798 prefix, rel_param0.param_str, reg->idx[0].offset);
2799 else if (reg->idx[0].offset)
2800 string_buffer_sprintf(register_name, "%s_c[%s + %u]",
2801 prefix, rel_param0.param_str, reg->idx[0].offset);
2802 else
2803 string_buffer_sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2805 else
2807 if (shader_constant_is_local(shader, reg->idx[0].offset))
2808 string_buffer_sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2809 else
2810 string_buffer_sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2813 break;
2815 case WINED3DSPR_CONSTINT:
2816 string_buffer_sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2817 break;
2819 case WINED3DSPR_CONSTBOOL:
2820 string_buffer_sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2821 break;
2823 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2824 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2825 string_buffer_sprintf(register_name, "T%u", reg->idx[0].offset);
2826 else
2827 string_buffer_sprintf(register_name, "A%u", reg->idx[0].offset);
2828 break;
2830 case WINED3DSPR_LOOP:
2831 string_buffer_sprintf(register_name, "aL%u", ctx->state->current_loop_reg - 1);
2832 break;
2834 case WINED3DSPR_SAMPLER:
2835 string_buffer_sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2836 break;
2838 case WINED3DSPR_COLOROUT:
2839 if (reg->idx[0].offset >= gl_info->limits.buffers)
2840 WARN("Write to render target %u, only %d supported.\n",
2841 reg->idx[0].offset, gl_info->limits.buffers);
2843 string_buffer_sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2844 break;
2846 case WINED3DSPR_RASTOUT:
2847 string_buffer_sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2848 break;
2850 case WINED3DSPR_DEPTHOUT:
2851 case WINED3DSPR_DEPTHOUTGE:
2852 case WINED3DSPR_DEPTHOUTLE:
2853 string_buffer_sprintf(register_name, "gl_FragDepth");
2854 break;
2856 case WINED3DSPR_ATTROUT:
2857 if (!reg->idx[0].offset)
2858 string_buffer_sprintf(register_name, "%s_out[8]", prefix);
2859 else
2860 string_buffer_sprintf(register_name, "%s_out[9]", prefix);
2861 break;
2863 case WINED3DSPR_TEXCRDOUT:
2864 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2865 if (reg->idx[0].rel_addr)
2866 string_buffer_sprintf(register_name, "%s_out[%s + %u]",
2867 prefix, rel_param0.param_str, reg->idx[0].offset);
2868 else
2869 string_buffer_sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2870 break;
2872 case WINED3DSPR_MISCTYPE:
2873 if (!reg->idx[0].offset)
2875 /* vPos */
2876 string_buffer_sprintf(register_name, "vpos");
2878 else if (reg->idx[0].offset == 1)
2880 /* Note that gl_FrontFacing is a bool, while vFace is
2881 * a float for which the sign determines front/back */
2882 string_buffer_sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2884 else
2886 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2887 string_buffer_sprintf(register_name, "unrecognized_register");
2889 break;
2891 case WINED3DSPR_IMMCONST:
2892 switch (reg->immconst_type)
2894 case WINED3D_IMMCONST_SCALAR:
2895 switch (data_type)
2897 case WINED3D_DATA_UNORM:
2898 case WINED3D_DATA_SNORM:
2899 case WINED3D_DATA_FLOAT:
2900 string_buffer_clear(register_name);
2901 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 1, gl_info);
2902 break;
2903 case WINED3D_DATA_INT:
2904 string_buffer_sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2905 break;
2906 case WINED3D_DATA_RESOURCE:
2907 case WINED3D_DATA_SAMPLER:
2908 case WINED3D_DATA_UINT:
2909 string_buffer_sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2910 break;
2911 default:
2912 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2913 break;
2915 break;
2917 case WINED3D_IMMCONST_VEC4:
2918 switch (data_type)
2920 case WINED3D_DATA_UNORM:
2921 case WINED3D_DATA_SNORM:
2922 case WINED3D_DATA_FLOAT:
2923 string_buffer_clear(register_name);
2924 shader_glsl_append_imm_vec(register_name, (const float *)reg->u.immconst_data, 4, gl_info);
2925 break;
2926 case WINED3D_DATA_INT:
2927 string_buffer_sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2928 reg->u.immconst_data[0], reg->u.immconst_data[1],
2929 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2930 break;
2931 case WINED3D_DATA_RESOURCE:
2932 case WINED3D_DATA_SAMPLER:
2933 case WINED3D_DATA_UINT:
2934 string_buffer_sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2935 reg->u.immconst_data[0], reg->u.immconst_data[1],
2936 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2937 break;
2938 default:
2939 string_buffer_sprintf(register_name, "<unhandled data type %#x>", data_type);
2940 break;
2942 break;
2944 default:
2945 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2946 string_buffer_sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2948 break;
2950 case WINED3DSPR_CONSTBUFFER:
2951 if (reg->idx[1].rel_addr)
2952 string_buffer_sprintf(register_name, "%s_cb%u[%s + %u]",
2953 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2954 else
2955 string_buffer_sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2956 break;
2958 case WINED3DSPR_IMMCONSTBUFFER:
2959 if (reg->idx[0].rel_addr)
2960 string_buffer_sprintf(register_name, "%s_icb[%s + %u]",
2961 prefix, rel_param0.param_str, reg->idx[0].offset);
2962 else
2963 string_buffer_sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2964 break;
2966 case WINED3DSPR_PRIMID:
2967 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2968 string_buffer_sprintf(register_name, "gl_PrimitiveIDIn");
2969 else
2970 string_buffer_sprintf(register_name, "gl_PrimitiveID");
2971 break;
2973 case WINED3DSPR_IDXTEMP:
2974 if (reg->idx[1].rel_addr)
2975 string_buffer_sprintf(register_name, "X%u[%s + %u]",
2976 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2977 else
2978 string_buffer_sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2979 break;
2981 case WINED3DSPR_LOCALTHREADINDEX:
2982 shader_glsl_fixup_scalar_register_variable(register_name,
2983 "int(gl_LocalInvocationIndex)", gl_info);
2984 break;
2986 case WINED3DSPR_GSINSTID:
2987 case WINED3DSPR_OUTPOINTID:
2988 shader_glsl_fixup_scalar_register_variable(register_name,
2989 "gl_InvocationID", gl_info);
2990 break;
2992 case WINED3DSPR_THREADID:
2993 string_buffer_sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2994 break;
2996 case WINED3DSPR_THREADGROUPID:
2997 string_buffer_sprintf(register_name, "ivec3(gl_WorkGroupID)");
2998 break;
3000 case WINED3DSPR_LOCALTHREADID:
3001 string_buffer_sprintf(register_name, "ivec3(gl_LocalInvocationID)");
3002 break;
3004 case WINED3DSPR_FORKINSTID:
3005 case WINED3DSPR_JOININSTID:
3006 shader_glsl_fixup_scalar_register_variable(register_name,
3007 "phase_instance_id", gl_info);
3008 break;
3010 case WINED3DSPR_TESSCOORD:
3011 string_buffer_sprintf(register_name, "gl_TessCoord");
3012 break;
3014 case WINED3DSPR_OUTCONTROLPOINT:
3015 if (reg->idx[0].rel_addr)
3017 if (reg->idx[1].rel_addr)
3018 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%s + %u]",
3019 rel_param0.param_str, reg->idx[0].offset,
3020 rel_param1.param_str, reg->idx[1].offset);
3021 else
3022 string_buffer_sprintf(register_name, "shader_out[%s + %u].reg[%u]",
3023 rel_param0.param_str, reg->idx[0].offset,
3024 reg->idx[1].offset);
3026 else if (reg->idx[1].rel_addr)
3028 string_buffer_sprintf(register_name, "shader_out[%u].reg[%s + %u]",
3029 reg->idx[0].offset, rel_param1.param_str,
3030 reg->idx[1].offset);
3032 else
3034 string_buffer_sprintf(register_name, "shader_out[%u].reg[%u]",
3035 reg->idx[0].offset, reg->idx[1].offset);
3037 break;
3039 case WINED3DSPR_PATCHCONST:
3040 if (version->type == WINED3D_SHADER_TYPE_HULL)
3041 string_buffer_sprintf(register_name, "hs_out[%u]", reg->idx[0].offset);
3042 else
3043 string_buffer_sprintf(register_name, "vpc[%u]", reg->idx[0].offset);
3044 break;
3046 case WINED3DSPR_COVERAGE:
3047 string_buffer_sprintf(register_name, "gl_SampleMaskIn[0]");
3048 break;
3050 case WINED3DSPR_SAMPLEMASK:
3051 string_buffer_sprintf(register_name, "sample_mask");
3052 break;
3054 default:
3055 FIXME("Unhandled register type %#x.\n", reg->type);
3056 string_buffer_sprintf(register_name, "unrecognised_register");
3057 break;
3061 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
3063 *str++ = '.';
3064 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
3065 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
3066 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
3067 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
3068 *str = '\0';
3071 /* Get the GLSL write mask for the destination register */
3072 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
3074 DWORD mask = param->write_mask;
3076 if (shader_is_scalar(&param->reg))
3078 mask = WINED3DSP_WRITEMASK_0;
3079 *write_mask = '\0';
3081 else
3083 shader_glsl_write_mask_to_str(mask, write_mask);
3086 return mask;
3089 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
3091 unsigned int size = 0;
3093 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
3094 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
3095 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
3096 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
3098 return size;
3101 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
3102 unsigned int component_idx)
3104 /* swizzle bits fields: wwzzyyxx */
3105 return (swizzle >> (2 * component_idx)) & 0x3;
3108 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
3110 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
3111 * but addressed as "rgba". To fix this we need to swap the register's x
3112 * and z components. */
3113 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
3114 unsigned int i;
3116 *str++ = '.';
3117 for (i = 0; i < 4; ++i)
3119 if (mask & (WINED3DSP_WRITEMASK_0 << i))
3120 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
3122 *str = '\0';
3125 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
3126 BOOL fixup, DWORD mask, char *swizzle_str)
3128 if (shader_is_scalar(&param->reg))
3129 *swizzle_str = '\0';
3130 else
3131 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
3134 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
3135 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type, unsigned int size)
3137 if (dst_data_type == WINED3D_DATA_UNORM || dst_data_type == WINED3D_DATA_SNORM)
3138 dst_data_type = WINED3D_DATA_FLOAT;
3139 if (src_data_type == WINED3D_DATA_UNORM || src_data_type == WINED3D_DATA_SNORM)
3140 src_data_type = WINED3D_DATA_FLOAT;
3142 if (dst_data_type == src_data_type)
3144 string_buffer_sprintf(dst_param, "%s", src_param);
3145 return;
3148 if (src_data_type == WINED3D_DATA_FLOAT)
3150 switch (dst_data_type)
3152 case WINED3D_DATA_INT:
3153 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
3154 return;
3155 case WINED3D_DATA_RESOURCE:
3156 case WINED3D_DATA_SAMPLER:
3157 case WINED3D_DATA_UINT:
3158 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
3159 return;
3160 default:
3161 break;
3165 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
3167 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
3168 return;
3171 if (src_data_type == WINED3D_DATA_INT)
3173 switch (dst_data_type)
3175 case WINED3D_DATA_FLOAT:
3176 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
3177 return;
3178 case WINED3D_DATA_UINT:
3179 if (size == 1)
3180 string_buffer_sprintf(dst_param, "uint(%s)", src_param);
3181 else
3182 string_buffer_sprintf(dst_param, "uvec%u(%s)", size, src_param);
3183 return;
3184 default:
3185 break;
3189 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3190 string_buffer_sprintf(dst_param, "%s", src_param);
3193 /* From a given parameter token, generate the corresponding GLSL string.
3194 * Also, return the actual register name and swizzle in case the
3195 * caller needs this information as well. */
3196 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_context *ctx,
3197 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3198 enum wined3d_data_type data_type)
3200 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3201 struct wined3d_string_buffer *param_str = string_buffer_get(priv->string_buffers);
3202 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3203 enum wined3d_data_type param_data_type;
3204 BOOL is_color = FALSE;
3205 char swizzle_str[6];
3206 unsigned int size;
3208 glsl_src->param_str[0] = '\0';
3209 swizzle_str[0] = '\0';
3211 shader_glsl_get_register_name(&wined3d_src->reg, data_type, reg_name, &is_color, ctx);
3212 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3214 switch (wined3d_src->reg.type)
3216 case WINED3DSPR_IMMCONST:
3217 param_data_type = data_type;
3218 size = wined3d_src->reg.immconst_type == WINED3D_IMMCONST_SCALAR ? 1 : 4;
3219 break;
3220 case WINED3DSPR_FORKINSTID:
3221 case WINED3DSPR_GSINSTID:
3222 case WINED3DSPR_JOININSTID:
3223 case WINED3DSPR_LOCALTHREADINDEX:
3224 case WINED3DSPR_OUTPOINTID:
3225 case WINED3DSPR_PRIMID:
3226 param_data_type = WINED3D_DATA_INT;
3227 size = 1;
3228 break;
3229 case WINED3DSPR_LOCALTHREADID:
3230 case WINED3DSPR_THREADGROUPID:
3231 case WINED3DSPR_THREADID:
3232 param_data_type = WINED3D_DATA_INT;
3233 size = 3;
3234 break;
3235 default:
3236 param_data_type = WINED3D_DATA_FLOAT;
3237 size = 4;
3238 break;
3241 shader_glsl_sprintf_cast(param_str, reg_name->buffer, data_type, param_data_type, size);
3242 shader_glsl_gen_modifier(wined3d_src->modifiers, param_str->buffer, swizzle_str, glsl_src->param_str);
3244 string_buffer_release(priv->string_buffers, reg_name);
3245 string_buffer_release(priv->string_buffers, param_str);
3248 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3249 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3251 shader_glsl_add_src_param_ext(ins->ctx, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3254 /* From a given parameter token, generate the corresponding GLSL string.
3255 * Also, return the actual register name and swizzle in case the
3256 * caller needs this information as well. */
3257 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3258 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3260 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3261 struct wined3d_string_buffer *reg_name;
3262 size_t len;
3264 glsl_dst->mask_str[0] = '\0';
3266 reg_name = string_buffer_get(priv->string_buffers);
3267 shader_glsl_get_register_name(&wined3d_dst->reg, wined3d_dst->reg.data_type, reg_name, NULL, ins->ctx);
3268 len = min(reg_name->content_size, ARRAY_SIZE(glsl_dst->reg_name) - 1);
3269 memcpy(glsl_dst->reg_name, reg_name->buffer, len);
3270 glsl_dst->reg_name[len] = '\0';
3271 string_buffer_release(priv->string_buffers, reg_name);
3273 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3276 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3277 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3278 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3279 unsigned int dst_idx, enum wined3d_data_type data_type)
3281 struct glsl_dst_param glsl_dst;
3282 DWORD mask;
3284 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3286 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3287 sprintf(glsl_dst.reg_name, "tmp_precise[%u]", dst_idx);
3289 switch (data_type)
3291 case WINED3D_DATA_FLOAT:
3292 case WINED3D_DATA_UNORM:
3293 case WINED3D_DATA_SNORM:
3294 shader_addline(buffer, "%s%s = %s(",
3295 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3296 break;
3297 case WINED3D_DATA_INT:
3298 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3299 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3300 break;
3301 case WINED3D_DATA_RESOURCE:
3302 case WINED3D_DATA_SAMPLER:
3303 case WINED3D_DATA_UINT:
3304 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3305 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3306 break;
3307 default:
3308 FIXME("Unhandled data type %#x.\n", data_type);
3309 shader_addline(buffer, "%s%s = %s(",
3310 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3311 break;
3315 return mask;
3318 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3319 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3321 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3324 /** Process GLSL instruction modifiers */
3325 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3327 const struct wined3d_shader_dst_param *dst;
3328 struct glsl_dst_param dst_param;
3329 DWORD modifiers;
3330 unsigned int i;
3332 for (i = 0; i < ins->dst_count; ++i)
3334 if ((dst = &ins->dst[i])->reg.type == WINED3DSPR_NULL)
3335 continue;
3337 if (ins->flags & WINED3DSI_PRECISE_XYZW)
3339 shader_glsl_add_dst_param(ins, dst, &dst_param);
3340 shader_addline(ins->ctx->buffer, "%s%s = tmp_precise[%u]%s;\n",
3341 dst_param.reg_name, dst_param.mask_str, i, dst_param.mask_str);
3344 if (!(modifiers = dst->modifiers))
3345 continue;
3347 shader_glsl_add_dst_param(ins, dst, &dst_param);
3349 if (modifiers & WINED3DSPDM_SATURATE)
3351 /* _SAT means to clamp the value of the register to between 0 and 1 */
3352 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3353 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3356 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3358 FIXME("_centroid modifier not handled\n");
3361 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3363 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3368 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3370 switch (op)
3372 case WINED3D_SHADER_REL_OP_GT: return ">";
3373 case WINED3D_SHADER_REL_OP_EQ: return "==";
3374 case WINED3D_SHADER_REL_OP_GE: return ">=";
3375 case WINED3D_SHADER_REL_OP_LT: return "<";
3376 case WINED3D_SHADER_REL_OP_NE: return "!=";
3377 case WINED3D_SHADER_REL_OP_LE: return "<=";
3378 default:
3379 FIXME("Unrecognized operator %#x.\n", op);
3380 return "(\?\?)";
3384 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info)
3386 return shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3389 static void shader_glsl_get_coord_size(enum wined3d_shader_resource_type resource_type,
3390 unsigned int *coord_size, unsigned int *deriv_size)
3392 const BOOL is_array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3393 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3395 *coord_size = resource_type_info[resource_type].coord_size;
3396 *deriv_size = *coord_size;
3397 if (is_array)
3398 --(*deriv_size);
3401 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3402 DWORD resource_idx, DWORD sampler_idx, uint32_t flags, struct glsl_sample_function *sample_function)
3404 enum wined3d_shader_resource_type resource_type;
3405 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3406 const struct wined3d_gl_info *gl_info = priv->gl_info;
3407 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3408 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3409 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3410 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3411 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3412 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3413 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3414 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3415 const char *base = "texture", *type_part = "", *suffix = "";
3416 unsigned int coord_size, deriv_size;
3418 resource_type = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3419 ? pixelshader_get_resource_type(ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
3420 : ctx->reg_maps->resource_info[resource_idx].type;
3422 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3424 if (resource_type >= ARRAY_SIZE(resource_type_info))
3426 ERR("Unexpected resource type %#x.\n", resource_type);
3427 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3430 /* Note that there's no such thing as a projected cube texture. */
3431 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3432 projected = FALSE;
3434 if (needs_legacy_glsl_syntax(gl_info))
3436 if (shadow)
3437 base = "shadow";
3439 type_part = resource_type_info[resource_type].type_part;
3440 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3441 type_part = "2DRect";
3442 if (!type_part[0] && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY)
3443 FIXME("Unhandled resource type %#x.\n", resource_type);
3445 if (!lod && grad && !shader_glsl_has_core_grad(gl_info))
3447 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3448 suffix = "ARB";
3449 else
3450 FIXME("Unsupported grad function.\n");
3454 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3456 static const uint32_t texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3457 if (flags & ~texel_fetch_flags)
3458 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3460 base = "texelFetch";
3461 type_part = "";
3464 sample_function->name = string_buffer_get(priv->string_buffers);
3465 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3466 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3468 shader_glsl_get_coord_size(resource_type, &coord_size, &deriv_size);
3469 if (shadow)
3470 ++coord_size;
3471 sample_function->offset_size = offset ? deriv_size : 0;
3472 sample_function->coord_mask = wined3d_mask_from_size(coord_size);
3473 sample_function->deriv_mask = wined3d_mask_from_size(deriv_size);
3474 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3477 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3478 struct glsl_sample_function *sample_function)
3480 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3482 string_buffer_release(priv->string_buffers, sample_function->name);
3485 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3486 BOOL sign_fixup, enum fixup_channel_source channel_source)
3488 switch(channel_source)
3490 case CHANNEL_SOURCE_ZERO:
3491 strcat(arguments, "0.0");
3492 break;
3494 case CHANNEL_SOURCE_ONE:
3495 strcat(arguments, "1.0");
3496 break;
3498 case CHANNEL_SOURCE_X:
3499 strcat(arguments, reg_name);
3500 strcat(arguments, ".x");
3501 break;
3503 case CHANNEL_SOURCE_Y:
3504 strcat(arguments, reg_name);
3505 strcat(arguments, ".y");
3506 break;
3508 case CHANNEL_SOURCE_Z:
3509 strcat(arguments, reg_name);
3510 strcat(arguments, ".z");
3511 break;
3513 case CHANNEL_SOURCE_W:
3514 strcat(arguments, reg_name);
3515 strcat(arguments, ".w");
3516 break;
3518 default:
3519 FIXME("Unhandled channel source %#x\n", channel_source);
3520 strcat(arguments, "undefined");
3521 break;
3524 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3527 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3528 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3530 unsigned int mask_size, remaining;
3531 DWORD fixup_mask = 0;
3532 char arguments[256];
3533 char mask_str[6];
3535 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3536 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3537 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3538 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3539 if (!(mask &= fixup_mask))
3540 return;
3542 if (is_complex_fixup(fixup))
3544 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3545 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3546 return;
3549 shader_glsl_write_mask_to_str(mask, mask_str);
3550 mask_size = shader_glsl_get_write_mask_size(mask);
3552 arguments[0] = '\0';
3553 remaining = mask_size;
3554 if (mask & WINED3DSP_WRITEMASK_0)
3556 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3557 if (--remaining) strcat(arguments, ", ");
3559 if (mask & WINED3DSP_WRITEMASK_1)
3561 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3562 if (--remaining) strcat(arguments, ", ");
3564 if (mask & WINED3DSP_WRITEMASK_2)
3566 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3567 if (--remaining) strcat(arguments, ", ");
3569 if (mask & WINED3DSP_WRITEMASK_3)
3571 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3572 if (--remaining) strcat(arguments, ", ");
3575 if (mask_size > 1)
3576 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3577 else
3578 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3581 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3583 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3584 struct wined3d_string_buffer *reg_name;
3586 reg_name = string_buffer_get(priv->string_buffers);
3587 shader_glsl_get_register_name(&ins->dst[0].reg, ins->dst[0].reg.data_type, reg_name, NULL, ins->ctx);
3588 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name->buffer, ins->dst[0].write_mask, fixup);
3589 string_buffer_release(priv->string_buffers, reg_name);
3592 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3593 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3594 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3595 const char *coord_reg_fmt, ...)
3597 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3598 char dst_swizzle[6];
3599 struct color_fixup_desc fixup;
3600 BOOL np2_fixup = FALSE;
3601 va_list args;
3602 int ret;
3604 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3606 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3607 * We actually rely on it for vertex shaders and SM4+. */
3608 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3610 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3611 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3613 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3614 np2_fixup = TRUE;
3616 else
3618 fixup = COLOR_FIXUP_IDENTITY;
3621 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, sample_function->data_type);
3623 if (sample_function->output_single_component)
3624 shader_addline(ins->ctx->buffer, "vec4(");
3626 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3627 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3629 for (;;)
3631 va_start(args, coord_reg_fmt);
3632 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3633 va_end(args);
3634 if (!ret)
3635 break;
3636 if (!string_buffer_resize(ins->ctx->buffer, ret))
3637 break;
3640 if (np2_fixup)
3642 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3643 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3645 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3647 case 1:
3648 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3649 idx >> 1, (idx % 2) ? "z" : "x");
3650 break;
3651 case 2:
3652 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3653 idx >> 1, (idx % 2) ? "zw" : "xy");
3654 break;
3655 case 3:
3656 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3657 idx >> 1, (idx % 2) ? "zw" : "xy");
3658 break;
3659 case 4:
3660 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3661 idx >> 1, (idx % 2) ? "zw" : "xy");
3662 break;
3665 if (dx && dy)
3666 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3667 else if (bias)
3668 shader_addline(ins->ctx->buffer, ", %s", bias);
3669 if (sample_function->offset_size)
3671 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3672 shader_addline(ins->ctx->buffer, ", ");
3673 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3675 shader_addline(ins->ctx->buffer, ")");
3677 if (sample_function->output_single_component)
3678 shader_addline(ins->ctx->buffer, ")");
3680 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3682 if (!is_identity_fixup(fixup))
3683 shader_glsl_color_correction(ins, fixup);
3686 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer, BOOL use_viewport_index)
3688 /* Write the final position.
3690 * OpenGL coordinates specify the center of the pixel while D3D coords
3691 * specify the corner. The offsets are stored in z and w in
3692 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3693 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3694 * a MAD. */
3695 if (use_viewport_index)
3697 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup[gl_ViewportIndex].y;\n");
3698 shader_addline(buffer, "gl_Position.xy += pos_fixup[gl_ViewportIndex].zw * gl_Position.ww;\n");
3700 else
3702 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3703 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3706 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3707 * in utils.c
3709 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3710 * shaders are run before the homogeneous divide, so we have to take the w
3711 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3712 * z = z * 2 - w. */
3713 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3716 /*****************************************************************************
3717 * Begin processing individual instruction opcodes
3718 ****************************************************************************/
3720 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3722 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3723 struct glsl_src_param src0_param;
3724 struct glsl_src_param src1_param;
3725 DWORD write_mask;
3726 const char *op;
3728 /* Determine the GLSL operator to use based on the opcode */
3729 switch (ins->handler_idx)
3731 case WINED3DSIH_ADD: op = "+"; break;
3732 case WINED3DSIH_AND: op = "&"; break;
3733 case WINED3DSIH_DIV: op = "/"; break;
3734 case WINED3DSIH_IADD: op = "+"; break;
3735 case WINED3DSIH_ISHL: op = "<<"; break;
3736 case WINED3DSIH_ISHR: op = ">>"; break;
3737 case WINED3DSIH_MUL: op = "*"; break;
3738 case WINED3DSIH_OR: op = "|"; break;
3739 case WINED3DSIH_SUB: op = "-"; break;
3740 case WINED3DSIH_USHR: op = ">>"; break;
3741 case WINED3DSIH_XOR: op = "^"; break;
3742 default:
3743 op = "<unhandled operator>";
3744 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3745 break;
3748 write_mask = shader_glsl_append_dst(buffer, ins);
3749 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3750 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3751 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3754 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3756 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3757 struct glsl_src_param src0_param;
3758 struct glsl_src_param src1_param;
3759 unsigned int mask_size;
3760 DWORD write_mask;
3761 const char *op;
3763 write_mask = shader_glsl_append_dst(buffer, ins);
3764 mask_size = shader_glsl_get_write_mask_size(write_mask);
3765 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3766 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3768 if (mask_size > 1)
3770 switch (ins->handler_idx)
3772 case WINED3DSIH_EQ: op = "equal"; break;
3773 case WINED3DSIH_IEQ: op = "equal"; break;
3774 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3775 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3776 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3777 case WINED3DSIH_LT: op = "lessThan"; break;
3778 case WINED3DSIH_ILT: op = "lessThan"; break;
3779 case WINED3DSIH_ULT: op = "lessThan"; break;
3780 case WINED3DSIH_NE: op = "notEqual"; break;
3781 case WINED3DSIH_INE: op = "notEqual"; break;
3782 default:
3783 op = "<unhandled operator>";
3784 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3785 break;
3788 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3789 mask_size, op, src0_param.param_str, src1_param.param_str);
3791 else
3793 switch (ins->handler_idx)
3795 case WINED3DSIH_EQ: op = "=="; break;
3796 case WINED3DSIH_IEQ: op = "=="; break;
3797 case WINED3DSIH_GE: op = ">="; break;
3798 case WINED3DSIH_IGE: op = ">="; break;
3799 case WINED3DSIH_UGE: op = ">="; break;
3800 case WINED3DSIH_LT: op = "<"; break;
3801 case WINED3DSIH_ILT: op = "<"; break;
3802 case WINED3DSIH_ULT: op = "<"; break;
3803 case WINED3DSIH_NE: op = "!="; break;
3804 case WINED3DSIH_INE: op = "!="; break;
3805 default:
3806 op = "<unhandled operator>";
3807 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3808 break;
3811 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3812 src0_param.param_str, op, src1_param.param_str);
3816 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3818 struct glsl_src_param src_param;
3819 DWORD write_mask;
3820 const char *op;
3822 switch (ins->handler_idx)
3824 case WINED3DSIH_INEG: op = "-"; break;
3825 case WINED3DSIH_NOT: op = "~"; break;
3826 default:
3827 op = "<unhandled operator>";
3828 ERR("Unhandled opcode %s.\n",
3829 debug_d3dshaderinstructionhandler(ins->handler_idx));
3830 break;
3833 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3834 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3835 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3838 static void shader_glsl_mul_extended(const struct wined3d_shader_instruction *ins)
3840 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3841 struct glsl_src_param src0_param;
3842 struct glsl_src_param src1_param;
3843 DWORD write_mask;
3845 /* If we have ARB_gpu_shader5, we can use imulExtended() / umulExtended().
3846 * If not, we can emulate it. */
3847 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3848 FIXME("64-bit integer multiplies not implemented.\n");
3850 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3852 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3853 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3854 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3856 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3857 src0_param.param_str, src1_param.param_str);
3861 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3863 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3864 struct glsl_src_param src0_param, src1_param;
3865 DWORD write_mask;
3867 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3869 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3871 char dst_mask[6];
3873 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3874 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3875 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3876 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3877 dst_mask, src0_param.param_str, src1_param.param_str);
3879 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3880 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3881 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3882 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3884 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
3885 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3887 else
3889 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
3890 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3891 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3892 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3895 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3897 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
3898 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3899 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3900 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3904 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3905 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3907 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3908 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3909 const struct wined3d_gl_info *gl_info = priv->gl_info;
3910 struct glsl_src_param src0_param;
3911 DWORD write_mask;
3913 write_mask = shader_glsl_append_dst(buffer, ins);
3914 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3916 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3917 * shader versions WINED3DSIO_MOVA is used for this. */
3918 if (ins->ctx->reg_maps->shader_version.major == 1
3919 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3920 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3922 /* This is a simple floor() */
3923 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3924 if (mask_size > 1) {
3925 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3926 } else {
3927 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3930 else if (ins->handler_idx == WINED3DSIH_MOVA)
3932 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3934 if (shader_glsl_get_version(gl_info) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3936 if (mask_size > 1)
3937 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3938 else
3939 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3941 else
3943 if (mask_size > 1)
3944 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3945 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3946 else
3947 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3948 src0_param.param_str, src0_param.param_str);
3951 else
3953 shader_addline(buffer, "%s);\n", src0_param.param_str);
3957 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3958 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3960 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3961 struct glsl_src_param src0_param;
3962 struct glsl_src_param src1_param;
3963 DWORD dst_write_mask, src_write_mask;
3964 unsigned int dst_size;
3966 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3967 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3969 /* dp4 works on vec4, dp3 on vec3, etc. */
3970 if (ins->handler_idx == WINED3DSIH_DP4)
3971 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3972 else if (ins->handler_idx == WINED3DSIH_DP3)
3973 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3974 else
3975 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3977 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3978 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3980 if (dst_size > 1) {
3981 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3982 } else {
3983 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3987 /* Note that this instruction has some restrictions. The destination write mask
3988 * can't contain the w component, and the source swizzles have to be .xyzw */
3989 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3991 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3992 struct glsl_src_param src0_param;
3993 struct glsl_src_param src1_param;
3994 char dst_mask[6];
3996 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3997 shader_glsl_append_dst(ins->ctx->buffer, ins);
3998 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3999 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4000 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
4003 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
4005 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
4007 if (!stream)
4008 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
4009 else
4010 FIXME("Unhandled primitive stream %u.\n", stream);
4013 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
4014 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
4015 * GLSL uses the value as-is. */
4016 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
4018 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4019 struct glsl_src_param src0_param;
4020 struct glsl_src_param src1_param;
4021 DWORD dst_write_mask;
4022 unsigned int dst_size;
4024 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4025 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4027 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4028 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4030 if (dst_size > 1)
4032 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
4033 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
4035 else
4037 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
4038 src1_param.param_str, src0_param.param_str, src1_param.param_str);
4042 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
4043 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
4045 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4046 bool y_correction = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4047 ? priv->cur_ps_args->y_correction : false;
4048 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4049 struct glsl_src_param src_param;
4050 const char *instruction;
4051 DWORD write_mask;
4052 unsigned i;
4054 /* Determine the GLSL function to use based on the opcode */
4055 /* TODO: Possibly make this a table for faster lookups */
4056 switch (ins->handler_idx)
4058 case WINED3DSIH_ABS: instruction = "abs"; break;
4059 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
4060 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
4061 case WINED3DSIH_DSX: instruction = "dFdx"; break;
4062 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
4063 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
4064 case WINED3DSIH_DSY: instruction = y_correction ? "ycorrection.y * dFdy" : "dFdy"; break;
4065 case WINED3DSIH_DSY_COARSE: instruction = y_correction ? "ycorrection.y * dFdyCoarse" : "dFdyCoarse"; break;
4066 case WINED3DSIH_DSY_FINE: instruction = y_correction ? "ycorrection.y * dFdyFine" : "dFdyFine"; break;
4067 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
4068 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
4069 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
4070 case WINED3DSIH_FRC: instruction = "fract"; break;
4071 case WINED3DSIH_IMAX: instruction = "max"; break;
4072 case WINED3DSIH_IMIN: instruction = "min"; break;
4073 case WINED3DSIH_MAX: instruction = "max"; break;
4074 case WINED3DSIH_MIN: instruction = "min"; break;
4075 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
4076 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
4077 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
4078 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
4079 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
4080 case WINED3DSIH_UMAX: instruction = "max"; break;
4081 case WINED3DSIH_UMIN: instruction = "min"; break;
4082 default: instruction = "";
4083 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4084 break;
4087 write_mask = shader_glsl_append_dst(buffer, ins);
4089 /* In D3D bits are numbered from the most significant bit. */
4090 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
4091 shader_addline(buffer, "31 - ");
4092 shader_addline(buffer, "%s(", instruction);
4094 if (ins->src_count)
4096 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4097 shader_addline(buffer, "%s", src_param.param_str);
4098 for (i = 1; i < ins->src_count; ++i)
4100 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
4101 shader_addline(buffer, ", %s", src_param.param_str);
4105 shader_addline(buffer, "));\n");
4108 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
4110 struct wined3d_shader_dst_param dst;
4111 struct glsl_src_param src;
4112 DWORD write_mask;
4113 const char *fmt;
4114 unsigned int i;
4116 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
4117 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
4119 dst = ins->dst[0];
4120 for (i = 0; i < 4; ++i)
4122 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4123 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
4124 &dst, 0, dst.reg.data_type)))
4125 continue;
4127 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
4128 shader_addline(ins->ctx->buffer, fmt, src.param_str);
4132 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
4134 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4135 struct wined3d_shader_dst_param dst;
4136 struct glsl_src_param src[4];
4137 const char *instruction;
4138 BOOL tmp_dst = FALSE;
4139 char mask_char[6];
4140 unsigned int i, j;
4141 DWORD write_mask;
4143 switch (ins->handler_idx)
4145 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
4146 case WINED3DSIH_IBFE: instruction = "bitfieldExtract"; break;
4147 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
4148 default:
4149 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4150 return;
4153 for (i = 0; i < ins->src_count; ++i)
4155 if (ins->dst[0].reg.idx[0].offset == ins->src[i].reg.idx[0].offset
4156 && ins->dst[0].reg.type == ins->src[i].reg.type)
4157 tmp_dst = TRUE;
4160 dst = ins->dst[0];
4161 for (i = 0; i < 4; ++i)
4163 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4164 if (tmp_dst && (write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4165 shader_addline(buffer, "tmp0%s = %sBitsToFloat(", mask_char,
4166 dst.reg.data_type == WINED3D_DATA_INT ? "int" : "uint");
4167 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst, 0, dst.reg.data_type)))
4168 continue;
4170 for (j = 0; j < ins->src_count; ++j)
4171 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
4172 shader_addline(buffer, "%s(", instruction);
4173 for (j = 0; j < ins->src_count - 2; ++j)
4174 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
4175 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
4178 if (tmp_dst)
4180 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, WINED3D_DATA_FLOAT);
4181 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4182 shader_addline(buffer, "tmp0%s);\n", mask_char);
4186 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
4188 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
4190 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4191 struct glsl_src_param src_param;
4192 unsigned int mask_size;
4193 DWORD write_mask;
4194 char dst_mask[6];
4196 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
4197 mask_size = shader_glsl_get_write_mask_size(write_mask);
4198 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4200 if (mask_size > 3)
4201 shader_addline(buffer, "tmp0.x = dot(vec3(%s), vec3(%s));\n",
4202 src_param.param_str, src_param.param_str);
4203 else
4204 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
4205 src_param.param_str, src_param.param_str);
4206 shader_glsl_append_dst(buffer, ins);
4208 shader_addline(buffer, "tmp0.x == 0.0 ? %s : (%s * inversesqrt(tmp0.x)));\n",
4209 src_param.param_str, src_param.param_str);
4212 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
4214 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4215 ins->ctx->reg_maps->shader_version.minor);
4216 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4217 struct glsl_src_param src0_param;
4218 const char *prefix, *suffix;
4219 unsigned int dst_size;
4220 DWORD dst_write_mask;
4222 dst_write_mask = shader_glsl_append_dst(buffer, ins);
4223 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
4225 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
4226 dst_write_mask = WINED3DSP_WRITEMASK_3;
4228 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
4230 switch (ins->handler_idx)
4232 case WINED3DSIH_EXP:
4233 case WINED3DSIH_EXPP:
4234 prefix = "exp2(";
4235 suffix = ")";
4236 break;
4238 case WINED3DSIH_LOG:
4239 case WINED3DSIH_LOGP:
4240 prefix = "log2(abs(";
4241 suffix = "))";
4242 break;
4244 case WINED3DSIH_RCP:
4245 prefix = "1.0 / ";
4246 suffix = "";
4247 break;
4249 case WINED3DSIH_RSQ:
4250 prefix = "inversesqrt(abs(";
4251 suffix = "))";
4252 break;
4254 default:
4255 prefix = "";
4256 suffix = "";
4257 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4258 break;
4261 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4262 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4263 else
4264 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4267 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4268 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4269 * dst.x = 2^(floor(src))
4270 * dst.y = src - floor(src)
4271 * dst.z = 2^src (partial precision is allowed, but optional)
4272 * dst.w = 1.0;
4273 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4274 * dst = 2^src; (partial precision is allowed, but optional)
4276 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4278 if (ins->ctx->reg_maps->shader_version.major < 2)
4280 struct glsl_src_param src_param;
4281 char dst_mask[6];
4283 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4285 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4286 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4287 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4288 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4290 shader_glsl_append_dst(ins->ctx->buffer, ins);
4291 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4292 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4293 return;
4296 shader_glsl_scalar_op(ins);
4299 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4300 const char *vector_constructor, const char *scalar_constructor)
4302 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4303 struct glsl_src_param src_param;
4304 unsigned int mask_size;
4305 DWORD write_mask;
4307 write_mask = shader_glsl_append_dst(buffer, ins);
4308 mask_size = shader_glsl_get_write_mask_size(write_mask);
4309 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4311 if (mask_size > 1)
4312 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4313 else
4314 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4317 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4319 shader_glsl_cast(ins, "ivec", "int");
4322 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4324 shader_glsl_cast(ins, "uvec", "uint");
4327 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4329 shader_glsl_cast(ins, "vec", "float");
4332 /** Process signed comparison opcodes in GLSL. */
4333 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4335 struct glsl_src_param src0_param;
4336 struct glsl_src_param src1_param;
4337 DWORD write_mask;
4338 unsigned int mask_size;
4340 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4341 mask_size = shader_glsl_get_write_mask_size(write_mask);
4342 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4343 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4345 if (mask_size > 1) {
4346 const char *compare;
4348 switch(ins->handler_idx)
4350 case WINED3DSIH_SLT: compare = "lessThan"; break;
4351 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4352 default: compare = "";
4353 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4356 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4357 src0_param.param_str, src1_param.param_str);
4358 } else {
4359 switch(ins->handler_idx)
4361 case WINED3DSIH_SLT:
4362 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4363 * to return 0.0 but step returns 1.0 because step is not < x
4364 * An alternative is a bvec compare padded with an unused second component.
4365 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4366 * issue. Playing with not() is not possible either because not() does not accept
4367 * a scalar.
4369 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4370 src0_param.param_str, src1_param.param_str);
4371 break;
4372 case WINED3DSIH_SGE:
4373 /* Here we can use the step() function and safe a conditional */
4374 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4375 break;
4376 default:
4377 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4383 static void shader_glsl_swapc(const struct wined3d_shader_instruction *ins)
4385 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4386 struct wined3d_shader_dst_param dst[2];
4387 struct glsl_src_param src[3];
4388 unsigned int i, j, k;
4389 char mask_char[6];
4390 DWORD write_mask;
4391 BOOL tmp_dst[2];
4393 for (i = 0; i < ins->dst_count; ++i)
4395 tmp_dst[i] = FALSE;
4396 for (j = 0; j < ins->src_count; ++j)
4398 if (ins->dst[i].reg.idx[0].offset == ins->src[j].reg.idx[0].offset
4399 && ins->dst[i].reg.type == ins->src[j].reg.type)
4400 tmp_dst[i] = TRUE;
4404 dst[0] = ins->dst[0];
4405 dst[1] = ins->dst[1];
4406 for (i = 0; i < 4; ++i)
4408 for (j = 0; j < ARRAY_SIZE(dst); ++j)
4410 dst[j].write_mask = ins->dst[j].write_mask & (WINED3DSP_WRITEMASK_0 << i);
4411 if (tmp_dst[j] && (write_mask = shader_glsl_get_write_mask(&dst[j], mask_char)))
4412 shader_addline(buffer, "tmp%u%s = (", j, mask_char);
4413 else if (!(write_mask = shader_glsl_append_dst_ext(buffer, ins, &dst[j], j, dst[j].reg.data_type)))
4414 continue;
4416 for (k = 0; k < ARRAY_SIZE(src); ++k)
4417 shader_glsl_add_src_param(ins, &ins->src[k], write_mask, &src[k]);
4419 shader_addline(buffer, "%sbool(%s) ? %s : %s);\n", !j ? "!" : "",
4420 src[0].param_str, src[1].param_str, src[2].param_str);
4424 for (i = 0; i < ARRAY_SIZE(tmp_dst); ++i)
4426 if (tmp_dst[i])
4428 shader_glsl_get_write_mask(&ins->dst[i], mask_char);
4429 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[i], i, ins->dst[i].reg.data_type);
4430 shader_addline(buffer, "tmp%u%s);\n", i, mask_char);
4435 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4437 const char *condition_prefix, *condition_suffix;
4438 struct wined3d_shader_dst_param dst;
4439 struct glsl_src_param src0_param;
4440 struct glsl_src_param src1_param;
4441 struct glsl_src_param src2_param;
4442 BOOL temp_destination = FALSE;
4443 DWORD cmp_channel = 0;
4444 unsigned int i, j;
4445 char mask_char[6];
4446 DWORD write_mask;
4448 switch (ins->handler_idx)
4450 case WINED3DSIH_CMP:
4451 condition_prefix = "";
4452 condition_suffix = " >= 0.0";
4453 break;
4455 case WINED3DSIH_CND:
4456 condition_prefix = "";
4457 condition_suffix = " > 0.5";
4458 break;
4460 case WINED3DSIH_MOVC:
4461 condition_prefix = "bool(";
4462 condition_suffix = ")";
4463 break;
4465 default:
4466 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4467 condition_prefix = "<unhandled prefix>";
4468 condition_suffix = "<unhandled suffix>";
4469 break;
4472 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4474 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4475 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4476 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4477 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4479 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4480 condition_prefix, src0_param.param_str, condition_suffix,
4481 src1_param.param_str, src2_param.param_str);
4482 return;
4485 dst = ins->dst[0];
4487 /* Splitting the instruction up in multiple lines imposes a problem:
4488 * The first lines may overwrite source parameters of the following lines.
4489 * Deal with that by using a temporary destination register if needed. */
4490 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4491 && ins->src[0].reg.type == dst.reg.type)
4492 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4493 && ins->src[1].reg.type == dst.reg.type)
4494 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4495 && ins->src[2].reg.type == dst.reg.type))
4496 temp_destination = TRUE;
4498 /* Cycle through all source0 channels. */
4499 for (i = 0; i < 4; ++i)
4501 write_mask = 0;
4502 /* Find the destination channels which use the current source0 channel. */
4503 for (j = 0; j < 4; ++j)
4505 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4507 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4508 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4511 dst.write_mask = ins->dst[0].write_mask & write_mask;
4513 if (temp_destination)
4515 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4516 continue;
4517 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4519 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type)))
4520 continue;
4522 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4523 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4524 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4526 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4527 condition_prefix, src0_param.param_str, condition_suffix,
4528 src1_param.param_str, src2_param.param_str);
4531 if (temp_destination)
4533 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4534 shader_glsl_append_dst(ins->ctx->buffer, ins);
4535 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4539 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4540 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4541 * the compare is done per component of src0. */
4542 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4544 struct glsl_src_param src0_param;
4545 struct glsl_src_param src1_param;
4546 struct glsl_src_param src2_param;
4547 DWORD write_mask;
4548 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4549 ins->ctx->reg_maps->shader_version.minor);
4551 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4553 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4554 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4555 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4556 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4558 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4559 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4560 else
4561 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4562 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4563 return;
4566 shader_glsl_conditional_move(ins);
4569 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4570 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4572 struct glsl_src_param src0_param;
4573 struct glsl_src_param src1_param;
4574 struct glsl_src_param src2_param;
4575 DWORD write_mask;
4577 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4578 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4579 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4580 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4581 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4582 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4585 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4586 Vertex shaders to GLSL codes */
4587 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4589 int i;
4590 int nComponents = 0;
4591 struct wined3d_shader_dst_param tmp_dst = {{0}};
4592 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4593 struct wined3d_shader_instruction tmp_ins;
4595 memset(&tmp_ins, 0, sizeof(tmp_ins));
4597 /* Set constants for the temporary argument */
4598 tmp_ins.ctx = ins->ctx;
4599 tmp_ins.dst_count = 1;
4600 tmp_ins.dst = &tmp_dst;
4601 tmp_ins.src_count = 2;
4602 tmp_ins.src = tmp_src;
4604 switch(ins->handler_idx)
4606 case WINED3DSIH_M4x4:
4607 nComponents = 4;
4608 tmp_ins.handler_idx = WINED3DSIH_DP4;
4609 break;
4610 case WINED3DSIH_M4x3:
4611 nComponents = 3;
4612 tmp_ins.handler_idx = WINED3DSIH_DP4;
4613 break;
4614 case WINED3DSIH_M3x4:
4615 nComponents = 4;
4616 tmp_ins.handler_idx = WINED3DSIH_DP3;
4617 break;
4618 case WINED3DSIH_M3x3:
4619 nComponents = 3;
4620 tmp_ins.handler_idx = WINED3DSIH_DP3;
4621 break;
4622 case WINED3DSIH_M3x2:
4623 nComponents = 2;
4624 tmp_ins.handler_idx = WINED3DSIH_DP3;
4625 break;
4626 default:
4627 break;
4630 tmp_dst = ins->dst[0];
4631 tmp_src[0] = ins->src[0];
4632 tmp_src[1] = ins->src[1];
4633 for (i = 0; i < nComponents; ++i)
4635 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4636 shader_glsl_dot(&tmp_ins);
4637 ++tmp_src[1].reg.idx[0].offset;
4642 The LRP instruction performs a component-wise linear interpolation
4643 between the second and third operands using the first operand as the
4644 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4645 This is equivalent to mix(src2, src1, src0);
4647 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4649 struct glsl_src_param src0_param;
4650 struct glsl_src_param src1_param;
4651 struct glsl_src_param src2_param;
4652 DWORD write_mask;
4654 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4656 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4657 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4658 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4660 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4661 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4664 /** Process the WINED3DSIO_LIT instruction in GLSL:
4665 * dst.x = dst.w = 1.0
4666 * dst.y = (src0.x > 0) ? src0.x
4667 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4668 * where src.w is clamped at +- 128
4670 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4672 struct glsl_src_param src0_param;
4673 struct glsl_src_param src1_param;
4674 struct glsl_src_param src3_param;
4675 char dst_mask[6];
4677 shader_glsl_append_dst(ins->ctx->buffer, ins);
4678 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4680 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4681 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4682 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4684 /* The sdk specifies the instruction like this
4685 * dst.x = 1.0;
4686 * if(src.x > 0.0) dst.y = src.x
4687 * else dst.y = 0.0.
4688 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4689 * else dst.z = 0.0;
4690 * dst.w = 1.0;
4691 * (where power = src.w clamped between -128 and 128)
4693 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4694 * dst.x = 1.0 ... No further explanation needed
4695 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4696 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4697 * dst.w = 1.0. ... Nothing fancy.
4699 * So we still have one conditional in there. So do this:
4700 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4702 * 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),
4703 * 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.
4704 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4706 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4707 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4708 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4710 shader_addline(ins->ctx->buffer,
4711 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4712 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4713 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4714 src0_param.param_str, src3_param.param_str, dst_mask);
4717 /** Process the WINED3DSIO_DST instruction in GLSL:
4718 * dst.x = 1.0
4719 * dst.y = src0.x * src0.y
4720 * dst.z = src0.z
4721 * dst.w = src1.w
4723 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4725 struct glsl_src_param src0y_param;
4726 struct glsl_src_param src0z_param;
4727 struct glsl_src_param src1y_param;
4728 struct glsl_src_param src1w_param;
4729 char dst_mask[6];
4731 shader_glsl_append_dst(ins->ctx->buffer, ins);
4732 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4734 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4735 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4736 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4737 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4739 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4740 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4743 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4744 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4745 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4747 * dst.x = cos(src0.?)
4748 * dst.y = sin(src0.?)
4749 * dst.z = dst.z
4750 * dst.w = dst.w
4752 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4754 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4755 struct glsl_src_param src0_param;
4756 DWORD write_mask;
4758 if (ins->ctx->reg_maps->shader_version.major < 4)
4760 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4762 write_mask = shader_glsl_append_dst(buffer, ins);
4763 switch (write_mask)
4765 case WINED3DSP_WRITEMASK_0:
4766 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4767 break;
4769 case WINED3DSP_WRITEMASK_1:
4770 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4771 break;
4773 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4774 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4775 src0_param.param_str, src0_param.param_str);
4776 break;
4778 default:
4779 ERR("Write mask should be .x, .y or .xy\n");
4780 break;
4783 return;
4786 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4789 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4791 char dst_mask[6];
4793 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4794 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4795 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4797 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4798 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4799 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4801 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4802 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4804 else
4806 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, ins->dst[0].reg.data_type);
4807 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4808 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4811 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4813 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], 1, ins->dst[1].reg.data_type);
4814 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4815 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4819 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4820 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4821 * generate invalid code
4823 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4825 struct glsl_src_param src0_param;
4826 DWORD write_mask;
4828 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4829 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4831 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4834 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4835 * Start a for() loop where src1.y is the initial value of aL,
4836 * increment aL by src1.z for a total of src1.x iterations.
4837 * Need to use a temporary variable for this operation.
4839 /* FIXME: I don't think nested loops will work correctly this way. */
4840 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4842 struct wined3d_shader_parser_state *state = ins->ctx->state;
4843 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4844 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4845 const struct wined3d_shader *shader = ins->ctx->shader;
4846 const struct wined3d_shader_lconst *constant;
4847 struct wined3d_string_buffer *reg_name;
4848 const unsigned int *control_values = NULL;
4850 if (ins->ctx->reg_maps->shader_version.major < 4)
4852 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4853 * class hardware doesn't support real varying indexing, but Microsoft
4854 * designed this feature for Shader model 2.x+. If the loop control is
4855 * known at compile time, the GLSL compiler can unroll the loop, and
4856 * replace indirect addressing with direct addressing. */
4857 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4859 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4861 if (constant->idx == ins->src[1].reg.idx[0].offset)
4863 control_values = constant->value;
4864 break;
4869 if (control_values)
4871 struct wined3d_shader_loop_control loop_control;
4872 loop_control.count = control_values[0];
4873 loop_control.start = control_values[1];
4874 loop_control.step = (int)control_values[2];
4876 if (loop_control.step > 0)
4878 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4879 state->current_loop_depth, loop_control.start,
4880 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4881 state->current_loop_depth, loop_control.step);
4883 else if (loop_control.step < 0)
4885 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4886 state->current_loop_depth, loop_control.start,
4887 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4888 state->current_loop_depth, loop_control.step);
4890 else
4892 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4893 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4894 state->current_loop_depth, loop_control.count,
4895 state->current_loop_depth);
4898 else
4900 reg_name = string_buffer_get(priv->string_buffers);
4901 shader_glsl_get_register_name(&ins->src[1].reg, ins->src[1].reg.data_type, reg_name, NULL, ins->ctx);
4903 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4904 state->current_loop_depth, state->current_loop_reg, reg_name->buffer,
4905 state->current_loop_depth, reg_name->buffer,
4906 state->current_loop_depth, state->current_loop_reg, reg_name->buffer);
4908 string_buffer_release(priv->string_buffers, reg_name);
4912 ++state->current_loop_reg;
4914 else
4916 shader_addline(buffer, "for (;;)\n{\n");
4919 ++state->current_loop_depth;
4922 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4924 struct wined3d_shader_parser_state *state = ins->ctx->state;
4926 shader_addline(ins->ctx->buffer, "}\n");
4928 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4930 --state->current_loop_depth;
4931 --state->current_loop_reg;
4934 if (ins->handler_idx == WINED3DSIH_ENDREP)
4936 --state->current_loop_depth;
4940 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4942 struct wined3d_shader_parser_state *state = ins->ctx->state;
4943 const struct wined3d_shader *shader = ins->ctx->shader;
4944 const struct wined3d_shader_lconst *constant;
4945 struct glsl_src_param src0_param;
4946 const unsigned int *control_values = NULL;
4948 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4949 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4951 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4953 if (constant->idx == ins->src[0].reg.idx[0].offset)
4955 control_values = constant->value;
4956 break;
4961 if (control_values)
4963 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4964 state->current_loop_depth, state->current_loop_depth,
4965 control_values[0], state->current_loop_depth);
4967 else
4969 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4970 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4971 state->current_loop_depth, state->current_loop_depth,
4972 src0_param.param_str, state->current_loop_depth);
4975 ++state->current_loop_depth;
4978 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4980 struct glsl_src_param src0_param;
4982 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4983 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4986 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4988 struct glsl_src_param src0_param;
4990 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4991 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4994 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4996 shader_addline(ins->ctx->buffer, "default:\n");
4999 static void shader_glsl_generate_condition(const struct wined3d_shader_instruction *ins)
5001 struct glsl_src_param src_param;
5002 const char *condition;
5004 condition = ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ ? "bool" : "!bool";
5005 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5006 shader_addline(ins->ctx->buffer, "if (%s(%s))\n", condition, src_param.param_str);
5009 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
5011 shader_glsl_generate_condition(ins);
5012 shader_addline(ins->ctx->buffer, "{\n");
5015 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
5017 struct glsl_src_param src0_param;
5018 struct glsl_src_param src1_param;
5020 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5021 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5023 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
5024 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5027 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
5029 shader_addline(ins->ctx->buffer, "} else {\n");
5032 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
5034 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
5035 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5036 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5038 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
5039 if (!priv->gl_info->supported[ARB_CLIP_CONTROL])
5040 shader_glsl_fixup_position(ins->ctx->buffer, reg_maps->viewport_array);
5042 if (!stream)
5043 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
5044 else
5045 FIXME("Unhandled primitive stream %u.\n", stream);
5048 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
5050 shader_addline(ins->ctx->buffer, "break;\n");
5053 /* FIXME: According to MSDN the compare is done per component. */
5054 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
5056 struct glsl_src_param src0_param;
5057 struct glsl_src_param src1_param;
5059 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
5060 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5062 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
5063 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
5066 static void shader_glsl_conditional_op(const struct wined3d_shader_instruction *ins)
5068 const char *op;
5070 switch (ins->handler_idx)
5072 case WINED3DSIH_BREAKP:
5073 op = "break;";
5074 break;
5075 case WINED3DSIH_CONTINUEP:
5076 op = "continue;";
5077 break;
5078 case WINED3DSIH_RETP:
5079 op = "return;";
5080 break;
5081 default:
5082 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5083 return;
5086 shader_glsl_generate_condition(ins);
5087 if (ins->handler_idx == WINED3DSIH_RETP)
5089 shader_addline(ins->ctx->buffer, "{\n");
5090 shader_glsl_generate_shader_epilogue(ins->ctx);
5092 shader_addline(ins->ctx->buffer, " %s\n", op);
5093 if (ins->handler_idx == WINED3DSIH_RETP)
5094 shader_addline(ins->ctx->buffer, "}\n");
5097 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
5099 shader_addline(ins->ctx->buffer, "continue;\n");
5102 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
5104 shader_addline(ins->ctx->buffer, "}\n");
5105 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
5107 /* Subroutines appear at the end of the shader. */
5108 ins->ctx->state->in_subroutine = TRUE;
5111 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
5113 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
5116 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
5118 struct glsl_src_param src1_param;
5120 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
5121 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
5122 src1_param.param_str, ins->src[0].reg.idx[0].offset);
5125 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
5127 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
5129 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
5131 shader_glsl_generate_shader_epilogue(ins->ctx);
5132 shader_addline(ins->ctx->buffer, "return;\n");
5136 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
5138 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
5139 ins->ctx->reg_maps->shader_version.minor);
5140 struct glsl_sample_function sample_function;
5141 DWORD sample_flags = 0;
5142 unsigned int resource_idx;
5143 DWORD mask = 0, swizzle;
5144 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5145 enum wined3d_shader_resource_type resource_type;
5147 /* 1.0-1.4: Use destination register as sampler source.
5148 * 2.0+: Use provided sampler source. */
5149 if (shader_version < WINED3D_SHADER_VERSION(2,0))
5150 resource_idx = ins->dst[0].reg.idx[0].offset;
5151 else
5152 resource_idx = ins->src[1].reg.idx[0].offset;
5154 resource_type = ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
5155 ? pixelshader_get_resource_type(ins->ctx->reg_maps, resource_idx, priv->cur_ps_args->tex_types)
5156 : ins->ctx->reg_maps->resource_info[resource_idx].type;
5158 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5160 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5161 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5163 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
5164 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5166 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5167 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5169 case WINED3D_TTFF_COUNT1:
5170 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5171 break;
5172 case WINED3D_TTFF_COUNT2:
5173 mask = WINED3DSP_WRITEMASK_1;
5174 break;
5175 case WINED3D_TTFF_COUNT3:
5176 mask = WINED3DSP_WRITEMASK_2;
5177 break;
5178 case WINED3D_TTFF_COUNT4:
5179 case WINED3D_TTFF_DISABLE:
5180 mask = WINED3DSP_WRITEMASK_3;
5181 break;
5185 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
5187 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5189 if (src_mod == WINED3DSPSM_DZ) {
5190 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5191 mask = WINED3DSP_WRITEMASK_2;
5192 } else if (src_mod == WINED3DSPSM_DW) {
5193 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5194 mask = WINED3DSP_WRITEMASK_3;
5197 else
5199 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
5200 && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
5202 /* ps 2.0 texldp instruction always divides by the fourth component. */
5203 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
5204 mask = WINED3DSP_WRITEMASK_3;
5208 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
5209 mask |= sample_function.coord_mask;
5210 sample_function.coord_mask = mask;
5212 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
5213 else swizzle = ins->src[1].swizzle;
5215 /* 1.0-1.3: Use destination register as coordinate source.
5216 1.4+: Use provided coordinate source register. */
5217 if (shader_version < WINED3D_SHADER_VERSION(1,4))
5219 char coord_mask[6];
5220 shader_glsl_write_mask_to_str(mask, coord_mask);
5221 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5222 "T%u%s", resource_idx, coord_mask);
5224 else
5226 struct glsl_src_param coord_param;
5227 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
5228 if (ins->flags & WINED3DSI_TEXLD_BIAS)
5230 struct glsl_src_param bias;
5231 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
5232 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
5233 NULL, "%s", coord_param.param_str);
5234 } else {
5235 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
5236 "%s", coord_param.param_str);
5239 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5242 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
5244 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5245 const struct wined3d_gl_info *gl_info = priv->gl_info;
5246 struct glsl_src_param coord_param, dx_param, dy_param;
5247 struct glsl_sample_function sample_function;
5248 DWORD sampler_idx;
5249 DWORD swizzle = ins->src[1].swizzle;
5251 if (!shader_glsl_has_core_grad(gl_info) && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5253 FIXME("texldd used, but not supported by hardware. Falling back to regular tex.\n");
5254 shader_glsl_tex(ins);
5255 return;
5258 sampler_idx = ins->src[1].reg.idx[0].offset;
5260 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
5261 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5262 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
5263 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
5265 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
5266 NULL, NULL, "%s", coord_param.param_str);
5267 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5270 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
5272 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
5273 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5274 const struct wined3d_gl_info *gl_info = priv->gl_info;
5275 struct glsl_src_param coord_param, lod_param;
5276 struct glsl_sample_function sample_function;
5277 DWORD swizzle = ins->src[1].swizzle;
5278 DWORD sampler_idx;
5280 sampler_idx = ins->src[1].reg.idx[0].offset;
5282 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
5283 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5285 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5287 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info)
5288 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5290 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
5291 * However, the NVIDIA drivers allow them in fragment shaders as well,
5292 * even without the appropriate extension. */
5293 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
5295 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
5296 "%s", coord_param.param_str);
5297 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5300 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
5301 unsigned int resource_idx, unsigned int sampler_idx)
5303 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
5304 unsigned int i;
5306 for (i = 0; i < sampler_map->count; ++i)
5308 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
5309 return entries[i].bind_idx;
5312 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
5314 return ~0u;
5317 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
5319 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
5320 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
5321 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5322 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5323 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5324 struct glsl_src_param structure_idx, offset, data, data2;
5325 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5326 enum wined3d_shader_resource_type resource_type;
5327 struct wined3d_string_buffer *address;
5328 enum wined3d_data_type data_type;
5329 unsigned int resource_idx, stride;
5330 const char *op, *resource;
5331 DWORD coord_mask;
5332 BOOL is_tgsm;
5334 resource_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
5335 is_tgsm = ins->dst[is_imm_instruction].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5336 if (is_tgsm)
5338 if (resource_idx >= reg_maps->tgsm_count)
5340 ERR("Invalid TGSM index %u.\n", resource_idx);
5341 return;
5343 resource = "g";
5344 data_type = WINED3D_DATA_UINT;
5345 coord_mask = 1;
5346 stride = reg_maps->tgsm[resource_idx].stride;
5348 else
5350 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5352 ERR("Invalid UAV index %u.\n", resource_idx);
5353 return;
5355 resource_type = reg_maps->uav_resource_info[resource_idx].type;
5356 if (resource_type >= ARRAY_SIZE(resource_type_info))
5358 ERR("Unexpected resource type %#x.\n", resource_type);
5359 return;
5361 resource = "image";
5362 data_type = reg_maps->uav_resource_info[resource_idx].data_type;
5363 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5364 stride = reg_maps->uav_resource_info[resource_idx].stride;
5367 switch (ins->handler_idx)
5369 case WINED3DSIH_ATOMIC_AND:
5370 case WINED3DSIH_IMM_ATOMIC_AND:
5371 if (is_tgsm)
5372 op = "atomicAnd";
5373 else
5374 op = "imageAtomicAnd";
5375 break;
5376 case WINED3DSIH_ATOMIC_CMP_STORE:
5377 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
5378 if (is_tgsm)
5379 op = "atomicCompSwap";
5380 else
5381 op = "imageAtomicCompSwap";
5382 break;
5383 case WINED3DSIH_ATOMIC_IADD:
5384 case WINED3DSIH_IMM_ATOMIC_IADD:
5385 if (is_tgsm)
5386 op = "atomicAdd";
5387 else
5388 op = "imageAtomicAdd";
5389 break;
5390 case WINED3DSIH_ATOMIC_IMAX:
5391 case WINED3DSIH_IMM_ATOMIC_IMAX:
5392 if (is_tgsm)
5393 op = "atomicMax";
5394 else
5395 op = "imageAtomicMax";
5396 if (data_type != WINED3D_DATA_INT)
5398 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5399 return;
5401 break;
5402 case WINED3DSIH_ATOMIC_IMIN:
5403 case WINED3DSIH_IMM_ATOMIC_IMIN:
5404 if (is_tgsm)
5405 op = "atomicMin";
5406 else
5407 op = "imageAtomicMin";
5408 if (data_type != WINED3D_DATA_INT)
5410 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5411 return;
5413 break;
5414 case WINED3DSIH_ATOMIC_OR:
5415 case WINED3DSIH_IMM_ATOMIC_OR:
5416 if (is_tgsm)
5417 op = "atomicOr";
5418 else
5419 op = "imageAtomicOr";
5420 break;
5421 case WINED3DSIH_ATOMIC_UMAX:
5422 case WINED3DSIH_IMM_ATOMIC_UMAX:
5423 if (is_tgsm)
5424 op = "atomicMax";
5425 else
5426 op = "imageAtomicMax";
5427 if (data_type != WINED3D_DATA_UINT)
5429 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5430 return;
5432 break;
5433 case WINED3DSIH_ATOMIC_UMIN:
5434 case WINED3DSIH_IMM_ATOMIC_UMIN:
5435 if (is_tgsm)
5436 op = "atomicMin";
5437 else
5438 op = "imageAtomicMin";
5439 if (data_type != WINED3D_DATA_UINT)
5441 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5442 return;
5444 break;
5445 case WINED3DSIH_ATOMIC_XOR:
5446 case WINED3DSIH_IMM_ATOMIC_XOR:
5447 if (is_tgsm)
5448 op = "atomicXor";
5449 else
5450 op = "imageAtomicXor";
5451 break;
5452 case WINED3DSIH_IMM_ATOMIC_EXCH:
5453 if (is_tgsm)
5454 op = "atomicExchange";
5455 else
5456 op = "imageAtomicExchange";
5457 break;
5458 default:
5459 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5460 return;
5463 address = string_buffer_get(priv->string_buffers);
5464 if (stride)
5466 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5467 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5468 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5470 else
5472 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5473 string_buffer_sprintf(address, "%s", offset.param_str);
5474 if (is_tgsm || (reg_maps->uav_resource_info[resource_idx].flags & WINED3D_VIEW_BUFFER_RAW))
5475 shader_addline(address, "/ 4");
5478 if (is_imm_instruction)
5479 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5481 if (is_tgsm)
5482 shader_addline(buffer, "%s(%s_%s%u[%s], ",
5483 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5484 else
5485 shader_addline(buffer, "%s(%s_%s%u, %s, ",
5486 op, shader_glsl_get_prefix(version->type), resource, resource_idx, address->buffer);
5488 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5489 shader_addline(buffer, "%s", data.param_str);
5490 if (ins->src_count >= 3)
5492 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5493 shader_addline(buffer, ", %s", data2.param_str);
5496 if (is_imm_instruction)
5497 shader_addline(buffer, ")");
5498 shader_addline(buffer, ");\n");
5500 string_buffer_release(priv->string_buffers, address);
5503 static void shader_glsl_uav_counter(const struct wined3d_shader_instruction *ins)
5505 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5506 const char *op;
5508 if (ins->handler_idx == WINED3DSIH_IMM_ATOMIC_ALLOC)
5509 op = "atomicCounterIncrement";
5510 else
5511 op = "atomicCounterDecrement";
5513 shader_glsl_append_dst(ins->ctx->buffer, ins);
5514 shader_addline(ins->ctx->buffer, "%s(%s_counter%u));\n", op, prefix, ins->src[0].reg.idx[0].offset);
5517 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5519 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5520 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5521 enum wined3d_shader_resource_type resource_type;
5522 struct glsl_src_param image_coord_param;
5523 enum wined3d_data_type data_type;
5524 DWORD coord_mask, write_mask;
5525 unsigned int uav_idx;
5526 char dst_swizzle[6];
5528 uav_idx = ins->src[1].reg.idx[0].offset;
5529 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5531 ERR("Invalid UAV index %u.\n", uav_idx);
5532 return;
5534 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5535 if (resource_type >= ARRAY_SIZE(resource_type_info))
5537 ERR("Unexpected resource type %#x.\n", resource_type);
5538 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5540 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5541 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5543 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], 0, data_type);
5544 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5546 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5547 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5548 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5551 static void shader_glsl_ld_raw_structured(const struct wined3d_shader_instruction *ins)
5553 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5554 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5555 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5556 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5557 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5558 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5559 struct glsl_src_param structure_idx, offset;
5560 struct wined3d_string_buffer *address;
5561 struct wined3d_shader_dst_param dst;
5562 const char *function, *resource;
5564 resource_idx = src->reg.idx[0].offset;
5565 if (src->reg.type == WINED3DSPR_RESOURCE)
5567 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5569 ERR("Invalid resource index %u.\n", resource_idx);
5570 return;
5572 stride = reg_maps->resource_info[resource_idx].stride;
5573 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5574 function = "texelFetch";
5575 resource = "sampler";
5577 else if (src->reg.type == WINED3DSPR_UAV)
5579 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5581 ERR("Invalid UAV index %u.\n", resource_idx);
5582 return;
5584 stride = reg_maps->uav_resource_info[resource_idx].stride;
5585 bind_idx = resource_idx;
5586 function = "imageLoad";
5587 resource = "image";
5589 else
5591 if (resource_idx >= reg_maps->tgsm_count)
5593 ERR("Invalid TGSM index %u.\n", resource_idx);
5594 return;
5596 stride = reg_maps->tgsm[resource_idx].stride;
5597 bind_idx = resource_idx;
5598 function = NULL;
5599 resource = "g";
5602 address = string_buffer_get(priv->string_buffers);
5603 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5605 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5606 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5608 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5609 shader_addline(address, "%s / 4", offset.param_str);
5611 dst = ins->dst[0];
5612 if (shader_glsl_get_write_mask_size(dst.write_mask) > 1)
5614 /* The instruction is split into multiple lines. The first lines may
5615 * overwrite source parameters of the following lines. */
5616 shader_addline(buffer, "tmp0.x = intBitsToFloat(%s);\n", address->buffer);
5617 string_buffer_sprintf(address, "floatBitsToInt(tmp0.x)");
5620 for (i = 0; i < 4; ++i)
5622 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5623 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, 0, dst.reg.data_type))
5624 continue;
5626 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5627 if (function)
5628 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5629 function, prefix, resource, bind_idx, address->buffer, swizzle);
5630 else
5631 shader_addline(buffer, "%s_%s%u[%s + %u]);\n",
5632 prefix, resource, bind_idx, address->buffer, swizzle);
5635 string_buffer_release(priv->string_buffers, address);
5638 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5640 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5641 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5642 struct glsl_src_param image_coord_param, image_data_param;
5643 enum wined3d_shader_resource_type resource_type;
5644 enum wined3d_data_type data_type;
5645 unsigned int uav_idx;
5646 DWORD coord_mask;
5648 uav_idx = ins->dst[0].reg.idx[0].offset;
5649 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5651 ERR("Invalid UAV index %u.\n", uav_idx);
5652 return;
5654 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5655 if (resource_type >= ARRAY_SIZE(resource_type_info))
5657 ERR("Unexpected resource type %#x.\n", resource_type);
5658 return;
5660 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5661 coord_mask = wined3d_mask_from_size(resource_type_info[resource_type].coord_size);
5663 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5664 shader_glsl_add_src_param_ext(ins->ctx, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5665 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5666 shader_glsl_get_prefix(version->type), uav_idx,
5667 image_coord_param.param_str, image_data_param.param_str);
5670 static void shader_glsl_store_raw_structured(const struct wined3d_shader_instruction *ins)
5672 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5673 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5674 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5675 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5676 struct glsl_src_param structure_idx, offset, data;
5677 unsigned int i, resource_idx, stride, src_idx = 0;
5678 struct wined3d_string_buffer *address;
5679 DWORD write_mask;
5680 BOOL is_tgsm;
5682 resource_idx = ins->dst[0].reg.idx[0].offset;
5683 is_tgsm = ins->dst[0].reg.type == WINED3DSPR_GROUPSHAREDMEM;
5684 if (is_tgsm)
5686 if (resource_idx >= reg_maps->tgsm_count)
5688 ERR("Invalid TGSM index %u.\n", resource_idx);
5689 return;
5691 stride = reg_maps->tgsm[resource_idx].stride;
5693 else
5695 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5697 ERR("Invalid UAV index %u.\n", resource_idx);
5698 return;
5700 stride = reg_maps->uav_resource_info[resource_idx].stride;
5703 address = string_buffer_get(priv->string_buffers);
5704 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5706 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5707 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5709 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5710 shader_addline(address, "%s / 4", offset.param_str);
5712 for (i = 0; i < 4; ++i)
5714 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5715 continue;
5717 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5719 if (is_tgsm)
5720 shader_addline(buffer, "%s_g%u[%s + %u] = %s;\n",
5721 prefix, resource_idx, address->buffer, i, data.param_str);
5722 else
5723 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5724 prefix, resource_idx, address->buffer, i, data.param_str);
5727 string_buffer_release(priv->string_buffers, address);
5730 static void shader_glsl_sync(const struct wined3d_shader_instruction *ins)
5732 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5733 unsigned int sync_flags = ins->flags;
5735 if (sync_flags & WINED3DSSF_THREAD_GROUP)
5737 shader_addline(buffer, "barrier();\n");
5738 sync_flags &= ~(WINED3DSSF_THREAD_GROUP | WINED3DSSF_GROUP_SHARED_MEMORY);
5741 if (sync_flags & WINED3DSSF_GROUP_SHARED_MEMORY)
5743 shader_addline(buffer, "memoryBarrierShared();\n");
5744 sync_flags &= ~WINED3DSSF_GROUP_SHARED_MEMORY;
5747 if (sync_flags & WINED3DSSF_GLOBAL_UAV)
5749 shader_addline(buffer, "memoryBarrier();\n");
5750 sync_flags &= ~WINED3DSSF_GLOBAL_UAV;
5753 if (sync_flags)
5754 FIXME("Unhandled sync flags %#x.\n", sync_flags);
5757 static const struct wined3d_shader_resource_info *shader_glsl_get_resource_info(
5758 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_register *reg)
5760 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5761 unsigned int idx = reg->idx[0].offset;
5763 if (reg->type == WINED3DSPR_RESOURCE)
5765 if (idx >= ARRAY_SIZE(reg_maps->resource_info))
5767 ERR("Invalid resource index %u.\n", idx);
5768 return NULL;
5770 return &reg_maps->resource_info[idx];
5773 if (reg->type == WINED3DSPR_UAV)
5775 if (idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5777 ERR("Invalid UAV index %u.\n", idx);
5778 return NULL;
5780 return &reg_maps->uav_resource_info[idx];
5783 FIXME("Unhandled register type %#x.\n", reg->type);
5784 return NULL;
5787 static void shader_glsl_bufinfo(const struct wined3d_shader_instruction *ins)
5789 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5790 const struct wined3d_shader_resource_info *resource_info;
5791 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5792 unsigned int resource_idx;
5793 char dst_swizzle[6];
5794 DWORD write_mask;
5796 write_mask = shader_glsl_append_dst(buffer, ins);
5797 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5799 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[0].reg)))
5800 return;
5801 resource_idx = ins->src[0].reg.idx[0].offset;
5803 shader_addline(buffer, "ivec2(");
5804 if (ins->src[0].reg.type == WINED3DSPR_RESOURCE)
5806 unsigned int bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5807 resource_idx, WINED3D_SAMPLER_DEFAULT);
5808 shader_addline(buffer, "textureSize(%s_sampler%u)", prefix, bind_idx);
5810 else
5812 shader_addline(buffer, "imageSize(%s_image%u)", prefix, resource_idx);
5814 if (resource_info->stride)
5815 shader_addline(buffer, " / %u", resource_info->stride);
5816 else if (resource_info->flags & WINED3D_VIEW_BUFFER_RAW)
5817 shader_addline(buffer, " * 4");
5818 shader_addline(buffer, ", %u)%s);\n", resource_info->stride, dst_swizzle);
5821 static BOOL is_multisampled(enum wined3d_shader_resource_type resource_type)
5823 return resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMS
5824 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY;
5827 static BOOL is_mipmapped(enum wined3d_shader_resource_type resource_type)
5829 return resource_type != WINED3D_SHADER_RESOURCE_BUFFER && !is_multisampled(resource_type);
5832 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5834 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5835 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5836 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5837 const struct wined3d_gl_info *gl_info = priv->gl_info;
5838 enum wined3d_shader_resource_type resource_type;
5839 enum wined3d_shader_register_type reg_type;
5840 unsigned int resource_idx, bind_idx, i;
5841 enum wined3d_data_type dst_data_type;
5842 struct glsl_src_param lod_param;
5843 BOOL supports_mipmaps;
5844 char dst_swizzle[6];
5845 DWORD write_mask;
5847 dst_data_type = ins->dst[0].reg.data_type;
5848 if (ins->flags == WINED3DSI_RESINFO_UINT)
5849 dst_data_type = WINED3D_DATA_UINT;
5850 else if (ins->flags)
5851 FIXME("Unhandled flags %#x.\n", ins->flags);
5853 reg_type = ins->src[1].reg.type;
5854 resource_idx = ins->src[1].reg.idx[0].offset;
5855 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5856 if (reg_type == WINED3DSPR_RESOURCE)
5858 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5859 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5860 resource_idx, WINED3D_SAMPLER_DEFAULT);
5862 else
5864 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5865 bind_idx = resource_idx;
5868 if (resource_type >= ARRAY_SIZE(resource_type_info))
5870 ERR("Unexpected resource type %#x.\n", resource_type);
5871 return;
5874 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, dst_data_type);
5875 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5877 if (dst_data_type == WINED3D_DATA_UINT)
5878 shader_addline(buffer, "uvec4(");
5879 else
5880 shader_addline(buffer, "vec4(");
5882 if (reg_type == WINED3DSPR_RESOURCE)
5884 shader_addline(buffer, "textureSize(%s_sampler%u",
5885 shader_glsl_get_prefix(version->type), bind_idx);
5887 else
5889 shader_addline(buffer, "imageSize(%s_image%u",
5890 shader_glsl_get_prefix(version->type), bind_idx);
5893 supports_mipmaps = is_mipmapped(resource_type) && reg_type != WINED3DSPR_UAV;
5894 if (supports_mipmaps)
5895 shader_addline(buffer, ", %s", lod_param.param_str);
5896 shader_addline(buffer, "), ");
5898 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5899 shader_addline(buffer, "0, ");
5901 if (supports_mipmaps)
5903 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5905 shader_addline(buffer, "textureQueryLevels(%s_sampler%u)",
5906 shader_glsl_get_prefix(version->type), bind_idx);
5908 else
5910 FIXME("textureQueryLevels is not supported, returning 1 level.\n");
5911 shader_addline(buffer, "1");
5914 else
5916 shader_addline(buffer, "1");
5919 shader_addline(buffer, ")%s);\n", dst_swizzle);
5922 static void shader_glsl_sample_info(const struct wined3d_shader_instruction *ins)
5924 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5925 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5926 const struct wined3d_gl_info *gl_info = priv->gl_info;
5927 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5928 const struct wined3d_shader_dst_param *dst = ins->dst;
5929 const struct wined3d_shader_src_param *src = ins->src;
5930 enum wined3d_shader_resource_type resource_type;
5931 enum wined3d_data_type dst_data_type;
5932 unsigned int resource_idx, bind_idx;
5933 char dst_swizzle[6];
5934 DWORD write_mask;
5936 dst_data_type = dst->reg.data_type;
5937 if (ins->flags == WINED3DSI_SAMPLE_INFO_UINT)
5938 dst_data_type = WINED3D_DATA_UINT;
5939 else if (ins->flags)
5940 FIXME("Unhandled flags %#x.\n", ins->flags);
5942 write_mask = shader_glsl_append_dst_ext(buffer, ins, dst, 0, dst_data_type);
5943 shader_glsl_get_swizzle(src, FALSE, write_mask, dst_swizzle);
5945 if (dst_data_type == WINED3D_DATA_UINT)
5946 shader_addline(buffer, "uvec4(");
5947 else
5948 shader_addline(buffer, "vec4(");
5950 if (src->reg.type == WINED3DSPR_RASTERIZER)
5952 if (gl_info->supported[ARB_SAMPLE_SHADING])
5954 shader_addline(buffer, "gl_NumSamples");
5956 else
5958 FIXME("OpenGL implementation does not support ARB_sample_shading.\n");
5959 shader_addline(buffer, "1");
5962 else
5964 resource_idx = src->reg.idx[0].offset;
5965 resource_type = reg_maps->resource_info[resource_idx].type;
5966 if (resource_type >= ARRAY_SIZE(resource_type_info))
5968 ERR("Unexpected resource type %#x.\n", resource_type);
5969 return;
5971 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5973 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
5975 shader_addline(buffer, "textureSamples(%s_sampler%u)",
5976 shader_glsl_get_prefix(reg_maps->shader_version.type), bind_idx);
5978 else
5980 FIXME("textureSamples() is not supported.\n");
5981 shader_addline(buffer, "1");
5985 shader_addline(buffer, ", 0, 0, 0)%s);\n", dst_swizzle);
5988 static void shader_glsl_interpolate(const struct wined3d_shader_instruction *ins)
5990 const struct wined3d_shader_src_param *input = &ins->src[0];
5991 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5992 struct glsl_src_param sample_param;
5993 char dst_swizzle[6];
5994 DWORD write_mask;
5996 write_mask = shader_glsl_append_dst(buffer, ins);
5997 shader_glsl_get_swizzle(input, FALSE, write_mask, dst_swizzle);
5999 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &sample_param);
6000 shader_addline(buffer, "interpolateAtSample(shader_in.reg%u, %s)%s);\n",
6001 input->reg.idx[0].offset, sample_param.param_str, dst_swizzle);
6004 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
6006 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6007 struct glsl_src_param coord_param, lod_param, sample_param;
6008 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6009 struct glsl_sample_function sample_function;
6010 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
6011 BOOL has_lod_param;
6013 if (wined3d_shader_instruction_has_texel_offset(ins))
6014 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6016 resource_idx = ins->src[1].reg.idx[0].offset;
6017 sampler_idx = WINED3D_SAMPLER_DEFAULT;
6019 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
6021 ERR("Invalid resource index %u.\n", resource_idx);
6022 return;
6024 has_lod_param = is_mipmapped(reg_maps->resource_info[resource_idx].type);
6026 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6027 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6028 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
6029 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6030 if (is_multisampled(reg_maps->resource_info[resource_idx].type))
6032 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &sample_param);
6033 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6034 NULL, NULL, NULL, &ins->texel_offset, "%s, %s", coord_param.param_str, sample_param.param_str);
6036 else
6038 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6039 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
6040 "%s", coord_param.param_str);
6042 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6045 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
6047 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
6048 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
6049 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6050 struct glsl_sample_function sample_function;
6051 DWORD flags = 0;
6053 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
6054 flags |= WINED3D_GLSL_SAMPLE_GRAD;
6055 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
6056 flags |= WINED3D_GLSL_SAMPLE_LOD;
6057 if (wined3d_shader_instruction_has_texel_offset(ins))
6058 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6060 resource_idx = ins->src[1].reg.idx[0].offset;
6061 sampler_idx = ins->src[2].reg.idx[0].offset;
6063 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6064 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
6066 switch (ins->handler_idx)
6068 case WINED3DSIH_SAMPLE:
6069 break;
6070 case WINED3DSIH_SAMPLE_B:
6071 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6072 lod_param_str = lod_param.param_str;
6073 break;
6074 case WINED3DSIH_SAMPLE_GRAD:
6075 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
6076 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
6077 dx_param_str = dx_param.param_str;
6078 dy_param_str = dy_param.param_str;
6079 break;
6080 case WINED3DSIH_SAMPLE_LOD:
6081 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
6082 lod_param_str = lod_param.param_str;
6083 break;
6084 default:
6085 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
6086 break;
6089 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6090 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
6091 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
6092 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6095 /* Unless EXT_texture_shadow_lod is available, GLSL doesn't provide a function
6096 * to sample from level zero with depth comparison for array textures and cube
6097 * textures. We use textureGrad*() to implement sample_c_lz in that case. */
6098 static void shader_glsl_gen_sample_c_lz_emulation(const struct wined3d_shader_instruction *ins,
6099 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function,
6100 unsigned int coord_size, const char *coord_param, const char *ref_param)
6102 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
6103 unsigned int deriv_size = wined3d_popcount(sample_function->deriv_mask);
6104 const struct wined3d_shader_texel_offset *offset = &ins->texel_offset;
6105 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6106 char dst_swizzle[6];
6108 WARN("Emitting textureGrad() for sample_c_lz.\n");
6110 shader_glsl_swizzle_to_str(WINED3DSP_NOSWIZZLE, FALSE, ins->dst[0].write_mask, dst_swizzle);
6111 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, sample_function->data_type);
6112 shader_addline(buffer, "vec4(textureGrad%s(%s_sampler%u, vec%u(%s, %s), vec%u(0.0), vec%u(0.0)",
6113 sample_function->offset_size ? "Offset" : "",
6114 shader_glsl_get_prefix(version->type), sampler_bind_idx,
6115 coord_size, coord_param, ref_param, deriv_size, deriv_size);
6116 if (sample_function->offset_size)
6118 int offset_immdata[4] = {offset->u, offset->v, offset->w};
6119 shader_addline(buffer, ", ");
6120 shader_glsl_append_imm_ivec(buffer, offset_immdata, sample_function->offset_size);
6122 shader_addline(buffer, "))%s);\n", dst_swizzle);
6125 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
6127 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6128 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
6129 const struct wined3d_shader_resource_info *resource_info;
6130 const struct wined3d_gl_info *gl_info = priv->gl_info;
6131 struct glsl_src_param coord_param, compare_param;
6132 struct glsl_sample_function sample_function;
6133 const char *lod_param = NULL;
6134 unsigned int coord_size;
6135 DWORD flags = 0;
6137 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
6139 lod_param = "0";
6140 flags |= WINED3D_GLSL_SAMPLE_LOD;
6143 if (wined3d_shader_instruction_has_texel_offset(ins))
6144 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
6146 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[1].reg)))
6147 return;
6148 resource_idx = ins->src[1].reg.idx[0].offset;
6149 sampler_idx = ins->src[2].reg.idx[0].offset;
6151 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
6152 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6153 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
6154 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
6155 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
6156 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ
6157 && !gl_info->supported[EXT_TEXTURE_SHADOW_LOD]
6158 && (resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
6159 || resource_info->type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE))
6161 shader_glsl_gen_sample_c_lz_emulation(ins, sampler_bind_idx, &sample_function,
6162 coord_size, coord_param.param_str, compare_param.param_str);
6164 else
6166 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
6167 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
6168 coord_size, coord_param.param_str, compare_param.param_str);
6170 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6173 static void shader_glsl_gather4(const struct wined3d_shader_instruction *ins)
6175 unsigned int resource_param_idx, resource_idx, sampler_idx, sampler_bind_idx, component_idx;
6176 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
6177 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6178 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6179 struct glsl_src_param coord_param, compare_param, offset_param;
6180 const struct wined3d_gl_info *gl_info = priv->gl_info;
6181 const struct wined3d_shader_resource_info *resource_info;
6182 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6183 unsigned int coord_size, offset_size;
6184 char dst_swizzle[6];
6185 BOOL has_offset;
6187 if (!gl_info->supported[ARB_TEXTURE_GATHER])
6189 FIXME("OpenGL implementation does not support textureGather.\n");
6190 return;
6193 has_offset = ins->handler_idx == WINED3DSIH_GATHER4_PO
6194 || ins->handler_idx == WINED3DSIH_GATHER4_PO_C
6195 || wined3d_shader_instruction_has_texel_offset(ins);
6197 resource_param_idx =
6198 (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C) ? 2 : 1;
6199 resource_idx = ins->src[resource_param_idx].reg.idx[0].offset;
6200 sampler_idx = ins->src[resource_param_idx + 1].reg.idx[0].offset;
6201 component_idx = shader_glsl_swizzle_get_component(ins->src[resource_param_idx + 1].swizzle, 0);
6202 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
6204 if (!(resource_info = shader_glsl_get_resource_info(ins, &ins->src[resource_param_idx].reg)))
6205 return;
6207 if (resource_info->type >= ARRAY_SIZE(resource_type_info))
6209 ERR("Unexpected resource type %#x.\n", resource_info->type);
6210 return;
6212 shader_glsl_get_coord_size(resource_info->type, &coord_size, &offset_size);
6214 shader_glsl_swizzle_to_str(ins->src[resource_param_idx].swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
6215 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], 0, resource_info->data_type);
6217 shader_glsl_add_src_param(ins, &ins->src[0], wined3d_mask_from_size(coord_size), &coord_param);
6219 shader_addline(buffer, "textureGather%s(%s_sampler%u, %s",
6220 has_offset ? "Offset" : "", prefix, sampler_bind_idx, coord_param.param_str);
6221 if (ins->handler_idx == WINED3DSIH_GATHER4_C || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6223 shader_glsl_add_src_param(ins, &ins->src[resource_param_idx + 2], WINED3DSP_WRITEMASK_0, &compare_param);
6224 shader_addline(buffer, ", %s", compare_param.param_str);
6226 if (ins->handler_idx == WINED3DSIH_GATHER4_PO || ins->handler_idx == WINED3DSIH_GATHER4_PO_C)
6228 shader_glsl_add_src_param(ins, &ins->src[1], wined3d_mask_from_size(offset_size), &offset_param);
6229 shader_addline(buffer, ", %s", offset_param.param_str);
6231 else if (has_offset)
6233 int offset_immdata[4] = {ins->texel_offset.u, ins->texel_offset.v, ins->texel_offset.w};
6234 shader_addline(buffer, ", ");
6235 shader_glsl_append_imm_ivec(buffer, offset_immdata, offset_size);
6237 if (component_idx)
6238 shader_addline(buffer, ", %u", component_idx);
6240 shader_addline(buffer, ")%s);\n", dst_swizzle);
6243 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
6245 /* FIXME: Make this work for more than just 2D textures */
6246 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6247 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6249 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
6251 char dst_mask[6];
6253 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6254 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
6255 ins->dst[0].reg.idx[0].offset, dst_mask);
6257 else
6259 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
6260 unsigned int reg = ins->src[0].reg.idx[0].offset;
6261 char dst_swizzle[6];
6263 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
6265 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
6267 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
6268 struct glsl_src_param div_param;
6269 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
6271 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
6273 if (mask_size > 1)
6274 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
6275 else
6276 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
6278 else
6280 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
6285 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
6286 * Take a 3-component dot product of the TexCoord[dstreg] and src,
6287 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
6288 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
6290 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6291 unsigned int sampler_idx = ins->dst[0].reg.idx[0].offset;
6292 struct glsl_sample_function sample_function;
6293 struct glsl_src_param src0_param;
6294 UINT mask_size;
6296 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6298 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
6299 * scalar, and projected sampling would require 4.
6301 * It is a dependent read - not valid with conditional NP2 textures
6303 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6304 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
6306 switch(mask_size)
6308 case 1:
6309 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6310 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
6311 break;
6313 case 2:
6314 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6315 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
6316 break;
6318 case 3:
6319 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
6320 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
6321 break;
6323 default:
6324 FIXME("Unexpected mask size %u\n", mask_size);
6325 break;
6327 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6330 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
6331 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
6332 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
6334 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6335 unsigned int dstreg = ins->dst[0].reg.idx[0].offset;
6336 struct glsl_src_param src0_param;
6337 DWORD dst_mask;
6338 unsigned int mask_size;
6340 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6341 mask_size = shader_glsl_get_write_mask_size(dst_mask);
6342 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6344 if (mask_size > 1) {
6345 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
6346 } else {
6347 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
6351 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
6352 * Calculate the depth as dst.x / dst.y */
6353 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
6355 struct glsl_dst_param dst_param;
6357 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6359 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
6360 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
6361 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
6362 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
6363 * >= 1.0 or < 0.0
6365 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
6366 dst_param.reg_name, dst_param.reg_name);
6369 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
6370 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
6371 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
6372 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
6374 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
6376 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6377 unsigned int dstreg = ins->dst[0].reg.idx[0].offset;
6378 struct glsl_src_param src0_param;
6380 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6382 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
6383 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
6386 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
6387 * Calculate the 1st of a 2-row matrix multiplication. */
6388 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
6390 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6391 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6392 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6393 struct glsl_src_param src0_param;
6395 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6396 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6399 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
6400 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
6401 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
6403 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6404 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6405 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6406 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6407 struct glsl_src_param src0_param;
6409 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6410 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
6411 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
6414 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
6416 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6417 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6418 struct glsl_sample_function sample_function;
6419 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6420 struct glsl_src_param src0_param;
6422 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6423 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6425 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6427 /* Sample the texture using the calculated coordinates */
6428 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
6429 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6432 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
6433 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
6434 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
6436 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6437 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
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(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6445 /* Dependent read, not valid with conditional NP2 */
6446 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6448 /* Sample the texture using the calculated coordinates */
6449 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
6450 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6452 tex_mx->current_row = 0;
6455 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
6456 * Perform the 3rd row of a 3x3 matrix multiply */
6457 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
6459 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6460 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6461 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6462 struct glsl_src_param src0_param;
6463 char dst_mask[6];
6465 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6467 shader_glsl_append_dst(ins->ctx->buffer, ins);
6468 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
6469 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
6471 tex_mx->current_row = 0;
6474 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
6475 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6476 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
6478 struct glsl_src_param src0_param;
6479 struct glsl_src_param src1_param;
6480 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6481 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6482 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6483 struct glsl_sample_function sample_function;
6484 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6485 char coord_mask[6];
6487 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6488 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
6490 /* Perform the last matrix multiply operation */
6491 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
6492 /* Reflection calculation */
6493 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
6495 /* Dependent read, not valid with conditional NP2 */
6496 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6497 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6499 /* Sample the texture */
6500 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6501 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6502 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6504 tex_mx->current_row = 0;
6507 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
6508 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
6509 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
6511 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
6512 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
6513 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
6514 struct glsl_sample_function sample_function;
6515 unsigned int reg = ins->dst[0].reg.idx[0].offset;
6516 struct glsl_src_param src0_param;
6517 char coord_mask[6];
6519 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
6521 /* Perform the last matrix multiply operation */
6522 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
6524 /* Construct the eye-ray vector from w coordinates */
6525 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
6526 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
6527 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
6529 /* Dependent read, not valid with conditional NP2 */
6530 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
6531 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
6533 /* Sample the texture using the calculated coordinates */
6534 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
6535 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
6536 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6538 tex_mx->current_row = 0;
6541 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
6542 * Apply a fake bump map transform.
6543 * texbem is pshader <= 1.3 only, this saves a few version checks
6545 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
6547 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6548 struct glsl_sample_function sample_function;
6549 struct glsl_src_param coord_param;
6550 unsigned int sampler_idx;
6551 DWORD mask;
6552 DWORD flags;
6553 char coord_mask[6];
6555 sampler_idx = ins->dst[0].reg.idx[0].offset;
6556 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
6557 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
6559 /* Dependent read, not valid with conditional NP2 */
6560 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6561 mask = sample_function.coord_mask;
6563 shader_glsl_write_mask_to_str(mask, coord_mask);
6565 /* With projected textures, texbem only divides the static texture coord,
6566 * not the displacement, so we can't let GL handle this. */
6567 if (flags & WINED3D_PSARGS_PROJECTED)
6569 DWORD div_mask=0;
6570 char coord_div_mask[3];
6571 switch (flags & ~WINED3D_PSARGS_PROJECTED)
6573 case WINED3D_TTFF_COUNT1:
6574 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
6575 break;
6576 case WINED3D_TTFF_COUNT2:
6577 div_mask = WINED3DSP_WRITEMASK_1;
6578 break;
6579 case WINED3D_TTFF_COUNT3:
6580 div_mask = WINED3DSP_WRITEMASK_2;
6581 break;
6582 case WINED3D_TTFF_COUNT4:
6583 case WINED3D_TTFF_DISABLE:
6584 div_mask = WINED3DSP_WRITEMASK_3;
6585 break;
6587 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
6588 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
6591 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
6593 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6594 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
6595 coord_param.param_str, coord_mask);
6597 if (ins->handler_idx == WINED3DSIH_TEXBEML)
6599 struct glsl_src_param luminance_param;
6600 struct glsl_dst_param dst_param;
6602 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
6603 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6605 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
6606 dst_param.reg_name, dst_param.mask_str,
6607 luminance_param.param_str, sampler_idx, sampler_idx);
6609 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6612 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
6614 unsigned int sampler_idx = ins->dst[0].reg.idx[0].offset;
6615 struct glsl_src_param src0_param, src1_param;
6617 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6618 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6620 shader_glsl_append_dst(ins->ctx->buffer, ins);
6621 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
6622 src0_param.param_str, sampler_idx, src1_param.param_str);
6625 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
6626 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
6627 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
6629 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
6630 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6631 struct glsl_sample_function sample_function;
6632 struct wined3d_string_buffer *reg_name;
6634 reg_name = string_buffer_get(priv->string_buffers);
6635 shader_glsl_get_register_name(&ins->src[0].reg, ins->src[0].reg.data_type, reg_name, NULL, ins->ctx);
6637 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6638 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6639 "%s.wx", reg_name->buffer);
6640 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6642 string_buffer_release(priv->string_buffers, reg_name);
6645 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
6646 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
6647 static void shader_glsl_texreg2gb(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.yz", 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_TEXREG2RGB instruction in GLSL
6666 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
6667 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
6669 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
6670 struct glsl_sample_function sample_function;
6671 struct glsl_src_param src0_param;
6673 /* Dependent read, not valid with conditional NP2 */
6674 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
6675 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
6677 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
6678 "%s", src0_param.param_str);
6679 shader_glsl_release_sample_function(ins->ctx, &sample_function);
6682 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
6683 * If any of the first 3 components are < 0, discard this pixel */
6684 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
6686 if (ins->ctx->reg_maps->shader_version.major >= 4)
6688 shader_glsl_generate_condition(ins);
6689 shader_addline(ins->ctx->buffer, " discard;\n");
6691 else
6693 struct glsl_dst_param dst_param;
6695 /* The argument is a destination parameter, and no writemasks are allowed */
6696 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
6698 /* 2.0 shaders compare all 4 components in texkill. */
6699 if (ins->ctx->reg_maps->shader_version.major >= 2)
6700 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
6701 /* 1.x shaders only compare the first 3 components, probably due to
6702 * the nature of the texkill instruction as a tex* instruction, and
6703 * phase, which kills all .w components. Even if all 4 components are
6704 * defined, only the first 3 are used. */
6705 else
6706 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
6710 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
6711 * dst = dot2(src0, src1) + src2 */
6712 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
6714 struct glsl_src_param src0_param;
6715 struct glsl_src_param src1_param;
6716 struct glsl_src_param src2_param;
6717 DWORD write_mask;
6718 unsigned int mask_size;
6720 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
6721 mask_size = shader_glsl_get_write_mask_size(write_mask);
6723 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
6724 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
6725 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
6727 if (mask_size > 1) {
6728 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
6729 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
6730 } else {
6731 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
6732 src0_param.param_str, src1_param.param_str, src2_param.param_str);
6736 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
6737 const struct wined3d_shader_signature *input_signature,
6738 const struct wined3d_shader_reg_maps *reg_maps,
6739 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info, BOOL unroll)
6741 unsigned int i;
6743 for (i = 0; i < input_signature->element_count; ++i)
6745 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6746 const char *semantic_name;
6747 UINT semantic_idx;
6748 char reg_mask[6];
6750 /* Unused */
6751 if (!(reg_maps->input_registers & (1u << input->register_idx)))
6752 continue;
6754 semantic_name = input->semantic_name;
6755 semantic_idx = input->semantic_idx;
6756 shader_glsl_write_mask_to_str(input->mask, reg_mask);
6758 if (args->vp_mode == WINED3D_VP_MODE_SHADER)
6760 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6762 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
6763 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6765 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6767 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
6769 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
6771 shader_addline(buffer, "ps_in[%u]%s = uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u);\n",
6772 input->register_idx, reg_mask);
6774 else if (input->sysval_semantic == WINED3D_SV_SAMPLE_INDEX)
6776 if (gl_info->supported[ARB_SAMPLE_SHADING])
6777 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_SampleID);\n",
6778 input->register_idx, reg_mask);
6779 else
6780 FIXME("ARB_sample_shading is not supported.\n");
6782 else if (input->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
6784 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
6785 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_Layer);\n",
6786 input->register_idx, reg_mask);
6787 else
6788 FIXME("ARB_fragment_layer_viewport is not supported.\n");
6790 else if (input->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
6792 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
6793 shader_addline(buffer, "ps_in[%u]%s = intBitsToFloat(gl_ViewportIndex);\n",
6794 input->register_idx, reg_mask);
6795 else
6796 FIXME("ARB_viewport_array is not supported.\n");
6798 else
6800 if (input->sysval_semantic)
6801 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
6802 shader_addline(buffer, unroll ? "ps_in[%u]%s = %s%u%s;\n" : "ps_in[%u]%s = %s[%u]%s;\n",
6803 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6804 shader_glsl_shader_input_name(gl_info),
6805 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
6808 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6810 if (args->pointsprite)
6811 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
6812 shader->u.ps.input_reg_map[input->register_idx]);
6813 else if (args->vp_mode == WINED3D_VP_MODE_NONE && args->texcoords_initialized & (1u << semantic_idx))
6814 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6815 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6816 needs_legacy_glsl_syntax(gl_info)
6817 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6818 else
6819 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6820 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6822 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6824 if (!semantic_idx)
6825 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6826 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6827 else if (semantic_idx == 1)
6828 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6829 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6830 else
6831 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6832 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6834 else
6836 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6837 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6842 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6844 struct glsl_program_key key;
6846 key.vs_id = entry->vs.id;
6847 key.hs_id = entry->hs.id;
6848 key.ds_id = entry->ds.id;
6849 key.gs_id = entry->gs.id;
6850 key.ps_id = entry->ps.id;
6851 key.cs_id = entry->cs.id;
6853 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6855 ERR("Failed to insert program entry.\n");
6859 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6860 const struct glsl_program_key *key)
6862 struct wine_rb_entry *entry;
6864 entry = wine_rb_get(&priv->program_lookup, key);
6865 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6868 /* Context activation is done by the caller. */
6869 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6870 struct glsl_shader_prog_link *entry)
6872 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6874 GL_EXTCALL(glDeleteProgram(entry->id));
6875 if (entry->vs.id)
6876 list_remove(&entry->vs.shader_entry);
6877 if (entry->hs.id)
6878 list_remove(&entry->hs.shader_entry);
6879 if (entry->ds.id)
6880 list_remove(&entry->ds.shader_entry);
6881 if (entry->gs.id)
6882 list_remove(&entry->gs.shader_entry);
6883 if (entry->ps.id)
6884 list_remove(&entry->ps.shader_entry);
6885 if (entry->cs.id)
6886 list_remove(&entry->cs.shader_entry);
6887 heap_free(entry);
6890 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6891 const struct wined3d_gl_info *gl_info, const unsigned int *map,
6892 const struct wined3d_shader_signature *input_signature,
6893 const struct wined3d_shader_reg_maps *reg_maps_in,
6894 const struct wined3d_shader_signature *output_signature,
6895 const struct wined3d_shader_reg_maps *reg_maps_out)
6897 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6898 const char *out_array_name = shader_glsl_shader_output_name(gl_info);
6899 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6900 unsigned int in_count = vec4_varyings(3, gl_info);
6901 unsigned int max_varyings = needs_legacy_glsl_syntax(gl_info) ? in_count + 2 : in_count;
6902 unsigned int in_idx;
6903 DWORD *set = NULL;
6904 unsigned int i, j;
6905 char reg_mask[6];
6907 set = heap_calloc(max_varyings, sizeof(*set));
6909 for (i = 0; i < input_signature->element_count; ++i)
6911 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6913 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6914 continue;
6916 in_idx = map[input->register_idx];
6917 /* Declared, but not read register */
6918 if (in_idx == ~0u)
6919 continue;
6920 if (in_idx >= max_varyings)
6922 FIXME("More input varyings declared than supported, expect issues.\n");
6923 continue;
6926 if (in_idx == in_count)
6927 string_buffer_sprintf(destination, "gl_FrontColor");
6928 else if (in_idx == in_count + 1)
6929 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6930 else
6931 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6933 if (!set[in_idx])
6934 set[in_idx] = ~0u;
6936 for (j = 0; j < output_signature->element_count; ++j)
6938 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6939 DWORD mask;
6941 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6942 || input->semantic_idx != output->semantic_idx
6943 || strcmp(input->semantic_name, output->semantic_name)
6944 || !(mask = input->mask & output->mask))
6945 continue;
6947 if (set[in_idx] == ~0u)
6948 set[in_idx] = 0;
6949 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6950 shader_glsl_write_mask_to_str(mask, reg_mask);
6952 shader_addline(buffer, "%s%s = outputs[%u]%s;\n",
6953 destination->buffer, reg_mask, output->register_idx, reg_mask);
6957 for (i = 0; i < max_varyings; ++i)
6959 unsigned int size;
6961 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6962 continue;
6964 if (set[i] == ~0u)
6965 set[i] = 0;
6967 size = 0;
6968 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6969 reg_mask[size++] = 'x';
6970 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6971 reg_mask[size++] = 'y';
6972 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6973 reg_mask[size++] = 'z';
6974 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6975 reg_mask[size++] = 'w';
6976 reg_mask[size] = '\0';
6978 if (i == in_count)
6979 string_buffer_sprintf(destination, "gl_FrontColor");
6980 else if (i == in_count + 1)
6981 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6982 else
6983 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6985 if (size == 1)
6986 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6987 else
6988 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6991 heap_free(set);
6992 string_buffer_release(&priv->string_buffers, destination);
6995 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6996 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6997 const struct wined3d_shader_reg_maps *reg_maps_out, const char *output_variable_name,
6998 BOOL rasterizer_setup)
7000 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7001 char reg_mask[6];
7002 unsigned int i;
7004 for (i = 0; i < output_signature->element_count; ++i)
7006 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7008 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7009 continue;
7011 if (output->stream_idx)
7012 continue;
7014 if (output->register_idx >= input_count)
7015 continue;
7017 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7019 shader_addline(buffer,
7020 rasterizer_setup ? "%s.reg%u%s = outputs[%u]%s;\n" : "%s.reg[%u]%s = outputs[%u]%s;\n",
7021 output_variable_name, output->register_idx, reg_mask, output->register_idx, reg_mask);
7025 static void shader_glsl_generate_clip_or_cull_distances(struct wined3d_string_buffer *buffer,
7026 const struct wined3d_shader_signature_element *element, DWORD clip_or_cull_distance_mask)
7028 unsigned int i, clip_or_cull_index;
7029 const char *name;
7030 char reg_mask[6];
7032 name = element->sysval_semantic == WINED3D_SV_CLIP_DISTANCE ? "Clip" : "Cull";
7033 /* Assign consecutive indices starting from 0. */
7034 clip_or_cull_index = element->semantic_idx ? wined3d_popcount(clip_or_cull_distance_mask & 0xf) : 0;
7035 for (i = 0; i < 4; ++i)
7037 if (!(element->mask & (WINED3DSP_WRITEMASK_0 << i)))
7038 continue;
7040 shader_glsl_write_mask_to_str(WINED3DSP_WRITEMASK_0 << i, reg_mask);
7041 shader_addline(buffer, "gl_%sDistance[%u] = outputs[%u]%s;\n",
7042 name, clip_or_cull_index, element->register_idx, reg_mask);
7043 ++clip_or_cull_index;
7047 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
7048 const struct wined3d_gl_info *gl_info, const unsigned int *map,
7049 const struct wined3d_shader_signature *input_signature,
7050 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
7051 const struct wined3d_shader_signature *output_signature,
7052 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
7054 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7055 const char *semantic_name;
7056 unsigned int semantic_idx;
7057 char reg_mask[6];
7058 unsigned int i;
7060 /* First, sort out position and point size system values. */
7061 for (i = 0; i < output_signature->element_count; ++i)
7063 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7065 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
7066 continue;
7068 if (output->stream_idx)
7069 continue;
7071 semantic_name = output->semantic_name;
7072 semantic_idx = output->semantic_idx;
7073 shader_glsl_write_mask_to_str(output->mask, reg_mask);
7075 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
7077 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7078 reg_mask, output->register_idx, reg_mask);
7080 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7082 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7083 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7085 else if (output->sysval_semantic == WINED3D_SV_RENDER_TARGET_ARRAY_INDEX && !semantic_idx)
7087 shader_addline(buffer, "gl_Layer = floatBitsToInt(outputs[%u])%s;\n",
7088 output->register_idx, reg_mask);
7090 else if (output->sysval_semantic == WINED3D_SV_VIEWPORT_ARRAY_INDEX && !semantic_idx)
7092 shader_addline(buffer, "gl_ViewportIndex = floatBitsToInt(outputs[%u])%s;\n",
7093 output->register_idx, reg_mask);
7095 else if (output->sysval_semantic == WINED3D_SV_CLIP_DISTANCE)
7097 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->clip_distance_mask);
7099 else if (output->sysval_semantic == WINED3D_SV_CULL_DISTANCE)
7101 shader_glsl_generate_clip_or_cull_distances(buffer, output, reg_maps_out->cull_distance_mask);
7103 else if (output->sysval_semantic)
7105 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
7109 /* Then, setup the pixel shader input. */
7110 if (reg_maps_out->shader_version.major < 4)
7111 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
7112 output_signature, reg_maps_out);
7113 else
7114 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "shader_out", TRUE);
7117 /* Context activation is done by the caller. */
7118 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
7119 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
7120 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
7122 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7123 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
7124 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7125 const char *semantic_name;
7126 UINT semantic_idx;
7127 char reg_mask[6];
7128 unsigned int i;
7129 GLuint ret;
7131 string_buffer_clear(buffer);
7133 shader_glsl_add_version_declaration(buffer, gl_info);
7135 shader_addline(buffer, "invariant gl_Position;\n");
7137 if (per_vertex_point_size)
7139 shader_addline(buffer, "uniform struct\n{\n");
7140 shader_addline(buffer, " float size_min;\n");
7141 shader_addline(buffer, " float size_max;\n");
7142 shader_addline(buffer, "} ffp_point;\n");
7145 if (ps_major < 3)
7147 DWORD colors_written_mask[2] = {0};
7148 DWORD texcoords_written_mask[WINED3D_MAX_TEXTURES] = {0};
7150 if (!legacy_syntax)
7152 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
7153 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
7154 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7155 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7158 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7160 for (i = 0; i < vs->output_signature.element_count; ++i)
7162 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
7163 DWORD write_mask;
7165 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
7166 continue;
7168 semantic_name = output->semantic_name;
7169 semantic_idx = output->semantic_idx;
7170 write_mask = output->mask;
7171 shader_glsl_write_mask_to_str(write_mask, reg_mask);
7173 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
7175 if (legacy_syntax)
7176 shader_addline(buffer, "gl_Front%sColor%s = outputs[%u]%s;\n",
7177 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
7178 else
7179 shader_addline(buffer, "ffp_varying_%s%s = clamp(outputs[%u]%s, 0.0, 1.0);\n",
7180 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
7182 colors_written_mask[semantic_idx] = write_mask;
7184 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
7186 shader_addline(buffer, "gl_Position%s = outputs[%u]%s;\n",
7187 reg_mask, output->register_idx, reg_mask);
7189 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
7191 if (semantic_idx < WINED3D_MAX_TEXTURES)
7193 shader_addline(buffer, "%s[%u]%s = outputs[%u]%s;\n",
7194 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord",
7195 semantic_idx, reg_mask, output->register_idx, reg_mask);
7196 texcoords_written_mask[semantic_idx] = write_mask;
7199 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
7201 shader_addline(buffer, "gl_PointSize = clamp(outputs[%u].%c, "
7202 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
7204 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
7206 shader_addline(buffer, "%s = clamp(outputs[%u].%c, 0.0, 1.0);\n",
7207 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
7208 output->register_idx, reg_mask[1]);
7212 for (i = 0; i < 2; ++i)
7214 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7216 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7217 if (!i)
7218 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
7219 legacy_syntax ? "gl_FrontColor" : "ffp_varying_diffuse",
7220 reg_mask, reg_mask);
7221 else
7222 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
7223 legacy_syntax ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
7224 reg_mask, reg_mask);
7227 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
7229 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
7230 continue;
7232 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
7234 if (!shader_glsl_full_ffp_varyings(gl_info) && !texcoords_written_mask[i])
7235 continue;
7237 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
7238 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
7239 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
7243 else
7245 unsigned int in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
7247 shader_glsl_declare_shader_outputs(gl_info, buffer, in_count, FALSE, NULL);
7248 shader_addline(buffer, "void setup_vs_output(in vec4 outputs[%u])\n{\n", vs->limits->packed_output);
7249 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
7250 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
7253 shader_addline(buffer, "}\n");
7255 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7256 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
7257 shader_glsl_compile(gl_info, ret, buffer->buffer);
7259 return ret;
7262 static void shader_glsl_generate_stream_output_setup(struct wined3d_string_buffer *buffer,
7263 const struct wined3d_shader *shader)
7265 const struct wined3d_stream_output_desc *so_desc = shader->u.gs.so_desc;
7266 unsigned int i, register_idx, component_idx;
7268 shader_addline(buffer, "out shader_in_out\n{\n");
7269 for (i = 0; i < so_desc->element_count; ++i)
7271 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7273 if (e->stream_idx)
7275 FIXME("Unhandled stream %u.\n", e->stream_idx);
7276 continue;
7278 if (!e->semantic_name)
7279 continue;
7280 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7281 continue;
7283 if (component_idx || e->component_count != 4)
7285 if (e->component_count == 1)
7286 shader_addline(buffer, "float");
7287 else
7288 shader_addline(buffer, "vec%u", e->component_count);
7290 shader_addline(buffer, " reg%u_%u_%u;\n",
7291 register_idx, component_idx, component_idx + e->component_count - 1);
7293 else
7295 shader_addline(buffer, "vec4 reg%u;\n", register_idx);
7298 shader_addline(buffer, "} shader_out;\n");
7300 shader_addline(buffer, "void setup_gs_output(in vec4 outputs[%u])\n{\n",
7301 shader->limits->packed_output);
7302 for (i = 0; i < so_desc->element_count; ++i)
7304 const struct wined3d_stream_output_element *e = &so_desc->elements[i];
7306 if (e->stream_idx)
7308 FIXME("Unhandled stream %u.\n", e->stream_idx);
7309 continue;
7311 if (!e->semantic_name)
7312 continue;
7313 if (!shader_get_stream_output_register_info(shader, e, &register_idx, &component_idx))
7314 continue;
7316 if (component_idx || e->component_count != 4)
7318 DWORD write_mask;
7319 char str_mask[6];
7321 write_mask = wined3d_mask_from_size(e->component_count) << component_idx;
7322 shader_glsl_write_mask_to_str(write_mask, str_mask);
7323 shader_addline(buffer, "shader_out.reg%u_%u_%u = outputs[%u]%s;\n",
7324 register_idx, component_idx, component_idx + e->component_count - 1,
7325 register_idx, str_mask);
7327 else
7329 shader_addline(buffer, "shader_out.reg%u = outputs[%u];\n", register_idx, register_idx);
7332 shader_addline(buffer, "}\n");
7335 static void shader_glsl_generate_sm4_output_setup(struct shader_glsl_priv *priv,
7336 const struct wined3d_shader *shader, unsigned int input_count,
7337 const struct wined3d_gl_info *gl_info, BOOL rasterizer_setup, const uint32_t *interpolation_mode)
7339 const char *prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
7340 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7342 if (rasterizer_setup)
7343 input_count = min(vec4_varyings(4, gl_info), input_count);
7345 if (input_count)
7346 shader_glsl_declare_shader_outputs(gl_info, buffer, input_count, rasterizer_setup, interpolation_mode);
7348 shader_addline(buffer, "void setup_%s_output(in vec4 outputs[%u])\n{\n",
7349 prefix, shader->limits->packed_output);
7351 if (rasterizer_setup)
7352 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
7353 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
7354 else
7355 shader_glsl_setup_sm4_shader_output(priv, input_count, &shader->output_signature,
7356 &shader->reg_maps, "shader_out", rasterizer_setup);
7358 shader_addline(buffer, "}\n");
7361 static void shader_glsl_generate_patch_constant_name(struct wined3d_string_buffer *buffer,
7362 const struct wined3d_shader_signature_element *constant, unsigned int *user_constant_idx,
7363 const char *reg_mask)
7365 if (!constant->sysval_semantic)
7367 shader_addline(buffer, "user_patch_constant[%u]%s", (*user_constant_idx)++, reg_mask);
7368 return;
7371 switch (constant->sysval_semantic)
7373 case WINED3D_SV_TESS_FACTOR_QUADEDGE:
7374 case WINED3D_SV_TESS_FACTOR_TRIEDGE:
7375 case WINED3D_SV_TESS_FACTOR_LINEDET:
7376 case WINED3D_SV_TESS_FACTOR_LINEDEN:
7377 shader_addline(buffer, "gl_TessLevelOuter[%u]", constant->semantic_idx);
7378 break;
7379 case WINED3D_SV_TESS_FACTOR_QUADINT:
7380 case WINED3D_SV_TESS_FACTOR_TRIINT:
7381 shader_addline(buffer, "gl_TessLevelInner[%u]", constant->semantic_idx);
7382 break;
7383 default:
7384 FIXME("Unhandled sysval semantic %#x.\n", constant->sysval_semantic);
7385 shader_addline(buffer, "vec4(0.0)%s", reg_mask);
7389 static void shader_glsl_generate_patch_constant_setup(struct wined3d_string_buffer *buffer,
7390 const struct wined3d_shader_signature *signature, BOOL input_setup)
7392 unsigned int i, register_count, user_constant_index, user_constant_count;
7394 register_count = user_constant_count = 0;
7395 for (i = 0; i < signature->element_count; ++i)
7397 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7398 register_count = max(constant->register_idx + 1, register_count);
7399 if (!constant->sysval_semantic)
7400 ++user_constant_count;
7403 if (user_constant_count)
7404 shader_addline(buffer, "patch %s vec4 user_patch_constant[%u];\n",
7405 input_setup ? "in" : "out", user_constant_count);
7406 if (input_setup)
7407 shader_addline(buffer, "vec4 vpc[%u];\n", register_count);
7409 shader_addline(buffer, "void setup_patch_constant_%s()\n{\n", input_setup ? "input" : "output");
7410 for (i = 0, user_constant_index = 0; i < signature->element_count; ++i)
7412 const struct wined3d_shader_signature_element *constant = &signature->elements[i];
7413 char reg_mask[6];
7415 shader_glsl_write_mask_to_str(constant->mask, reg_mask);
7417 if (input_setup)
7418 shader_addline(buffer, "vpc[%u]%s", constant->register_idx, reg_mask);
7419 else
7420 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7422 shader_addline(buffer, " = ");
7424 if (input_setup)
7425 shader_glsl_generate_patch_constant_name(buffer, constant, &user_constant_index, reg_mask);
7426 else
7427 shader_addline(buffer, "hs_out[%u]%s", constant->register_idx, reg_mask);
7429 shader_addline(buffer, ";\n");
7431 shader_addline(buffer, "}\n");
7434 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
7435 const struct wined3d_gl_info *gl_info)
7437 const char *output = get_fragment_output(gl_info);
7439 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
7440 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
7441 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
7442 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
7443 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
7444 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
7447 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
7448 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
7450 const char *output = get_fragment_output(gl_info);
7452 switch (mode)
7454 case WINED3D_FFP_PS_FOG_OFF:
7455 return;
7457 case WINED3D_FFP_PS_FOG_LINEAR:
7458 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
7459 break;
7461 case WINED3D_FFP_PS_FOG_EXP:
7462 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
7463 break;
7465 case WINED3D_FFP_PS_FOG_EXP2:
7466 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
7467 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
7468 break;
7470 default:
7471 ERR("Invalid fog mode %#x.\n", mode);
7472 return;
7475 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
7476 output, output);
7479 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
7480 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
7482 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
7483 * flipping all the operators here, just negate the comparison below. */
7484 static const char * const comparison_operator[] =
7486 "", /* WINED3D_CMP_NEVER */
7487 "<", /* WINED3D_CMP_LESS */
7488 "==", /* WINED3D_CMP_EQUAL */
7489 "<=", /* WINED3D_CMP_LESSEQUAL */
7490 ">", /* WINED3D_CMP_GREATER */
7491 "!=", /* WINED3D_CMP_NOTEQUAL */
7492 ">=", /* WINED3D_CMP_GREATEREQUAL */
7493 "" /* WINED3D_CMP_ALWAYS */
7496 if (alpha_func == WINED3D_CMP_ALWAYS)
7497 return;
7499 if (alpha_func != WINED3D_CMP_NEVER)
7500 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
7501 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
7502 shader_addline(buffer, " discard;\n");
7505 static void shader_glsl_generate_colour_key_test(struct wined3d_string_buffer *buffer,
7506 const char *colour, const char *colour_key_low, const char *colour_key_high)
7508 shader_addline(buffer, "if (all(greaterThanEqual(%s, %s)) && all(lessThan(%s, %s)))\n",
7509 colour, colour_key_low, colour, colour_key_high);
7510 shader_addline(buffer, " discard;\n");
7513 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
7514 const struct wined3d_gl_info *gl_info)
7516 if (gl_info->supported[ARB_CULL_DISTANCE])
7517 shader_addline(buffer, "#extension GL_ARB_cull_distance : enable\n");
7518 if (gl_info->supported[ARB_GPU_SHADER5])
7519 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
7520 if (gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS])
7521 shader_addline(buffer, "#extension GL_ARB_shader_atomic_counters : enable\n");
7522 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
7523 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
7524 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
7525 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
7526 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
7527 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
7528 if (gl_info->supported[ARB_SHADER_STORAGE_BUFFER_OBJECT])
7529 shader_addline(buffer, "#extension GL_ARB_shader_storage_buffer_object : enable\n");
7530 if (gl_info->supported[ARB_SHADER_TEXTURE_IMAGE_SAMPLES])
7531 shader_addline(buffer, "#extension GL_ARB_shader_texture_image_samples : enable\n");
7532 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
7533 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
7534 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
7535 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
7536 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
7537 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
7538 if (gl_info->supported[ARB_TEXTURE_GATHER])
7539 shader_addline(buffer, "#extension GL_ARB_texture_gather : enable\n");
7540 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
7541 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
7542 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
7543 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
7544 if (gl_info->supported[ARB_VIEWPORT_ARRAY])
7545 shader_addline(buffer, "#extension GL_ARB_viewport_array : enable\n");
7546 if (gl_info->supported[EXT_GPU_SHADER4])
7547 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
7548 if (gl_info->supported[EXT_TEXTURE_ARRAY])
7549 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
7550 if (gl_info->supported[EXT_TEXTURE_SHADOW_LOD])
7551 shader_addline(buffer, "#extension GL_EXT_texture_shadow_lod : enable\n");
7554 static void shader_glsl_generate_color_output(struct wined3d_string_buffer *buffer,
7555 const struct wined3d_gl_info *gl_info, const struct wined3d_shader *shader,
7556 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7558 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7559 struct wined3d_string_buffer *src, *assignment;
7560 enum wined3d_data_type dst_data_type;
7561 const char *swizzle;
7562 unsigned int i;
7564 if (output_signature->element_count)
7566 src = string_buffer_get(string_buffers);
7567 assignment = string_buffer_get(string_buffers);
7568 for (i = 0; i < output_signature->element_count; ++i)
7570 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7572 /* register_idx is set to ~0u for non-color outputs. */
7573 if (output->register_idx == ~0u)
7574 continue;
7575 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7577 FIXME("Unhandled component type %#x.\n", output->component_type);
7578 continue;
7580 dst_data_type = component_type_info[output->component_type].data_type;
7581 shader_addline(buffer, "color_out%u = ", output->semantic_idx);
7582 string_buffer_sprintf(src, "ps_out[%u]", output->semantic_idx);
7583 shader_glsl_sprintf_cast(assignment, src->buffer, dst_data_type, WINED3D_DATA_FLOAT, 4);
7584 swizzle = args->rt_alpha_swizzle & (1u << output->semantic_idx) ? ".argb" : "";
7585 shader_addline(buffer, "%s%s;\n", assignment->buffer, swizzle);
7587 string_buffer_release(string_buffers, src);
7588 string_buffer_release(string_buffers, assignment);
7590 else
7592 uint32_t mask = shader->reg_maps.rt_mask;
7594 while (mask)
7596 i = wined3d_bit_scan(&mask);
7597 swizzle = args->rt_alpha_swizzle & (1u << i) ? ".argb" : "";
7598 shader_addline(buffer, "color_out%u = ps_out[%u]%s;\n", i, i, swizzle);
7603 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
7604 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7605 const struct ps_compile_args *args, struct wined3d_string_buffer_list *string_buffers)
7607 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7609 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
7610 if (reg_maps->shader_version.major < 2)
7611 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
7613 if (args->srgb_correction)
7614 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7616 /* SM < 3 does not replace the fog stage. */
7617 if (reg_maps->shader_version.major < 3)
7618 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
7620 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
7622 if (reg_maps->sample_mask)
7623 shader_addline(buffer, "gl_SampleMask[0] = floatBitsToInt(sample_mask);\n");
7625 if (!use_legacy_fragment_output(gl_info))
7626 shader_glsl_generate_color_output(buffer, gl_info, shader, args, string_buffers);
7629 /* Context activation is done by the caller. */
7630 static GLuint shader_glsl_generate_fragment_shader(const struct wined3d_context_gl *context_gl,
7631 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7632 const struct wined3d_shader *shader, const struct ps_compile_args *args,
7633 struct ps_np2fixup_info *np2fixup_info)
7635 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7636 const struct wined3d_shader_version *version = &reg_maps->shader_version;
7637 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
7638 const char *prefix = shader_glsl_get_prefix(version->type);
7639 BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7640 unsigned int i, extra_constants_needed = 0;
7641 struct shader_glsl_ctx_priv priv_ctx;
7642 GLuint shader_id;
7643 uint32_t map;
7645 memset(&priv_ctx, 0, sizeof(priv_ctx));
7646 priv_ctx.gl_info = gl_info;
7647 priv_ctx.cur_ps_args = args;
7648 priv_ctx.cur_np2fixup_info = np2fixup_info;
7649 priv_ctx.string_buffers = string_buffers;
7651 shader_glsl_add_version_declaration(buffer, gl_info);
7653 shader_glsl_enable_extensions(buffer, gl_info);
7654 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7655 shader_addline(buffer, "#extension GL_ARB_conservative_depth : enable\n");
7656 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
7657 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
7658 if (shader_glsl_use_explicit_attrib_location(gl_info))
7659 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7660 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7661 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
7662 if (gl_info->supported[ARB_FRAGMENT_LAYER_VIEWPORT])
7663 shader_addline(buffer, "#extension GL_ARB_fragment_layer_viewport : enable\n");
7664 if (gl_info->supported[ARB_SAMPLE_SHADING])
7665 shader_addline(buffer, "#extension GL_ARB_sample_shading : enable\n");
7666 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
7667 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
7668 /* The spec says that it doesn't have to be explicitly enabled, but the
7669 * nvidia drivers write a warning if we don't do so. */
7670 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7671 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7673 /* Base Declarations */
7674 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
7676 if (gl_info->supported[ARB_CONSERVATIVE_DEPTH])
7678 if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTGE)
7679 shader_addline(buffer, "layout (depth_greater) out float gl_FragDepth;\n");
7680 else if (shader->u.ps.depth_output == WINED3DSPR_DEPTHOUTLE)
7681 shader_addline(buffer, "layout (depth_less) out float gl_FragDepth;\n");
7684 /* Declare uniforms for NP2 texcoord fixup:
7685 * This is NOT done inside the loop that declares the texture samplers
7686 * since the NP2 fixup code is currently only used for the GeforceFX
7687 * series and when forcing the ARB_npot extension off. Modern cards just
7688 * skip the code anyway, so put it inside a separate loop. */
7689 if (args->np2_fixup)
7691 struct ps_np2fixup_info *fixup = priv_ctx.cur_np2fixup_info;
7692 unsigned int cur = 0;
7694 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
7695 * while D3D has them in the (normalized) [0,1]x[0,1] range.
7696 * samplerNP2Fixup stores texture dimensions and is updated through
7697 * shader_glsl_load_np2fixup_constants when the sampler changes. */
7699 for (i = 0; i < shader->limits->sampler; ++i)
7701 enum wined3d_shader_resource_type resource_type;
7703 resource_type = pixelshader_get_resource_type(reg_maps, i, args->tex_types);
7705 if (!resource_type || !(args->np2_fixup & (1u << i)))
7706 continue;
7708 if (resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
7710 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
7711 continue;
7714 fixup->idx[i] = cur++;
7717 fixup->num_consts = (cur + 1) >> 1;
7718 fixup->active = args->np2_fixup;
7719 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
7722 if (version->major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7724 shader_addline(buffer, "uniform struct\n{\n");
7725 shader_addline(buffer, " vec4 color;\n");
7726 shader_addline(buffer, " float density;\n");
7727 shader_addline(buffer, " float end;\n");
7728 shader_addline(buffer, " float scale;\n");
7729 shader_addline(buffer, "} ffp_fog;\n");
7731 if (legacy_syntax)
7733 if (glsl_is_color_reg_read(shader, 0))
7734 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7735 if (glsl_is_color_reg_read(shader, 1))
7736 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7737 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7738 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7740 else
7742 if (glsl_is_color_reg_read(shader, 0))
7743 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
7744 if (glsl_is_color_reg_read(shader, 1))
7745 declare_in_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
7746 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7747 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
7748 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7752 if (version->major >= 3)
7754 unsigned int in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
7756 if (args->vp_mode == WINED3D_VP_MODE_SHADER && reg_maps->input_registers)
7757 shader_glsl_declare_shader_inputs(gl_info, buffer, in_count,
7758 shader->u.ps.interpolation_mode, version->major >= 4);
7759 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
7762 map = reg_maps->bumpmat;
7763 while (map)
7765 i = wined3d_bit_scan(&map);
7766 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
7768 if (reg_maps->luminanceparams & (1u << i))
7770 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
7771 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
7772 extra_constants_needed++;
7775 extra_constants_needed++;
7778 if (args->srgb_correction)
7780 shader_addline(buffer, "const vec4 srgb_const0 = ");
7781 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
7782 shader_addline(buffer, ";\n");
7783 shader_addline(buffer, "const vec4 srgb_const1 = ");
7784 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
7785 shader_addline(buffer, ";\n");
7787 if ((reg_maps->usesdsy && wined3d_settings.offscreen_rendering_mode != ORM_FBO)
7788 || (reg_maps->vpos && !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS]))
7790 ++extra_constants_needed;
7791 shader_addline(buffer, "uniform vec4 ycorrection;\n");
7793 if (reg_maps->vpos)
7795 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7797 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7798 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
7799 args->y_correction ? "origin_upper_left, " : "");
7800 else if (args->y_correction)
7801 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
7803 shader_addline(buffer, "vec4 vpos;\n");
7806 if (args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
7807 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7809 if (!use_legacy_fragment_output(gl_info))
7811 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
7813 if (args->dual_source_blend)
7814 shader_addline(buffer, "vec4 ps_out[2];\n");
7815 else
7816 shader_addline(buffer, "vec4 ps_out[%u];\n", gl_info->limits.buffers);
7817 if (output_signature->element_count)
7819 for (i = 0; i < output_signature->element_count; ++i)
7821 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
7823 if (output->register_idx == ~0u)
7824 continue;
7825 if ((unsigned int)output->component_type >= ARRAY_SIZE(component_type_info))
7827 FIXME("Unhandled component type %#x.\n", output->component_type);
7828 continue;
7830 if (shader_glsl_use_explicit_attrib_location(gl_info))
7832 if (args->dual_source_blend)
7833 shader_addline(buffer, "layout(location = 0, index = %u) ", output->semantic_idx);
7834 else
7835 shader_addline(buffer, "layout(location = %u) ", output->semantic_idx);
7837 shader_addline(buffer, "out %s4 color_out%u;\n",
7838 component_type_info[output->component_type].glsl_vector_type, output->semantic_idx);
7841 else
7843 uint32_t mask = reg_maps->rt_mask;
7845 while (mask)
7847 i = wined3d_bit_scan(&mask);
7848 if (shader_glsl_use_explicit_attrib_location(gl_info))
7850 if (args->dual_source_blend)
7851 shader_addline(buffer, "layout(location = 0, index = %u) ", i);
7852 else
7853 shader_addline(buffer, "layout(location = %u) ", i);
7855 shader_addline(buffer, "out vec4 color_out%u;\n", i);
7860 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
7861 FIXME("Insufficient uniforms to run this shader.\n");
7863 if (shader->u.ps.force_early_depth_stencil)
7864 shader_addline(buffer, "layout(early_fragment_tests) in;\n");
7866 shader_addline(buffer, "void main()\n{\n");
7868 if (reg_maps->sample_mask)
7869 shader_addline(buffer, "float sample_mask = uintBitsToFloat(0xffffffffu);\n");
7871 /* Direct3D applications expect integer vPos values, while OpenGL drivers
7872 * add approximately 0.5. This causes off-by-one problems as spotted by
7873 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
7874 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
7875 * causes precision troubles when we just subtract 0.5.
7877 * To deal with that, just floor() the position. This will eliminate the
7878 * fraction on all cards.
7880 * TODO: Test how this behaves with multisampling.
7882 * An advantage of floor is that it works even if the driver doesn't add
7883 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
7884 * to return in gl_FragCoord, even though coordinates specify the pixel
7885 * centers instead of the pixel corners. This code will behave correctly
7886 * on drivers that returns integer values. */
7887 if (reg_maps->vpos)
7889 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
7890 shader_addline(buffer, "vpos = gl_FragCoord;\n");
7891 else if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
7892 shader_addline(buffer,
7893 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
7894 else
7895 shader_addline(buffer,
7896 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
7899 if (reg_maps->shader_version.major < 3 || args->vp_mode != WINED3D_VP_MODE_SHADER)
7901 unsigned int i;
7903 if (legacy_syntax)
7905 if (glsl_is_color_reg_read(shader, 0))
7906 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7907 if (glsl_is_color_reg_read(shader, 1))
7908 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7911 map = reg_maps->texcoord;
7912 while (map)
7914 i = wined3d_bit_scan(&map);
7915 if (args->pointsprite)
7916 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
7917 else if (args->texcoords_initialized & (1u << i))
7918 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
7919 legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", i);
7920 else
7921 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
7922 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
7925 if (legacy_syntax)
7926 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7929 /* Pack 3.0 inputs */
7930 if (reg_maps->shader_version.major >= 3)
7931 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info,
7932 reg_maps->shader_version.major >= 4);
7934 /* Base Shader Body */
7935 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
7936 return 0;
7938 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
7939 if (reg_maps->shader_version.major < 4)
7940 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args, string_buffers);
7942 shader_addline(buffer, "}\n");
7944 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7945 TRACE("Compiling shader object %u.\n", shader_id);
7946 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7948 return shader_id;
7951 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
7952 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
7953 const struct vs_compile_args *args)
7955 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
7956 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
7957 unsigned int i;
7959 /* Unpack outputs. */
7960 shader_addline(buffer, "setup_vs_output(vs_out);\n");
7962 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
7963 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
7964 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
7965 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
7967 if (reg_maps->shader_version.major < 3)
7969 if (args->fog_src == VS_FOG_Z)
7970 shader_addline(buffer, "%s = gl_Position.z;\n",
7971 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7972 else if (!reg_maps->fog)
7973 shader_addline(buffer, "%s = 0.0;\n",
7974 legacy_syntax ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
7977 /* We always store the clipplanes without y inversion. */
7978 if (args->clip_enabled)
7980 if (legacy_syntax)
7981 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
7982 else
7983 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7984 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
7987 if (args->point_size && !args->per_vertex_point_size)
7988 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
7990 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
7991 shader_glsl_fixup_position(buffer, FALSE);
7994 /* Context activation is done by the caller. */
7995 static GLuint shader_glsl_generate_vertex_shader(const struct wined3d_context_gl *context_gl,
7996 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
7998 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
7999 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8000 const struct wined3d_shader_version *version = &reg_maps->shader_version;
8001 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8002 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8003 struct shader_glsl_ctx_priv priv_ctx;
8004 GLuint shader_id;
8005 unsigned int i;
8007 memset(&priv_ctx, 0, sizeof(priv_ctx));
8008 priv_ctx.gl_info = gl_info;
8009 priv_ctx.cur_vs_args = args;
8010 priv_ctx.string_buffers = string_buffers;
8012 shader_glsl_add_version_declaration(buffer, gl_info);
8014 shader_glsl_enable_extensions(buffer, gl_info);
8015 if (gl_info->supported[ARB_DRAW_INSTANCED])
8016 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
8017 if (shader_glsl_use_explicit_attrib_location(gl_info))
8018 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
8019 if (gl_info->supported[ARB_SHADER_VIEWPORT_LAYER_ARRAY])
8020 shader_addline(buffer, "#extension GL_ARB_shader_viewport_layer_array : enable\n");
8022 /* Base Declarations */
8023 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8025 for (i = 0; i < shader->input_signature.element_count; ++i)
8026 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
8028 if (args->point_size && !args->per_vertex_point_size)
8030 shader_addline(buffer, "uniform struct\n{\n");
8031 shader_addline(buffer, " float size;\n");
8032 shader_addline(buffer, " float size_min;\n");
8033 shader_addline(buffer, " float size_max;\n");
8034 shader_addline(buffer, "} ffp_point;\n");
8037 if (!needs_legacy_glsl_syntax(gl_info))
8039 if (args->clip_enabled)
8040 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
8042 if (version->major < 3)
8044 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_diffuse;\n");
8045 declare_out_varying(gl_info, buffer, args->flatshading, "vec4 ffp_varying_specular;\n");
8046 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
8047 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
8051 if (version->major < 4)
8052 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
8054 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8055 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8057 if (reg_maps->shader_version.major >= 4)
8058 shader_glsl_generate_sm4_output_setup(priv, shader, args->next_shader_input_count,
8059 gl_info, args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8061 shader_addline(buffer, "void main()\n{\n");
8063 if (reg_maps->input_rel_addressing)
8065 unsigned int highest_input_register = wined3d_log2i(reg_maps->input_registers);
8066 shader_addline(buffer, "vec4 vs_in[%u];\n", highest_input_register + 1);
8067 for (i = 0; i < shader->input_signature.element_count; ++i)
8069 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
8070 shader_addline(buffer, "vs_in[%u] = vs_in%u;\n", e->register_idx, e->register_idx);
8074 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8075 return 0;
8077 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
8078 if (reg_maps->shader_version.major < 4)
8079 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
8081 shader_addline(buffer, "}\n");
8083 shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
8084 TRACE("Compiling shader object %u.\n", shader_id);
8085 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8087 return shader_id;
8090 static void shader_glsl_generate_default_control_point_phase(const struct wined3d_shader *shader,
8091 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps)
8093 const struct wined3d_shader_signature *output_signature = &shader->output_signature;
8094 char reg_mask[6];
8095 unsigned int i;
8097 for (i = 0; i < output_signature->element_count; ++i)
8099 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
8101 shader_glsl_write_mask_to_str(output->mask, reg_mask);
8102 shader_addline(buffer, "shader_out[gl_InvocationID].reg[%u]%s = shader_in[gl_InvocationID].reg[%u]%s;\n",
8103 output->register_idx, reg_mask, output->register_idx, reg_mask);
8107 static HRESULT shader_glsl_generate_shader_phase(const struct wined3d_shader *shader,
8108 struct wined3d_string_buffer *buffer, const struct wined3d_shader_reg_maps *reg_maps,
8109 struct shader_glsl_ctx_priv *priv_ctx, const struct wined3d_shader_phase *phase,
8110 const char *phase_name, unsigned phase_idx)
8112 unsigned int i;
8113 HRESULT hr;
8115 shader_addline(buffer, "void hs_%s_phase%u(%s)\n{\n",
8116 phase_name, phase_idx, phase->instance_count ? "int phase_instance_id" : "");
8117 for (i = 0; i < phase->temporary_count; ++i)
8118 shader_addline(buffer, "vec4 R%u;\n", i);
8119 hr = shader_generate_code(shader, buffer, reg_maps, priv_ctx, phase->start, phase->end);
8120 shader_addline(buffer, "}\n");
8121 return hr;
8124 static void shader_glsl_generate_shader_phase_invocation(struct wined3d_string_buffer *buffer,
8125 const struct wined3d_shader_phase *phase, const char *phase_name, unsigned int phase_idx)
8127 if (phase->instance_count)
8129 shader_addline(buffer, "for (int i = 0; i < %u; ++i)\n{\n", phase->instance_count);
8130 shader_addline(buffer, "hs_%s_phase%u(i);\n", phase_name, phase_idx);
8131 shader_addline(buffer, "}\n");
8133 else
8135 shader_addline(buffer, "hs_%s_phase%u();\n", phase_name, phase_idx);
8139 static GLuint shader_glsl_generate_hull_shader(const struct wined3d_context_gl *context_gl,
8140 struct shader_glsl_priv *priv, const struct wined3d_shader *shader)
8142 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8143 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8144 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8145 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8146 const struct wined3d_hull_shader *hs = &shader->u.hs;
8147 const struct wined3d_shader_phase *phase;
8148 struct shader_glsl_ctx_priv priv_ctx;
8149 GLuint shader_id;
8150 unsigned int i;
8152 memset(&priv_ctx, 0, sizeof(priv_ctx));
8153 priv_ctx.gl_info = gl_info;
8154 priv_ctx.string_buffers = string_buffers;
8156 shader_glsl_add_version_declaration(buffer, gl_info);
8158 shader_glsl_enable_extensions(buffer, gl_info);
8159 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8161 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8163 shader_addline(buffer, "layout(vertices = %u) out;\n", hs->output_vertex_count);
8165 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8166 shader_addline(buffer, "out shader_in_out { vec4 reg[%u]; } shader_out[];\n", shader->limits->packed_output);
8168 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, FALSE);
8170 if (hs->phases.control_point)
8172 shader_addline(buffer, "void setup_hs_output(in vec4 outputs[%u])\n{\n",
8173 shader->limits->packed_output);
8174 shader_glsl_setup_sm4_shader_output(priv, shader->limits->packed_output, &shader->output_signature,
8175 &shader->reg_maps, "shader_out[gl_InvocationID]", FALSE);
8176 shader_addline(buffer, "}\n");
8179 shader_addline(buffer, "void hs_control_point_phase()\n{\n");
8180 if ((phase = hs->phases.control_point))
8182 for (i = 0; i < phase->temporary_count; ++i)
8183 shader_addline(buffer, "vec4 R%u;\n", i);
8184 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, phase->start, phase->end)))
8185 return 0;
8186 shader_addline(buffer, "setup_hs_output(hs_out);\n");
8188 else
8190 shader_glsl_generate_default_control_point_phase(shader, buffer, reg_maps);
8192 shader_addline(buffer, "}\n");
8194 for (i = 0; i < hs->phases.fork_count; ++i)
8196 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8197 &hs->phases.fork[i], "fork", i)))
8198 return 0;
8201 for (i = 0; i < hs->phases.join_count; ++i)
8203 if (FAILED(shader_glsl_generate_shader_phase(shader, buffer, reg_maps, &priv_ctx,
8204 &hs->phases.join[i], "join", i)))
8205 return 0;
8208 shader_addline(buffer, "void main()\n{\n");
8209 shader_addline(buffer, "hs_control_point_phase();\n");
8210 if (reg_maps->vocp)
8211 shader_addline(buffer, "barrier();\n");
8212 for (i = 0; i < hs->phases.fork_count; ++i)
8213 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.fork[i], "fork", i);
8214 for (i = 0; i < hs->phases.join_count; ++i)
8215 shader_glsl_generate_shader_phase_invocation(buffer, &hs->phases.join[i], "join", i);
8216 shader_addline(buffer, "setup_patch_constant_output();\n");
8217 shader_addline(buffer, "}\n");
8219 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_CONTROL_SHADER));
8220 TRACE("Compiling shader object %u.\n", shader_id);
8221 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8223 return shader_id;
8226 static void shader_glsl_generate_ds_epilogue(const struct wined3d_gl_info *gl_info,
8227 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
8228 const struct ds_compile_args *args)
8230 shader_addline(buffer, "setup_ds_output(ds_out);\n");
8232 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8233 shader_glsl_fixup_position(buffer, FALSE);
8236 static GLuint shader_glsl_generate_domain_shader(const struct wined3d_context_gl *context_gl,
8237 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct ds_compile_args *args)
8239 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8240 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8241 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8242 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8243 struct shader_glsl_ctx_priv priv_ctx;
8244 GLuint shader_id;
8246 memset(&priv_ctx, 0, sizeof(priv_ctx));
8247 priv_ctx.gl_info = gl_info;
8248 priv_ctx.cur_ds_args = args;
8249 priv_ctx.string_buffers = string_buffers;
8251 shader_glsl_add_version_declaration(buffer, gl_info);
8253 shader_glsl_enable_extensions(buffer, gl_info);
8254 shader_addline(buffer, "#extension GL_ARB_tessellation_shader : enable\n");
8256 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8258 shader_addline(buffer, "layout(");
8259 switch (shader->u.ds.tessellator_domain)
8261 case WINED3D_TESSELLATOR_DOMAIN_LINE:
8262 shader_addline(buffer, "isolines");
8263 break;
8264 case WINED3D_TESSELLATOR_DOMAIN_QUAD:
8265 shader_addline(buffer, "quads");
8266 break;
8267 case WINED3D_TESSELLATOR_DOMAIN_TRIANGLE:
8268 shader_addline(buffer, "triangles");
8269 break;
8271 switch (args->tessellator_output_primitive)
8273 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CW:
8274 if (args->render_offscreen)
8275 shader_addline(buffer, ", ccw");
8276 else
8277 shader_addline(buffer, ", cw");
8278 break;
8279 case WINED3D_TESSELLATOR_OUTPUT_TRIANGLE_CCW:
8280 if (args->render_offscreen)
8281 shader_addline(buffer, ", cw");
8282 else
8283 shader_addline(buffer, ", ccw");
8284 break;
8285 case WINED3D_TESSELLATOR_OUTPUT_POINT:
8286 shader_addline(buffer, ", point_mode");
8287 break;
8288 case WINED3D_TESSELLATOR_OUTPUT_LINE:
8289 break;
8291 switch (args->tessellator_partitioning)
8293 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_ODD:
8294 shader_addline(buffer, ", fractional_odd_spacing");
8295 break;
8296 case WINED3D_TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN:
8297 shader_addline(buffer, ", fractional_even_spacing");
8298 break;
8299 case WINED3D_TESSELLATOR_PARTITIONING_INTEGER:
8300 case WINED3D_TESSELLATOR_PARTITIONING_POW2:
8301 shader_addline(buffer, ", equal_spacing");
8302 break;
8304 shader_addline(buffer, ") in;\n");
8306 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8308 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
8309 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
8311 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count, gl_info,
8312 args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL, args->interpolation_mode);
8313 shader_glsl_generate_patch_constant_setup(buffer, &shader->patch_constant_signature, TRUE);
8315 shader_addline(buffer, "void main()\n{\n");
8316 shader_addline(buffer, "setup_patch_constant_input();\n");
8318 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8319 return 0;
8321 shader_addline(buffer, "}\n");
8323 shader_id = GL_EXTCALL(glCreateShader(GL_TESS_EVALUATION_SHADER));
8324 TRACE("Compiling shader object %u.\n", shader_id);
8325 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8327 return shader_id;
8330 /* Context activation is done by the caller. */
8331 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context_gl *context_gl,
8332 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
8334 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
8335 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8336 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8337 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8338 const struct wined3d_shader_signature_element *output;
8339 enum wined3d_primitive_type primitive_type;
8340 struct shader_glsl_ctx_priv priv_ctx;
8341 unsigned int max_vertices;
8342 unsigned int i, j;
8343 GLuint shader_id;
8345 memset(&priv_ctx, 0, sizeof(priv_ctx));
8346 priv_ctx.gl_info = gl_info;
8347 priv_ctx.string_buffers = string_buffers;
8349 shader_glsl_add_version_declaration(buffer, gl_info);
8351 shader_glsl_enable_extensions(buffer, gl_info);
8353 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8355 primitive_type = shader->u.gs.input_type ? shader->u.gs.input_type : args->primitive_type;
8356 shader_addline(buffer, "layout(%s", glsl_primitive_type_from_d3d(primitive_type));
8357 if (shader->u.gs.instance_count > 1)
8358 shader_addline(buffer, ", invocations = %u", shader->u.gs.instance_count);
8359 shader_addline(buffer, ") in;\n");
8361 primitive_type = shader->u.gs.output_type ? shader->u.gs.output_type : args->primitive_type;
8362 if (!(max_vertices = shader->u.gs.vertices_out))
8364 switch (args->primitive_type)
8366 case WINED3D_PT_POINTLIST:
8367 max_vertices = 1;
8368 break;
8369 case WINED3D_PT_LINELIST:
8370 max_vertices = 2;
8371 break;
8372 case WINED3D_PT_TRIANGLELIST:
8373 max_vertices = 3;
8374 break;
8375 default:
8376 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(args->primitive_type));
8377 break;
8380 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
8381 glsl_primitive_type_from_d3d(primitive_type), max_vertices);
8382 shader_addline(buffer, "in shader_in_out { vec4 reg[%u]; } shader_in[];\n", shader->limits->packed_input);
8384 if (!gl_info->supported[ARB_CLIP_CONTROL])
8386 shader_addline(buffer, "uniform vec4 pos_fixup");
8387 if (reg_maps->viewport_array)
8388 shader_addline(buffer, "[%u]", WINED3D_MAX_VIEWPORTS);
8389 shader_addline(buffer, ";\n");
8392 if (is_rasterization_disabled(shader))
8394 shader_glsl_generate_stream_output_setup(buffer, shader);
8396 else
8398 shader_glsl_generate_sm4_output_setup(priv, shader, args->output_count,
8399 gl_info, TRUE, args->interpolation_mode);
8402 shader_addline(buffer, "void main()\n{\n");
8403 if (shader->function)
8405 if (FAILED(shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL)))
8406 return 0;
8408 else
8410 for (i = 0; i < max_vertices; ++i)
8412 for (j = 0; j < shader->output_signature.element_count; ++j)
8414 output = &shader->output_signature.elements[j];
8415 shader_addline(buffer, "gs_out[%u] = shader_in[%u].reg[%u];\n",
8416 output->register_idx, i, output->register_idx);
8418 shader_addline(buffer, "setup_gs_output(gs_out);\n");
8419 if (!gl_info->supported[ARB_CLIP_CONTROL])
8420 shader_glsl_fixup_position(buffer, FALSE);
8421 shader_addline(buffer, "EmitVertex();\n");
8424 shader_addline(buffer, "}\n");
8426 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
8427 TRACE("Compiling shader object %u.\n", shader_id);
8428 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8430 return shader_id;
8433 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
8435 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
8436 const struct wined3d_gl_info *gl_info = priv->gl_info;
8437 struct wined3d_string_buffer *buffer = ctx->buffer;
8438 const struct wined3d_shader *shader = ctx->shader;
8440 switch (shader->reg_maps.shader_version.type)
8442 case WINED3D_SHADER_TYPE_PIXEL:
8443 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, priv->cur_ps_args, priv->string_buffers);
8444 break;
8445 case WINED3D_SHADER_TYPE_VERTEX:
8446 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, priv->cur_vs_args);
8447 break;
8448 case WINED3D_SHADER_TYPE_DOMAIN:
8449 shader_glsl_generate_ds_epilogue(gl_info, buffer, shader, priv->cur_ds_args);
8450 break;
8451 case WINED3D_SHADER_TYPE_GEOMETRY:
8452 case WINED3D_SHADER_TYPE_COMPUTE:
8453 break;
8454 default:
8455 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8456 break;
8460 /* Context activation is done by the caller. */
8461 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context_gl *context_gl,
8462 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8463 const struct wined3d_shader *shader)
8465 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
8466 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
8467 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
8468 struct shader_glsl_ctx_priv priv_ctx;
8469 GLuint shader_id;
8470 unsigned int i;
8472 memset(&priv_ctx, 0, sizeof(priv_ctx));
8473 priv_ctx.gl_info = gl_info;
8474 priv_ctx.string_buffers = string_buffers;
8476 shader_glsl_add_version_declaration(buffer, gl_info);
8478 shader_glsl_enable_extensions(buffer, gl_info);
8479 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
8481 shader_generate_glsl_declarations(context_gl, buffer, shader, reg_maps, &priv_ctx);
8483 for (i = 0; i < reg_maps->tgsm_count; ++i)
8485 if (reg_maps->tgsm[i].size)
8486 shader_addline(buffer, "shared uint cs_g%u[%u];\n", i, reg_maps->tgsm[i].size);
8489 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
8490 thread_group_size->x, thread_group_size->y, thread_group_size->z);
8492 shader_addline(buffer, "void main()\n{\n");
8493 shader_generate_code(shader, buffer, reg_maps, &priv_ctx, NULL, NULL);
8494 shader_addline(buffer, "}\n");
8496 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
8497 TRACE("Compiling shader object %u.\n", shader_id);
8498 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8500 return shader_id;
8503 static GLuint find_glsl_fragment_shader(const struct wined3d_context_gl *context_gl,
8504 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
8505 struct wined3d_shader *shader,
8506 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
8508 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
8509 struct glsl_shader_private *shader_data;
8510 struct ps_np2fixup_info *np2fixup;
8511 UINT i;
8512 DWORD new_size;
8513 GLuint ret;
8515 if (!shader->backend_data)
8517 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8519 ERR("Failed to allocate backend data.\n");
8520 return 0;
8523 shader_data = shader->backend_data;
8524 gl_shaders = shader_data->gl_shaders.ps;
8526 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8527 * so a linear search is more performant than a hashmap or a binary search
8528 * (cache coherency etc)
8530 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8532 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8534 if (args->np2_fixup)
8535 *np2fixup_info = &gl_shaders[i].np2fixup;
8536 return gl_shaders[i].id;
8540 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8541 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8543 if (shader_data->num_gl_shaders)
8545 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8546 new_array = heap_realloc(shader_data->gl_shaders.ps, new_size * sizeof(*gl_shaders));
8548 else
8550 new_array = heap_alloc(sizeof(*gl_shaders));
8551 new_size = 1;
8554 if(!new_array) {
8555 ERR("Out of memory\n");
8556 return 0;
8558 shader_data->gl_shaders.ps = new_array;
8559 shader_data->shader_array_size = new_size;
8560 gl_shaders = new_array;
8563 gl_shaders[shader_data->num_gl_shaders].args = *args;
8565 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
8566 memset(np2fixup, 0, sizeof(*np2fixup));
8567 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
8569 string_buffer_clear(buffer);
8570 ret = shader_glsl_generate_fragment_shader(context_gl, buffer, string_buffers, shader, args, np2fixup);
8571 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8573 return ret;
8576 static BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
8577 const DWORD use_map)
8579 if ((stored->swizzle_map & use_map) != new->swizzle_map)
8580 return FALSE;
8581 if ((stored->clip_enabled) != new->clip_enabled)
8582 return FALSE;
8583 if (stored->point_size != new->point_size)
8584 return FALSE;
8585 if (stored->per_vertex_point_size != new->per_vertex_point_size)
8586 return FALSE;
8587 if (stored->flatshading != new->flatshading)
8588 return FALSE;
8589 if (stored->next_shader_type != new->next_shader_type)
8590 return FALSE;
8591 if (stored->next_shader_input_count != new->next_shader_input_count)
8592 return FALSE;
8593 if (stored->fog_src != new->fog_src)
8594 return FALSE;
8595 return !memcmp(stored->interpolation_mode, new->interpolation_mode, sizeof(new->interpolation_mode));
8598 static GLuint find_glsl_vertex_shader(const struct wined3d_context_gl *context_gl,
8599 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct vs_compile_args *args)
8601 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
8602 uint32_t use_map = context_gl->c.stream_info.use_map;
8603 struct glsl_shader_private *shader_data;
8604 unsigned int i, new_size;
8605 GLuint ret;
8607 if (!shader->backend_data)
8609 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8611 ERR("Failed to allocate backend data.\n");
8612 return 0;
8615 shader_data = shader->backend_data;
8616 gl_shaders = shader_data->gl_shaders.vs;
8618 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
8619 * so a linear search is more performant than a hashmap or a binary search
8620 * (cache coherency etc)
8622 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8624 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
8625 return gl_shaders[i].id;
8628 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8630 if (shader_data->shader_array_size == shader_data->num_gl_shaders)
8632 if (shader_data->num_gl_shaders)
8634 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
8635 new_array = heap_realloc(shader_data->gl_shaders.vs, new_size * sizeof(*gl_shaders));
8637 else
8639 new_array = heap_alloc(sizeof(*gl_shaders));
8640 new_size = 1;
8643 if(!new_array) {
8644 ERR("Out of memory\n");
8645 return 0;
8647 shader_data->gl_shaders.vs = new_array;
8648 shader_data->shader_array_size = new_size;
8649 gl_shaders = new_array;
8652 gl_shaders[shader_data->num_gl_shaders].args = *args;
8654 string_buffer_clear(&priv->shader_buffer);
8655 ret = shader_glsl_generate_vertex_shader(context_gl, priv, shader, args);
8656 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8658 return ret;
8661 static GLuint find_glsl_hull_shader(const struct wined3d_context_gl *context_gl,
8662 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
8664 struct glsl_hs_compiled_shader *gl_shaders, *new_array;
8665 struct glsl_shader_private *shader_data;
8666 unsigned int new_size;
8667 GLuint ret;
8669 if (!shader->backend_data)
8671 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8673 ERR("Failed to allocate backend data.\n");
8674 return 0;
8677 shader_data = shader->backend_data;
8678 gl_shaders = shader_data->gl_shaders.hs;
8680 if (shader_data->num_gl_shaders > 0)
8682 assert(shader_data->num_gl_shaders == 1);
8683 return gl_shaders[0].id;
8686 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8688 assert(!shader_data->gl_shaders.hs);
8689 new_size = 1;
8690 if (!(new_array = heap_alloc(sizeof(*new_array))))
8692 ERR("Failed to allocate GL shaders array.\n");
8693 return 0;
8695 shader_data->gl_shaders.hs = new_array;
8696 shader_data->shader_array_size = new_size;
8697 gl_shaders = new_array;
8699 string_buffer_clear(&priv->shader_buffer);
8700 ret = shader_glsl_generate_hull_shader(context_gl, priv, shader);
8701 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8703 return ret;
8706 static GLuint find_glsl_domain_shader(const struct wined3d_context_gl *context_gl,
8707 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct ds_compile_args *args)
8709 struct glsl_ds_compiled_shader *gl_shaders, *new_array;
8710 struct glsl_shader_private *shader_data;
8711 unsigned int i, new_size;
8712 GLuint ret;
8714 if (!shader->backend_data)
8716 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8718 ERR("Failed to allocate backend data.\n");
8719 return 0;
8722 shader_data = shader->backend_data;
8723 gl_shaders = shader_data->gl_shaders.ds;
8725 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8727 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8728 return gl_shaders[i].id;
8731 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8733 if (shader_data->num_gl_shaders)
8735 new_size = shader_data->shader_array_size + 1;
8736 new_array = heap_realloc(shader_data->gl_shaders.ds, new_size * sizeof(*new_array));
8738 else
8740 new_array = heap_alloc(sizeof(*new_array));
8741 new_size = 1;
8744 if (!new_array)
8746 ERR("Failed to allocate GL shaders array.\n");
8747 return 0;
8749 shader_data->gl_shaders.ds = new_array;
8750 shader_data->shader_array_size = new_size;
8751 gl_shaders = new_array;
8753 string_buffer_clear(&priv->shader_buffer);
8754 ret = shader_glsl_generate_domain_shader(context_gl, priv, shader, args);
8755 gl_shaders[shader_data->num_gl_shaders].args = *args;
8756 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8758 return ret;
8761 static GLuint find_glsl_geometry_shader(const struct wined3d_context_gl *context_gl,
8762 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
8764 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
8765 struct glsl_shader_private *shader_data;
8766 unsigned int i, new_size;
8767 GLuint ret;
8769 if (!shader->backend_data)
8771 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
8773 ERR("Failed to allocate backend data.\n");
8774 return 0;
8777 shader_data = shader->backend_data;
8778 gl_shaders = shader_data->gl_shaders.gs;
8780 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8782 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
8783 return gl_shaders[i].id;
8786 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
8788 if (shader_data->num_gl_shaders)
8790 new_size = shader_data->shader_array_size + 1;
8791 new_array = heap_realloc(shader_data->gl_shaders.gs, new_size * sizeof(*new_array));
8793 else
8795 new_array = heap_alloc(sizeof(*new_array));
8796 new_size = 1;
8799 if (!new_array)
8801 ERR("Failed to allocate GL shaders array.\n");
8802 return 0;
8804 shader_data->gl_shaders.gs = new_array;
8805 shader_data->shader_array_size = new_size;
8806 gl_shaders = new_array;
8808 string_buffer_clear(&priv->shader_buffer);
8809 ret = shader_glsl_generate_geometry_shader(context_gl, priv, shader, args);
8810 gl_shaders[shader_data->num_gl_shaders].args = *args;
8811 gl_shaders[shader_data->num_gl_shaders++].id = ret;
8813 return ret;
8816 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
8818 switch (mcs)
8820 case WINED3D_MCS_MATERIAL:
8821 return material;
8822 case WINED3D_MCS_COLOR1:
8823 return "ffp_attrib_diffuse";
8824 case WINED3D_MCS_COLOR2:
8825 return "ffp_attrib_specular";
8826 default:
8827 ERR("Invalid material color source %#x.\n", mcs);
8828 return "<invalid>";
8832 static void shader_glsl_ffp_vertex_lighting_footer(struct wined3d_string_buffer *buffer,
8833 const struct wined3d_ffp_vs_settings *settings, unsigned int idx, BOOL legacy_lighting)
8835 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
8836 " * ffp_light[%u].diffuse.xyz * att;\n", idx);
8837 if (settings->localviewer)
8838 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
8839 else
8840 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
8841 shader_addline(buffer, "if (dot(dir, normal) > 0.0 && t > 0.0%s) specular +="
8842 " pow(t, ffp_material.shininess) * ffp_light[%u].specular * att;\n",
8843 legacy_lighting ? " && ffp_material.shininess > 0.0" : "", idx);
8846 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
8847 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
8849 const char *diffuse, *specular, *emissive, *ambient;
8850 unsigned int i, idx;
8852 if (!settings->lighting)
8854 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
8855 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
8856 return;
8859 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
8860 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
8861 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
8862 shader_addline(buffer, "vec3 dir, dst;\n");
8863 shader_addline(buffer, "float att, t;\n");
8865 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
8866 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
8867 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
8868 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
8870 idx = 0;
8871 for (i = 0; i < settings->point_light_count; ++i, ++idx)
8873 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8874 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8875 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8876 shader_addline(buffer, "dst.x = 1.0;\n");
8877 if (legacy_lighting)
8879 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8880 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8881 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8883 else
8885 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8887 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8888 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
8889 if (!legacy_lighting)
8890 shader_addline(buffer, "att = 1.0 / att;\n");
8891 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8892 if (!settings->normal)
8894 shader_addline(buffer, "}\n");
8895 continue;
8897 shader_addline(buffer, "dir = normalize(dir);\n");
8898 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8899 shader_addline(buffer, "}\n");
8902 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
8904 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
8905 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
8906 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
8907 shader_addline(buffer, "dst.x = 1.0;\n");
8908 if (legacy_lighting)
8910 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
8911 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
8912 shader_addline(buffer, "if (dst.y > 0.0)\n{\n");
8914 else
8916 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
8918 shader_addline(buffer, "dir = normalize(dir);\n");
8919 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
8920 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
8921 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
8922 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
8923 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
8924 idx, idx, idx, idx);
8925 if (legacy_lighting)
8926 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8927 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8928 idx, idx, idx);
8929 else
8930 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
8931 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
8932 idx, idx, idx);
8933 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
8934 if (!settings->normal)
8936 shader_addline(buffer, "}\n");
8937 continue;
8939 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8940 shader_addline(buffer, "}\n");
8943 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
8945 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8946 if (!settings->normal)
8947 continue;
8948 shader_addline(buffer, "att = 1.0;\n");
8949 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
8950 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8953 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
8955 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
8956 if (!settings->normal)
8957 continue;
8958 shader_addline(buffer, "att = 1.0;\n");
8959 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
8960 shader_glsl_ffp_vertex_lighting_footer(buffer, settings, idx, legacy_lighting);
8963 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
8964 ambient, diffuse, emissive);
8965 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
8966 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
8969 /* Context activation is done by the caller. */
8970 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
8971 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
8973 static const struct attrib_info
8975 const char type[6];
8976 const char name[24];
8978 attrib_info[] =
8980 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
8981 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
8982 /* TODO: Indexed vertex blending */
8983 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
8984 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
8985 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
8986 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
8987 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
8989 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
8990 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
8991 BOOL output_legacy_fogcoord = legacy_syntax;
8992 BOOL legacy_lighting = priv->legacy_lighting;
8993 GLuint shader_obj;
8994 unsigned int i;
8996 string_buffer_clear(buffer);
8998 shader_glsl_add_version_declaration(buffer, gl_info);
9000 if (shader_glsl_use_explicit_attrib_location(gl_info))
9001 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9003 shader_addline(buffer, "invariant gl_Position;\n");
9005 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
9007 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
9009 if (shader_glsl_use_explicit_attrib_location(gl_info))
9010 shader_addline(buffer, "layout(location = %u) ", i);
9011 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
9013 shader_addline(buffer, "\n");
9015 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
9016 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
9017 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
9018 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", WINED3D_MAX_TEXTURES);
9020 shader_addline(buffer, "uniform struct\n{\n");
9021 shader_addline(buffer, " vec4 emissive;\n");
9022 shader_addline(buffer, " vec4 ambient;\n");
9023 shader_addline(buffer, " vec4 diffuse;\n");
9024 shader_addline(buffer, " vec4 specular;\n");
9025 shader_addline(buffer, " float shininess;\n");
9026 shader_addline(buffer, "} ffp_material;\n");
9028 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
9029 shader_addline(buffer, "uniform struct\n{\n");
9030 shader_addline(buffer, " vec4 diffuse;\n");
9031 shader_addline(buffer, " vec4 specular;\n");
9032 shader_addline(buffer, " vec4 ambient;\n");
9033 shader_addline(buffer, " vec4 position;\n");
9034 shader_addline(buffer, " vec3 direction;\n");
9035 shader_addline(buffer, " float range;\n");
9036 shader_addline(buffer, " float falloff;\n");
9037 shader_addline(buffer, " float c_att;\n");
9038 shader_addline(buffer, " float l_att;\n");
9039 shader_addline(buffer, " float q_att;\n");
9040 shader_addline(buffer, " float cos_htheta;\n");
9041 shader_addline(buffer, " float cos_hphi;\n");
9042 shader_addline(buffer, "} ffp_light[%u];\n", WINED3D_MAX_ACTIVE_LIGHTS);
9044 if (settings->point_size)
9046 shader_addline(buffer, "uniform struct\n{\n");
9047 shader_addline(buffer, " float size;\n");
9048 shader_addline(buffer, " float size_min;\n");
9049 shader_addline(buffer, " float size_max;\n");
9050 shader_addline(buffer, " float c_att;\n");
9051 shader_addline(buffer, " float l_att;\n");
9052 shader_addline(buffer, " float q_att;\n");
9053 shader_addline(buffer, "} ffp_point;\n");
9056 if (legacy_syntax)
9058 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9059 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9060 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9061 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9063 else
9065 if (settings->clipping)
9066 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
9068 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9069 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9070 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9071 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9074 shader_addline(buffer, "\nvoid main()\n{\n");
9075 shader_addline(buffer, "float m;\n");
9076 shader_addline(buffer, "vec3 r;\n");
9078 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
9080 if (attrib_info[i].name[0])
9081 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
9082 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
9084 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9086 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
9087 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
9088 && settings->texcoords & (1u << i))
9089 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
9092 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
9094 if (settings->transformed)
9096 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
9097 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9098 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
9100 else
9102 for (i = 0; i < settings->vertexblends; ++i)
9103 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
9105 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
9106 for (i = 0; i < settings->vertexblends + 1; ++i)
9107 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
9109 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
9110 if (settings->clipping)
9112 if (legacy_syntax)
9113 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
9114 else
9115 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9116 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
9118 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
9121 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
9122 if (settings->normal)
9124 if (!settings->vertexblends)
9126 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
9128 else
9130 for (i = 0; i < settings->vertexblends + 1; ++i)
9131 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
9134 if (settings->normalize)
9135 shader_addline(buffer, "normal = normalize(normal);\n");
9138 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
9139 if (legacy_syntax)
9141 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
9142 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
9144 else
9146 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
9147 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
9150 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9152 BOOL output_legacy_texcoord = legacy_syntax;
9154 switch (settings->texgen[i] & 0xffff0000)
9156 case WINED3DTSS_TCI_PASSTHRU:
9157 if (settings->texcoords & (1u << i))
9158 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
9159 i, i, i);
9160 else if (shader_glsl_full_ffp_varyings(gl_info))
9161 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
9162 else
9163 output_legacy_texcoord = FALSE;
9164 break;
9166 case WINED3DTSS_TCI_CAMERASPACENORMAL:
9167 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
9168 break;
9170 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
9171 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
9172 break;
9174 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
9175 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9176 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
9177 break;
9179 case WINED3DTSS_TCI_SPHEREMAP:
9180 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
9181 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
9182 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
9183 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
9184 break;
9186 default:
9187 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
9188 break;
9190 if (output_legacy_texcoord)
9191 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
9194 switch (settings->fog_mode)
9196 case WINED3D_FFP_VS_FOG_OFF:
9197 output_legacy_fogcoord = FALSE;
9198 break;
9200 case WINED3D_FFP_VS_FOG_FOGCOORD:
9201 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
9202 break;
9204 case WINED3D_FFP_VS_FOG_RANGE:
9205 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
9206 break;
9208 case WINED3D_FFP_VS_FOG_DEPTH:
9209 if (settings->ortho_fog)
9211 if (gl_info->supported[ARB_CLIP_CONTROL])
9212 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
9213 else
9214 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
9215 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
9217 else if (settings->transformed)
9219 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
9221 else
9223 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
9225 break;
9227 default:
9228 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
9229 break;
9231 if (output_legacy_fogcoord)
9232 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
9234 if (settings->point_size)
9236 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
9237 " + ffp_point.l_att * length(ec_pos.xyz)"
9238 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
9239 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
9240 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
9243 shader_addline(buffer, "}\n");
9245 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
9246 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
9248 return shader_obj;
9251 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
9252 unsigned int argnum, unsigned int stage, DWORD arg)
9254 const char *ret;
9256 if (arg == ARG_UNUSED)
9257 return "<unused arg>";
9259 switch (arg & WINED3DTA_SELECTMASK)
9261 case WINED3DTA_DIFFUSE:
9262 ret = "ffp_varying_diffuse";
9263 break;
9265 case WINED3DTA_CURRENT:
9266 ret = "ret";
9267 break;
9269 case WINED3DTA_TEXTURE:
9270 switch (stage)
9272 case 0: ret = "tex0"; break;
9273 case 1: ret = "tex1"; break;
9274 case 2: ret = "tex2"; break;
9275 case 3: ret = "tex3"; break;
9276 case 4: ret = "tex4"; break;
9277 case 5: ret = "tex5"; break;
9278 case 6: ret = "tex6"; break;
9279 case 7: ret = "tex7"; break;
9280 default:
9281 ret = "<invalid texture>";
9282 break;
9284 break;
9286 case WINED3DTA_TFACTOR:
9287 ret = "tex_factor";
9288 break;
9290 case WINED3DTA_SPECULAR:
9291 ret = "ffp_varying_specular";
9292 break;
9294 case WINED3DTA_TEMP:
9295 ret = "temp_reg";
9296 break;
9298 case WINED3DTA_CONSTANT:
9299 switch (stage)
9301 case 0: ret = "tss_const0"; break;
9302 case 1: ret = "tss_const1"; break;
9303 case 2: ret = "tss_const2"; break;
9304 case 3: ret = "tss_const3"; break;
9305 case 4: ret = "tss_const4"; break;
9306 case 5: ret = "tss_const5"; break;
9307 case 6: ret = "tss_const6"; break;
9308 case 7: ret = "tss_const7"; break;
9309 default:
9310 ret = "<invalid constant>";
9311 break;
9313 break;
9315 default:
9316 return "<unhandled arg>";
9319 if (arg & WINED3DTA_COMPLEMENT)
9321 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
9322 if (argnum == 0)
9323 ret = "arg0";
9324 else if (argnum == 1)
9325 ret = "arg1";
9326 else if (argnum == 2)
9327 ret = "arg2";
9330 if (arg & WINED3DTA_ALPHAREPLICATE)
9332 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
9333 if (argnum == 0)
9334 ret = "arg0";
9335 else if (argnum == 1)
9336 ret = "arg1";
9337 else if (argnum == 2)
9338 ret = "arg2";
9341 return ret;
9344 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
9345 BOOL alpha, BOOL tmp_dst, unsigned int op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
9347 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
9349 if (color && alpha)
9350 dstmask = "";
9351 else if (color)
9352 dstmask = ".xyz";
9353 else
9354 dstmask = ".w";
9356 dstreg = tmp_dst ? "temp_reg" : "ret";
9358 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
9359 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
9360 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
9362 switch (op)
9364 case WINED3D_TOP_DISABLE:
9365 break;
9367 case WINED3D_TOP_SELECT_ARG1:
9368 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
9369 break;
9371 case WINED3D_TOP_SELECT_ARG2:
9372 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
9373 break;
9375 case WINED3D_TOP_MODULATE:
9376 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9377 break;
9379 case WINED3D_TOP_MODULATE_4X:
9380 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
9381 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9382 break;
9384 case WINED3D_TOP_MODULATE_2X:
9385 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
9386 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9387 break;
9389 case WINED3D_TOP_ADD:
9390 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
9391 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9392 break;
9394 case WINED3D_TOP_ADD_SIGNED:
9395 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
9396 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9397 break;
9399 case WINED3D_TOP_ADD_SIGNED_2X:
9400 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
9401 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9402 break;
9404 case WINED3D_TOP_SUBTRACT:
9405 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
9406 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
9407 break;
9409 case WINED3D_TOP_ADD_SMOOTH:
9410 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
9411 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
9412 break;
9414 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
9415 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
9416 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9417 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9418 break;
9420 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9421 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9422 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9423 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9424 break;
9426 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9427 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
9428 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9429 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9430 break;
9432 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9433 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
9434 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9435 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
9436 break;
9438 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
9439 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
9440 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
9441 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
9442 break;
9444 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
9445 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
9446 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9447 break;
9449 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
9450 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
9451 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9452 break;
9454 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
9455 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
9456 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
9457 break;
9458 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
9459 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
9460 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
9461 break;
9463 case WINED3D_TOP_BUMPENVMAP:
9464 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9465 /* These are handled in the first pass, nothing to do. */
9466 break;
9468 case WINED3D_TOP_DOTPRODUCT3:
9469 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
9470 dstreg, dstmask, arg1, arg2, dstmask);
9471 break;
9473 case WINED3D_TOP_MULTIPLY_ADD:
9474 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
9475 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
9476 break;
9478 case WINED3D_TOP_LERP:
9479 /* MSDN isn't quite right here. */
9480 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
9481 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
9482 break;
9484 default:
9485 FIXME("Unhandled operation %#x.\n", op);
9486 break;
9490 /* Context activation is done by the caller. */
9491 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
9492 const struct ffp_frag_settings *settings, const struct wined3d_context_gl *context_gl)
9494 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
9495 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
9496 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
9497 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
9498 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
9499 const BOOL legacy_syntax = needs_legacy_glsl_syntax(gl_info);
9500 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
9501 UINT lowest_disabled_stage;
9502 GLuint shader_id;
9503 DWORD arg0, arg1, arg2;
9504 unsigned int stage;
9506 string_buffer_clear(buffer);
9508 /* Find out which textures are read */
9509 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9511 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9512 break;
9514 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
9515 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
9516 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
9518 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
9519 || (stage == 0 && settings->color_key_enabled))
9520 tex_map |= 1u << stage;
9521 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9522 tfactor_used = TRUE;
9523 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9524 tempreg_used = TRUE;
9525 if (settings->op[stage].tmp_dst)
9526 tempreg_used = TRUE;
9527 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9528 tss_const_map |= 1u << stage;
9530 switch (settings->op[stage].cop)
9532 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
9533 lum_map |= 1u << stage;
9534 /* fall through */
9535 case WINED3D_TOP_BUMPENVMAP:
9536 bump_map |= 1u << stage;
9537 /* fall through */
9538 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
9539 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
9540 tex_map |= 1u << stage;
9541 break;
9543 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
9544 tfactor_used = TRUE;
9545 break;
9547 default:
9548 break;
9551 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9552 continue;
9554 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
9555 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
9556 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
9558 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
9559 tex_map |= 1u << stage;
9560 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
9561 tfactor_used = TRUE;
9562 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
9563 tempreg_used = TRUE;
9564 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
9565 tss_const_map |= 1u << stage;
9567 lowest_disabled_stage = stage;
9569 shader_glsl_add_version_declaration(buffer, gl_info);
9571 if (shader_glsl_use_explicit_attrib_location(gl_info))
9572 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
9573 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
9574 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
9575 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
9576 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
9578 if (!use_legacy_fragment_output(gl_info))
9580 shader_addline(buffer, "vec4 ps_out[1];\n");
9581 if (shader_glsl_use_explicit_attrib_location(gl_info))
9582 shader_addline(buffer, "layout(location = 0) ");
9583 shader_addline(buffer, "out vec4 color_out0;\n");
9586 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
9587 shader_addline(buffer, "vec4 ret;\n");
9588 if (tempreg_used || settings->sRGB_write)
9589 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
9590 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
9592 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9594 const char *sampler_type;
9596 if (tss_const_map & (1u << stage))
9597 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
9599 if (!(tex_map & (1u << stage)))
9600 continue;
9602 switch (settings->op[stage].tex_type)
9604 case WINED3D_GL_RES_TYPE_TEX_1D:
9605 sampler_type = "1D";
9606 break;
9607 case WINED3D_GL_RES_TYPE_TEX_2D:
9608 sampler_type = "2D";
9609 break;
9610 case WINED3D_GL_RES_TYPE_TEX_3D:
9611 sampler_type = "3D";
9612 break;
9613 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9614 sampler_type = "Cube";
9615 break;
9616 case WINED3D_GL_RES_TYPE_TEX_RECT:
9617 sampler_type = "2DRect";
9618 break;
9619 default:
9620 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
9621 sampler_type = NULL;
9622 break;
9624 if (sampler_type)
9626 if (shader_glsl_use_layout_binding_qualifier(gl_info))
9627 shader_glsl_append_sampler_binding_qualifier(buffer, &context_gl->c, NULL, stage);
9628 shader_addline(buffer, "uniform sampler%s ps_sampler%u;\n", sampler_type, stage);
9631 shader_addline(buffer, "vec4 tex%u;\n", stage);
9633 if (!(bump_map & (1u << stage)))
9634 continue;
9635 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
9637 if (!(lum_map & (1u << stage)))
9638 continue;
9639 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
9640 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
9642 if (tfactor_used)
9643 shader_addline(buffer, "uniform vec4 tex_factor;\n");
9644 if (settings->color_key_enabled)
9645 shader_addline(buffer, "uniform vec4 color_key[2];\n");
9646 shader_addline(buffer, "uniform vec4 specular_enable;\n");
9648 if (settings->sRGB_write)
9650 shader_addline(buffer, "const vec4 srgb_const0 = ");
9651 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[0].x, 4, gl_info);
9652 shader_addline(buffer, ";\n");
9653 shader_addline(buffer, "const vec4 srgb_const1 = ");
9654 shader_glsl_append_imm_vec(buffer, &wined3d_srgb_const[1].x, 4, gl_info);
9655 shader_addline(buffer, ";\n");
9658 shader_addline(buffer, "uniform struct\n{\n");
9659 shader_addline(buffer, " vec4 color;\n");
9660 shader_addline(buffer, " float density;\n");
9661 shader_addline(buffer, " float end;\n");
9662 shader_addline(buffer, " float scale;\n");
9663 shader_addline(buffer, "} ffp_fog;\n");
9665 if (alpha_test_func != WINED3D_CMP_ALWAYS)
9666 shader_addline(buffer, "uniform float alpha_test_ref;\n");
9668 if (legacy_syntax)
9670 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
9671 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
9672 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9673 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9674 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
9676 else
9678 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
9679 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
9680 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9681 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", WINED3D_MAX_TEXTURES);
9682 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
9685 shader_addline(buffer, "void main()\n{\n");
9687 if (legacy_syntax)
9689 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
9690 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
9693 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9695 if (tex_map & (1u << stage))
9697 if (settings->pointsprite)
9698 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
9699 else if (settings->texcoords_initialized & (1u << stage))
9700 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
9701 stage, legacy_syntax ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
9702 else
9703 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
9707 if (legacy_syntax && settings->fog != WINED3D_FFP_PS_FOG_OFF)
9708 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
9710 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
9711 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
9713 /* Generate texture sampling instructions */
9714 for (stage = 0; stage < WINED3D_MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
9716 const char *texture_function, *coord_mask;
9717 BOOL proj;
9719 if (!(tex_map & (1u << stage)))
9720 continue;
9722 if (settings->op[stage].projected == WINED3D_PROJECTION_NONE)
9724 proj = FALSE;
9726 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4
9727 || settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9729 proj = TRUE;
9731 else
9733 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
9734 proj = TRUE;
9737 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
9738 proj = FALSE;
9740 switch (settings->op[stage].tex_type)
9742 case WINED3D_GL_RES_TYPE_TEX_1D:
9743 texture_function = "texture1D";
9744 coord_mask = "x";
9745 break;
9746 case WINED3D_GL_RES_TYPE_TEX_2D:
9747 texture_function = "texture2D";
9748 coord_mask = "xy";
9749 break;
9750 case WINED3D_GL_RES_TYPE_TEX_3D:
9751 texture_function = "texture3D";
9752 coord_mask = "xyz";
9753 break;
9754 case WINED3D_GL_RES_TYPE_TEX_CUBE:
9755 texture_function = "textureCube";
9756 coord_mask = "xyz";
9757 break;
9758 case WINED3D_GL_RES_TYPE_TEX_RECT:
9759 texture_function = "texture2DRect";
9760 coord_mask = "xy";
9761 break;
9762 default:
9763 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
9764 texture_function = "";
9765 coord_mask = "xyzw";
9766 proj = FALSE;
9767 break;
9769 if (!legacy_syntax)
9770 texture_function = "texture";
9772 if (stage > 0
9773 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
9774 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
9776 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
9778 /* With projective textures, texbem only divides the static
9779 * texture coordinate, not the displacement, so multiply the
9780 * displacement with the dividing parameter before passing it to
9781 * TXP. */
9782 if (settings->op[stage].projected != WINED3D_PROJECTION_NONE)
9784 if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT4)
9786 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
9787 stage, stage);
9788 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
9790 else
9792 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
9793 stage, stage);
9794 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
9797 else
9799 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
9802 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ret.%s%s);\n",
9803 stage, texture_function, proj ? "Proj" : "", stage, coord_mask, proj ? "w" : "");
9805 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9806 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
9807 stage, stage - 1, stage - 1, stage - 1);
9809 else if (settings->op[stage].projected == WINED3D_PROJECTION_COUNT3)
9811 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
9812 stage, texture_function, proj ? "Proj" : "", stage, stage);
9814 else
9816 shader_addline(buffer, "tex%u = %s%s(ps_sampler%u, ffp_texcoord[%u].%s%s);\n",
9817 stage, texture_function, proj ? "Proj" : "", stage, stage, coord_mask, proj ? "w" : "");
9820 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
9821 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
9822 settings->op[stage].color_fixup);
9825 if (settings->color_key_enabled)
9826 shader_glsl_generate_colour_key_test(buffer, "tex0", "color_key[0]", "color_key[1]");
9828 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
9830 /* Generate the main shader */
9831 for (stage = 0; stage < WINED3D_MAX_TEXTURES; ++stage)
9833 BOOL op_equal;
9835 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
9836 break;
9838 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9839 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9840 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
9841 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
9842 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9843 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
9844 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9845 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
9846 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
9847 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
9848 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
9849 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
9850 else
9851 op_equal = settings->op[stage].aop == settings->op[stage].cop
9852 && settings->op[stage].carg0 == settings->op[stage].aarg0
9853 && settings->op[stage].carg1 == settings->op[stage].aarg1
9854 && settings->op[stage].carg2 == settings->op[stage].aarg2;
9856 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
9858 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9859 settings->op[stage].cop, settings->op[stage].carg0,
9860 settings->op[stage].carg1, settings->op[stage].carg2);
9862 else if (op_equal)
9864 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].tmp_dst,
9865 settings->op[stage].cop, settings->op[stage].carg0,
9866 settings->op[stage].carg1, settings->op[stage].carg2);
9868 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
9869 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
9871 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].tmp_dst,
9872 settings->op[stage].cop, settings->op[stage].carg0,
9873 settings->op[stage].carg1, settings->op[stage].carg2);
9874 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].tmp_dst,
9875 settings->op[stage].aop, settings->op[stage].aarg0,
9876 settings->op[stage].aarg1, settings->op[stage].aarg2);
9880 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
9881 get_fragment_output(gl_info));
9883 if (settings->sRGB_write)
9884 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
9886 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
9888 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
9889 if (!use_legacy_fragment_output(gl_info))
9890 shader_addline(buffer, "color_out0 = ps_out[0];\n");
9892 shader_addline(buffer, "}\n");
9894 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
9895 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
9897 string_buffer_release(&priv->string_buffers, tex_reg_name);
9898 return shader_id;
9901 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
9902 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
9904 struct glsl_ffp_vertex_shader *shader;
9905 const struct wine_rb_entry *entry;
9907 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
9908 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
9910 if (!(shader = heap_alloc(sizeof(*shader))))
9911 return NULL;
9913 shader->desc.settings = *settings;
9914 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
9915 list_init(&shader->linked_programs);
9916 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
9917 ERR("Failed to insert ffp vertex shader.\n");
9919 return shader;
9922 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
9923 const struct ffp_frag_settings *args, const struct wined3d_context_gl *context_gl)
9925 struct glsl_ffp_fragment_shader *glsl_desc;
9926 const struct ffp_frag_desc *desc;
9928 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
9929 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
9931 if (!(glsl_desc = heap_alloc(sizeof(*glsl_desc))))
9932 return NULL;
9934 glsl_desc->entry.settings = *args;
9935 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, context_gl);
9936 list_init(&glsl_desc->linked_programs);
9937 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
9939 return glsl_desc;
9943 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
9944 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
9946 unsigned int i;
9947 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
9949 for (i = 0; i < vs_c_count; ++i)
9951 string_buffer_sprintf(name, "vs_c[%u]", i);
9952 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9954 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
9956 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
9958 string_buffer_sprintf(name, "vs_i[%u]", i);
9959 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9962 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
9964 string_buffer_sprintf(name, "vs_b[%u]", i);
9965 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9968 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
9969 vs->base_vertex_id_location = GL_EXTCALL(glGetUniformLocation(program_id, "base_vertex_id"));
9971 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
9973 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
9974 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9976 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
9977 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
9978 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
9980 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
9981 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9983 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
9984 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
9985 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
9986 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
9987 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
9988 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
9989 for (i = 0; i < WINED3D_MAX_ACTIVE_LIGHTS; ++i)
9991 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
9992 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9993 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
9994 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9995 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
9996 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9997 string_buffer_sprintf(name, "ffp_light[%u].position", i);
9998 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
9999 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
10000 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10001 string_buffer_sprintf(name, "ffp_light[%u].range", i);
10002 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10003 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
10004 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10005 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
10006 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10007 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
10008 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10009 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
10010 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10011 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
10012 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10013 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
10014 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10016 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
10017 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
10018 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
10019 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
10020 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
10021 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
10022 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
10024 string_buffer_release(&priv->string_buffers, name);
10027 static void shader_glsl_init_ds_uniform_locations(const struct wined3d_gl_info *gl_info,
10028 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ds_program *ds)
10030 ds->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10033 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
10034 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
10036 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
10039 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
10040 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
10042 unsigned int i;
10043 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
10045 for (i = 0; i < ps_c_count; ++i)
10047 string_buffer_sprintf(name, "ps_c[%u]", i);
10048 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10050 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
10052 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
10054 string_buffer_sprintf(name, "ps_i[%u]", i);
10055 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10058 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
10060 string_buffer_sprintf(name, "ps_b[%u]", i);
10061 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10064 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10066 string_buffer_sprintf(name, "bumpenv_mat%u", i);
10067 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10068 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
10069 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10070 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
10071 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10072 string_buffer_sprintf(name, "tss_const%u", i);
10073 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
10076 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
10077 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
10079 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
10080 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
10081 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
10082 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
10084 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
10086 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
10087 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
10088 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
10090 string_buffer_release(&priv->string_buffers, name);
10093 static HRESULT shader_glsl_compile_compute_shader(struct shader_glsl_priv *priv,
10094 const struct wined3d_context_gl *context_gl, struct wined3d_shader *shader)
10096 struct glsl_context_data *ctx_data = context_gl->c.shader_backend_data;
10097 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10098 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
10099 struct glsl_cs_compiled_shader *gl_shaders;
10100 struct glsl_shader_private *shader_data;
10101 struct glsl_shader_prog_link *entry;
10102 GLuint shader_id, program_id;
10104 if (!(entry = heap_alloc(sizeof(*entry))))
10106 ERR("Out of memory.\n");
10107 return E_OUTOFMEMORY;
10110 if (!(shader->backend_data = heap_alloc_zero(sizeof(*shader_data))))
10112 ERR("Failed to allocate backend data.\n");
10113 heap_free(entry);
10114 return E_OUTOFMEMORY;
10116 shader_data = shader->backend_data;
10118 if (!(shader_data->gl_shaders.cs = heap_alloc(sizeof(*gl_shaders))))
10120 ERR("Failed to allocate GL shader array.\n");
10121 heap_free(entry);
10122 heap_free(shader->backend_data);
10123 shader->backend_data = NULL;
10124 return E_OUTOFMEMORY;
10126 shader_data->shader_array_size = 1;
10127 gl_shaders = shader_data->gl_shaders.cs;
10129 TRACE("Compiling compute shader %p.\n", shader);
10131 string_buffer_clear(buffer);
10132 shader_id = shader_glsl_generate_compute_shader(context_gl, buffer, &priv->string_buffers, shader);
10133 gl_shaders[shader_data->num_gl_shaders++].id = shader_id;
10135 program_id = GL_EXTCALL(glCreateProgram());
10136 TRACE("Created new GLSL shader program %u.\n", program_id);
10138 entry->id = program_id;
10139 entry->vs.id = 0;
10140 entry->hs.id = 0;
10141 entry->ds.id = 0;
10142 entry->gs.id = 0;
10143 entry->ps.id = 0;
10144 entry->cs.id = shader_id;
10145 entry->constant_version = 0;
10146 entry->shader_controlled_clip_distances = 0;
10147 entry->ps.np2_fixup_info = NULL;
10148 add_glsl_program_entry(priv, entry);
10150 TRACE("Attaching GLSL shader object %u to program %u.\n", shader_id, program_id);
10151 GL_EXTCALL(glAttachShader(program_id, shader_id));
10152 checkGLcall("glAttachShader");
10154 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
10156 TRACE("Linking GLSL shader program %u.\n", program_id);
10157 GL_EXTCALL(glLinkProgram(program_id));
10158 shader_glsl_validate_link(gl_info, program_id);
10160 GL_EXTCALL(glUseProgram(program_id));
10161 checkGLcall("glUseProgram");
10162 shader_glsl_load_program_resources(context_gl, priv, program_id, shader);
10163 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
10165 entry->constant_update_mask = 0;
10167 GL_EXTCALL(glUseProgram(ctx_data->glsl_program ? ctx_data->glsl_program->id : 0));
10168 checkGLcall("glUseProgram");
10169 return WINED3D_OK;
10172 static GLuint find_glsl_compute_shader(const struct wined3d_context_gl *context_gl,
10173 struct shader_glsl_priv *priv, struct wined3d_shader *shader)
10175 struct glsl_shader_private *shader_data;
10177 if (!shader->backend_data)
10179 WARN("Failed to find GLSL program for compute shader %p.\n", shader);
10180 if (FAILED(shader_glsl_compile_compute_shader(priv, context_gl, shader)))
10182 ERR("Failed to compile compute shader %p.\n", shader);
10183 return 0;
10186 shader_data = shader->backend_data;
10187 return shader_data->gl_shaders.cs[0].id;
10190 /* Context activation is done by the caller. */
10191 static void set_glsl_compute_shader_program(const struct wined3d_context_gl *context_gl,
10192 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10194 struct glsl_shader_prog_link *entry;
10195 struct wined3d_shader *shader;
10196 struct glsl_program_key key;
10197 GLuint cs_id;
10199 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
10200 return;
10202 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
10204 WARN("Compute shader is NULL.\n");
10205 ctx_data->glsl_program = NULL;
10206 return;
10209 cs_id = find_glsl_compute_shader(context_gl, priv, shader);
10210 memset(&key, 0, sizeof(key));
10211 key.cs_id = cs_id;
10212 if (!(entry = get_glsl_program_entry(priv, &key)))
10213 ERR("Failed to find GLSL program for compute shader %p.\n", shader);
10214 ctx_data->glsl_program = entry;
10217 /* Context activation is done by the caller. */
10218 static void set_glsl_shader_program(const struct wined3d_context_gl *context_gl, const struct wined3d_state *state,
10219 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
10221 const struct wined3d_d3d_info *d3d_info = context_gl->c.d3d_info;
10222 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10223 const struct wined3d_shader *pre_rasterization_shader;
10224 const struct ps_np2fixup_info *np2fixup_info = NULL;
10225 struct wined3d_shader *hshader, *dshader, *gshader;
10226 struct glsl_shader_prog_link *entry = NULL;
10227 struct wined3d_shader *vshader = NULL;
10228 struct wined3d_shader *pshader = NULL;
10229 GLuint reorder_shader_id = 0;
10230 struct glsl_program_key key;
10231 uint32_t attribs_map;
10232 GLuint program_id;
10233 unsigned int i;
10234 GLuint vs_id = 0;
10235 GLuint hs_id = 0;
10236 GLuint ds_id = 0;
10237 GLuint gs_id = 0;
10238 GLuint ps_id = 0;
10239 struct list *ps_list, *vs_list;
10240 struct wined3d_string_buffer *tmp_name;
10242 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
10244 vs_id = ctx_data->glsl_program->vs.id;
10245 vs_list = &ctx_data->glsl_program->vs.shader_entry;
10247 if (use_vs(state))
10248 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10250 else if (use_vs(state))
10252 struct vs_compile_args vs_compile_args;
10254 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
10256 find_vs_compile_args(state, vshader, &vs_compile_args, &context_gl->c);
10257 vs_id = find_glsl_vertex_shader(context_gl, priv, vshader, &vs_compile_args);
10258 vs_list = &vshader->linked_programs;
10260 else if (priv->vertex_pipe == &glsl_vertex_pipe)
10262 struct glsl_ffp_vertex_shader *ffp_shader;
10263 struct wined3d_ffp_vs_settings settings;
10265 wined3d_ffp_get_vs_settings(&context_gl->c, state, &settings);
10266 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
10267 vs_id = ffp_shader->id;
10268 vs_list = &ffp_shader->linked_programs;
10271 hshader = state->shader[WINED3D_SHADER_TYPE_HULL];
10272 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_HULL)) && ctx_data->glsl_program)
10273 hs_id = ctx_data->glsl_program->hs.id;
10274 else if (hshader)
10275 hs_id = find_glsl_hull_shader(context_gl, priv, hshader);
10277 dshader = state->shader[WINED3D_SHADER_TYPE_DOMAIN];
10278 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_DOMAIN)) && ctx_data->glsl_program)
10280 ds_id = ctx_data->glsl_program->ds.id;
10282 else if (dshader)
10284 struct ds_compile_args args;
10286 find_ds_compile_args(state, dshader, &args, &context_gl->c);
10287 ds_id = find_glsl_domain_shader(context_gl, priv, dshader, &args);
10290 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
10291 if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY)) && ctx_data->glsl_program)
10293 gs_id = ctx_data->glsl_program->gs.id;
10295 else if (gshader)
10297 struct gs_compile_args args;
10299 find_gs_compile_args(state, gshader, &args, &context_gl->c);
10300 gs_id = find_glsl_geometry_shader(context_gl, priv, gshader, &args);
10303 /* A pixel shader is not used when rasterization is disabled. */
10304 if (is_rasterization_disabled(gshader))
10306 ps_id = 0;
10307 ps_list = NULL;
10309 else if (!(context_gl->c.shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
10311 ps_id = ctx_data->glsl_program->ps.id;
10312 ps_list = &ctx_data->glsl_program->ps.shader_entry;
10314 if (use_ps(state))
10315 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10317 else if (use_ps(state))
10319 struct ps_compile_args ps_compile_args;
10320 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
10321 find_ps_compile_args(state, pshader, context_gl->c.stream_info.position_transformed,
10322 &ps_compile_args, &context_gl->c);
10323 ps_id = find_glsl_fragment_shader(context_gl, &priv->shader_buffer, &priv->string_buffers,
10324 pshader, &ps_compile_args, &np2fixup_info);
10325 ps_list = &pshader->linked_programs;
10327 else if (priv->fragment_pipe == &glsl_fragment_pipe
10328 && !(vshader && vshader->reg_maps.shader_version.major >= 4))
10330 struct glsl_ffp_fragment_shader *ffp_shader;
10331 struct ffp_frag_settings settings;
10333 wined3d_ffp_get_fs_settings(&context_gl->c, state, &settings, FALSE);
10334 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, &settings, context_gl);
10335 ps_id = ffp_shader->id;
10336 ps_list = &ffp_shader->linked_programs;
10339 key.vs_id = vs_id;
10340 key.hs_id = hs_id;
10341 key.ds_id = ds_id;
10342 key.gs_id = gs_id;
10343 key.ps_id = ps_id;
10344 key.cs_id = 0;
10345 if ((!vs_id && !hs_id && !ds_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
10347 ctx_data->glsl_program = entry;
10348 return;
10351 /* If we get to this point, then no matching program exists, so we create one */
10352 program_id = GL_EXTCALL(glCreateProgram());
10353 TRACE("Created new GLSL shader program %u.\n", program_id);
10355 /* Create the entry */
10356 entry = heap_alloc(sizeof(*entry));
10357 entry->id = program_id;
10358 entry->vs.id = vs_id;
10359 entry->hs.id = hs_id;
10360 entry->ds.id = ds_id;
10361 entry->gs.id = gs_id;
10362 entry->ps.id = ps_id;
10363 entry->cs.id = 0;
10364 entry->constant_version = 0;
10365 entry->shader_controlled_clip_distances = 0;
10366 entry->ps.np2_fixup_info = np2fixup_info;
10367 /* Add the hash table entry */
10368 add_glsl_program_entry(priv, entry);
10370 /* Set the current program */
10371 ctx_data->glsl_program = entry;
10373 /* Attach GLSL vshader */
10374 if (vs_id)
10376 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
10377 GL_EXTCALL(glAttachShader(program_id, vs_id));
10378 checkGLcall("glAttachShader");
10380 list_add_head(vs_list, &entry->vs.shader_entry);
10383 if (vshader)
10385 attribs_map = vshader->reg_maps.input_registers;
10386 if (vshader->reg_maps.shader_version.major < 4)
10388 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
10389 state->primitive_type == WINED3D_PT_POINTLIST && vshader->reg_maps.point_size,
10390 d3d_info->emulated_flatshading
10391 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
10392 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
10393 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
10394 checkGLcall("glAttachShader");
10395 /* Flag the reorder function for deletion, it will be freed
10396 * automatically when the program is destroyed. */
10397 GL_EXTCALL(glDeleteShader(reorder_shader_id));
10400 else
10402 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
10405 if (!shader_glsl_use_explicit_attrib_location(gl_info))
10407 /* Bind vertex attributes to a corresponding index number to match
10408 * the same index numbers as ARB_vertex_programs (makes loading
10409 * vertex attributes simpler). With this method, we can use the
10410 * exact same code to load the attributes later for both ARB and
10411 * GLSL shaders.
10413 * We have to do this here because we need to know the Program ID
10414 * in order to make the bindings work, and it has to be done prior
10415 * to linking the GLSL program. */
10416 tmp_name = string_buffer_get(&priv->string_buffers);
10417 while (attribs_map)
10419 i = wined3d_bit_scan(&attribs_map);
10420 string_buffer_sprintf(tmp_name, "vs_in%u", i);
10421 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10422 if (vshader && vshader->reg_maps.shader_version.major >= 4)
10424 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
10425 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10426 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
10427 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
10430 checkGLcall("glBindAttribLocation");
10432 if (!use_legacy_fragment_output(gl_info))
10434 for (i = 0; i < WINED3D_MAX_RENDER_TARGETS; ++i)
10436 string_buffer_sprintf(tmp_name, "color_out%u", i);
10437 if (state->blend_state && state->blend_state->dual_source)
10438 GL_EXTCALL(glBindFragDataLocationIndexed(program_id, 0, i, tmp_name->buffer));
10439 else
10440 GL_EXTCALL(glBindFragDataLocation(program_id, i, tmp_name->buffer));
10441 checkGLcall("glBindFragDataLocation");
10444 string_buffer_release(&priv->string_buffers, tmp_name);
10447 if (hshader)
10449 TRACE("Attaching GLSL tessellation control shader object %u to program %u.\n", hs_id, program_id);
10450 GL_EXTCALL(glAttachShader(program_id, hs_id));
10451 checkGLcall("glAttachShader");
10453 list_add_head(&hshader->linked_programs, &entry->hs.shader_entry);
10456 if (dshader)
10458 TRACE("Attaching GLSL tessellation evaluation shader object %u to program %u.\n", ds_id, program_id);
10459 GL_EXTCALL(glAttachShader(program_id, ds_id));
10460 checkGLcall("glAttachShader");
10462 list_add_head(&dshader->linked_programs, &entry->ds.shader_entry);
10465 if (gshader)
10467 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
10468 GL_EXTCALL(glAttachShader(program_id, gs_id));
10469 checkGLcall("glAttachShader");
10471 shader_glsl_init_transform_feedback(context_gl, priv, program_id, gshader);
10473 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
10476 /* Attach GLSL pshader */
10477 if (ps_id)
10479 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
10480 GL_EXTCALL(glAttachShader(program_id, ps_id));
10481 checkGLcall("glAttachShader");
10483 list_add_head(ps_list, &entry->ps.shader_entry);
10486 /* Link the program */
10487 TRACE("Linking GLSL shader program %u.\n", program_id);
10488 GL_EXTCALL(glLinkProgram(program_id));
10489 shader_glsl_validate_link(gl_info, program_id);
10491 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
10492 vshader ? vshader->limits->constant_float : 0);
10493 shader_glsl_init_ds_uniform_locations(gl_info, priv, program_id, &entry->ds);
10494 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
10495 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
10496 pshader ? pshader->limits->constant_float : 0);
10497 checkGLcall("find glsl program uniform locations");
10499 pre_rasterization_shader = gshader ? gshader : dshader ? dshader : vshader;
10500 if (pre_rasterization_shader && pre_rasterization_shader->reg_maps.shader_version.major >= 4)
10502 unsigned int clip_distance_count = wined3d_popcount(pre_rasterization_shader->reg_maps.clip_distance_mask);
10503 entry->shader_controlled_clip_distances = 1;
10504 entry->clip_distance_mask = wined3d_mask_from_size(clip_distance_count);
10507 if (needs_legacy_glsl_syntax(gl_info))
10509 if (pshader && pshader->reg_maps.shader_version.major >= 3
10510 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
10512 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
10513 entry->vs.vertex_color_clamp = GL_FALSE;
10515 else
10517 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10520 else
10522 /* With core profile we never change vertex_color_clamp from
10523 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
10524 * glClampColorARB(). */
10525 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
10528 /* Set the shader to allow uniform loading on it */
10529 GL_EXTCALL(glUseProgram(program_id));
10530 checkGLcall("glUseProgram");
10532 entry->constant_update_mask = 0;
10533 if (vshader)
10535 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
10536 if (vshader->reg_maps.integer_constants)
10537 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
10538 if (vshader->reg_maps.boolean_constants)
10539 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
10540 if (entry->vs.pos_fixup_location != -1)
10541 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10542 if (entry->vs.base_vertex_id_location != -1)
10543 entry->constant_update_mask |= WINED3D_SHADER_CONST_BASE_VERTEX_ID;
10545 shader_glsl_load_program_resources(context_gl, priv, program_id, vshader);
10547 else
10549 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
10550 | WINED3D_SHADER_CONST_FFP_PROJ;
10552 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
10554 if (entry->vs.modelview_matrix_location[i] != -1)
10556 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
10557 break;
10561 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10563 if (entry->vs.texture_matrix_location[i] != -1)
10565 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
10566 break;
10569 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
10570 || entry->vs.material_specular_location != -1
10571 || entry->vs.material_emissive_location != -1
10572 || entry->vs.material_shininess_location != -1)
10573 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
10574 if (entry->vs.light_ambient_location != -1)
10575 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
10577 if (entry->vs.clip_planes_location != -1)
10578 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
10579 if (entry->vs.pointsize_min_location != -1)
10580 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
10582 if (hshader)
10583 shader_glsl_load_program_resources(context_gl, priv, program_id, hshader);
10585 if (dshader)
10587 if (entry->ds.pos_fixup_location != -1)
10588 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10590 shader_glsl_load_program_resources(context_gl, priv, program_id, dshader);
10593 if (gshader)
10595 if (entry->gs.pos_fixup_location != -1)
10596 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
10598 shader_glsl_load_program_resources(context_gl, priv, program_id, gshader);
10601 if (ps_id)
10603 if (pshader)
10605 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
10606 if (pshader->reg_maps.integer_constants)
10607 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
10608 if (pshader->reg_maps.boolean_constants)
10609 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
10610 if (entry->ps.ycorrection_location != -1)
10611 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
10613 shader_glsl_load_program_resources(context_gl, priv, program_id, pshader);
10614 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
10616 else
10618 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10620 shader_glsl_load_samplers(&context_gl->c, priv, program_id, NULL);
10623 for (i = 0; i < WINED3D_MAX_TEXTURES; ++i)
10625 if (entry->ps.bumpenv_mat_location[i] != -1)
10627 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
10628 break;
10632 if (entry->ps.fog_color_location != -1)
10633 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10634 if (entry->ps.alpha_test_ref_location != -1)
10635 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10636 if (entry->ps.np2_fixup_location != -1)
10637 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
10638 if (entry->ps.color_key_location != -1)
10639 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10643 static void shader_glsl_precompile(void *shader_priv, struct wined3d_shader *shader)
10645 struct wined3d_device *device = shader->device;
10646 struct wined3d_context *context;
10648 if (shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_COMPUTE)
10650 context = context_acquire(device, NULL, 0);
10651 shader_glsl_compile_compute_shader(shader_priv, wined3d_context_gl(context), shader);
10652 context_release(context);
10656 /* Context activation is done by the caller. */
10657 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
10658 const struct wined3d_state *state)
10660 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10661 struct glsl_context_data *ctx_data = context->shader_backend_data;
10662 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10663 struct shader_glsl_priv *priv = shader_priv;
10664 struct glsl_shader_prog_link *glsl_program;
10665 GLenum current_vertex_color_clamp;
10666 GLuint program_id, prev_id;
10668 priv->vertex_pipe->vp_enable(context, !use_vs(state));
10669 priv->fragment_pipe->fp_enable(context, !use_ps(state));
10671 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10672 set_glsl_shader_program(context_gl, state, priv, ctx_data);
10673 glsl_program = ctx_data->glsl_program;
10675 if (glsl_program)
10677 program_id = glsl_program->id;
10678 current_vertex_color_clamp = glsl_program->vs.vertex_color_clamp;
10679 if (glsl_program->shader_controlled_clip_distances)
10680 wined3d_context_gl_enable_clip_distances(context_gl, glsl_program->clip_distance_mask);
10682 else
10684 program_id = 0;
10685 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
10688 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
10690 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
10691 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10693 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
10694 checkGLcall("glClampColorARB");
10696 else
10698 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
10702 TRACE("Using GLSL program %u.\n", program_id);
10704 if (prev_id != program_id)
10706 GL_EXTCALL(glUseProgram(program_id));
10707 checkGLcall("glUseProgram");
10709 if (glsl_program)
10710 context->constant_update_mask |= glsl_program->constant_update_mask;
10713 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
10716 /* Context activation is done by the caller. */
10717 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
10718 const struct wined3d_state *state)
10720 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10721 struct glsl_context_data *ctx_data = context->shader_backend_data;
10722 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10723 struct shader_glsl_priv *priv = shader_priv;
10724 GLuint program_id, prev_id;
10726 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10727 set_glsl_compute_shader_program(context_gl, state, priv, ctx_data);
10728 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
10730 TRACE("Using GLSL program %u.\n", program_id);
10732 if (prev_id != program_id)
10734 GL_EXTCALL(glUseProgram(program_id));
10735 checkGLcall("glUseProgram");
10738 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
10739 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10740 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10741 | (1u << WINED3D_SHADER_TYPE_HULL)
10742 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
10745 /* "context" is not necessarily the currently active context. */
10746 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
10748 struct glsl_context_data *ctx_data = context->shader_backend_data;
10750 ctx_data->glsl_program = NULL;
10751 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
10752 | (1u << WINED3D_SHADER_TYPE_VERTEX)
10753 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
10754 | (1u << WINED3D_SHADER_TYPE_HULL)
10755 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
10756 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
10759 /* Context activation is done by the caller. */
10760 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
10762 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
10763 struct glsl_context_data *ctx_data = context->shader_backend_data;
10764 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
10765 struct shader_glsl_priv *priv = shader_priv;
10767 shader_glsl_invalidate_current_program(context);
10768 GL_EXTCALL(glUseProgram(0));
10769 checkGLcall("glUseProgram");
10771 priv->vertex_pipe->vp_enable(context, FALSE);
10772 priv->fragment_pipe->fp_enable(context, FALSE);
10774 if (needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
10776 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
10777 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
10778 checkGLcall("glClampColorARB");
10782 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
10783 const struct glsl_shader_prog_link *program)
10785 const struct glsl_context_data *ctx_data;
10786 struct wined3d_context *context;
10787 unsigned int i;
10789 for (i = 0; i < device->context_count; ++i)
10791 context = device->contexts[i];
10792 ctx_data = context->shader_backend_data;
10794 if (ctx_data->glsl_program == program)
10795 shader_glsl_invalidate_current_program(context);
10799 static void shader_glsl_destroy(struct wined3d_shader *shader)
10801 struct glsl_shader_private *shader_data = shader->backend_data;
10802 struct wined3d_device *device = shader->device;
10803 struct shader_glsl_priv *priv = device->shader_priv;
10804 const struct wined3d_gl_info *gl_info;
10805 const struct list *linked_programs;
10806 struct wined3d_context *context;
10808 if (!shader_data || !shader_data->num_gl_shaders)
10810 heap_free(shader_data);
10811 shader->backend_data = NULL;
10812 return;
10815 context = context_acquire(device, NULL, 0);
10816 gl_info = wined3d_context_gl(context)->gl_info;
10818 TRACE("Deleting linked programs.\n");
10819 linked_programs = &shader->linked_programs;
10820 if (!list_empty(linked_programs))
10822 struct glsl_shader_prog_link *entry, *entry2;
10823 UINT i;
10825 switch (shader->reg_maps.shader_version.type)
10827 case WINED3D_SHADER_TYPE_PIXEL:
10829 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
10831 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10833 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
10834 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10835 checkGLcall("glDeleteShader");
10837 heap_free(shader_data->gl_shaders.ps);
10839 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10840 struct glsl_shader_prog_link, ps.shader_entry)
10842 shader_glsl_invalidate_contexts_program(device, entry);
10843 delete_glsl_program_entry(priv, gl_info, entry);
10846 break;
10849 case WINED3D_SHADER_TYPE_VERTEX:
10851 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
10853 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10855 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
10856 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10857 checkGLcall("glDeleteShader");
10859 heap_free(shader_data->gl_shaders.vs);
10861 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10862 struct glsl_shader_prog_link, vs.shader_entry)
10864 shader_glsl_invalidate_contexts_program(device, entry);
10865 delete_glsl_program_entry(priv, gl_info, entry);
10868 break;
10871 case WINED3D_SHADER_TYPE_HULL:
10873 struct glsl_hs_compiled_shader *gl_shaders = shader_data->gl_shaders.hs;
10875 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10877 TRACE("Deleting hull shader %u.\n", gl_shaders[i].id);
10878 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10879 checkGLcall("glDeleteShader");
10881 heap_free(shader_data->gl_shaders.hs);
10883 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10884 struct glsl_shader_prog_link, hs.shader_entry)
10886 shader_glsl_invalidate_contexts_program(device, entry);
10887 delete_glsl_program_entry(priv, gl_info, entry);
10890 break;
10893 case WINED3D_SHADER_TYPE_DOMAIN:
10895 struct glsl_ds_compiled_shader *gl_shaders = shader_data->gl_shaders.ds;
10897 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10899 TRACE("Deleting domain shader %u.\n", gl_shaders[i].id);
10900 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10901 checkGLcall("glDeleteShader");
10903 heap_free(shader_data->gl_shaders.ds);
10905 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10906 struct glsl_shader_prog_link, ds.shader_entry)
10908 shader_glsl_invalidate_contexts_program(device, entry);
10909 delete_glsl_program_entry(priv, gl_info, entry);
10912 break;
10915 case WINED3D_SHADER_TYPE_GEOMETRY:
10917 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
10919 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10921 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
10922 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10923 checkGLcall("glDeleteShader");
10925 heap_free(shader_data->gl_shaders.gs);
10927 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10928 struct glsl_shader_prog_link, gs.shader_entry)
10930 shader_glsl_invalidate_contexts_program(device, entry);
10931 delete_glsl_program_entry(priv, gl_info, entry);
10934 break;
10937 case WINED3D_SHADER_TYPE_COMPUTE:
10939 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
10941 for (i = 0; i < shader_data->num_gl_shaders; ++i)
10943 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
10944 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
10945 checkGLcall("glDeleteShader");
10947 heap_free(shader_data->gl_shaders.cs);
10949 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
10950 struct glsl_shader_prog_link, cs.shader_entry)
10952 shader_glsl_invalidate_contexts_program(device, entry);
10953 delete_glsl_program_entry(priv, gl_info, entry);
10956 break;
10959 default:
10960 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
10961 break;
10965 heap_free(shader->backend_data);
10966 shader->backend_data = NULL;
10968 context_release(context);
10971 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
10973 const struct glsl_program_key *k = key;
10974 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
10975 const struct glsl_shader_prog_link, program_lookup_entry);
10976 int ret;
10978 if ((ret = wined3d_uint32_compare(k->vs_id, prog->vs.id)))
10979 return ret;
10980 if ((ret = wined3d_uint32_compare(k->gs_id, prog->gs.id)))
10981 return ret;
10982 if ((ret = wined3d_uint32_compare(k->ps_id, prog->ps.id)))
10983 return ret;
10984 if ((ret = wined3d_uint32_compare(k->hs_id, prog->hs.id)))
10985 return ret;
10986 if ((ret = wined3d_uint32_compare(k->ds_id, prog->ds.id)))
10987 return ret;
10988 if ((ret = wined3d_uint32_compare(k->cs_id, prog->cs.id)))
10989 return ret;
10991 return 0;
10994 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
10996 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
10997 + constant_count * sizeof(*heap->contained)
10998 + constant_count * sizeof(*heap->positions);
10999 void *mem;
11001 if (!(mem = heap_alloc(size)))
11003 ERR("Failed to allocate memory\n");
11004 return FALSE;
11007 heap->entries = mem;
11008 heap->entries[1].version = 0;
11009 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
11010 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
11011 heap->positions = (unsigned int *)(heap->contained + constant_count);
11012 heap->size = 1;
11014 return TRUE;
11017 static void constant_heap_free(struct constant_heap *heap)
11019 heap_free(heap->entries);
11022 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
11023 const struct wined3d_fragment_pipe_ops *fragment_pipe)
11025 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
11026 struct fragment_caps fragment_caps;
11027 void *vertex_priv, *fragment_priv;
11028 struct shader_glsl_priv *priv;
11030 if (!(priv = heap_alloc_zero(sizeof(*priv))))
11031 return E_OUTOFMEMORY;
11033 string_buffer_list_init(&priv->string_buffers);
11035 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
11037 ERR("Failed to initialize vertex pipe.\n");
11038 heap_free(priv);
11039 return E_FAIL;
11042 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
11044 ERR("Failed to initialize fragment pipe.\n");
11045 vertex_pipe->vp_free(device, NULL);
11046 heap_free(priv);
11047 return E_FAIL;
11050 if (!string_buffer_init(&priv->shader_buffer))
11052 ERR("Failed to initialize shader buffer.\n");
11053 goto fail;
11056 if (!(priv->stack = heap_calloc(stack_size, sizeof(*priv->stack))))
11058 ERR("Failed to allocate memory.\n");
11059 goto fail;
11062 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
11064 ERR("Failed to initialize vertex shader constant heap\n");
11065 goto fail;
11068 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
11070 ERR("Failed to initialize pixel shader constant heap\n");
11071 goto fail;
11074 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
11076 priv->next_constant_version = 1;
11077 priv->vertex_pipe = vertex_pipe;
11078 priv->fragment_pipe = fragment_pipe;
11079 fragment_pipe->get_caps(device->adapter, &fragment_caps);
11080 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
11081 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
11083 device->vertex_priv = vertex_priv;
11084 device->fragment_priv = fragment_priv;
11085 device->shader_priv = priv;
11087 return WINED3D_OK;
11089 fail:
11090 constant_heap_free(&priv->pconst_heap);
11091 constant_heap_free(&priv->vconst_heap);
11092 heap_free(priv->stack);
11093 string_buffer_free(&priv->shader_buffer);
11094 fragment_pipe->free_private(device, NULL);
11095 vertex_pipe->vp_free(device, NULL);
11096 heap_free(priv);
11097 return E_OUTOFMEMORY;
11100 /* Context activation is done by the caller. */
11101 static void shader_glsl_free(struct wined3d_device *device, struct wined3d_context *context)
11103 struct shader_glsl_priv *priv = device->shader_priv;
11105 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
11106 constant_heap_free(&priv->pconst_heap);
11107 constant_heap_free(&priv->vconst_heap);
11108 heap_free(priv->stack);
11109 string_buffer_list_cleanup(&priv->string_buffers);
11110 string_buffer_free(&priv->shader_buffer);
11111 priv->fragment_pipe->free_private(device, context);
11112 priv->vertex_pipe->vp_free(device, context);
11114 heap_free(device->shader_priv);
11115 device->shader_priv = NULL;
11118 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
11120 struct glsl_context_data *ctx_data;
11122 if (!(ctx_data = heap_alloc_zero(sizeof(*ctx_data))))
11123 return FALSE;
11124 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
11125 context->shader_backend_data = ctx_data;
11126 return TRUE;
11129 static void shader_glsl_free_context_data(struct wined3d_context *context)
11131 heap_free(context->shader_backend_data);
11134 static void shader_glsl_init_context_state(struct wined3d_context *context)
11136 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11137 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11139 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
11140 checkGLcall("GL_PROGRAM_POINT_SIZE");
11143 static unsigned int shader_glsl_get_shader_model(const struct wined3d_gl_info *gl_info)
11145 BOOL shader_model_4 = gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
11146 && gl_info->supported[ARB_SHADER_BIT_ENCODING]
11147 && gl_info->supported[ARB_TEXTURE_SWIZZLE];
11149 if (shader_model_4
11150 && gl_info->supported[ARB_COMPUTE_SHADER]
11151 && gl_info->supported[ARB_CULL_DISTANCE]
11152 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
11153 && gl_info->supported[ARB_GPU_SHADER5]
11154 && gl_info->supported[ARB_SHADER_ATOMIC_COUNTERS]
11155 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
11156 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
11157 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING]
11158 && gl_info->supported[ARB_TESSELLATION_SHADER]
11159 && gl_info->supported[ARB_TEXTURE_GATHER]
11160 && gl_info->supported[ARB_TRANSFORM_FEEDBACK3])
11161 return 5;
11163 if (shader_model_4)
11164 return 4;
11166 /* Support for texldd and texldl instructions in pixel shaders is required
11167 * for SM3. */
11168 if (shader_glsl_has_core_grad(gl_info) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
11169 return 3;
11171 return 2;
11174 static void shader_glsl_get_caps(const struct wined3d_adapter *adapter, struct shader_caps *caps)
11176 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11177 unsigned int shader_model = shader_glsl_get_shader_model(gl_info);
11179 TRACE("Shader model %u.\n", shader_model);
11181 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
11182 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
11183 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
11184 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
11185 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
11186 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
11188 caps->vs_version = gl_info->supported[ARB_VERTEX_SHADER] ? caps->vs_version : 0;
11189 caps->ps_version = gl_info->supported[ARB_FRAGMENT_SHADER] ? caps->ps_version : 0;
11191 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
11192 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
11193 caps->varying_count = gl_info->limits.glsl_varyings;
11195 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
11196 * Direct3D minimum requirement.
11198 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
11199 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
11201 * The problem is that the refrast clamps temporary results in the shader to
11202 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
11203 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
11204 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
11205 * offer a way to query this.
11207 if (shader_model >= 4)
11208 caps->ps_1x_max_value = FLT_MAX;
11209 else
11210 caps->ps_1x_max_value = 1024.0f;
11212 /* Ideally we'd only set caps like sRGB writes here if supported by both
11213 * the shader backend and the fragment pipe, but we can get called before
11214 * shader_glsl_alloc(). */
11215 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
11216 | WINED3D_SHADER_CAP_SRGB_WRITE;
11217 if (needs_interpolation_qualifiers_for_shader_outputs(gl_info))
11218 caps->wined3d_caps |= WINED3D_SHADER_CAP_OUTPUT_INTERPOLATION;
11219 if (shader_glsl_full_ffp_varyings(gl_info))
11220 caps->wined3d_caps |= WINED3D_SHADER_CAP_FULL_FFP_VARYINGS;
11223 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
11225 /* We support everything except YUV conversions. */
11226 return !is_complex_fixup(fixup);
11229 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
11231 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
11232 /* WINED3DSIH_ADD */ shader_glsl_binop,
11233 /* WINED3DSIH_AND */ shader_glsl_binop,
11234 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
11235 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
11236 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
11237 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
11238 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
11239 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
11240 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
11241 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
11242 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
11243 /* WINED3DSIH_BEM */ shader_glsl_bem,
11244 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
11245 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
11246 /* WINED3DSIH_BREAK */ shader_glsl_break,
11247 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
11248 /* WINED3DSIH_BREAKP */ shader_glsl_conditional_op,
11249 /* WINED3DSIH_BUFINFO */ shader_glsl_bufinfo,
11250 /* WINED3DSIH_CALL */ shader_glsl_call,
11251 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
11252 /* WINED3DSIH_CASE */ shader_glsl_case,
11253 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
11254 /* WINED3DSIH_CND */ shader_glsl_cnd,
11255 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
11256 /* WINED3DSIH_CONTINUEP */ shader_glsl_conditional_op,
11257 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
11258 /* WINED3DSIH_CRS */ shader_glsl_cross,
11259 /* WINED3DSIH_CUT */ shader_glsl_cut,
11260 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
11261 /* WINED3DSIH_DCL */ shader_glsl_nop,
11262 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
11263 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
11264 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
11265 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
11266 /* WINED3DSIH_DCL_GS_INSTANCES */ shader_glsl_nop,
11267 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11268 /* WINED3DSIH_DCL_HS_JOIN_PHASE_INSTANCE_COUNT */ shader_glsl_nop,
11269 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ shader_glsl_nop,
11270 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
11271 /* WINED3DSIH_DCL_INDEX_RANGE */ shader_glsl_nop,
11272 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
11273 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
11274 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11275 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
11276 /* WINED3DSIH_DCL_INPUT_PS */ shader_glsl_nop,
11277 /* WINED3DSIH_DCL_INPUT_PS_SGV */ shader_glsl_nop,
11278 /* WINED3DSIH_DCL_INPUT_PS_SIV */ shader_glsl_nop,
11279 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
11280 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
11281 /* WINED3DSIH_DCL_INTERFACE */ NULL,
11282 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
11283 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ shader_glsl_nop,
11284 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
11285 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
11286 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
11287 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
11288 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
11289 /* WINED3DSIH_DCL_STREAM */ NULL,
11290 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
11291 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ shader_glsl_nop,
11292 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ shader_glsl_nop,
11293 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ shader_glsl_nop,
11294 /* WINED3DSIH_DCL_TGSM_RAW */ shader_glsl_nop,
11295 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ shader_glsl_nop,
11296 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
11297 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
11298 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
11299 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
11300 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
11301 /* WINED3DSIH_DEF */ shader_glsl_nop,
11302 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
11303 /* WINED3DSIH_DEFB */ shader_glsl_nop,
11304 /* WINED3DSIH_DEFI */ shader_glsl_nop,
11305 /* WINED3DSIH_DIV */ shader_glsl_binop,
11306 /* WINED3DSIH_DP2 */ shader_glsl_dot,
11307 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
11308 /* WINED3DSIH_DP3 */ shader_glsl_dot,
11309 /* WINED3DSIH_DP4 */ shader_glsl_dot,
11310 /* WINED3DSIH_DST */ shader_glsl_dst,
11311 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
11312 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
11313 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
11314 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
11315 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
11316 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
11317 /* WINED3DSIH_ELSE */ shader_glsl_else,
11318 /* WINED3DSIH_EMIT */ shader_glsl_emit,
11319 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
11320 /* WINED3DSIH_ENDIF */ shader_glsl_end,
11321 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
11322 /* WINED3DSIH_ENDREP */ shader_glsl_end,
11323 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
11324 /* WINED3DSIH_EQ */ shader_glsl_relop,
11325 /* WINED3DSIH_EVAL_CENTROID */ NULL,
11326 /* WINED3DSIH_EVAL_SAMPLE_INDEX */ shader_glsl_interpolate,
11327 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
11328 /* WINED3DSIH_EXPP */ shader_glsl_expp,
11329 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
11330 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
11331 /* WINED3DSIH_FCALL */ NULL,
11332 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
11333 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
11334 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
11335 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
11336 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
11337 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
11338 /* WINED3DSIH_GATHER4 */ shader_glsl_gather4,
11339 /* WINED3DSIH_GATHER4_C */ shader_glsl_gather4,
11340 /* WINED3DSIH_GATHER4_PO */ shader_glsl_gather4,
11341 /* WINED3DSIH_GATHER4_PO_C */ shader_glsl_gather4,
11342 /* WINED3DSIH_GE */ shader_glsl_relop,
11343 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ shader_glsl_nop,
11344 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
11345 /* WINED3DSIH_HS_FORK_PHASE */ shader_glsl_nop,
11346 /* WINED3DSIH_HS_JOIN_PHASE */ shader_glsl_nop,
11347 /* WINED3DSIH_IADD */ shader_glsl_binop,
11348 /* WINED3DSIH_IBFE */ shader_glsl_bitwise_op,
11349 /* WINED3DSIH_IEQ */ shader_glsl_relop,
11350 /* WINED3DSIH_IF */ shader_glsl_if,
11351 /* WINED3DSIH_IFC */ shader_glsl_ifc,
11352 /* WINED3DSIH_IGE */ shader_glsl_relop,
11353 /* WINED3DSIH_ILT */ shader_glsl_relop,
11354 /* WINED3DSIH_IMAD */ shader_glsl_mad,
11355 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
11356 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
11357 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ shader_glsl_uav_counter,
11358 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
11359 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
11360 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ shader_glsl_uav_counter,
11361 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
11362 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
11363 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
11364 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
11365 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
11366 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
11367 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
11368 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
11369 /* WINED3DSIH_IMUL */ shader_glsl_mul_extended,
11370 /* WINED3DSIH_INE */ shader_glsl_relop,
11371 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
11372 /* WINED3DSIH_ISHL */ shader_glsl_binop,
11373 /* WINED3DSIH_ISHR */ shader_glsl_binop,
11374 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
11375 /* WINED3DSIH_LABEL */ shader_glsl_label,
11376 /* WINED3DSIH_LD */ shader_glsl_ld,
11377 /* WINED3DSIH_LD2DMS */ shader_glsl_ld,
11378 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw_structured,
11379 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_raw_structured,
11380 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
11381 /* WINED3DSIH_LIT */ shader_glsl_lit,
11382 /* WINED3DSIH_LOD */ NULL,
11383 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
11384 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
11385 /* WINED3DSIH_LOOP */ shader_glsl_loop,
11386 /* WINED3DSIH_LRP */ shader_glsl_lrp,
11387 /* WINED3DSIH_LT */ shader_glsl_relop,
11388 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
11389 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
11390 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
11391 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
11392 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
11393 /* WINED3DSIH_MAD */ shader_glsl_mad,
11394 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
11395 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
11396 /* WINED3DSIH_MOV */ shader_glsl_mov,
11397 /* WINED3DSIH_MOVA */ shader_glsl_mov,
11398 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
11399 /* WINED3DSIH_MUL */ shader_glsl_binop,
11400 /* WINED3DSIH_NE */ shader_glsl_relop,
11401 /* WINED3DSIH_NOP */ shader_glsl_nop,
11402 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
11403 /* WINED3DSIH_NRM */ shader_glsl_nrm,
11404 /* WINED3DSIH_OR */ shader_glsl_binop,
11405 /* WINED3DSIH_PHASE */ shader_glsl_nop,
11406 /* WINED3DSIH_POW */ shader_glsl_pow,
11407 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
11408 /* WINED3DSIH_REP */ shader_glsl_rep,
11409 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
11410 /* WINED3DSIH_RET */ shader_glsl_ret,
11411 /* WINED3DSIH_RETP */ shader_glsl_conditional_op,
11412 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
11413 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
11414 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
11415 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
11416 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
11417 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
11418 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
11419 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
11420 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
11421 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
11422 /* WINED3DSIH_SAMPLE_INFO */ shader_glsl_sample_info,
11423 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
11424 /* WINED3DSIH_SAMPLE_POS */ NULL,
11425 /* WINED3DSIH_SETP */ NULL,
11426 /* WINED3DSIH_SGE */ shader_glsl_compare,
11427 /* WINED3DSIH_SGN */ shader_glsl_sgn,
11428 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
11429 /* WINED3DSIH_SLT */ shader_glsl_compare,
11430 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
11431 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw_structured,
11432 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_raw_structured,
11433 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
11434 /* WINED3DSIH_SUB */ shader_glsl_binop,
11435 /* WINED3DSIH_SWAPC */ shader_glsl_swapc,
11436 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
11437 /* WINED3DSIH_SYNC */ shader_glsl_sync,
11438 /* WINED3DSIH_TEX */ shader_glsl_tex,
11439 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
11440 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
11441 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
11442 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
11443 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
11444 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
11445 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
11446 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
11447 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
11448 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
11449 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
11450 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
11451 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
11452 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
11453 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
11454 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
11455 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
11456 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
11457 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
11458 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
11459 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
11460 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
11461 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
11462 /* WINED3DSIH_UGE */ shader_glsl_relop,
11463 /* WINED3DSIH_ULT */ shader_glsl_relop,
11464 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
11465 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
11466 /* WINED3DSIH_UMUL */ shader_glsl_mul_extended,
11467 /* WINED3DSIH_USHR */ shader_glsl_binop,
11468 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
11469 /* WINED3DSIH_XOR */ shader_glsl_binop,
11472 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
11473 SHADER_HANDLER hw_fct;
11475 /* Select handler */
11476 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
11478 /* Unhandled opcode */
11479 if (!hw_fct)
11481 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
11482 return;
11484 hw_fct(ins);
11486 shader_glsl_add_instruction_modifiers(ins);
11489 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
11491 struct shader_glsl_priv *priv = shader_priv;
11493 return priv->ffp_proj_control;
11496 static uint64_t shader_glsl_shader_compile(struct wined3d_context *context, const struct wined3d_shader_desc *shader_desc,
11497 enum wined3d_shader_type shader_type)
11499 ERR("Not implemented.\n");
11500 return 0;
11503 const struct wined3d_shader_backend_ops glsl_shader_backend =
11505 shader_glsl_handle_instruction,
11506 shader_glsl_precompile,
11507 shader_glsl_select,
11508 shader_glsl_select_compute,
11509 shader_glsl_disable,
11510 shader_glsl_update_float_vertex_constants,
11511 shader_glsl_update_float_pixel_constants,
11512 shader_glsl_load_constants,
11513 shader_glsl_destroy,
11514 shader_glsl_alloc,
11515 shader_glsl_free,
11516 shader_glsl_allocate_context_data,
11517 shader_glsl_free_context_data,
11518 shader_glsl_init_context_state,
11519 shader_glsl_get_caps,
11520 shader_glsl_color_fixup_supported,
11521 shader_glsl_has_ffp_proj_control,
11522 shader_glsl_shader_compile,
11525 static void glsl_vertex_pipe_vp_enable(const struct wined3d_context *context, BOOL enable) {}
11527 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_adapter *adapter, struct wined3d_vertex_caps *caps)
11529 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
11531 caps->xyzrhw = TRUE;
11532 caps->emulated_flatshading = !needs_legacy_glsl_syntax(gl_info);
11533 caps->ffp_generic_attributes = TRUE;
11534 caps->max_active_lights = WINED3D_MAX_ACTIVE_LIGHTS;
11535 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
11536 caps->max_vertex_blend_matrix_index = 0;
11537 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
11538 | WINED3DVTXPCAPS_MATERIALSOURCE7
11539 | WINED3DVTXPCAPS_VERTEXFOG
11540 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
11541 | WINED3DVTXPCAPS_POSITIONALLIGHTS
11542 | WINED3DVTXPCAPS_LOCALVIEWER
11543 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
11544 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
11545 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
11546 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
11549 static unsigned int glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
11551 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
11552 return GL_EXT_EMUL_ARB_MULTITEXTURE;
11553 return 0;
11556 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
11558 struct shader_glsl_priv *priv;
11560 if (shader_backend == &glsl_shader_backend)
11562 priv = shader_priv;
11563 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
11564 return priv;
11567 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
11569 return NULL;
11572 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *param)
11574 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
11575 struct glsl_ffp_vertex_shader, desc.entry);
11576 struct glsl_shader_prog_link *program, *program2;
11577 struct glsl_ffp_destroy_ctx *ctx = param;
11578 const struct wined3d_gl_info *gl_info;
11580 gl_info = ctx->context_gl->gl_info;
11581 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
11582 struct glsl_shader_prog_link, vs.shader_entry)
11584 delete_glsl_program_entry(ctx->priv, gl_info, program);
11586 GL_EXTCALL(glDeleteShader(shader->id));
11587 heap_free(shader);
11590 /* Context activation is done by the caller. */
11591 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device, struct wined3d_context *context)
11593 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11594 struct shader_glsl_priv *priv = device->vertex_priv;
11595 struct glsl_ffp_destroy_ctx ctx;
11597 ctx.priv = priv;
11598 ctx.context_gl = context_gl;
11599 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
11602 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
11603 const struct wined3d_state *state, DWORD state_id) {}
11605 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
11606 const struct wined3d_state *state, DWORD state_id)
11608 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11611 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
11612 const struct wined3d_state *state, DWORD state_id)
11614 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11615 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11616 BOOL specular = !!(context->stream_info.use_map & (1u << WINED3D_FFP_SPECULAR));
11617 BOOL diffuse = !!(context->stream_info.use_map & (1u << WINED3D_FFP_DIFFUSE));
11618 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
11619 const BOOL legacy_clip_planes = needs_legacy_glsl_syntax(gl_info);
11620 BOOL transformed = context->stream_info.position_transformed;
11621 BOOL wasrhw = context->last_was_rhw;
11622 unsigned int i;
11624 context->last_was_rhw = transformed;
11626 /* If the vertex declaration contains a transformed position attribute,
11627 * the draw uses the fixed function vertex pipeline regardless of any
11628 * vertex shader set by the application. */
11629 if (transformed != wasrhw
11630 || context->stream_info.swizzle_map != context->last_swizzle_map)
11631 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11633 context->last_swizzle_map = context->stream_info.swizzle_map;
11635 if (!use_vs(state))
11637 if (context->last_was_vshader)
11639 if (legacy_clip_planes)
11640 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11641 clipplane(context, state, STATE_CLIPPLANE(i));
11642 else
11643 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11646 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11648 /* Because of settings->texcoords, we have to regenerate the vertex
11649 * shader on a vdecl change if there aren't enough varyings to just
11650 * always output all the texture coordinates.
11652 * Likewise, we have to invalidate the shader when using per-vertex
11653 * colours and diffuse/specular attribute presence changes, or when
11654 * normal presence changes. */
11655 if (!shader_glsl_full_ffp_varyings(gl_info) || (state->render_states[WINED3D_RS_COLORVERTEX]
11656 && (diffuse != context->last_was_diffuse || specular != context->last_was_specular))
11657 || normal != context->last_was_normal)
11658 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11660 if (use_ps(state)
11661 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
11662 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
11663 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11665 else
11667 if (!context->last_was_vshader)
11669 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
11670 if (legacy_clip_planes)
11671 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
11672 clipplane(context, state, STATE_CLIPPLANE(i));
11673 else
11674 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11678 context->last_was_vshader = use_vs(state);
11679 context->last_was_diffuse = diffuse;
11680 context->last_was_specular = specular;
11681 context->last_was_normal = normal;
11684 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
11685 const struct wined3d_state *state, DWORD state_id)
11687 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11688 /* Different vertex shaders potentially require a different vertex attributes setup. */
11689 if (!isStateDirty(context, STATE_VDECL))
11690 context_apply_state(context, state, STATE_VDECL);
11693 static void glsl_vertex_pipe_hs(struct wined3d_context *context,
11694 const struct wined3d_state *state, DWORD state_id)
11696 /* In Direct3D tessellator options (e.g. output primitive type, primitive
11697 * winding) are defined in Hull Shaders, while in GLSL those are
11698 * specified in Tessellation Evaluation Shaders. */
11699 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11701 if (state->shader[WINED3D_SHADER_TYPE_VERTEX])
11702 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11705 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
11706 const struct wined3d_state *state, DWORD state_id)
11708 struct glsl_context_data *ctx_data = context->shader_backend_data;
11709 BOOL rasterization_disabled;
11711 rasterization_disabled = is_rasterization_disabled(state->shader[WINED3D_SHADER_TYPE_GEOMETRY]);
11712 if (ctx_data->rasterization_disabled != rasterization_disabled)
11713 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
11714 ctx_data->rasterization_disabled = rasterization_disabled;
11716 if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11717 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11718 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11719 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11720 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11723 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
11724 const struct wined3d_state *state, DWORD state_id)
11726 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
11727 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
11728 else if (state->shader[WINED3D_SHADER_TYPE_DOMAIN])
11729 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_DOMAIN;
11730 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
11731 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
11732 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11735 static void glsl_vertex_pipe_world(struct wined3d_context *context,
11736 const struct wined3d_state *state, DWORD state_id)
11738 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
11741 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
11742 const struct wined3d_state *state, DWORD state_id)
11744 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11747 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
11749 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11750 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11751 unsigned int k;
11753 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
11754 | WINED3D_SHADER_CONST_FFP_LIGHTS
11755 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
11757 if (needs_legacy_glsl_syntax(gl_info))
11759 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
11761 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
11762 clipplane(context, state, STATE_CLIPPLANE(k));
11765 else
11767 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11771 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
11772 const struct wined3d_state *state, DWORD state_id)
11774 /* Table fog behavior depends on the projection matrix. */
11775 if (state->render_states[WINED3D_RS_FOGENABLE]
11776 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
11777 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11778 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
11781 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
11782 const struct wined3d_state *state, DWORD state_id)
11784 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
11785 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
11786 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
11787 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
11788 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11789 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
11792 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
11793 const struct wined3d_state *state, DWORD state_id)
11795 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11798 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
11799 const struct wined3d_state *state, DWORD state_id)
11801 DWORD sampler = state_id - STATE_SAMPLER(0);
11802 const struct wined3d_texture *texture = state->textures[sampler];
11803 BOOL np2;
11805 if (!texture)
11806 return;
11808 if (sampler >= WINED3D_MAX_TEXTURES)
11809 return;
11811 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
11812 || context->lastWasPow2Texture & (1u << sampler))
11814 if (np2)
11815 context->lastWasPow2Texture |= 1u << sampler;
11816 else
11817 context->lastWasPow2Texture &= ~(1u << sampler);
11819 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
11823 static void glsl_vertex_pipe_material(struct wined3d_context *context,
11824 const struct wined3d_state *state, DWORD state_id)
11826 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
11829 static void glsl_vertex_pipe_light(struct wined3d_context *context,
11830 const struct wined3d_state *state, DWORD state_id)
11832 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
11835 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
11836 const struct wined3d_state *state, DWORD state_id)
11838 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11841 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
11842 const struct wined3d_state *state, DWORD state_id)
11844 if (!use_vs(state))
11845 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
11848 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
11849 const struct wined3d_state *state, DWORD state_id)
11851 static unsigned int once;
11853 if (state->primitive_type == WINED3D_PT_POINTLIST
11854 && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
11855 FIXME("Non-point sprite points not supported in core profile.\n");
11858 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
11859 const struct wined3d_state *state, DWORD state_id)
11861 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
11864 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
11865 const struct wined3d_state *state, DWORD state_id)
11867 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
11868 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
11869 UINT index = state_id - STATE_CLIPPLANE(0);
11871 if (index >= gl_info->limits.user_clip_distances)
11872 return;
11874 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
11877 static const struct wined3d_state_entry_template glsl_vertex_pipe_vp_states[] =
11879 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
11880 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
11881 {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), {STATE_SHADER(WINED3D_SHADER_TYPE_HULL), glsl_vertex_pipe_hs }, WINED3D_GL_EXT_NONE },
11882 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
11883 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
11884 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
11885 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
11886 /* Clip planes */
11887 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11888 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
11889 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11890 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
11891 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11892 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
11893 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11894 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
11895 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11896 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
11897 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11898 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
11899 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11900 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
11901 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GLSL_130 },
11902 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
11903 /* Lights */
11904 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11905 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11906 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11907 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11908 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11909 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11910 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11911 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11912 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11913 /* Viewport */
11914 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
11915 /* Transform states */
11916 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
11917 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
11918 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11919 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11920 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11921 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11922 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11923 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11924 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11925 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
11926 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
11927 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11928 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11929 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
11930 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11931 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11932 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11933 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11934 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11935 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11936 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11937 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
11938 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11939 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11940 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11941 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11942 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11943 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11944 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11945 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11946 /* Fog */
11947 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11948 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11949 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11950 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
11951 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
11952 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
11953 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11954 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
11955 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
11956 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11957 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11958 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11959 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11960 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11961 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11962 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
11963 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11964 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
11965 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
11966 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
11967 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
11968 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
11969 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11970 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11971 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
11972 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
11973 /* NP2 texture matrix fixups. They are not needed if
11974 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
11975 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
11976 * matrix. */
11977 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11978 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11979 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11980 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11981 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11982 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11983 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11984 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11985 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11986 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11987 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11988 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11989 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11990 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11991 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11992 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11993 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11994 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11995 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11996 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
11997 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
11998 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
11999 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
12000 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
12001 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
12002 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GLSL_130 },
12003 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_EXT_NONE },
12004 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
12007 /* TODO:
12008 * - Implement vertex tweening. */
12009 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
12011 glsl_vertex_pipe_vp_enable,
12012 glsl_vertex_pipe_vp_get_caps,
12013 glsl_vertex_pipe_vp_get_emul_mask,
12014 glsl_vertex_pipe_vp_alloc,
12015 glsl_vertex_pipe_vp_free,
12016 glsl_vertex_pipe_vp_states,
12019 static void glsl_fragment_pipe_enable(const struct wined3d_context *context, BOOL enable)
12021 /* Nothing to do. */
12024 static void glsl_fragment_pipe_get_caps(const struct wined3d_adapter *adapter, struct fragment_caps *caps)
12026 const struct wined3d_gl_info *gl_info = &adapter->gl_info;
12028 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
12029 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
12030 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
12031 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
12032 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
12033 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
12034 | WINED3DTEXOPCAPS_SELECTARG1
12035 | WINED3DTEXOPCAPS_SELECTARG2
12036 | WINED3DTEXOPCAPS_MODULATE4X
12037 | WINED3DTEXOPCAPS_MODULATE2X
12038 | WINED3DTEXOPCAPS_MODULATE
12039 | WINED3DTEXOPCAPS_ADDSIGNED2X
12040 | WINED3DTEXOPCAPS_ADDSIGNED
12041 | WINED3DTEXOPCAPS_ADD
12042 | WINED3DTEXOPCAPS_SUBTRACT
12043 | WINED3DTEXOPCAPS_ADDSMOOTH
12044 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
12045 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
12046 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
12047 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
12048 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
12049 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
12050 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
12051 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
12052 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
12053 | WINED3DTEXOPCAPS_DOTPRODUCT3
12054 | WINED3DTEXOPCAPS_MULTIPLYADD
12055 | WINED3DTEXOPCAPS_LERP
12056 | WINED3DTEXOPCAPS_BUMPENVMAP
12057 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
12058 caps->MaxTextureBlendStages = WINED3D_MAX_TEXTURES;
12059 caps->MaxSimultaneousTextures = min(gl_info->limits.samplers[WINED3D_SHADER_TYPE_PIXEL], WINED3D_MAX_TEXTURES);
12062 static unsigned int glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
12064 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
12065 return GL_EXT_EMUL_ARB_MULTITEXTURE;
12066 return 0;
12069 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
12071 struct shader_glsl_priv *priv;
12073 if (shader_backend == &glsl_shader_backend)
12075 priv = shader_priv;
12076 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
12077 return priv;
12080 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
12082 return NULL;
12085 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *param)
12087 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
12088 struct glsl_ffp_fragment_shader, entry.entry);
12089 struct glsl_shader_prog_link *program, *program2;
12090 struct glsl_ffp_destroy_ctx *ctx = param;
12091 const struct wined3d_gl_info *gl_info;
12093 gl_info = ctx->context_gl->gl_info;
12094 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
12095 struct glsl_shader_prog_link, ps.shader_entry)
12097 delete_glsl_program_entry(ctx->priv, gl_info, program);
12099 GL_EXTCALL(glDeleteShader(shader->id));
12100 heap_free(shader);
12103 /* Context activation is done by the caller. */
12104 static void glsl_fragment_pipe_free(struct wined3d_device *device, struct wined3d_context *context)
12106 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12107 struct shader_glsl_priv *priv = device->fragment_priv;
12108 struct glsl_ffp_destroy_ctx ctx;
12110 ctx.priv = priv;
12111 ctx.context_gl = context_gl;
12112 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
12115 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
12116 const struct wined3d_state *state, DWORD state_id)
12118 context->last_was_pshader = use_ps(state);
12120 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12123 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
12124 const struct wined3d_state *state, DWORD state_id)
12126 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12129 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
12130 const struct wined3d_state *state, DWORD state_id)
12132 BOOL use_vshader = use_vs(state);
12133 enum fogsource new_source;
12134 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
12135 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
12137 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12139 if (!state->render_states[WINED3D_RS_FOGENABLE])
12140 return;
12142 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
12144 if (use_vshader)
12145 new_source = FOGSOURCE_VS;
12146 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
12147 new_source = FOGSOURCE_COORD;
12148 else
12149 new_source = FOGSOURCE_FFP;
12151 else
12153 new_source = FOGSOURCE_FFP;
12156 if (new_source != context->fog_source || fogstart == fogend)
12158 context->fog_source = new_source;
12159 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
12163 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
12164 const struct wined3d_state *state, DWORD state_id)
12166 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12168 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12169 if (!shader_glsl_full_ffp_varyings(gl_info))
12170 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12172 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
12173 glsl_fragment_pipe_fog(context, state, state_id);
12176 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
12177 const struct wined3d_state *state, DWORD state_id)
12179 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12181 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
12182 if (!shader_glsl_full_ffp_varyings(gl_info))
12183 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12186 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
12187 const struct wined3d_state *state, DWORD state_id)
12189 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12192 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
12193 const struct wined3d_state *state, DWORD state_id)
12195 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
12198 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
12199 const struct wined3d_state *state, DWORD state_id)
12201 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12202 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
12203 float ref = wined3d_alpha_ref(state);
12205 if (func)
12207 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
12208 checkGLcall("glAlphaFunc");
12212 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
12213 const struct wined3d_state *state, DWORD state_id)
12215 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12218 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
12219 const struct wined3d_state *state, DWORD state_id)
12221 const struct wined3d_gl_info *gl_info = wined3d_context_gl(context)->gl_info;
12223 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
12225 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
12226 checkGLcall("glEnable(GL_ALPHA_TEST)");
12228 else
12230 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
12231 checkGLcall("glDisable(GL_ALPHA_TEST)");
12235 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
12236 const struct wined3d_state *state, DWORD state_id)
12238 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
12241 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
12242 const struct wined3d_state *state, DWORD state_id)
12244 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
12247 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
12248 const struct wined3d_state *state, DWORD state_id)
12250 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
12253 static const struct wined3d_state_entry_template glsl_fragment_pipe_state_template[] =
12255 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
12256 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
12257 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12258 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12259 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12260 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12261 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12262 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12263 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12264 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12265 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12266 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12267 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12268 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12269 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12270 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12271 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12272 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12273 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12274 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12275 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12276 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12277 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12278 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12279 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12280 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12281 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12282 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12283 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12284 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12285 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12286 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12287 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12288 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12289 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12290 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12291 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12292 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12293 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12294 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12295 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12296 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12297 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12298 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12299 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12300 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12301 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12302 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12303 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12304 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12305 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12306 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12307 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12308 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12309 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12310 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12311 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12312 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12313 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12314 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12315 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12316 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12317 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12318 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12319 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12320 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12321 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12322 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12323 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12324 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12325 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12326 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12327 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12328 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12329 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12330 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12331 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
12332 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
12333 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
12334 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
12335 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
12336 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
12337 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12338 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
12339 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
12340 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12341 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
12342 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12343 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
12344 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
12345 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
12346 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12347 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
12348 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
12349 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
12350 {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 },
12351 {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 },
12352 {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 },
12353 {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 },
12354 {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 },
12355 {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 },
12356 {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 },
12357 {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 },
12358 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12359 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12360 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12361 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12362 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12363 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12364 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12365 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12366 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
12367 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
12368 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GLSL_130 },
12369 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_EXT_NONE },
12370 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
12373 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
12375 return TRUE;
12378 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
12382 const struct wined3d_fragment_pipe_ops glsl_fragment_pipe =
12384 glsl_fragment_pipe_enable,
12385 glsl_fragment_pipe_get_caps,
12386 glsl_fragment_pipe_get_emul_mask,
12387 glsl_fragment_pipe_alloc,
12388 glsl_fragment_pipe_free,
12389 glsl_fragment_pipe_alloc_context_data,
12390 glsl_fragment_pipe_free_context_data,
12391 shader_glsl_color_fixup_supported,
12392 glsl_fragment_pipe_state_template,
12395 struct glsl_blitter_args
12397 GLenum texture_type;
12398 struct color_fixup_desc fixup;
12399 unsigned short use_colour_key;
12402 struct glsl_blitter_program
12404 struct wine_rb_entry entry;
12405 struct glsl_blitter_args args;
12406 GLuint id;
12409 struct wined3d_glsl_blitter
12411 struct wined3d_blitter blitter;
12412 struct wined3d_string_buffer_list string_buffers;
12413 struct wine_rb_tree programs;
12414 GLuint palette_texture;
12417 static int glsl_blitter_args_compare(const void *key, const struct wine_rb_entry *entry)
12419 const struct glsl_blitter_args *a = key;
12420 const struct glsl_blitter_args *b = &WINE_RB_ENTRY_VALUE(entry, const struct glsl_blitter_program, entry)->args;
12422 return memcmp(a, b, sizeof(*a));
12425 /* Context activation is done by the caller. */
12426 static void glsl_free_blitter_program(struct wine_rb_entry *entry, void *ctx)
12428 struct glsl_blitter_program *program = WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12429 struct wined3d_context_gl *context_gl = ctx;
12430 const struct wined3d_gl_info *gl_info;
12432 gl_info = context_gl->gl_info;
12433 GL_EXTCALL(glDeleteProgram(program->id));
12434 checkGLcall("glDeleteProgram()");
12435 heap_free(program);
12438 /* Context activation is done by the caller. */
12439 static void glsl_blitter_destroy(struct wined3d_blitter *blitter, struct wined3d_context *context)
12441 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
12442 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12443 struct wined3d_glsl_blitter *glsl_blitter;
12444 struct wined3d_blitter *next;
12446 if ((next = blitter->next))
12447 next->ops->blitter_destroy(next, context);
12449 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
12451 if (glsl_blitter->palette_texture)
12452 gl_info->gl_ops.gl.p_glDeleteTextures(1, &glsl_blitter->palette_texture);
12454 wine_rb_destroy(&glsl_blitter->programs, glsl_free_blitter_program, context_gl);
12455 string_buffer_list_cleanup(&glsl_blitter->string_buffers);
12457 heap_free(glsl_blitter);
12460 static void glsl_blitter_generate_p8_shader(struct wined3d_string_buffer *buffer,
12461 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12462 const char *output, const char *tex_type, const char *swizzle)
12464 shader_addline(buffer, "uniform sampler1D sampler_palette;\n");
12465 shader_addline(buffer, "\nvoid main()\n{\n");
12466 /* The alpha-component contains the palette index. */
12467 shader_addline(buffer, " float index = texture%s(sampler, out_texcoord.%s).%c;\n",
12468 needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle,
12469 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x');
12470 /* Scale the index by 255/256 and add a bias of 0.5 in order to sample in
12471 * the middle. */
12472 shader_addline(buffer, " index = (index * 255.0 + 0.5) / 256.0;\n");
12473 shader_addline(buffer, " %s = texture%s(sampler_palette, index);\n",
12474 output, needs_legacy_glsl_syntax(gl_info) ? "1D" : "");
12475 if (args->use_colour_key)
12476 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12477 shader_addline(buffer, "}\n");
12480 static void gen_packed_yuv_read(struct wined3d_string_buffer *buffer,
12481 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12482 const char *tex_type)
12484 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12485 char chroma, luminance;
12486 const char *tex;
12488 /* The YUY2 and UYVY formats contain two pixels packed into a 32 bit
12489 * macropixel, giving effectively 16 bits per pixel. The color consists of
12490 * a luminance(Y) and two chroma(U and V) values. Each macropixel has two
12491 * luminance values, one for each single pixel it contains, and one U and
12492 * one V value shared between both pixels.
12494 * The data is loaded into an A8L8 texture. With YUY2, the luminance
12495 * component contains the luminance and alpha the chroma. With UYVY it is
12496 * vice versa. Thus take the format into account when generating the read
12497 * swizzles
12499 * Reading the Y value is straightforward - just sample the texture. The
12500 * hardware takes care of filtering in the horizontal and vertical
12501 * direction.
12503 * Reading the U and V values is harder. We have to avoid filtering
12504 * horizontally, because that would mix the U and V values of one pixel or
12505 * two adjacent pixels. Thus floor the texture coordinate and add 0.5 to
12506 * get an unfiltered read, regardless of the filtering setting. Vertical
12507 * filtering works automatically though - the U and V values of two rows
12508 * are mixed nicely.
12510 * Apart of avoiding filtering issues, the code has to know which value it
12511 * just read, and where it can find the other one. To determine this, it
12512 * checks if it sampled an even or odd pixel, and shifts the 2nd read
12513 * accordingly.
12515 * Handling horizontal filtering of U and V values requires reading a 2nd
12516 * pair of pixels, extracting U and V and mixing them. This is not
12517 * implemented yet.
12519 * An alternative implementation idea is to load the texture as A8R8G8B8
12520 * texture, with width / 2. This way one read gives all 3 values, finding
12521 * U and V is easy in an unfiltered situation. Finding the luminance on
12522 * the other hand requires finding out if it is an odd or even pixel. The
12523 * real drawback of this approach is filtering. This would have to be
12524 * emulated completely in the shader, reading up two 2 packed pixels in up
12525 * to 2 rows and interpolating both horizontally and vertically. Beyond
12526 * that it would require adjustments to the texture handling code to deal
12527 * with the width scaling. */
12529 if (complex_fixup == COMPLEX_FIXUP_UYVY)
12531 chroma = 'x';
12532 luminance = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12534 else
12536 chroma = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'y';
12537 luminance = 'x';
12540 tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12542 /* First we have to read the chroma values. This means we need at least
12543 * two pixels (no filtering), or 4 pixels (with filtering). To get the
12544 * unmodified chroma, we have to rid ourselves of the filtering when we
12545 * sample the texture. */
12546 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12547 /* We must not allow filtering between pixel x and x+1, this would mix U
12548 * and V. Vertical filtering is ok. However, bear in mind that the pixel
12549 * center is at 0.5, so add 0.5. */
12550 shader_addline(buffer, " texcoord.x = (floor(texcoord.x * size.x) + 0.5) / size.x;\n");
12551 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12553 /* Multiply the x coordinate by 0.5 and get the fraction. This gives 0.25
12554 * and 0.75 for the even and odd pixels respectively. */
12555 /* Put the value into either of the chroma values. */
12556 shader_addline(buffer, " bool even = fract(texcoord.x * size.x * 0.5) < 0.5;\n");
12557 shader_addline(buffer, " if (even)\n");
12558 shader_addline(buffer, " chroma.y = luminance;\n");
12559 shader_addline(buffer, " else\n");
12560 shader_addline(buffer, " chroma.x = luminance;\n");
12562 /* Sample pixel 2. If we read an even pixel, sample the pixel right to the
12563 * current one. Otherwise, sample the left pixel. */
12564 shader_addline(buffer, " texcoord.x += even ? 1.0 / size.x : -1.0 / size.x;\n");
12565 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, chroma);
12567 /* Put the value into the other chroma. */
12568 shader_addline(buffer, " if (even)\n");
12569 shader_addline(buffer, " chroma.x = luminance;\n");
12570 shader_addline(buffer, " else\n");
12571 shader_addline(buffer, " chroma.y = luminance;\n");
12573 /* TODO: If filtering is enabled, sample a 2nd pair of pixels left or right of
12574 * the current one and lerp the two U and V values. */
12576 /* This gives the correctly filtered luminance value. */
12577 shader_addline(buffer, " luminance = texture%s(sampler, out_texcoord.xy).%c;\n", tex, luminance);
12580 static void gen_yv12_read(struct wined3d_string_buffer *buffer,
12581 const struct wined3d_gl_info *gl_info, const char *tex_type)
12583 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12584 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12586 /* YV12 surfaces contain a WxH sized luminance plane, followed by a
12587 * (W/2)x(H/2) V and a (W/2)x(H/2) U plane, each with 8 bit per pixel. So
12588 * the effective bitdepth is 12 bits per pixel. Since the U and V planes
12589 * have only half the pitch of the luminance plane, the packing into the
12590 * gl texture is a bit unfortunate. If the whole texture is interpreted as
12591 * luminance data it looks approximately like this:
12593 * +----------------------------------+----
12594 * | |
12595 * | |
12596 * | |
12597 * | |
12598 * | | 2
12599 * | LUMINANCE | -
12600 * | | 3
12601 * | |
12602 * | |
12603 * | |
12604 * | |
12605 * +----------------+-----------------+----
12606 * | | |
12607 * | V even rows | V odd rows |
12608 * | | | 1
12609 * +----------------+------------------ -
12610 * | | | 3
12611 * | U even rows | U odd rows |
12612 * | | |
12613 * +----------------+-----------------+----
12614 * | | |
12615 * | 0.5 | 0.5 |
12617 * So it appears as if there are 4 chroma images, but in fact the odd rows
12618 * in the chroma images are in the same row as the even ones. So it is
12619 * kinda tricky to read. */
12621 /* First sample the chroma values. */
12622 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12623 /* The chroma planes have only half the width. */
12624 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12626 /* The first value is between 2/3 and 5/6 of the texture's height, so
12627 * scale+bias the coordinate. Also read the right side of the image when
12628 * reading odd lines.
12630 * Don't forget to clamp the y values in into the range, otherwise we'll
12631 * get filtering bleeding. */
12633 /* Read odd lines from the right side (add 0.5 to the x coordinate). Keep
12634 * in mind that each line of the chroma plane corresponds to 2 lines of the
12635 * resulting image. */
12636 shader_addline(buffer, " if (fract(texcoord.y * size.y * 0.25) >= 0.5)\n");
12637 shader_addline(buffer, " texcoord.x += 0.5;\n");
12639 /* Clamp, keep the half pixel origin in mind. */
12640 shader_addline(buffer, " texcoord.y = clamp(2.0 / 3.0 + texcoord.y / 6.0, "
12641 "2.0 / 3.0 + 0.5 / size.y, 5.0 / 6.0 - 0.5 / size.y);\n");
12643 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12645 /* The other chroma value is 1/6th of the texture lower, from 5/6th to
12646 * 6/6th No need to clamp because we're just reusing the already clamped
12647 * value from above. */
12648 shader_addline(buffer, " texcoord.y += 1.0 / 6.0;\n");
12649 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12651 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12652 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12653 * values from bleeding into the sampled luminance values due to
12654 * filtering. */
12655 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12656 /* Multiply the y coordinate by 2/3 and clamp it. */
12657 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12658 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12661 static void gen_nv12_read(struct wined3d_string_buffer *buffer,
12662 const struct wined3d_gl_info *gl_info, const char *tex_type)
12664 char component = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? 'w' : 'x';
12665 const char *tex = needs_legacy_glsl_syntax(gl_info) ? tex_type : "";
12667 /* NV12 surfaces contain a WxH sized luminance plane, followed by a
12668 * (W/2)x(H/2) sized plane where each component is an UV pair. So the
12669 * effective bitdepth is 12 bits per pixel. If the whole texture is
12670 * interpreted as luminance data it looks approximately like this:
12672 * +----------------------------------+----
12673 * | |
12674 * | |
12675 * | |
12676 * | |
12677 * | | 2
12678 * | LUMINANCE | -
12679 * | | 3
12680 * | |
12681 * | |
12682 * | |
12683 * | |
12684 * +----------------------------------+----
12685 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12686 * |UVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUVUV|
12687 * | | 1
12688 * | | -
12689 * | | 3
12690 * | |
12691 * | |
12692 * +----------------------------------+---- */
12694 /* First sample the chroma values. */
12695 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12696 /* We only have half the number of chroma pixels. */
12697 shader_addline(buffer, " texcoord.x *= 0.5;\n");
12698 shader_addline(buffer, " texcoord.y = (texcoord.y + 2.0) / 3.0;\n");
12700 /* We must not allow filtering horizontally, this would mix U and V.
12701 * Vertical filtering is ok. However, bear in mind that the pixel center
12702 * is at 0.5, so add 0.5. */
12704 /* Convert to non-normalised coordinates so we can find the individual
12705 * pixel. */
12706 shader_addline(buffer, " texcoord.x = floor(texcoord.x * size.x);\n");
12707 /* Multiply by 2 since chroma components are stored in UV pixel pairs, add
12708 * 0.5 to hit the center of the pixel. Then convert back to normalised
12709 * coordinates. */
12710 shader_addline(buffer, " texcoord.x = (texcoord.x * 2.0 + 0.5) / size.x;\n");
12711 /* Clamp, keep the half pixel origin in mind. */
12712 shader_addline(buffer, " texcoord.y = max(texcoord.y, 2.0 / 3.0 + 0.5 / size.y);\n");
12714 shader_addline(buffer, " chroma.y = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12715 /* Add 1.0 / size.x to sample the adjacent texel. */
12716 shader_addline(buffer, " texcoord.x += 1.0 / size.x;\n");
12717 shader_addline(buffer, " chroma.x = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12719 /* Sample the luminance value. It is in the top 2/3rd of the texture, so
12720 * scale the y coordinate. Clamp the y coordinate to prevent the chroma
12721 * values from bleeding into the sampled luminance values due to
12722 * filtering. */
12723 shader_addline(buffer, " texcoord.xy = out_texcoord.xy;\n");
12724 /* Multiply the y coordinate by 2/3 and clamp it. */
12725 shader_addline(buffer, " texcoord.y = min(texcoord.y * 2.0 / 3.0, 2.0 / 3.0 - 0.5 / size.y);\n");
12726 shader_addline(buffer, " luminance = texture%s(sampler, texcoord.xy).%c;\n", tex, component);
12729 static void glsl_blitter_generate_yuv_shader(struct wined3d_string_buffer *buffer,
12730 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12731 const char *output, const char *tex_type, const char *swizzle)
12733 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12735 shader_addline(buffer, "const vec4 yuv_coef = vec4(1.403, -0.344, -0.714, 1.770);\n");
12736 shader_addline(buffer, "float luminance;\n");
12737 shader_addline(buffer, "vec2 texcoord;\n");
12738 shader_addline(buffer, "vec2 chroma;\n");
12739 shader_addline(buffer, "uniform vec2 size;\n");
12741 shader_addline(buffer, "\nvoid main()\n{\n");
12743 switch (complex_fixup)
12745 case COMPLEX_FIXUP_UYVY:
12746 case COMPLEX_FIXUP_YUY2:
12747 gen_packed_yuv_read(buffer, gl_info, args, tex_type);
12748 break;
12750 case COMPLEX_FIXUP_YV12:
12751 gen_yv12_read(buffer, gl_info, tex_type);
12752 break;
12754 case COMPLEX_FIXUP_NV12:
12755 gen_nv12_read(buffer, gl_info, tex_type);
12756 break;
12758 case COMPLEX_FIXUP_YUV:
12759 /* With APPLE_rgb_422, things are much simpler. The only thing we
12760 * have to do here is Y'CbCr to RGB conversion. */
12761 shader_addline(buffer, " vec3 yuv = vec3(texture%s(sampler, out_texcoord.xy));\n",
12762 needs_legacy_glsl_syntax(gl_info) ? tex_type : "");
12763 shader_addline(buffer, " luminance = yuv.y;\n");
12764 shader_addline(buffer, " chroma = yuv.xz;\n");
12765 break;
12767 default:
12768 FIXME("Unsupported fixup %#x.\n", complex_fixup);
12769 string_buffer_free(buffer);
12770 return;
12773 /* Calculate the final result. Formula is taken from
12774 * http://www.fourcc.org/fccyvrgb.php. Note that the chroma
12775 * ranges from -0.5 to 0.5. */
12776 shader_addline(buffer, "\n chroma.xy -= 0.5;\n");
12778 shader_addline(buffer, " %s.x = luminance + chroma.x * yuv_coef.x;\n", output);
12779 shader_addline(buffer, " %s.y = luminance + chroma.y * yuv_coef.y + chroma.x * yuv_coef.z;\n", output);
12780 shader_addline(buffer, " %s.z = luminance + chroma.y * yuv_coef.w;\n", output);
12781 if (args->use_colour_key)
12782 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12784 shader_addline(buffer, "}\n");
12787 static void glsl_blitter_generate_plain_shader(struct wined3d_string_buffer *buffer,
12788 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args,
12789 const char *output, const char *tex_type, const char *swizzle)
12791 shader_addline(buffer, "\nvoid main()\n{\n");
12792 shader_addline(buffer, " %s = texture%s(sampler, out_texcoord.%s);\n",
12793 output, needs_legacy_glsl_syntax(gl_info) ? tex_type : "", swizzle);
12794 shader_glsl_color_correction_ext(buffer, output, WINED3DSP_WRITEMASK_ALL, args->fixup);
12795 if (args->use_colour_key)
12796 shader_glsl_generate_colour_key_test(buffer, output, "colour_key.low", "colour_key.high");
12797 shader_addline(buffer, "}\n");
12800 /* Context activation is done by the caller. */
12801 static GLuint glsl_blitter_generate_program(struct wined3d_glsl_blitter *blitter,
12802 const struct wined3d_gl_info *gl_info, const struct glsl_blitter_args *args)
12804 static const struct
12806 GLenum texture_target;
12807 const char texture_type[7];
12808 const char texcoord_swizzle[4];
12810 texture_data[] =
12812 {GL_TEXTURE_2D, "2D", "xy"},
12813 {GL_TEXTURE_CUBE_MAP, "Cube", "xyz"},
12814 {GL_TEXTURE_RECTANGLE_ARB, "2DRect", "xy"},
12816 static const char vshader_main[] =
12817 "\n"
12818 "void main()\n"
12819 "{\n"
12820 " gl_Position = vec4(pos, 0.0, 1.0);\n"
12821 " out_texcoord = texcoord;\n"
12822 "}\n";
12823 enum complex_fixup complex_fixup = get_complex_fixup(args->fixup);
12824 struct wined3d_string_buffer *buffer, *output;
12825 GLuint program, vshader_id, fshader_id;
12826 const char *tex_type = NULL, *swizzle = NULL, *ptr;
12827 unsigned int i;
12828 GLint loc;
12830 for (i = 0; i < ARRAY_SIZE(texture_data); ++i)
12832 if (args->texture_type == texture_data[i].texture_target)
12834 tex_type = texture_data[i].texture_type;
12835 swizzle = texture_data[i].texcoord_swizzle;
12836 break;
12839 if (i == ARRAY_SIZE(texture_data))
12841 FIXME("Unsupported texture type %#x.\n", args->texture_type);
12842 return 0;
12845 program = GL_EXTCALL(glCreateProgram());
12847 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
12849 buffer = string_buffer_get(&blitter->string_buffers);
12850 shader_glsl_add_version_declaration(buffer, gl_info);
12851 shader_addline(buffer, "%s vec2 pos;\n", get_attribute_keyword(gl_info));
12852 shader_addline(buffer, "%s vec3 texcoord;\n", get_attribute_keyword(gl_info));
12853 declare_out_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12854 shader_addline(buffer, vshader_main);
12856 ptr = buffer->buffer;
12857 GL_EXTCALL(glShaderSource(vshader_id, 1, &ptr, NULL));
12858 GL_EXTCALL(glAttachShader(program, vshader_id));
12859 GL_EXTCALL(glDeleteShader(vshader_id));
12861 fshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
12863 string_buffer_clear(buffer);
12864 shader_glsl_add_version_declaration(buffer, gl_info);
12865 shader_addline(buffer, "uniform sampler%s sampler;\n", tex_type);
12866 if (args->use_colour_key)
12868 shader_addline(buffer, "uniform struct\n{\n");
12869 shader_addline(buffer, " vec4 low, high;\n");
12870 shader_addline(buffer, "} colour_key;\n");
12872 declare_in_varying(gl_info, buffer, FALSE, "vec3 out_texcoord;\n");
12873 /* TODO: Declare the out variable with the correct type (and put it in the
12874 * blitter args). */
12875 if (!use_legacy_fragment_output(gl_info))
12876 shader_addline(buffer, "out vec4 ps_out[1];\n");
12878 output = string_buffer_get(&blitter->string_buffers);
12879 string_buffer_sprintf(output, "%s[0]", get_fragment_output(gl_info));
12881 switch (complex_fixup)
12883 case COMPLEX_FIXUP_P8:
12884 glsl_blitter_generate_p8_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12885 break;
12886 case COMPLEX_FIXUP_YUY2:
12887 case COMPLEX_FIXUP_UYVY:
12888 case COMPLEX_FIXUP_YV12:
12889 case COMPLEX_FIXUP_NV12:
12890 case COMPLEX_FIXUP_YUV:
12891 glsl_blitter_generate_yuv_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12892 break;
12893 case COMPLEX_FIXUP_NONE:
12894 glsl_blitter_generate_plain_shader(buffer, gl_info, args, output->buffer, tex_type, swizzle);
12897 string_buffer_release(&blitter->string_buffers, output);
12899 ptr = buffer->buffer;
12900 GL_EXTCALL(glShaderSource(fshader_id, 1, &ptr, NULL));
12901 string_buffer_release(&blitter->string_buffers, buffer);
12902 GL_EXTCALL(glAttachShader(program, fshader_id));
12903 GL_EXTCALL(glDeleteShader(fshader_id));
12905 GL_EXTCALL(glBindAttribLocation(program, 0, "pos"));
12906 GL_EXTCALL(glBindAttribLocation(program, 1, "texcoord"));
12908 if (!use_legacy_fragment_output(gl_info))
12909 GL_EXTCALL(glBindFragDataLocation(program, 0, "ps_out"));
12911 GL_EXTCALL(glCompileShader(vshader_id));
12912 print_glsl_info_log(gl_info, vshader_id, FALSE);
12913 GL_EXTCALL(glCompileShader(fshader_id));
12914 print_glsl_info_log(gl_info, fshader_id, FALSE);
12915 GL_EXTCALL(glLinkProgram(program));
12916 shader_glsl_validate_link(gl_info, program);
12918 GL_EXTCALL(glUseProgram(program));
12919 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler"));
12920 GL_EXTCALL(glUniform1i(loc, 0));
12921 if (complex_fixup == COMPLEX_FIXUP_P8)
12923 loc = GL_EXTCALL(glGetUniformLocation(program, "sampler_palette"));
12924 GL_EXTCALL(glUniform1i(loc, 1));
12927 return program;
12930 /* Context activation is done by the caller. */
12931 static void glsl_blitter_upload_palette(struct wined3d_glsl_blitter *blitter,
12932 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl)
12934 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12935 const struct wined3d_palette *palette;
12937 palette = texture_gl->t.swapchain ? texture_gl->t.swapchain->palette : NULL;
12939 if (!blitter->palette_texture)
12940 gl_info->gl_ops.gl.p_glGenTextures(1, &blitter->palette_texture);
12942 wined3d_context_gl_active_texture(context_gl, gl_info, 1);
12943 gl_info->gl_ops.gl.p_glBindTexture(GL_TEXTURE_1D, blitter->palette_texture);
12944 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
12945 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
12946 gl_info->gl_ops.gl.p_glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
12948 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
12949 checkGLcall("glBindBuffer");
12951 if (palette)
12953 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 256, 0, GL_BGRA,
12954 GL_UNSIGNED_INT_8_8_8_8_REV, palette->colors);
12956 else
12958 static const DWORD black;
12960 FIXME("P8 texture loaded without a palette.\n");
12961 gl_info->gl_ops.gl.p_glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 1, 0, GL_BGRA,
12962 GL_UNSIGNED_INT_8_8_8_8_REV, &black);
12965 wined3d_context_gl_active_texture(context_gl, gl_info, 0);
12968 /* Context activation is done by the caller. */
12969 static struct glsl_blitter_program *glsl_blitter_get_program(struct wined3d_glsl_blitter *blitter,
12970 struct wined3d_context_gl *context_gl, const struct wined3d_texture_gl *texture_gl, BOOL use_colour_key)
12972 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
12973 struct glsl_blitter_program *program;
12974 struct glsl_blitter_args args;
12975 struct wine_rb_entry *entry;
12977 memset(&args, 0, sizeof(args));
12978 args.texture_type = texture_gl->target;
12979 args.fixup = texture_gl->t.resource.format->color_fixup;
12980 args.use_colour_key = use_colour_key;
12982 if ((entry = wine_rb_get(&blitter->programs, &args)))
12983 return WINE_RB_ENTRY_VALUE(entry, struct glsl_blitter_program, entry);
12985 if (!(program = heap_alloc(sizeof(*program))))
12987 ERR("Failed to allocate blitter program memory.\n");
12988 return NULL;
12991 program->args = args;
12992 if (!(program->id = glsl_blitter_generate_program(blitter, gl_info, &args)))
12994 WARN("Failed to generate blitter program.\n");
12995 heap_free(program);
12996 return NULL;
12999 if (wine_rb_put(&blitter->programs, &program->args, &program->entry) == -1)
13001 ERR("Failed to store blitter program.\n");
13002 GL_EXTCALL(glDeleteProgram(program->id));
13003 heap_free(program);
13004 return NULL;
13007 return program;
13010 static BOOL glsl_blitter_supported(enum wined3d_blit_op blit_op, const struct wined3d_context *context,
13011 const struct wined3d_texture_gl *src_texture, DWORD src_location,
13012 const struct wined3d_texture_gl *dst_texture, DWORD dst_location)
13014 const struct wined3d_resource *src_resource = &src_texture->t.resource;
13015 const struct wined3d_resource *dst_resource = &dst_texture->t.resource;
13016 const struct wined3d_format *src_format = src_resource->format;
13017 const struct wined3d_format *dst_format = dst_resource->format;
13018 bool src_ds, dst_ds;
13019 BOOL decompress;
13021 if (blit_op == WINED3D_BLIT_OP_RAW_BLIT && dst_format->id == src_format->id)
13023 src_ds = src_format->depth_size || src_format->stencil_size;
13024 dst_ds = dst_format->depth_size || dst_format->stencil_size;
13025 /* We could in principle support raw depth/stencil <-> colour blits as
13026 * well in some cases, but note that typeless formats like e.g.
13027 * R16_TYPELESS may use a normalised GL format for depth/stencil
13028 * resources, and a floating-point format for colour resources, */
13029 if (src_ds && dst_ds)
13030 blit_op = WINED3D_BLIT_OP_DEPTH_BLIT;
13031 else if (!src_ds && !dst_ds)
13032 blit_op = WINED3D_BLIT_OP_COLOR_BLIT;
13035 if (blit_op != WINED3D_BLIT_OP_COLOR_BLIT && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY
13036 && blit_op != WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13038 TRACE("Unsupported blit_op %#x.\n", blit_op);
13039 return FALSE;
13042 if (src_resource->type != WINED3D_RTYPE_TEXTURE_2D)
13043 return FALSE;
13045 if (src_texture->target == GL_TEXTURE_2D_MULTISAMPLE
13046 || src_texture->target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)
13048 TRACE("Multi-sample source textures not supported.\n");
13049 return FALSE;
13052 if (!(dst_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13054 TRACE("Destination resource does not have GPU access.\n");
13055 return FALSE;
13058 /* We don't necessarily want to blit from resources without
13059 * WINED3D_RESOURCE_ACCESS_GPU, but that may be the only way to decompress
13060 * compressed textures. */
13061 decompress = (src_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED)
13062 && !(dst_format->attrs & WINED3D_FORMAT_ATTR_COMPRESSED);
13063 if (!decompress && !(src_resource->access & WINED3D_RESOURCE_ACCESS_GPU))
13065 TRACE("Source resource does not have GPU access.\n");
13066 return FALSE;
13069 if (!is_identity_fixup(dst_format->color_fixup)
13070 && (dst_format->id != src_format->id || dst_location != WINED3D_LOCATION_DRAWABLE))
13072 TRACE("Destination fixups are not supported.\n");
13073 return FALSE;
13076 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO
13077 && !((dst_format->caps[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3D_FORMAT_CAP_FBO_ATTACHABLE)
13078 || (dst_resource->bind_flags & WINED3D_BIND_RENDER_TARGET)))
13080 TRACE("Destination texture is not FBO attachable.\n");
13081 return FALSE;
13084 TRACE("Returning supported.\n");
13085 return TRUE;
13088 static DWORD glsl_blitter_blit(struct wined3d_blitter *blitter, enum wined3d_blit_op op,
13089 struct wined3d_context *context, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
13090 DWORD src_location, const RECT *src_rect, struct wined3d_texture *dst_texture,
13091 unsigned int dst_sub_resource_idx, DWORD dst_location, const RECT *dst_rect,
13092 const struct wined3d_color_key *colour_key, enum wined3d_texture_filter_type filter,
13093 const struct wined3d_format *resolve_format)
13095 struct wined3d_texture_gl *src_texture_gl = wined3d_texture_gl(src_texture);
13096 struct wined3d_texture_gl *dst_texture_gl = wined3d_texture_gl(dst_texture);
13097 struct wined3d_context_gl *context_gl = wined3d_context_gl(context);
13098 struct wined3d_device *device = dst_texture->resource.device;
13099 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
13100 struct wined3d_texture *staging_texture = NULL;
13101 struct wined3d_glsl_blitter *glsl_blitter;
13102 struct wined3d_color_key alpha_test_key;
13103 struct glsl_blitter_program *program;
13104 struct wined3d_blitter *next;
13105 unsigned int src_level;
13106 GLint location;
13107 RECT s, d;
13109 TRACE("blitter %p, op %#x, context %p, src_texture %p, src_sub_resource_idx %u, src_location %s, "
13110 "src_rect %s, dst_texture %p, dst_sub_resource_idx %u, dst_location %s, dst_rect %s, "
13111 "colour_key %p, filter %s, resolve format %p.\n",
13112 blitter, op, context, src_texture, src_sub_resource_idx, wined3d_debug_location(src_location),
13113 wine_dbgstr_rect(src_rect), dst_texture, dst_sub_resource_idx, wined3d_debug_location(dst_location),
13114 wine_dbgstr_rect(dst_rect), colour_key, debug_d3dtexturefiltertype(filter), resolve_format);
13116 if (!glsl_blitter_supported(op, context, src_texture_gl, src_location, dst_texture_gl, dst_location))
13118 if (!(next = blitter->next))
13120 ERR("No blitter to handle blit op %#x.\n", op);
13121 return dst_location;
13124 TRACE("Forwarding to blitter %p.\n", next);
13125 return next->ops->blitter_blit(next, op, context, src_texture, src_sub_resource_idx, src_location,
13126 src_rect, dst_texture, dst_sub_resource_idx, dst_location, dst_rect, colour_key, filter,
13127 resolve_format);
13130 glsl_blitter = CONTAINING_RECORD(blitter, struct wined3d_glsl_blitter, blitter);
13132 if (!(src_texture->resource.access & WINED3D_RESOURCE_ACCESS_GPU))
13134 struct wined3d_resource_desc desc;
13135 struct wined3d_box upload_box;
13136 HRESULT hr;
13138 TRACE("Source texture is not GPU accessible, creating a staging texture.\n");
13140 src_level = src_sub_resource_idx % src_texture->level_count;
13141 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
13142 desc.format = src_texture->resource.format->id;
13143 desc.multisample_type = src_texture->resource.multisample_type;
13144 desc.multisample_quality = src_texture->resource.multisample_quality;
13145 desc.usage = WINED3DUSAGE_PRIVATE;
13146 desc.bind_flags = 0;
13147 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
13148 desc.width = wined3d_texture_get_level_width(src_texture, src_level);
13149 desc.height = wined3d_texture_get_level_height(src_texture, src_level);
13150 desc.depth = 1;
13151 desc.size = 0;
13153 if (FAILED(hr = wined3d_texture_create(device, &desc, 1, 1, 0,
13154 NULL, NULL, &wined3d_null_parent_ops, &staging_texture)))
13156 ERR("Failed to create staging texture, hr %#lx.\n", hr);
13157 return dst_location;
13160 wined3d_box_set(&upload_box, 0, 0, desc.width, desc.height, 0, desc.depth);
13161 wined3d_texture_upload_from_texture(staging_texture, 0, 0, 0, 0,
13162 src_texture, src_sub_resource_idx, &upload_box);
13164 src_texture = staging_texture;
13165 src_texture_gl = wined3d_texture_gl(src_texture);
13166 src_sub_resource_idx = 0;
13168 else if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
13169 && (src_texture->sub_resources[src_sub_resource_idx].locations
13170 & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_DRAWABLE)) == WINED3D_LOCATION_DRAWABLE
13171 && !wined3d_resource_is_offscreen(&src_texture->resource))
13174 /* Without FBO blits transferring from the drawable to the texture is
13175 * expensive, because we have to flip the data in sysmem. Since we can
13176 * flip in the blitter, we don't actually need that flip anyway. So we
13177 * use the surface's texture as scratch texture, and flip the source
13178 * rectangle instead. */
13179 texture2d_load_fb_texture(src_texture_gl, src_sub_resource_idx, FALSE, context);
13181 s = *src_rect;
13182 src_level = src_sub_resource_idx % src_texture->level_count;
13183 s.top = wined3d_texture_get_level_height(src_texture, src_level) - s.top;
13184 s.bottom = wined3d_texture_get_level_height(src_texture, src_level) - s.bottom;
13185 src_rect = &s;
13187 else
13189 wined3d_texture_load(src_texture, context, FALSE);
13192 if (wined3d_texture_is_full_rect(dst_texture, dst_sub_resource_idx % dst_texture->level_count, dst_rect))
13193 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13194 else
13195 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, dst_location);
13197 wined3d_context_gl_apply_blit_state(context_gl, device);
13199 if (dst_location == WINED3D_LOCATION_DRAWABLE)
13201 d = *dst_rect;
13202 wined3d_texture_translate_drawable_coords(dst_texture, context_gl->window, &d);
13203 dst_rect = &d;
13206 context_gl_apply_texture_draw_state(context_gl, dst_texture, dst_sub_resource_idx, dst_location);
13208 if (op == WINED3D_BLIT_OP_COLOR_BLIT_ALPHATEST)
13210 const struct wined3d_format *f = src_texture->resource.format;
13212 alpha_test_key.color_space_low_value = 0;
13213 alpha_test_key.color_space_high_value = ~(wined3d_mask_from_size(f->alpha_size) << f->alpha_offset);
13214 colour_key = &alpha_test_key;
13216 else if (op != WINED3D_BLIT_OP_COLOR_BLIT_CKEY)
13218 colour_key = NULL;
13221 if (!(program = glsl_blitter_get_program(glsl_blitter, context_gl, src_texture_gl, !!colour_key)))
13223 ERR("Failed to get blitter program.\n");
13224 return dst_location;
13226 GL_EXTCALL(glUseProgram(program->id));
13227 switch (get_complex_fixup(program->args.fixup))
13229 case COMPLEX_FIXUP_P8:
13230 glsl_blitter_upload_palette(glsl_blitter, context_gl, src_texture_gl);
13231 break;
13233 case COMPLEX_FIXUP_YUY2:
13234 case COMPLEX_FIXUP_UYVY:
13235 case COMPLEX_FIXUP_YV12:
13236 case COMPLEX_FIXUP_NV12:
13237 case COMPLEX_FIXUP_YUV:
13238 src_level = src_sub_resource_idx % src_texture->level_count;
13239 location = GL_EXTCALL(glGetUniformLocation(program->id, "size"));
13240 GL_EXTCALL(glUniform2f(location, wined3d_texture_get_level_pow2_width(src_texture, src_level),
13241 wined3d_texture_get_level_pow2_height(src_texture, src_level)));
13242 break;
13244 default:
13245 break;
13247 if (colour_key)
13249 struct wined3d_color float_key[2];
13251 wined3d_format_get_float_color_key(src_texture->resource.format, colour_key, float_key);
13252 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.low"));
13253 GL_EXTCALL(glUniform4fv(location, 1, &float_key[0].r));
13254 location = GL_EXTCALL(glGetUniformLocation(program->id, "colour_key.high"));
13255 GL_EXTCALL(glUniform4fv(location, 1, &float_key[1].r));
13257 wined3d_context_gl_draw_shaded_quad(context_gl, src_texture_gl, src_sub_resource_idx, src_rect, dst_rect, filter);
13258 GL_EXTCALL(glUseProgram(0));
13260 if (dst_texture->swapchain && (dst_texture->swapchain->front_buffer == dst_texture))
13261 gl_info->gl_ops.gl.p_glFlush();
13263 if (staging_texture)
13264 wined3d_texture_decref(staging_texture);
13266 return dst_location;
13269 static void glsl_blitter_clear(struct wined3d_blitter *blitter, struct wined3d_device *device,
13270 unsigned int rt_count, const struct wined3d_fb_state *fb, unsigned int rect_count, const RECT *clear_rects,
13271 const RECT *draw_rect, uint32_t flags, const struct wined3d_color *color, float depth, unsigned int stencil)
13273 struct wined3d_blitter *next;
13275 if ((next = blitter->next))
13276 next->ops->blitter_clear(next, device, rt_count, fb, rect_count,
13277 clear_rects, draw_rect, flags, color, depth, stencil);
13280 static const struct wined3d_blitter_ops glsl_blitter_ops =
13282 glsl_blitter_destroy,
13283 glsl_blitter_clear,
13284 glsl_blitter_blit,
13287 struct wined3d_blitter *wined3d_glsl_blitter_create(struct wined3d_blitter **next,
13288 const struct wined3d_device *device)
13290 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
13291 struct wined3d_glsl_blitter *blitter;
13293 if (device->shader_backend != &glsl_shader_backend)
13294 return NULL;
13296 if (!gl_info->supported[ARB_VERTEX_SHADER] || !gl_info->supported[ARB_FRAGMENT_SHADER])
13297 return NULL;
13299 if (!(blitter = heap_alloc(sizeof(*blitter))))
13301 ERR("Failed to allocate blitter.\n");
13302 return NULL;
13305 TRACE("Created blitter %p.\n", blitter);
13307 blitter->blitter.ops = &glsl_blitter_ops;
13308 blitter->blitter.next = *next;
13309 string_buffer_list_init(&blitter->string_buffers);
13310 wine_rb_init(&blitter->programs, glsl_blitter_args_compare);
13311 blitter->palette_texture = 0;
13312 *next = &blitter->blitter;
13314 return *next;